Skip to content

Commit a19f2e5

Browse files
authored
refactor: set up function for setting innerHTML (#32404)
When setting `innerHTML`, we have to go through a different API internally. These changes add a file that we can replace during the sync process.
1 parent 72de755 commit a19f2e5

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

.ng-dev/google-sync-config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"src/**/*spec.ts",
2929
"src/cdk/schematics/**/*",
3030
"src/cdk/testing/private/**/*",
31+
"src/cdk/private/inner-html.ts",
3132
"src/material/schematics/**/*",
3233
"src/material/schematics/ng-generate/theme-color/**/*.bazel",
3334
"src/material/schematics/ng-generate/theme-color/index_bundled.d.ts",

src/cdk/private/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ng_project(
1111
assets = [":visually-hidden-styles"],
1212
deps = [
1313
"//:node_modules/@angular/core",
14+
"//:node_modules/@angular/platform-browser",
1415
],
1516
)
1617

src/cdk/private/inner-html.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import {SecurityContext} from '@angular/core';
10+
import {DomSanitizer, SafeHtml} from '@angular/platform-browser';
11+
import {trustedHTMLFromString} from './trusted-types';
12+
13+
// !!!Note!!! this file isn't synced into g3, but is replaced with a version that uses
14+
// internal-specific APIs. The internal version may have to be updated if the signature of
15+
// the function changes.
16+
17+
/** Sanitizes and sets the `innerHTML` of an element. */
18+
export function _setInnerHtml(element: HTMLElement, html: SafeHtml, sanitizer: DomSanitizer): void {
19+
const cleanHtml = sanitizer.sanitize(SecurityContext.HTML, html);
20+
21+
if (cleanHtml === null && (typeof ngDevMode === 'undefined' || ngDevMode)) {
22+
throw new Error(`Could not sanitize HTML: ${html}`);
23+
}
24+
25+
element.innerHTML = trustedHTMLFromString(cleanHtml || '') as unknown as string;
26+
}

0 commit comments

Comments
 (0)