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

IE11: Fix incorrect node order with conditionals and text nodes #4650

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion compat/src/portals.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function Portal(props) {
nodeType: 1,
parentNode: container,
childNodes: [],
contains: () => true,
isPortal: true,
Copy link
Member

Choose a reason for hiding this comment

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

This is an unsafe change as other faux roots could be using this, also contains is not a hack, it's a DOM built-in

// Technically this isn't needed
appendChild(child) {
this.childNodes.push(child);
Expand Down
4 changes: 2 additions & 2 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
MATCHED,
UNDEFINED
} from '../constants';
import { isArray } from '../util';
import { isArray, contains } from '../util';
import { getDomSibling } from '../component';

/**
Expand Down Expand Up @@ -339,7 +339,7 @@ function insert(parentVNode, oldDom, parentDom) {

return oldDom;
} else if (parentVNode._dom != oldDom) {
if (oldDom && parentVNode.type && !parentDom.contains(oldDom)) {
if (oldDom && parentVNode.type && !contains(parentDom, oldDom)) {
Copy link
Member

@JoviDeCroock JoviDeCroock Feb 14, 2025

Choose a reason for hiding this comment

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

I think we could solve this a lot simpler, similar to #4666 - alternatively, we could check whether oldDom is in fact a DOM-node rather than a text-node.

oldDom = getDomSibling(parentVNode);
}
parentDom.insertBefore(parentVNode._dom, oldDom || null);
Expand Down
19 changes: 19 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,23 @@ export function removeNode(node) {
if (node && node.parentNode) node.parentNode.removeChild(node);
}

/**
* Checks whether a given node is contained within a specified parent node's subtree.
* This function is a workaround for Internet Explorer 11's limited support for
* `Element.prototype.contains()`, which works only for DOM elements and does not
* support text nodes.
*
* @param {import('./internal').PreactElement} parent - The parent node within which to search for the `node`.
* @param {import('./index').ContainerNode} node - The node to check for containment within the `parent` subtree.
*/
export function contains(parent, node) {
if ('isPortal' in parent) return true;

while (node) {
if (node === parent) return true;
node = node.parentNode;
}
return false;
}

export const slice = EMPTY_ARR.slice;
Loading