forked from google/safevalues
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compat.ts
224 lines (193 loc) · 9.74 KB
/
compat.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* @license
* Copyright 2021 Google LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Defines build level visibility restricted unwrappers that
* support both Closure and TS safe types.
*/
import * as googUnchecked from 'goog:goog.html.reviewed'; // from //third_party/javascript/closure/html:reviewed
import GoogSafeStyle from 'goog:goog.html.SafeStyle'; // from //third_party/javascript/closure/html:safestyle
import GoogSafeStyleSheet from 'goog:goog.html.SafeStyleSheet'; // from //third_party/javascript/closure/html:safestylesheet
import GoogSafeUrl from 'goog:goog.html.SafeUrl'; // from //third_party/javascript/closure/html:safeurl
import GoogHtml from 'goog:goog.html.TrustedHTML'; // from //third_party/javascript/closure/html:safehtml
import GoogScript from 'goog:goog.html.TrustedScript'; // from //third_party/javascript/closure/html:safescript
import GoogScriptUrl from 'goog:goog.html.TrustedScriptURL'; // from //third_party/javascript/closure/html:trustedresourceurl
import Const from 'goog:goog.string.Const'; // from //third_party/javascript/closure/string:const
import {TrustedHTML as TSTrustedHTML} from 'google3/javascript/typescript/safevalues/html';
import * as unchecked from 'google3/javascript/typescript/safevalues/reviewed';
import {SafeStyle as TSSafeStyle} from 'google3/javascript/typescript/safevalues/safe_style';
import {SafeStyleSheet as TSSafeStyleSheet} from 'google3/javascript/typescript/safevalues/safe_style_sheet';
import {unwrapHtmlForSink as tsUnwrapTrustedHTML, unwrapSafeStyle as tsUnwrapSafeStyle, unwrapSafeStyleSheet as tsUnwrapSafeStyleSheet, unwrapSafeUrl as tsUnwrapSafeUrl, unwrapScriptForSink as tsUnwrapTrustedScript, unwrapScriptUrl as tsUnwrapScriptUrl, unwrapScriptUrlForSink as tsUnwrapTrustedScriptURL} from 'google3/javascript/typescript/safevalues/safe_unwrappers';
import {SafeUrl as TSSafeUrl} from 'google3/javascript/typescript/safevalues/safe_url';
import {TrustedScript as TSTrustedScript} from 'google3/javascript/typescript/safevalues/script';
import {TrustedScriptURL as TSTrustedScriptURL} from 'google3/javascript/typescript/safevalues/script_url';
/** Compat type for Closure and TS SafeTypes. */
export type TrustedHTML = GoogHtml|TSTrustedHTML;
/** Compat type for Closure and TS SafeTypes. */
export type TrustedScript = GoogScript|TSTrustedScript;
/** Compat type for Closure and TS SafeTypes. */
export type SafeStyle = GoogSafeStyle|TSSafeStyle;
/** Compat type for Closure and TS SafeTypes. */
export type SafeStyleSheet = GoogSafeStyleSheet|TSSafeStyleSheet;
/** Compat type for Closure and TS SafeTypes. */
export type SafeUrl = GoogSafeUrl|TSSafeUrl;
/** Compat type for Closure and TS SafeTypes. */
export type TrustedScriptURL = GoogScriptUrl|TSTrustedScriptURL;
/** Guard function that works for boths Closure and TS SafeTypes. */
export function isHtml(u: unknown): u is TrustedHTML {
return u instanceof GoogHtml || u instanceof TSTrustedHTML;
}
/** Guard function that works for boths Closure and TS SafeTypes. */
export function isScript(u: unknown): u is TrustedScript {
return u instanceof GoogScript || u instanceof TSTrustedScript;
}
/** Guard function that works for boths Closure and TS SafeTypes. */
export function isSafeStyle(u: unknown): u is SafeStyle {
return u instanceof GoogSafeStyle || u instanceof TSSafeStyle;
}
/** Guard function that works for boths Closure and TS SafeTypes. */
export function isSafeStyleSheet(u: unknown): u is SafeStyleSheet {
return u instanceof GoogSafeStyleSheet || u instanceof TSSafeStyleSheet;
}
/** Guard function that works for boths Closure and TS SafeTypes. */
export function isSafeUrl(u: unknown): u is SafeUrl {
return u instanceof GoogSafeUrl || u instanceof TSSafeUrl;
}
/** Guard function that works for boths Closure and TS SafeTypes. */
export function isScriptUrl(u: unknown): u is TrustedScriptURL {
return u instanceof GoogScriptUrl || u instanceof TSTrustedScriptURL;
}
/** Safe unwrapper that support both Closure and TS safe types. */
export function unwrapHtmlForSink(html: TrustedHTML): string {
if (html instanceof TSTrustedHTML) {
return tsUnwrapTrustedHTML(html);
}
return GoogHtml.unwrapHtmlForSink(html) as string;
}
/** Safe unwrapper that support both Closure and TS safe types. */
export function unwrapScriptUrlForSink(url: TrustedScriptURL): string {
if (url instanceof TSTrustedScriptURL) {
return tsUnwrapTrustedScriptURL(url);
}
return GoogScriptUrl.unwrapScriptUrlForSink(url) as string;
}
/** Safe unwrapper that support both Closure and TS safe types. */
export function unwrapSafeStyle(style: SafeStyle): string {
if (style instanceof TSSafeStyle) {
return tsUnwrapSafeStyle(style);
}
return GoogSafeStyle.unwrap(style);
}
/**
* Safe unwrapper that support both Closure and TS safe types.
* @noinline due to b/182875660. This prevents a Closure optimization that
* breaks IE11 with the location object.
*/
export function unwrapSafeUrl(url: SafeUrl): string {
if (url instanceof TSSafeUrl) {
return tsUnwrapSafeUrl(url);
}
return GoogSafeUrl.unwrap(url);
}
/** Safe unwrapper that support both Closure and TS safe types. */
export function unwrapScriptUrl(url: TrustedScriptURL) {
if (url instanceof TSTrustedScriptURL) {
return tsUnwrapScriptUrl(url);
}
return GoogScriptUrl.unwrap(url);
}
/** Safe unwrapper that support both Closure and TS safe types. */
export function unwrapScriptForSink(script: TrustedScript): string {
if (script instanceof TSTrustedScript) {
return tsUnwrapTrustedScript(script);
}
return GoogScript.unwrapScriptForSink(script) as string;
}
/** Safe unwrapper that support both Closure and TS safe types. */
export function unwrapSafeStyleSheet(styleSheet: SafeStyleSheet): string {
if (styleSheet instanceof TSSafeStyleSheet) {
return tsUnwrapSafeStyleSheet(styleSheet);
}
return GoogSafeStyleSheet.unwrap(styleSheet);
}
/** Converts a goog.html.TrustedHTML into a TypeScript TrustedHTML */
export function toTsHtml(html: TrustedHTML): TSTrustedHTML {
return unchecked.htmlFromStringKnownToSatisfyTypeContract(
unwrapHtmlForSink(html).toString(), 'Conversion from closure');
}
/** Converts a TypeScript TrustedHTML into a goog.html.TrustedHTML */
export function fromTsHtml(html: TrustedHTML): GoogHtml {
return googUnchecked.htmlFromStringKnownToSatisfyTypeContract(
Const.from('TS-Closure conversions of the same types'),
unwrapHtmlForSink(html).toString());
}
/** Converts a goog.html.TrustedScript into a TypeScript TrustedScript */
export function toTsScript(script: TrustedScript): TSTrustedScript {
return unchecked.scriptFromStringKnownToSatisfyTypeContract(
unwrapScriptForSink(script).toString(), 'Conversion from closure');
}
/** Converts a TypeScript TrustedScript into a goog.html.TrustedScript */
export function fromTsScript(script: TrustedScript): GoogScript {
return googUnchecked.scriptFromStringKnownToSatisfyTypeContract(
Const.from('TS-Closure conversions of the same types'),
unwrapScriptForSink(script).toString());
}
/** Converts a goog.html.SafeUrl into a TypeScript SafeUrl */
export function toTsSafeUrl(url: SafeUrl): TSSafeUrl {
return unchecked.safeUrlFromStringKnownToSatisfyTypeContract(
unwrapSafeUrl(url), 'Conversion from closure');
}
/** Converts a TypeScript SafeUrl into a goog.html.SafeUrl */
export function fromTsSafeUrl(url: SafeUrl): GoogSafeUrl {
return googUnchecked.safeUrlFromStringKnownToSatisfyTypeContract(
Const.from('TS-Closure conversions of the same types'),
unwrapSafeUrl(url));
}
/**
* Converts a goog.html.TrustedScriptURL into a TypeScript TrustedScriptURL
*/
export function toTsScriptUrl(url: TrustedScriptURL): TSTrustedScriptURL {
return unchecked.scriptUrlFromStringKnownToSatisfyTypeContract(
unwrapScriptUrlForSink(url).toString(), 'Conversion from closure');
}
/**
* Converts a TypeScript TrustedScriptURL into a goog.html.TrustedScriptURL
*/
export function fromTsScriptUrl(url: TrustedScriptURL): GoogScriptUrl {
return googUnchecked.scriptUrlFromStringKnownToSatisfyTypeContract(
Const.from('TS-Closure conversions of the same types'),
unwrapScriptUrlForSink(url).toString());
}
/** Converts a goog.html.SafeStyle into a TypeScript SafeStyle */
export function toTsSafeStyle(style: SafeStyle): TSSafeStyle {
return unchecked.safeStyleFromStringKnownToSatisfyTypeContract(
unwrapSafeStyle(style), 'Conversion from closure');
}
/** Converts a TypeScript SafeStyle into a goog.html.SafeStyle */
export function fromTsSafeStyle(style: SafeStyle): GoogSafeStyle {
return googUnchecked.safeStyleFromStringKnownToSatisfyTypeContract(
Const.from('TS-Closure conversions of the same types'),
unwrapSafeStyle(style));
}
/** Converts a goog.html.SafeStyleSheet into a TypeScript SafeStyleSheet */
export function toTsSafeStyleSheet(sheet: SafeStyleSheet): TSSafeStyleSheet {
return unchecked.safeStyleSheetFromStringKnownToSatisfyTypeContract(
unwrapSafeStyleSheet(sheet), 'Conversion from closure');
}
/** Converts a TypeScript SafeStyleSheet into a goog.html.SafeStyleSheet */
export function fromTsSafeStyleSheet(sheet: SafeStyleSheet):
GoogSafeStyleSheet {
return googUnchecked.safeStyleSheetFromStringKnownToSatisfyTypeContract(
Const.from('TS-Closure conversions of the same types'),
unwrapSafeStyleSheet(sheet));
}