refactor(core): don't use innerHTML in DOMTestComponentRenderer (#41099)

The use of innerHTML is unnecessary and causes TrustedType violations.

PR Close #41099
This commit is contained in:
Igor Minar 2021-03-05 20:02:21 -08:00 committed by Andrew Kushnir
parent 32dd3c5dd1
commit 8c062493a0
1 changed files with 3 additions and 12 deletions

View File

@ -20,23 +20,14 @@ export class DOMTestComponentRenderer extends TestComponentRenderer {
} }
insertRootElement(rootElId: string) { insertRootElement(rootElId: string) {
const template = getDOM().getDefaultDocument().createElement('template'); const rootElement = getDOM().getDefaultDocument().createElement('div');
template.innerHTML = `<div id="${rootElId}"></div>`; rootElement.setAttribute('id', rootElId);
const rootEl = <HTMLElement>getContent(template).firstChild;
// TODO(juliemr): can/should this be optional? // TODO(juliemr): can/should this be optional?
const oldRoots = this._doc.querySelectorAll('[id^=root]'); const oldRoots = this._doc.querySelectorAll('[id^=root]');
for (let i = 0; i < oldRoots.length; i++) { for (let i = 0; i < oldRoots.length; i++) {
getDOM().remove(oldRoots[i]); getDOM().remove(oldRoots[i]);
} }
this._doc.body.appendChild(rootEl); this._doc.body.appendChild(rootElement);
}
}
function getContent(node: Node): Node {
if ('content' in node) {
return (<any>node).content;
} else {
return node;
} }
} }