Rename trusted-create-element to trusted-create-html

To avoid confusion with AG's own `trusted-create-element`, which has
a different syntax and a different purpose.
This commit is contained in:
Raymond Hill 2025-04-13 07:34:36 -04:00
parent c15dc9d8ff
commit cac420a22d
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
2 changed files with 17 additions and 16 deletions

View file

@ -26,7 +26,7 @@ import { safeSelf } from './safe-self.js';
/******************************************************************************/
/**
* @scriptlet trusted-create-element
* @scriptlet trusted-create-html
*
* @description
* Element(s) from a parsed HTML string are added as child element(s) to a
@ -46,7 +46,7 @@ import { safeSelf } from './safe-self.js';
*
* */
function trustedCreateElement(
function trustedCreateHTML(
parentSelector,
htmlStr = '',
durationStr = ''
@ -54,16 +54,16 @@ function trustedCreateElement(
if ( parentSelector === '' ) { return; }
if ( htmlStr === '' ) { return; }
const safe = safeSelf();
const logPrefix = safe.makeLogPrefix('trusted-create-element', parentSelector, htmlStr, durationStr);
const logPrefix = safe.makeLogPrefix('trusted-create-html', parentSelector, htmlStr, durationStr);
// We do not want to recursively create elements
self.trustedCreateElement = true;
self.trustedCreateHTML = true;
let ancestor = self.frameElement;
while ( ancestor !== null ) {
const doc = ancestor.ownerDocument;
if ( doc === null ) { break; }
const win = doc.defaultView;
if ( win === null ) { break; }
if ( win.trustedCreateElement ) { return; }
if ( win.trustedCreateHTML ) { return; }
ancestor = ancestor.frameElement;
}
const duration = parseInt(durationStr, 10);
@ -71,24 +71,25 @@ function trustedCreateElement(
const externalDoc = domParser.parseFromString(htmlStr, 'text/html');
const docFragment = new DocumentFragment();
const toRemove = [];
for ( const external of externalDoc.querySelectorAll('body > *') ) {
const imported = document.adoptNode(external);
docFragment.append(imported);
while ( externalDoc.body.firstChild !== null ) {
const imported = document.adoptNode(externalDoc.body.firstChild);
docFragment.appendChild(imported);
if ( isNaN(duration) ) { continue; }
toRemove.push(imported);
}
if ( docFragment.childElementCount === 0 ) { return; }
if ( docFragment.firstChild === null ) { return; }
const remove = ( ) => {
for ( const elem of toRemove ) {
elem.remove();
for ( const node of toRemove ) {
if ( node.parentNode === null ) { continue; }
node.parentNode.removeChild(node);
}
safe.uboLog(logPrefix, 'Element(s) removed');
safe.uboLog(logPrefix, 'Node(s) removed');
};
const append = ( ) => {
const parent = document.querySelector(parentSelector);
if ( parent === null ) { return false; }
parent.append(docFragment);
safe.uboLog(logPrefix, 'Element(s) appended');
safe.uboLog(logPrefix, 'Node(s) appended');
if ( toRemove.length === 0 ) { return true; }
setTimeout(remove, duration);
return true;
@ -100,8 +101,8 @@ function trustedCreateElement(
});
observer.observe(document, { childList: true, subtree: true });
}
registerScriptlet(trustedCreateElement, {
name: 'trusted-create-element.js',
registerScriptlet(trustedCreateHTML, {
name: 'trusted-create-html.js',
requiresTrust: true,
dependencies: [
safeSelf,

View file

@ -21,7 +21,7 @@
*/
import './attribute.js';
import './create-element.js';
import './create-html.js';
import './href-sanitizer.js';
import './json-edit.js';
import './json-prune.js';