|
21 | 21 | */ |
22 | 22 |
|
23 | 23 | import { registerScriptlet } from './base.js'; |
| 24 | +import { runAt } from './run-at.js'; |
24 | 25 | import { safeSelf } from './safe-self.js'; |
25 | 26 |
|
26 | 27 | /******************************************************************************/ |
@@ -55,6 +56,7 @@ function trustedCreateHTML( |
55 | 56 | if ( htmlStr === '' ) { return; } |
56 | 57 | const safe = safeSelf(); |
57 | 58 | const logPrefix = safe.makeLogPrefix('trusted-create-html', parentSelector, htmlStr, durationStr); |
| 59 | + const extraArgs = safe.getExtraArgs(Array.from(arguments), 3); |
58 | 60 | // We do not want to recursively create elements |
59 | 61 | self.trustedCreateHTML = true; |
60 | 62 | let ancestor = self.frameElement; |
@@ -94,17 +96,37 @@ function trustedCreateHTML( |
94 | 96 | setTimeout(remove, duration); |
95 | 97 | return true; |
96 | 98 | }; |
97 | | - if ( append() ) { return; } |
98 | | - const observer = new MutationObserver(( ) => { |
99 | | - if ( append() === false ) { return; } |
100 | | - observer.disconnect(); |
101 | | - }); |
102 | | - observer.observe(document, { childList: true, subtree: true }); |
| 99 | + const start = ( ) => { |
| 100 | + if ( append() ) { return; } |
| 101 | + const observer = new MutationObserver(( ) => { |
| 102 | + if ( append() === false ) { return; } |
| 103 | + observer.disconnect(); |
| 104 | + }); |
| 105 | + const observerOptions = { |
| 106 | + childList: true, |
| 107 | + subtree: true, |
| 108 | + }; |
| 109 | + if ( /[#.[]/.test(parentSelector) ) { |
| 110 | + observerOptions.attributes = true; |
| 111 | + if ( parentSelector.includes('[') === false ) { |
| 112 | + observerOptions.attributeFilter = []; |
| 113 | + if ( parentSelector.includes('#') ) { |
| 114 | + observerOptions.attributeFilter.push('id'); |
| 115 | + } |
| 116 | + if ( parentSelector.includes('.') ) { |
| 117 | + observerOptions.attributeFilter.push('class'); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + observer.observe(document, observerOptions); |
| 122 | + }; |
| 123 | + runAt(start, extraArgs.runAt || 'loading'); |
103 | 124 | } |
104 | 125 | registerScriptlet(trustedCreateHTML, { |
105 | 126 | name: 'trusted-create-html.js', |
106 | 127 | requiresTrust: true, |
107 | 128 | dependencies: [ |
| 129 | + runAt, |
108 | 130 | safeSelf, |
109 | 131 | ], |
110 | 132 | world: 'ISOLATED', |
|
0 commit comments