Skip to content

Commit

Permalink
Add Trusted Types support to wct-mocha. (#3503)
Browse files Browse the repository at this point in the history
  • Loading branch information
rictic authored Apr 30, 2020
1 parent d897ae2 commit 2f57496
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/wct-mocha/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
<!-- ## Unreleased -->
<!-- Add new, unreleased changes here. -->

## 1.1.0
* Added support for running with an enforced Trusted Types policy: https://github.com/WICG/trusted-types

## 1.0.1
* Added support for Mocha 6.0.0, which introduced a breaking change related to initialization of stats on Mocha's test-runner, preventing reporting of test results.

Expand Down
6 changes: 6 additions & 0 deletions packages/wct-mocha/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/wct-mocha/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"repository": "github:Polymer/tools",
"dependencies": {},
"devDependencies": {
"@types/socket.io": "^1.4.36"
"@types/socket.io": "^1.4.36",
"@types/trusted-types": "^1.0.4"
},
"peerDependencies": {
"mocha": "*"
Expand Down
16 changes: 14 additions & 2 deletions packages/wct-mocha/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import * as config from './config.js';
import * as reporters from './reporters.js';
import * as util from './util.js';

let policy = {
createHTML(v: string) {
return v;
}
};
if (window.trustedTypes) {
policy = window.trustedTypes.createPolicy('wct-mocha-init', policy) as
unknown as typeof policy;
}

/**
* Loads all environment scripts ...synchronously ...after us.
*/
Expand All @@ -33,15 +43,17 @@ export function loadSync() {
const url = util.expandUrl(path, config.get('root')!);
util.debug('Loading environment script:', url);
// Synchronous load.
document.write(`<script src="${encodeURI(url)}"></script>`);
document.write(
policy.createHTML(`<script src="${encodeURI(url)}"></script>`));
});
util.debug('Environment scripts loaded');
const imports = config.get('environmentImports');
imports.forEach((path) => {
const url = util.expandUrl(path, config.get('root')!);
util.debug('Loading environment import:', url);
// Synchronous load.
document.write(`<link rel="import" href="${encodeURI(url)}">`);
document.write(
policy.createHTML(`<link rel="import" href="${encodeURI(url)}">`));
});
util.debug('Environment imports loaded');
}
Expand Down
2 changes: 2 additions & 0 deletions packages/wct-mocha/test/test/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<!DOCTYPE html>
<html>
<body>
<meta http-equiv="Content-Security-Policy"
content="require-trusted-types-for 'script';">
<script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/chai/chai.js"></script>
Expand Down
4 changes: 4 additions & 0 deletions packages/wct-mocha/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
"lib": [
"dom",
"es2017"
],
"types": [
"trusted-types",
"mocha"
]
},
"exclude": [
Expand Down

0 comments on commit 2f57496

Please sign in to comment.