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

add Node.prototype.getRootNode #485

Closed
wants to merge 4 commits into from
Closed
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
23 changes: 23 additions & 0 deletions polyfills/Node/prototype/getRootNode/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
aliases = []
dependencies = []
docs = "https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode"
spec = "https://dom.spec.whatwg.org/#dom-node-getrootnode"

[browsers]
android = "<6"
bb = "*"
chrome = "<54"
edge = "*"
edge_mob = "*"
firefox = "<53"
firefox_mob = "<53"
ie = "*"
ie_mob = "*"
ios_chr = "10.3"
ios_saf = "10.3"
op_mini = "*"
op_mob = "<41"
opera = "<41"
safari = "<10.1"
samsung_mob = "<6"

1 change: 1 addition & 0 deletions polyfills/Node/prototype/getRootNode/detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'getRootNode' in Node.prototype
47 changes: 47 additions & 0 deletions polyfills/Node/prototype/getRootNode/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

/**
* Returns `this`’s shadow-including root if options’s `composed` is true.
* Returns `this`’s root otherwise.
*
* The root of an object is itself, if its parent is null, or else it is the root of its parent.
*
* The shadow-including root of an object is its root’s host’s shadow-including root,
* if the object’s root is a shadow root, and its root otherwise.
*
* https://dom.spec.whatwg.org/#dom-node-getrootnode
*
*/
function getRootNode(options) {
var composed = typeof options === 'object' && Boolean(options.composed);

return composed ? getShadowIncludingRoot(this) : getRoot(this);
}

function getShadowIncludingRoot(node) {
var root = getRoot(node);

if (isShadowRoot(root)) {
return getShadowIncludingRoot(root.host);
}

return root;
}

function getRoot(node) {
if (node.parentNode != null) {
return getRoot(node.parentNode);
}

return node;
}

function isShadowRoot(node) {
return node.nodeName === '#document-fragment' && node.constructor.name === 'ShadowRoot';
}

Object.defineProperty(Node.prototype, 'getRootNode', {
enumerable: false,
configurable: false,
value: getRootNode
});
136 changes: 136 additions & 0 deletions polyfills/Node/prototype/getRootNode/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/* eslint-env mocha */
/* globals proclaim */

function supportsShadowRoot() {
return typeof Element.prototype.attachShadow === "function";
}

it("is a function", function() {
proclaim.isFunction(Node.prototype.getRootNode);
});

it("has correct arity", function() {
proclaim.arity(Node.prototype.getRootNode, 1);
});

it("has correct name", function() {
proclaim.hasName(Node.prototype.getRootNode, "getRootNode");
});

it("is not enumerable", function() {
proclaim.isNotEnumerable(Node.prototype, "getRootNode");
});

describe("Node.prototype.getRootNode", function describeGetRootNode() {
function createShadowRoot(document) {
var node = document.createElement("div");
document.body.appendChild(node);
var shadowRoot = node.attachShadow({ mode: "open" });
var shadowNode = document.createElement("div");
shadowRoot.appendChild(shadowNode);

return { root: shadowRoot, node: shadowNode };
}

describe("returns the root of detached trees", function testGetRootDetached() {
function run(document) {
var rootNode = document.createElement("div");
var childNode = document.createElement("div");
var descendantNode = document.createElement("div");

rootNode.appendChild(childNode);
childNode.appendChild(descendantNode);

proclaim.deepStrictEqual(descendantNode.getRootNode(), rootNode);
}

it("current realm", function inRealm() {
run(window.document);
});
});

describe("returns the root of attached trees", function testGetRootAttached() {
function run(document) {
var rootNode = document.createElement("div");
var childNode = document.createElement("div");
var descendantNode = document.createElement("div");

document.body.appendChild(rootNode);
rootNode.appendChild(childNode);
childNode.appendChild(descendantNode);

proclaim.deepStrictEqual(descendantNode.getRootNode(), document);
}

it("current realm", function inRealm() {
run(window.document);
});
});

describe("returns itself if it is the root", function testGetSelf() {
function run(document) {
var detachedNode = document.createElement("div");

proclaim.deepStrictEqual(detachedNode.getRootNode(), detachedNode);
proclaim.deepStrictEqual(document.getRootNode(), document);
}

it("current realm", function inRealm() {
run(window.document);
});
});

describe("works with shadow roots", function testGetShadow() {
function run(document) {
var elems = createShadowRoot(document);
proclaim.deepStrictEqual(elems.node.getRootNode(), elems.root);
}

it("current realm", function inRealm() {
if (!supportsShadowRoot()) {
this.skip();
return;
}

run(window.document);
});
});

describe("\"composed\" option returns the shadow root's host's root", function testGetShadowsRoot() {
function run(document) {
var elems = createShadowRoot(document);
proclaim.deepStrictEqual(
elems.node.getRootNode({ composed: true }),
document
);
}

it("current realm", function inRealm() {
if (!supportsShadowRoot()) {
this.skip();
return;
}

run(window.document);
});
});

describe('"composed" option defaults to false', function testComposed() {
function run(document) {
var elems = createShadowRoot(document);
proclaim.deepStrictEqual(
elems.node.getRootNode(),
elems.node.getRootNode({ composed: false })
);
}

it("current realm", function inRealm() {
if (!supportsShadowRoot()) {
this.skip();
return;
}

run(window.document);
});
});
});