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

Conversation

theengineear
Copy link
Collaborator

@theengineear theengineear commented Nov 12, 2024

The default browser behavior when setting .textContent to null or undefined is to ultimately set the content to ''†. This is quite different from the behavior of createTextNode though… by splitting the text node creation into multiple steps we can consistently leverage .textContent and remove the need to ?? ''.

† Note that the WHATWG spec only defines that behavior for null, but
in practice all modern browsers seem to treat undefined similarly.

Here’s the spec for reference. This is related to #204.

The default browser behavior when setting `.textContent` to `null` or
`undefined` is to ultimately set the content to `''`†. This is quite
different from the behavior of `createTextNode` though… by splitting
the text node creation into multiple steps we can consistently leverage
`.textContent` and remove the need to `?? ''`.

† Note that the WHATWG spec only defines that behavior for `null`, but
  in practice all modern browsers seem to treat `undefined` similarly.
@@ -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.

@@ -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'.

@theengineear
Copy link
Collaborator Author

FYI @klebba — Hopefully this one is pretty easy to review. Shouldn’t be too controversial.

@theengineear
Copy link
Collaborator Author

@klebba — Gonna get this in to unblock myself on some other work.

@theengineear theengineear merged commit e975ecc into main Nov 14, 2024
1 check passed
@theengineear theengineear deleted the remove-nullish-coalescing branch November 14, 2024 01:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant