Skip to content

Commit 84aedb6

Browse files
authored
Renamed window.TrustedTypes to window.trustedTypes (#205)
Fixed #177. The polyfill will copy existing (i.e. native) window.TrustedTypes to window.trustedTypes. Please use window.trustedTypes or (window.trustedTypes || window.TrustedTypes) from now on.
1 parent 84d6179 commit 84aedb6

21 files changed

+171
-144
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ The ES5 / ES6 builds can be loaded directly in the browsers. There are two varia
2525
<!-- API only -->
2626
<script src="https://wicg.github.io/trusted-types/dist/es5/trustedtypes.api_only.build.js"></script>
2727
<script>
28-
const p = TrustedTypes.createPolicy('foo', ...)
28+
const p = trustedTypes.createPolicy('foo', ...)
2929
document.body.innerHTML = p.createHTML('foo'); // works
3030
document.body.innerHTML = 'foo'; // but this one works too (no enforcement).
3131
</script>
@@ -35,8 +35,8 @@ The ES5 / ES6 builds can be loaded directly in the browsers. There are two varia
3535
<!-- Full -->
3636
<script src="https://wicg.github.io/trusted-types/dist/es5/trustedtypes.build.js" data-csp="trusted-types foo bar"></script>
3737
<script>
38-
TrustedTypes.createPolicy('foo', ...);
39-
TrustedTypes.createPolicy('unknown', ...); // throws
38+
trustedTypes.createPolicy('foo', ...);
39+
trustedTypes.createPolicy('unknown', ...); // throws
4040
document.body.innerHTML = 'foo'; // throws
4141
</script>
4242
```
@@ -57,10 +57,10 @@ tt.createPolicy(...);
5757
### Tinyfill
5858

5959
Due to the way the API is designed, it's possible to polyfill the most important
60-
API surface (`TrustedTypes.createPolicy` function) with the following snippet:
60+
API surface (`trustedTypes.createPolicy` function) with the following snippet:
6161

6262
```javascript
63-
if(typeof TrustedTypes == 'undefined')TrustedTypes={createPolicy:(n, rules) => rules};
63+
if(typeof trustedTypes == 'undefined')trustedTypes={createPolicy:(n, rules) => rules};
6464
```
6565

6666
It does not enable the enforcement, but allows the creation of policies that

demo/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ <h1>Trusted Types demo</h1>
4949
(function() {
5050
// Create an unsafe policy - it can only be used on a trusted input,
5151
// inside of this function.
52-
var policy = TrustedTypes.createPolicy('unsafe', {
52+
var policy = trustedTypes.createPolicy('unsafe', {
5353
'createHTML': function(unsafe) {
5454
return unsafe;
5555
},
@@ -58,7 +58,7 @@ <h1>Trusted Types demo</h1>
5858
})();
5959

6060
// Create escaping policy
61-
var escapePolicy = TrustedTypes.createPolicy('escape', {
61+
var escapePolicy = trustedTypes.createPolicy('escape', {
6262
'createHTML': function(unsafe) {
6363
return unsafe
6464
.replace(/&/g, "&amp;")
@@ -128,7 +128,7 @@ <h1>Trusted Types demo</h1>
128128
el.appendChild(document.createElement('script')).src = 'data:,';
129129
}, true);
130130
runTest(['creating policy from outside whitelist'], function(el) {
131-
TrustedTypes.createPolicy('foo', {});
131+
trustedTypes.createPolicy('foo', {});
132132
}, true);
133133

134134
</script>

dist/cjs/trustedtypes.api_only.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ const trustedTypesBuilderTestOnly = function() {
565565
}
566566
} else {
567567
// eslint-disable-next-line no-console
568-
console.warn('TrustedTypes.createPolicy ' + pName +
568+
console.warn('trustedTypes.createPolicy ' + pName +
569569
' was given an empty policy');
570570
}
571571
freeze(innerPolicy);
@@ -637,7 +637,7 @@ const trustedTypesBuilderTestOnly = function() {
637637
});
638638

639639
return {
640-
TrustedTypes: freeze(api),
640+
trustedTypes: freeze(api),
641641
setAllowedPolicyNames,
642642
getDefaultPolicy,
643643
resetDefaultPolicy,
@@ -646,7 +646,7 @@ const trustedTypesBuilderTestOnly = function() {
646646

647647

648648
const {
649-
TrustedTypes,
649+
trustedTypes,
650650
setAllowedPolicyNames,
651651
getDefaultPolicy,
652652
resetDefaultPolicy,
@@ -661,15 +661,25 @@ const {
661661
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
662662
*/
663663

664-
const tt = TrustedTypes;
664+
const tt = trustedTypes;
665665

666666
/**
667667
* Sets up the public Trusted Types API in the global object.
668668
*/
669669
function setupPolyfill() {
670-
// Make sure Closure compiler exposes the names.
671-
if (typeof window === 'undefined' ||
672-
typeof window['TrustedTypes'] !== 'undefined') {
670+
// We use array accessors to make sure Closure compiler will not alter the
671+
// names of the properties..
672+
if (typeof window === 'undefined') {
673+
return;
674+
}
675+
const rootProperty = 'trustedTypes';
676+
677+
// Convert old window.TrustedTypes to window.trustedTypes.
678+
if (window['TrustedTypes'] && typeof window[rootProperty] === 'undefined') {
679+
window[rootProperty] = Object.freeze(window['TrustedTypes']);
680+
}
681+
682+
if (typeof window[rootProperty] !== 'undefined') {
673683
return;
674684
}
675685

@@ -687,7 +697,7 @@ function setupPolyfill() {
687697
'emptyHTML': tt.emptyHTML,
688698
'_isPolyfill_': true,
689699
});
690-
window['TrustedTypes'] = Object.freeze(publicApi);
700+
window[rootProperty] = Object.freeze(publicApi);
691701

692702
window['TrustedHTML'] = tt.TrustedHTML;
693703
window['TrustedURL'] = tt.TrustedURL;

dist/es5/trustedtypes.api_only.build.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/es5/trustedtypes.api_only.build.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)