Skip to content

Commit

Permalink
Merge pull request #129 from vaadin/fix-empty-check
Browse files Browse the repository at this point in the history
Fix empty slot check
  • Loading branch information
Limon Monte authored Oct 8, 2018
2 parents ea6d0db + 8b98be7 commit 230e74b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/vaadin-checkbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,22 @@

_updateLabelAttribute() {
const label = this.shadowRoot.querySelector('[part~="label"]');
if (label.firstElementChild.assignedNodes().length === 0) {
const assignedNodes = label.firstElementChild.assignedNodes();
if (this._isAssignedNodesEmpty(assignedNodes)) {
label.setAttribute('empty', '');
} else {
label.removeAttribute('empty');
}
}

_isAssignedNodesEmpty(nodes) {
// The assigned nodes considered to be empty if there is no slotted content or only one empty text node
return nodes.length === 0 ||
(nodes.length == 1
&& nodes[0].nodeType == Node.TEXT_NODE
&& nodes[0].textContent.trim() === '');
}

_checkedChanged(checked) {
if (this.indeterminate) {
this.setAttribute('aria-checked', 'mixed');
Expand Down
11 changes: 11 additions & 0 deletions test/vaadin-checkbox_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@
expect(label.hasAttribute('empty')).to.be.true;
});

it('should set empty attribute on part label when there is only one empty text node added', () => {
const textNode = document.createTextNode(' ');
vaadinCheckbox.appendChild(textNode);

// Workaround for not using timeouts
const evt = new CustomEvent('slotchange');
vaadinCheckbox.shadowRoot.querySelector('[part="label"]').querySelector('slot').dispatchEvent(evt);

expect(label.hasAttribute('empty')).to.be.true;
});

it('should remove empty attribute from part label when the label is added', () => {
const paragraph = document.createElement('p');
paragraph.textContent = 'Added label';
Expand Down

0 comments on commit 230e74b

Please sign in to comment.