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

🐛 allow ampshadow to be root for ContextNode #40099

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
5 changes: 3 additions & 2 deletions src/core/context/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {Values} from './values';
const NODE_PROP = '__AMP_NODE';
const ASSIGNED_SLOT_PROP = '__AMP_ASSIGNED_SLOT';
const AMP_PREFIX = 'AMP-';
const AMPDOC_PROP = '__AMPDOC';

// Relevant node types.
// See https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType.
Expand Down Expand Up @@ -192,14 +193,14 @@ export class ContextNode {
this.name = name;

/**
* Whether this node is a root. The Document DOM nodes are automatically
* Whether this node is a root. The Document DOM nodes and ampdoc roots are automatically
* considered as roots. But other nodes can become roots as well
* (e.g. shadow roots) via `setIsRoot()` API.
*
* @package
* @type {boolean}
*/
this.isRoot = node.nodeType == DOCUMENT_NODE;
this.isRoot = node.nodeType == DOCUMENT_NODE || !!node[AMPDOC_PROP];

/**
* The root context node. Always available for a DOM node connected to a
Expand Down
3 changes: 3 additions & 0 deletions src/core/context/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {ContextNode} from './node';

export interface IContextProp<T, DEP> {
/**
* A globally unique key. Extensions must use a fully qualified name such
Expand Down Expand Up @@ -92,5 +93,7 @@ declare global {

// Used to map a Node to its assigned slot.
__AMP_ASSIGNED_SLOT?: Node;

__AMPDOC?: import('../../service/ampdoc-impl').AmpDoc;
}
}
17 changes: 17 additions & 0 deletions test/unit/core/context/test-node-discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ describes.realWin('ContextNode', {}, (env) => {
});
});

it('should create a ampdoc holder shadow root', () => {
const frag = doc.createDocumentFragment();

frag.__AMPDOC = {};

const cn = ContextNode.get(frag);
expect(cn.node).to.equal(frag);
// Parent always starts as null.
expectContext(cn, {
parent: null,
isRoot: true,
root: frag,
children: [],
discoverable: false,
});
});

it('should create a document node', () => {
const cn = ContextNode.get(doc);
expect(cn.node).to.equal(doc);
Expand Down
Loading