MCPcopy Index your code
hub / github.com/ampproject/amphtml / polyfill

Function polyfill

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

* Does the polyfilling. * @param {!Window} win

(win)

Source from the content-addressed store, hash-verified

649 * @param {!Window} win
650 */
651function polyfill(win) {
652 const {Element, HTMLElement, document} = win;
653 const {createElement} = document;
654
655 const registry = new Registry(win);
656 const customElements = new CustomElementRegistry(win, registry);
657
658 // Expose the custom element registry.
659 // Object.getOwnPropertyDescriptor(window, 'customElements')
660 // {get: ƒ, set: undefined, enumerable: true, configurable: true}
661 Object.defineProperty(win, 'customElements', {
662 enumerable: true,
663 configurable: true,
664 // writable: false,
665 value: customElements,
666 });
667
668 // Have to patch shadow methods now, since there's no way to find shadow trees
669 // later.
670 const elProto = Element.prototype;
671 const {attachShadow, createShadowRoot} = elProto;
672 if (attachShadow) {
673 /**
674 * @param {{mode: string}} unused
675 * @return {!ShadowRoot}
676 */
677 elProto.attachShadow = function (unused) {
678 const shadow = attachShadow.apply(this, arguments);
679 registry.observe(shadow);
680 return shadow;
681 };
682 // Necessary for Shadow AMP
683 elProto.attachShadow.toString = function () {
684 return attachShadow.toString();
685 };
686 }
687 if (createShadowRoot) {
688 /** @return {!ShadowRoot} */
689 elProto.createShadowRoot = function () {
690 const shadow = createShadowRoot.apply(this, arguments);
691 registry.observe(shadow);
692 return shadow;
693 };
694 // Necessary for Shadow AMP
695 elProto.createShadowRoot.toString = function () {
696 return createShadowRoot.toString();
697 };
698 }
699
700 /**
701 * You can't use the real HTMLElement constructor, because you can't subclass
702 * it without using native classes. So, mock its approximation using
703 * createElement.
704 * @return {!ElementOrigDef}
705 */
706 function HTMLElementPolyfill() {
707 const {constructor} = this;
708

Callers 1

installFunction · 0.85

Calls 4

observeMethod · 0.95
subClassFunction · 0.85
applyMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected