mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-10-13 18:46:37 +00:00
merge: fold the #569 review fixes into the alias-aware layout rows
Four commits landed on this branch from review while the layout wiring was in progress, and the two solved overlapping problems differently. Theirs made elementKeys() append every layout-only key, matched by exact name. Against football that also lists `score` and `odds` -- entries the alias resolution already pairs with score_text and odds_text -- so those offsets would get two controls posting the same field. The row split here (styleRows via core's x-layout-key, positionRows for what nothing claims) replaces that append; elementKeys() is back to listing style elements only. Theirs also covered cases this side had missed, all kept: - A leaf straight under layout (a show_logo toggle, no x/y object) now gets a position row with a self-keyed column bound to customization.layout.<key>. Core no longer lets an element claim a leaf as its offsets. - Leaf columns keep their namespaced id, so a leaf sharing a name with another entry's axis cannot be shadowed. - columnsFor(schema) still works with one argument (every row), so their column tests run unchanged. - The owned key is CSS.escape()'d before it becomes a selector. test_style_editor_element_keys.js is rewritten for the new contract, and says why the old one had to go. All six JS suites pass; in a browser football's 92 layout fields each render once, and a synthetic leaf -- including one named `scale` alongside a `scale` axis -- gets exactly one correctly named control. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
// Regression test for how style-editor.js decides which rows to draw,
|
||||
// extracted verbatim from the shipped widget (own/ownObj/elementKeys/
|
||||
// styleRows/positionRows have no DOM dependency) so the test can't drift from
|
||||
// the real implementation.
|
||||
//
|
||||
// render() claims the whole `layout` child of a customization schema as the
|
||||
// widget's own -- removing it from the generic fallback renderer entirely,
|
||||
// because posting the same offset twice from two controls is worse than
|
||||
// owning slightly too much. That is only safe when every entry under `layout`
|
||||
// gets exactly one row:
|
||||
//
|
||||
// * styleRows(): one per style element, paired with the layout entry that
|
||||
// holds its offsets. The pairing is not by exact name -- a hand-written
|
||||
// schema styles 'score_text' but positions 'score' -- so core records it
|
||||
// as x-layout-key, resolved through the same alias map the renderer reads
|
||||
// offsets with.
|
||||
// * positionRows(): one per layout entry no style element claimed -- a logo,
|
||||
// a timeout indicator, a possession arrow, or a bare leaf like a
|
||||
// show_logo toggle.
|
||||
//
|
||||
// This contract replaced an earlier one in which elementKeys() itself
|
||||
// appended layout-only keys. That compared names exactly, so an aliased entry
|
||||
// ('score' for 'score_text') was listed a second time and its offsets got two
|
||||
// controls posting the same field.
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const V3 = path.resolve(__dirname, '../../../web_interface/static/v3');
|
||||
|
||||
const src = fs.readFileSync(path.join(V3, 'js/widgets/style-editor.js'), 'utf8');
|
||||
const start = src.indexOf('function own(obj, key)');
|
||||
const endMark = 'function control(opts)';
|
||||
const end = src.indexOf(endMark);
|
||||
if (start === -1 || end === -1) {
|
||||
console.error('FAIL: could not locate own()..positionRows() in style-editor.js -- slice markers need updating');
|
||||
process.exit(1);
|
||||
}
|
||||
eval(src.slice(start, end)); // defines own/ownObj/elementKeys/styleRows/positionRows
|
||||
|
||||
let failures = 0;
|
||||
function ok(desc, cond) {
|
||||
if (cond) { console.log(`ok - ${desc}`); }
|
||||
else { console.log(`FAIL - ${desc}`); failures++; }
|
||||
}
|
||||
|
||||
function layoutKeysOf(rows) { return rows.map(function (r) { return r.layoutKey; }); }
|
||||
|
||||
// score_text is styled and positioned under an alias; odds_text has no
|
||||
// offsets at all; possession is positioned but never styled; show_logo is a
|
||||
// bare leaf under layout.
|
||||
const schema = {
|
||||
properties: {
|
||||
score_text: {
|
||||
type: 'object', 'x-style-managed': true, 'x-layout-key': 'score',
|
||||
properties: { font: { type: 'string' }, text_color: { type: 'array' } },
|
||||
},
|
||||
odds_text: {
|
||||
type: 'object', 'x-style-managed': true,
|
||||
properties: { font: { type: 'string' } },
|
||||
},
|
||||
layout: {
|
||||
type: 'object',
|
||||
'x-propertyOrder': ['score', 'possession', 'show_logo'],
|
||||
properties: {
|
||||
score: { type: 'object', properties: { x_offset: {}, y_offset: {} } },
|
||||
possession: { type: 'object', properties: { x_offset: {}, y_offset: {} } },
|
||||
show_logo: { type: 'boolean', default: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const keys = elementKeys(schema);
|
||||
ok('elementKeys lists the style elements', keys.join(',') === 'score_text,odds_text');
|
||||
ok('layout and modes containers are never treated as elements',
|
||||
keys.indexOf('layout') === -1 && keys.indexOf('modes') === -1);
|
||||
|
||||
const styled = styleRows(schema);
|
||||
ok('a style element is paired with the layout entry core resolved for it',
|
||||
styled[0].key === 'score_text' && styled[0].layoutKey === 'score');
|
||||
ok('a style element with no offsets claims nothing', styled[1].layoutKey === null);
|
||||
|
||||
const positions = positionRows(schema);
|
||||
ok('a layout-only object entry gets a position row',
|
||||
layoutKeysOf(positions).indexOf('possession') !== -1);
|
||||
ok('a layout-only leaf gets a position row too',
|
||||
layoutKeysOf(positions).indexOf('show_logo') !== -1);
|
||||
ok('an entry claimed through an alias is not listed again as a position',
|
||||
layoutKeysOf(positions).indexOf('score') === -1);
|
||||
ok('positions follow the declared order',
|
||||
layoutKeysOf(positions).join(',') === 'possession,show_logo');
|
||||
|
||||
const every = layoutKeysOf(styled).filter(Boolean).concat(layoutKeysOf(positions));
|
||||
ok('every layout entry gets exactly one row',
|
||||
every.length === new Set(every).size
|
||||
&& every.slice().sort().join(',') === ['possession', 'score', 'show_logo'].join(','));
|
||||
|
||||
// A schema with no layout block at all must behave exactly as before --
|
||||
// the overwhelmingly common case must not gain a phantom row.
|
||||
const noLayout = {
|
||||
properties: {
|
||||
score_text: {
|
||||
type: 'object', 'x-style-managed': true,
|
||||
properties: { font: { type: 'string' } },
|
||||
},
|
||||
},
|
||||
};
|
||||
ok('a schema without a layout block has no positions',
|
||||
elementKeys(noLayout).join(',') === 'score_text' && positionRows(noLayout).length === 0);
|
||||
|
||||
// Without an annotation (the compact declaration, or a schema that never went
|
||||
// through expansion) the two blocks share one key.
|
||||
const unannotated = {
|
||||
properties: {
|
||||
home_logo: { type: 'object', 'x-style-managed': true, properties: {} },
|
||||
layout: {
|
||||
type: 'object',
|
||||
properties: { home_logo: { type: 'object', properties: { x_offset: {}, scale: {} } } },
|
||||
},
|
||||
},
|
||||
};
|
||||
ok('an exact-name layout entry is claimed without an annotation',
|
||||
styleRows(unannotated)[0].layoutKey === 'home_logo'
|
||||
&& positionRows(unannotated).length === 0);
|
||||
|
||||
if (failures) { console.log(`\n${failures} failure(s)`); process.exit(1); }
|
||||
console.log('\nall checks passed');
|
||||
@@ -0,0 +1,76 @@
|
||||
// Regression test for style-editor.js's columnsFor(), extracted verbatim
|
||||
// from the shipped widget (see test_style_editor_layout_leaf_columns.js for
|
||||
// the base case this builds on).
|
||||
//
|
||||
// A layout-only leaf key (e.g. a bare "scale" toggle directly under
|
||||
// `layout`, no x/y object underneath) used to be keyed in the internal
|
||||
// `seen` map by its own bare field name. If some *other* element's style
|
||||
// block, or another element's layout axis block, happened to declare a
|
||||
// sub-field with that exact same name (e.g. "scale" is also a real
|
||||
// COLUMN_ORDER axis name most elements use), the `!seen.has(key)` guard
|
||||
// skipped creating the leaf's column -- it silently reused the existing
|
||||
// 'element'/'layout' column instead. That column's row binding in
|
||||
// elementRow() then looked the field up under the wrong schema location for
|
||||
// the leaf's own row, found nothing, and rendered a blank cell: the same
|
||||
// "silently disappears" bug the leaf-column fix was meant to close, just
|
||||
// reached through a name collision instead of a missing column altogether.
|
||||
// This pins the fix: layout-leaf columns are keyed under a namespaced id so
|
||||
// they can never be shadowed by an unrelated column sharing their name.
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const V3 = path.resolve(__dirname, '../../../web_interface/static/v3');
|
||||
|
||||
const src = fs.readFileSync(path.join(V3, 'js/widgets/style-editor.js'), 'utf8');
|
||||
const start = src.indexOf('function own(obj, key)');
|
||||
const endMark = 'function control(opts)';
|
||||
const end = src.indexOf(endMark);
|
||||
if (start === -1 || end === -1) {
|
||||
console.error('FAIL: could not locate own()/columnsFor() in style-editor.js -- slice markers need updating');
|
||||
process.exit(1);
|
||||
}
|
||||
eval(src.slice(start, end)); // defines own/ownObj/elementKeys/columnsFor
|
||||
|
||||
let failures = 0;
|
||||
function ok(desc, cond) {
|
||||
if (cond) { console.log(`ok - ${desc}`); }
|
||||
else { console.log(`FAIL - ${desc}`); failures++; }
|
||||
}
|
||||
|
||||
// `possession` is a real style element whose layout axis block declares a
|
||||
// "scale" sub-field, which seeds seen.set('scale', 'layout'). `timeout_flag`
|
||||
// is layout-only and a bare leaf valued directly under layout, but its own
|
||||
// key is *also* "scale" -- an unrelated collision with that same name.
|
||||
const schema = {
|
||||
properties: {
|
||||
possession: {
|
||||
type: 'object',
|
||||
'x-style-managed': true,
|
||||
properties: { font: { type: 'string' } },
|
||||
},
|
||||
scale: {
|
||||
// The collision: a second element literally named "scale", entirely
|
||||
// unrelated to the layout-only leaf below sharing that string.
|
||||
type: 'boolean',
|
||||
},
|
||||
layout: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
possession: { type: 'object', properties: { scale: { type: 'number' } } },
|
||||
scale: { type: 'boolean', default: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const columns = columnsFor(schema);
|
||||
const leafColumns = columns.filter(function (c) { return c.where === 'layout-leaf'; });
|
||||
|
||||
ok('the colliding layout axis sub-field still gets its own column',
|
||||
columns.some(function (c) { return c.key === 'scale' && c.where === 'layout'; }));
|
||||
ok('the layout-only leaf key still gets its own column despite the name collision',
|
||||
leafColumns.length === 1 && leafColumns[0].key === 'scale');
|
||||
ok('the leaf column is distinct from the colliding element/layout column',
|
||||
columns.filter(function (c) { return c.key === 'scale'; }).length === 2);
|
||||
|
||||
if (failures) { console.log(`\n${failures} failure(s)`); process.exit(1); }
|
||||
console.log('\nall checks passed');
|
||||
@@ -0,0 +1,89 @@
|
||||
// Regression test for style-editor.js's columnsFor(), extracted verbatim
|
||||
// from the shipped widget (own/ownObj/elementKeys/columnsFor have no DOM
|
||||
// dependency) so the test can't drift from the real implementation.
|
||||
//
|
||||
// elementKeys() already appends a layout-only key that never got its own
|
||||
// top-level style block (see test_style_editor_element_keys.js), so table()
|
||||
// draws a row for it. But render() still claims the whole `layout` child as
|
||||
// the widget's own regardless of what that row actually shows -- and
|
||||
// columnsFor() only produced a column for a layout-only key whose own value
|
||||
// is an *object* with sub-fields (x_offset, y_offset, ...). A hand-written
|
||||
// schema can instead put a plain leaf value directly under layout -- a
|
||||
// "show_logo" toggle, a timeout indicator's on/off flag -- with no x/y
|
||||
// object underneath it. That key still got a row (data-element="show_logo"),
|
||||
// but zero columns ever matched it, so the row rendered with every cell
|
||||
// blank: no control, and no way back to the fallback since layout was
|
||||
// removed wholesale. This pins the fix: such a key gets its own
|
||||
// self-keyed 'layout-leaf' column.
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const V3 = path.resolve(__dirname, '../../../web_interface/static/v3');
|
||||
|
||||
const src = fs.readFileSync(path.join(V3, 'js/widgets/style-editor.js'), 'utf8');
|
||||
const start = src.indexOf('function own(obj, key)');
|
||||
const endMark = 'function control(opts)';
|
||||
const end = src.indexOf(endMark);
|
||||
if (start === -1 || end === -1) {
|
||||
console.error('FAIL: could not locate own()/columnsFor() in style-editor.js -- slice markers need updating');
|
||||
process.exit(1);
|
||||
}
|
||||
eval(src.slice(start, end)); // defines own/ownObj/elementKeys/columnsFor
|
||||
|
||||
let failures = 0;
|
||||
function ok(desc, cond) {
|
||||
if (cond) { console.log(`ok - ${desc}`); }
|
||||
else { console.log(`FAIL - ${desc}`); failures++; }
|
||||
}
|
||||
|
||||
// score_text is a real style element; possession is layout-only but shaped
|
||||
// like an object (x/y); show_logo is layout-only and a bare leaf value.
|
||||
const schema = {
|
||||
properties: {
|
||||
score_text: {
|
||||
type: 'object',
|
||||
'x-style-managed': true,
|
||||
properties: { font: { type: 'string' }, text_color: { type: 'array' } },
|
||||
},
|
||||
layout: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
score_text: { type: 'object', properties: { x_offset: {}, y_offset: {} } },
|
||||
possession: { type: 'object', properties: { x_offset: {}, y_offset: {} } },
|
||||
show_logo: { type: 'boolean', default: true },
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const columns = columnsFor(schema);
|
||||
const byKey = {};
|
||||
columns.forEach(function (c) { byKey[c.key] = c; });
|
||||
|
||||
ok('an element style field still gets its own column', byKey.font && byKey.font.where === 'element');
|
||||
ok('a layout-only object key still contributes its x/y columns',
|
||||
byKey.x_offset && byKey.x_offset.where === 'layout');
|
||||
ok('a layout-only leaf key gets a column keyed to itself',
|
||||
!!byKey.show_logo);
|
||||
ok('...marked as a layout-leaf column, not a shared sub-field',
|
||||
byKey.show_logo && byKey.show_logo.where === 'layout-leaf');
|
||||
ok('nothing is duplicated', columns.length === new Set(columns.map(function (c) { return c.key; })).size);
|
||||
|
||||
// A schema with no leaf-valued layout key must not gain a phantom column --
|
||||
// this is the common case (every existing scoreboard) and must be unaffected.
|
||||
const noLeaf = {
|
||||
properties: {
|
||||
score_text: {
|
||||
type: 'object', 'x-style-managed': true,
|
||||
properties: { font: { type: 'string' } },
|
||||
},
|
||||
layout: {
|
||||
type: 'object',
|
||||
properties: { score_text: { type: 'object', properties: { x_offset: {} } } },
|
||||
},
|
||||
},
|
||||
};
|
||||
ok('a schema with only object-shaped layout keys gets no layout-leaf column',
|
||||
columnsFor(noLeaf).every(function (c) { return c.where !== 'layout-leaf'; }));
|
||||
|
||||
if (failures) { console.log(`\n${failures} failure(s)`); process.exit(1); }
|
||||
console.log('\nall checks passed');
|
||||
Reference in New Issue
Block a user