UNPKG

@webcomponents/custom-elements

Version:
79 lines (70 loc) 2.44 kB
import Native from './Native.js'; import CustomElementInternals from '../CustomElementInternals.js'; import * as Utilities from '../Utilities.js'; import PatchParentNode from './Interface/ParentNode.js'; /** * @param {!CustomElementInternals} internals */ export default function(internals) { Utilities.setPropertyUnchecked(Document.prototype, 'createElement', /** * @this {Document} * @param {string} localName * @return {!Element} */ function(localName) { // Only create custom elements if this document is associated with the registry. if (this.__CE_hasRegistry) { const definition = internals.localNameToDefinition(localName); if (definition) { return new (definition.constructor)(); } } const result = /** @type {!Element} */ (Native.Document_createElement.call(this, localName)); internals.patch(result); return result; }); Utilities.setPropertyUnchecked(Document.prototype, 'importNode', /** * @this {Document} * @param {!Node} node * @param {boolean=} deep * @return {!Node} */ function(node, deep) { const clone = Native.Document_importNode.call(this, node, deep); // Only create custom elements if this document is associated with the registry. if (!this.__CE_hasRegistry) { internals.patchTree(clone); } else { internals.patchAndUpgradeTree(clone); } return clone; }); const NS_HTML = "http://www.w3.org/1999/xhtml"; Utilities.setPropertyUnchecked(Document.prototype, 'createElementNS', /** * @this {Document} * @param {?string} namespace * @param {string} localName * @return {!Element} */ function(namespace, localName) { // Only create custom elements if this document is associated with the registry. if (this.__CE_hasRegistry && (namespace === null || namespace === NS_HTML)) { const definition = internals.localNameToDefinition(localName); if (definition) { return new (definition.constructor)(); } } const result = /** @type {!Element} */ (Native.Document_createElementNS.call(this, namespace, localName)); internals.patch(result); return result; }); PatchParentNode(internals, Document.prototype, { prepend: Native.Document_prepend, append: Native.Document_append, }); };