* Attaches the shadow root and calls the supplied DOM builder. * @param {!Element} hostElement * @param {string} url * @param {!{[key: string]: string}|undefined} params * @param {function(!Object, !ShadowRoot, * !./service/ampdoc-impl.AmpDocShadow):!Promise} builder * @return {!./
(hostElement, url, params, builder)
| 61 | * @private |
| 62 | */ |
| 63 | attachShadowDoc_(hostElement, url, params, builder) { |
| 64 | params = params || Object.create(null); |
| 65 | this.purgeShadowRoots_(); |
| 66 | |
| 67 | setStyle(hostElement, 'visibility', 'hidden'); |
| 68 | const shadowRoot = createShadowRoot(hostElement); |
| 69 | |
| 70 | // TODO: closeShadowRoot_ is asynchronous. While this safety check is well |
| 71 | // intentioned, it leads to a race between unlayout and layout of custom |
| 72 | // elements. |
| 73 | if (shadowRoot.AMP) { |
| 74 | user().warn(TAG, "Shadow doc wasn't previously closed"); |
| 75 | this.closeShadowRoot_(shadowRoot); |
| 76 | } |
| 77 | |
| 78 | const amp = {}; |
| 79 | shadowRoot.AMP = amp; |
| 80 | amp.url = url; |
| 81 | const {origin} = parseUrlDeprecated(url); |
| 82 | |
| 83 | const ampdoc = this.ampdocService_.installShadowDoc(url, shadowRoot, { |
| 84 | params, |
| 85 | }); |
| 86 | /** @const {!./service/ampdoc-impl.AmpDocShadow} */ |
| 87 | amp.ampdoc = ampdoc; |
| 88 | dev().fine(TAG, 'Attach to shadow root:', shadowRoot, ampdoc); |
| 89 | |
| 90 | // Install runtime CSS. |
| 91 | installStylesForDoc( |
| 92 | ampdoc, |
| 93 | AMP.combinedCss, |
| 94 | /* callback */ null, |
| 95 | /* opt_isRuntimeCss */ true |
| 96 | ); |
| 97 | // Instal doc services. |
| 98 | AMP.installAmpdocServices(ampdoc); |
| 99 | |
| 100 | const viewer = Services.viewerForDoc(ampdoc); |
| 101 | |
| 102 | /** |
| 103 | * Sets the document's visibility state. |
| 104 | * @param {!VisibilityState_Enum} state |
| 105 | */ |
| 106 | amp['setVisibilityState'] = function (state) { |
| 107 | ampdoc.overrideVisibilityState(state); |
| 108 | }; |
| 109 | |
| 110 | // Messaging pipe. |
| 111 | /** |
| 112 | * Posts message to the ampdoc. |
| 113 | * @param {string} eventType |
| 114 | * @param {!JsonObject} data |
| 115 | * @param {boolean} unusedAwaitResponse |
| 116 | * @return {(!Promise<*>|undefined)} |
| 117 | */ |
| 118 | amp['postMessage'] = viewer.receiveMessage.bind(viewer); |
| 119 | |
| 120 | /** @type {?function(string, *, boolean):(!Promise<*>|undefined)|undefined} */ |
no test coverage detected