Node.prototype.getRootNode Polyfill

1.0.0 · active · verified Sun Apr 19

This package provides a polyfill for the `Node.prototype.getRootNode` DOM method, as defined by the WHATWG DOM specification. It allows developers to use this modern DOM API in environments that do not natively support it, ensuring consistent behavior across various browsers and Node.js environments where DOM-like structures might exist (e.g., JSDOM). The current stable version is 1.0.0. Polyfills like this typically have a low release cadence, updating primarily for specification changes or critical bug fixes, as their goal is to align with native browser implementations. Its key differentiator is its focused and spec-compliant implementation of a specific web standard, enabling forward compatibility for applications targeting modern DOM APIs while supporting older runtimes. This project helps bridge the gap for applications needing to support a wide range of browser versions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to apply the `getRootNode` polyfill and then use the method on simulated DOM-like nodes. It showcases the behavior of the `composed` option for both regular and shadow DOM-like hierarchies in a runnable JavaScript environment.

// Quickstart:
// Ensure the polyfill is applied early in your application lifecycle.
// In a browser environment, this would extend the native Node.prototype.
require('get-root-node-polyfill/implement');

// A minimal mock-up of a DOM Node for demonstration in a non-browser environment
// In a real browser application, you'd use actual DOM elements.
class MockNode {
  constructor(name, parent = null, host = null) {
    this.nodeName = name; // For identification in logs
    this.parentNode = parent; // Standard DOM parent
    this.host = host;       // For elements inside a Shadow DOM, this points to the Shadow Host
  }

  // Simulate calling the getRootNode method (which is now polyfilled on Node.prototype)
  // If MockNode were a real Node, we'd just call this.getRootNode(options)
  // For this example, we directly call the polyfilled method, passing 'this' as context.
  getTheRootNode(options) {
    // The polyfill, once 'implement' is called, makes this available on Node.prototype.
    // We simulate its call here for our mock object.
    const getRootNodePolyfill = require('get-root-node-polyfill');
    return getRootNodePolyfill.call(this, options);
  }
}

// 1. Regular DOM-like structure
const docRoot = new MockNode('document');
const body = new MockNode('body', docRoot);
const div = new MockNode('div', body);
const span = new MockNode('span', div);

console.log('--- Regular DOM-like structure ---');
// Get root node for a regular element (span)
let root = span.getTheRootNode({ composed: true });
console.log(`Span root (composed: true): ${root.nodeName} (Expected: document - ${root.nodeName === docRoot.nodeName})`);

root = span.getTheRootNode({ composed: false });
console.log(`Span root (composed: false): ${root.nodeName} (Expected: document - ${root.nodeName === docRoot.nodeName})`);


// 2. Structure involving a "Shadow DOM" host
// The shadow host is 'shadow-host-element'
const shadowHostElement = new MockNode('shadow-host-element', body);
// The shadow root itself is conceptually contained by shadowHostElement.
// For the purpose of getRootNode, an element *inside* a shadow tree has its 'host' property set.
const elementInShadowTree = new MockNode('element-in-shadow-tree', null, shadowHostElement); // parentNode is null, host is shadowHostElement

console.log('
--- Shadow DOM-like structure ---');
// Get root node for element within shadow tree
root = elementInShadowTree.getTheRootNode({ composed: true });
console.log(`Element in Shadow Tree root (composed: true): ${root.nodeName} (Expected: document - ${root.nodeName === docRoot.nodeName})`);

root = elementInShadowTree.getTheRootNode({ composed: false });
console.log(`Element in Shadow Tree root (composed: false): ${root.nodeName} (Expected: shadow-host-element - ${root.nodeName === shadowHostElement.nodeName})`);

// Check if the polyfill was actually applied
const isImplementedNatively = require('get-root-node-polyfill/is-implemented')();
console.log(`
Is Node.prototype.getRootNode natively implemented? ${isImplementedNatively ? 'Yes' : 'No (polyfill active)'}`);

view raw JSON →