Skip to content

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

2 participants