Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove nullish coalescing for textContent. #205

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion test/test-basic-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class TestElement extends XElement {
type: String,
default: null,
},
undefinedProperty: {
type: String,
},
typelessProperty: {},
typelessPropertyWithCustomAttribute: {
attribute: 'custom-attribute-typeless',
Expand All @@ -33,12 +36,13 @@ class TestElement extends XElement {
};
}
static template(html) {
return ({ normalProperty, camelCaseProperty, numericProperty, nullProperty }) => {
return ({ normalProperty, camelCaseProperty, numericProperty, nullProperty, undefinedProperty }) => {
return html`
<div id="normal">${normalProperty}</div>
<span id="camel">${camelCaseProperty}</span>
<span id="num">${numericProperty}</span>
<span id="nul">${nullProperty}</span>
<span id="undef">${undefinedProperty}</span>
`;
};
}
Expand All @@ -60,6 +64,12 @@ it('renders an empty string in place of null value', () => {
assert(el.shadowRoot.getElementById('nul').textContent === '');
});

it('renders an empty string in place of undefined value', () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this passed before, we’re just specifically testing it now.

const el = document.createElement('test-element');
document.body.append(el);
assert(el.shadowRoot.getElementById('undef').textContent === '');
});

it('property setter updates on next micro tick after connect', async () => {
const el = document.createElement('test-element');
el.camelCaseProperty = 'Nonconforming';
Expand Down Expand Up @@ -99,6 +109,7 @@ it('observes all dash-cased versions of public, typeless, serializable, and decl
'camel-case-property',
'numeric-property',
'null-property',
'undefined-property',
'typeless-property',
'custom-attribute-typeless',
];
Expand Down
5 changes: 3 additions & 2 deletions x-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -1488,10 +1488,11 @@ class TemplateEngine {
}
const previousSibling = node.previousSibling;
if (previousSibling === startNode) {
const textNode = document.createTextNode(value ?? '');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the only place we really needed to coalesce. If you pass null or undefined here you get 'null' or 'undefined'.

const textNode = document.createTextNode('');
textNode.textContent = value;
node.parentNode.insertBefore(textNode, node);
} else {
previousSibling.textContent = value ?? '';
previousSibling.textContent = value;
}
}
}
Expand Down
Loading