TrustedHTML: toJSON() メソッド
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
メモ: この機能はウェブワーカー内で利用可能です。
toJSON()
は TrustedHTML
インターフェイスのメソッドで、格納されているデータの JSON 表現を返します。
構文
js
toJSON()
引数
なし。
返値
文字列で、格納されているデータの JSON 表現が入ります。
例
定数 escaped
は、信頼型ポリシー escapeHTMLPolicy によって作成されたオブジェクトです。 toString()
メソッドは、文書に安全に挿入できる文字列を返します。
js
const escapeHTMLPolicy = trustedTypes.createPolicy("myEscapePolicy", {
createHTML: (string) => string.replace(/</g, "<"),
});
const escaped = escapeHTMLPolicy.createHTML("<img src=x onerror=alert(1)>");
console.log(escaped.toJSON());
仕様書
Specification |
---|
Trusted Types> # dom-trustedhtml-tojson> |
ブラウザーの互換性
Loading…