MCPcopy Create free account
hub / github.com/ampproject/amphtml / HTMLElementPolyfill

Function HTMLElementPolyfill

src/polyfills/custom-elements.js:706–740  ·  view source on GitHub ↗

* You can't use the real HTMLElement constructor, because you can't subclass * it without using native classes. So, mock its approximation using * createElement. * @return {!ElementOrigDef}

()

Source from the content-addressed store, hash-verified

704 * @return {!ElementOrigDef}
705 */
706 function HTMLElementPolyfill() {
707 const {constructor} = this;
708
709 // If we're upgrading an already created custom element, we can't create
710 // another new node (by the spec, it must be the same node).
711 let el = registry.current();
712
713 // If there's not a already created custom element, we're being invoked via
714 // `new`ing the constructor.
715 //
716 // Technically, we could get here via createElement, but we patched that.
717 // If it the custom element was registered, the patch turned it into a
718 // `new` call.
719 // If it was not registered, the native createElement is used. And if
720 // native createElement is being used and we got to this code, we're really
721 // in an infinite loop (a native createElement call just below) so we've
722 // got bigger problems.
723 //
724 // So just take my word we got here via `new`.
725 if (!el) {
726 // The custom element definition is an invariant. If the custom element
727 // is registered, everything works. If it's not, it throws in the member
728 // property access (only defined custom elements can be directly
729 // constructed via `new`).
730 const def = registry.getByConstructor(constructor);
731 el = createElement.call(document, def.name);
732 }
733
734 // Finally, if the node was already constructed, we need to reset its
735 // prototype to the custom element prototype. And if it wasn't already
736 // constructed, we created a new node via native createElement, and we need
737 // to reset its prototype. Basically always reset the prototype.
738 setPrototypeOf(el, constructor.prototype);
739 return el;
740 }
741 subClass(HTMLElement, HTMLElementPolyfill);
742
743 // Expose the polyfilled HTMLElement constructor for everyone to extend from.

Callers

nothing calls this directly

Calls 3

setPrototypeOfFunction · 0.85
currentMethod · 0.80
getByConstructorMethod · 0.80

Tested by

no test coverage detected