Skip to content

Commit e738e5e

Browse files
danilsomsikovDevtools-frontend LUCI CQ
authored andcommitted
Handle false in data grid boolean attributes
Bug: 394287937 Change-Id: If94a0bf9fefa57d1d68e017fe80fffb026e190ce Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6229633 Reviewed-by: Benedikt Meurer <[email protected]> Commit-Queue: Danil Somsikov <[email protected]> Auto-Submit: Danil Somsikov <[email protected]>
1 parent 1d6b9bf commit e738e5e

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

front_end/ui/legacy/components/data_grid/DataGridElement.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,48 @@ describeWithEnvironment('DataGrid', () => {
158158
assert.strictEqual(getAccessibleText(element), 'Display Name Row Column 1: Value 3, Column 2: Value 4');
159159
});
160160

161+
it('can set selection from template', async () => {
162+
let element = await renderDataGrid(html`
163+
<devtools-data-grid striped name=${'Display Name'}>
164+
<table>
165+
<tr>
166+
<th id="column-1">Column 1</th>
167+
<th id="column-2">Column 2</th>
168+
</tr>
169+
<tr>
170+
<td>Value 1</td>
171+
<td>Value 2</td>
172+
</tr>
173+
<tr selected>
174+
<td>Value 3</td>
175+
<td>Value 4</td>
176+
</tr>
177+
</table>
178+
</devtools-data-grid>`);
179+
// clang-format off
180+
assert.strictEqual(getAccessibleText(element), 'Display Name Row Column 1: Value 3, Column 2: Value 4');
181+
182+
element = await renderDataGrid(html`
183+
<devtools-data-grid striped name=${'Display Name'}>
184+
<table>
185+
<tr>
186+
<th id="column-1">Column 1</th>
187+
<th id="column-2">Column 2</th>
188+
</tr>
189+
<tr>
190+
<td>Value 1</td>
191+
<td>Value 2</td>
192+
</tr>
193+
<tr selected="false">
194+
<td>Value 3</td>
195+
<td>Value 4</td>
196+
</tr>
197+
</table>
198+
</devtools-data-grid>`);
199+
// clang-format off
200+
assert.isTrue(getAccessibleText(element).startsWith('Display Name Rows: 2'));
201+
});
202+
161203
it('supports editable columns', async () => {
162204
const editCallback = sinon.stub();
163205
const element = await renderDataGrid(html`

front_end/ui/legacy/components/data_grid/DataGridElement.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ class DataGridElement extends HTMLElement {
120120
}
121121

122122
get striped(): boolean {
123-
return this.hasAttribute('striped');
123+
return hasBooleanAttribute(this, 'striped');
124124
}
125125

126126
set inline(striped: boolean) {
127127
this.toggleAttribute('inline', striped);
128128
}
129129

130130
get inline(): boolean {
131-
return this.hasAttribute('inline');
131+
return hasBooleanAttribute(this, 'inline');
132132
}
133133

134134
set displayName(displayName: string) {
@@ -167,7 +167,7 @@ class DataGridElement extends HTMLElement {
167167
title += child.shadowRoot ? child.shadowRoot.textContent : child.textContent;
168168
}
169169
}
170-
const sortable = column.hasAttribute('sortable');
170+
const sortable = hasBooleanAttribute(column, 'sortable');
171171
const width = column.getAttribute('width') ?? undefined;
172172
const fixedWidth = column.hasAttribute('fixed');
173173
let align = column.getAttribute('align') ?? undefined;
@@ -190,7 +190,7 @@ class DataGridElement extends HTMLElement {
190190
weight,
191191
editable
192192
});
193-
if (column.hasAttribute('hideable')) {
193+
if (hasBooleanAttribute(column, 'hideable')) {
194194
this.#hideableColumns.add(id);
195195
}
196196
}
@@ -224,7 +224,7 @@ class DataGridElement extends HTMLElement {
224224
const index = nextNode ? parentNode.children.indexOf(nextNode) : parentNode.children.length;
225225
const node = new DataGridElementNode(element, this);
226226
parentNode.insertChild(node, index);
227-
if (element.hasAttribute('selected')) {
227+
if (hasBooleanAttribute(element, 'selected')) {
228228
node.select();
229229
}
230230
}
@@ -247,7 +247,7 @@ class DataGridElement extends HTMLElement {
247247
const dataGridNode = dataRow ? DataGridElementNode.get(dataRow) : null;
248248
if (dataGridNode) {
249249
if (selectionOnly) {
250-
if (dataRow?.hasAttribute('selected')) {
250+
if (dataRow && hasBooleanAttribute(dataRow, 'selected')) {
251251
dataGridNode.select();
252252
} else {
253253
dataGridNode.deselect();
@@ -290,9 +290,6 @@ class DataGridElementNode extends SortableDataGridNode<DataGridElementNode> {
290290
DataGridElementNode.#elementToNode.set(configElement, this);
291291
this.#dataGridElement = dataGridElement;
292292
this.#updateData();
293-
if (this.#configElement.hasAttribute('selected')) {
294-
this.select();
295-
}
296293
}
297294

298295
static get(configElement: Element|undefined): DataGridElementNode|undefined {
@@ -373,5 +370,8 @@ class DataGridElementNode extends SortableDataGridNode<DataGridElementNode> {
373370
}
374371
}
375372

376-
// TODO(dsv): Rename to devtools-data-grid once the other one is removed.
377373
customElements.define('devtools-data-grid', DataGridElement);
374+
375+
function hasBooleanAttribute(element: Element, name: string): boolean {
376+
return element.hasAttribute(name) && element.getAttribute(name) !== 'false';
377+
}

0 commit comments

Comments
 (0)