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

Ownerdocument rebased #4501

Open
wants to merge 20 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
3 changes: 2 additions & 1 deletion compat/src/portals.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ function Portal(props) {
removeChild(child) {
this.childNodes.splice(this.childNodes.indexOf(child) >>> 1, 1);
_this._container.removeChild(child);
}
},
ownerDocument: container.ownerDocument
};
}

Expand Down
6 changes: 4 additions & 2 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ function renderComponent(component) {
let oldVNode = component._vnode,
oldDom = oldVNode._dom,
commitQueue = [],
refQueue = [];
refQueue = [],
parentDom = component._parentDom;

if (component._parentDom) {
const newVNode = assign({}, oldVNode);
Expand All @@ -141,7 +142,8 @@ function renderComponent(component) {
commitQueue,
oldDom == null ? getDomSibling(oldVNode) : oldDom,
!!(oldVNode._flags & MODE_HYDRATE),
refQueue
refQueue,
parentDom.ownerDocument
);

newVNode._original = oldVNode._original;
Expand Down
7 changes: 5 additions & 2 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { getDomSibling } from '../component';
* siblings. In most cases, it starts out as `oldChildren[0]._dom`.
* @param {boolean} isHydrating Whether or not we are in hydration
* @param {any[]} refQueue an array of elements needed to invoke refs
* @param {Document} doc The owner document of the parentNode
*/
export function diffChildren(
parentDom,
Expand All @@ -37,7 +38,8 @@ export function diffChildren(
commitQueue,
oldDom,
isHydrating,
refQueue
refQueue,
doc
) {
let i,
/** @type {VNode} */
Expand Down Expand Up @@ -86,7 +88,8 @@ export function diffChildren(
commitQueue,
oldDom,
isHydrating,
refQueue
refQueue,
doc
);

// Adjust DOM nodes
Expand Down
22 changes: 15 additions & 7 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import options from '../options';
* siblings. In most cases, it starts out as `oldChildren[0]._dom`.
* @param {boolean} isHydrating Whether or not we are in hydration
* @param {any[]} refQueue an array of elements needed to invoke refs
* @param {Document} doc The owner document of the parentNode
*/
export function diff(
parentDom,
Expand All @@ -39,7 +40,8 @@ export function diff(
commitQueue,
oldDom,
isHydrating,
refQueue
refQueue,
doc
) {
/** @type {any} */
let tmp,
Expand Down Expand Up @@ -254,7 +256,8 @@ export function diff(
commitQueue,
oldDom,
isHydrating,
refQueue
refQueue,
doc
);

c.base = newVNode._dom;
Expand Down Expand Up @@ -304,7 +307,8 @@ export function diff(
excessDomChildren,
commitQueue,
isHydrating,
refQueue
refQueue,
doc,
);
}

Expand Down Expand Up @@ -353,6 +357,7 @@ export function commitRoot(commitQueue, root, refQueue) {
* to invoke in commitRoot
* @param {boolean} isHydrating Whether or not we are in hydration
* @param {any[]} refQueue an array of elements needed to invoke refs
* @param {Document} doc The owner document of the parentNode
* @returns {PreactElement}
*/
function diffElementNodes(
Expand All @@ -364,7 +369,8 @@ function diffElementNodes(
excessDomChildren,
commitQueue,
isHydrating,
refQueue
refQueue,
doc
) {
let oldProps = oldVNode.props;
let newProps = newVNode.props;
Expand Down Expand Up @@ -408,10 +414,10 @@ function diffElementNodes(

if (dom == null) {
if (nodeType === null) {
return document.createTextNode(newProps);
return doc.createTextNode(newProps);
}

dom = document.createElementNS(
dom = doc.createElementNS(
namespace,
nodeType,
newProps.is && newProps
Expand All @@ -424,6 +430,7 @@ function diffElementNodes(
options._hydrationMismatch(newVNode, excessDomChildren);
isHydrating = false;
}

// we created a new parent, so none of the previously attached children can be reused:
excessDomChildren = null;
}
Expand Down Expand Up @@ -517,7 +524,8 @@ function diffElementNodes(
? excessDomChildren[0]
: oldVNode._children && getDomSibling(oldVNode, 0),
isHydrating,
refQueue
refQueue,
doc
);

// Remove children that are not part of any vnode.
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ interface ContainerNode {
readonly parentNode: ContainerNode | null;
readonly firstChild: ContainerNode | null;
readonly childNodes: ArrayLike<ContainerNode>;
readonly ownerDocument: Document | null;

contains(other: ContainerNode | null): boolean;
insertBefore(node: ContainerNode, child: ContainerNode | null): ContainerNode;
Expand Down
3 changes: 2 additions & 1 deletion src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export function render(vnode, parentDom, replaceNode) {
? oldVNode._dom
: parentDom.firstChild,
isHydrating,
refQueue
refQueue,
parentDom.ownerDocument
);

// Flush all queued effects
Expand Down
50 changes: 50 additions & 0 deletions test/browser/getOwnerDocument.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { createElement, render } from 'preact';
import { setupScratch, teardown } from '../_util/helpers';

/** @jsx createElement */

describe('parentDom.ownerDocument', () => {
/** @type {HTMLDivElement} */
let scratch;

before(() => {
scratch = setupScratch();
});

after(() => {
teardown(scratch);
});

it.skip('should reference the correct document from the parent node', () => {
let iframe = document.createElement('iframe');

scratch.appendChild(iframe);

let iframeDoc = iframe.contentDocument;

iframeDoc.write(
'<!DOCTYPE html><html><head></head><body><div></div></body></html>'
);

iframeDoc.close();

let rootTextSpy = sinon.spy(document, 'createTextNode');
let rootElementSpy = sinon.spy(document, 'createElement');

let iframeTextSpy = sinon.spy(iframeDoc, 'createTextNode');
let iframeElementSpy = sinon.spy(iframeDoc, 'createElement');

let iframeRootNode = iframeDoc.querySelector('div');

render(<span>Hello</span>, iframeRootNode);

expect(rootTextSpy).not.to.be.called;
expect(rootElementSpy).not.to.be.called;
expect(iframeTextSpy).to.be.called;
expect(iframeElementSpy).to.be.called;

expect(iframeRootNode.textContent).to.be.equal('Hello');
expect(iframeRootNode.firstChild.ownerDocument).to.be.equal(iframeDoc);
expect(iframeRootNode.firstChild.ownerDocument).to.not.be.equal(document);
});
});
Loading