Skip to content

Commit

Permalink
Add trusted types support
Browse files Browse the repository at this point in the history
Makes the jasmine html reporter compatible with an enforced Trusted Types policy.

This should only be necessary for a limited time, as the next version of the proposed specification will not require any special code for URLS (except javascript: URLS).

More info about the proposed Trusted Types standard at https://github.com/WICG/trusted-type
  • Loading branch information
rictic committed Sep 5, 2019
1 parent 35d1508 commit c8d07e2
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions src/html/HtmlReporter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
jasmineRequire.HtmlReporter = function(j$) {
// This is a no-op if not running with a Trusted Types CSP policy, and
// lets tests declare that they trust the way that jasmine creates URLs.
//
// More info about the proposed Trusted Types standard at
// https://github.com/WICG/trusted-types
var policy = {
createURL: function(s) {
return s;
}
};
var trustedTypes = window.trustedTypes || window.TrustedTypes;
if (trustedTypes) {
policy = trustedTypes.createPolicy('jasmine', policy);
if (!policy.createURL) {
// Install createURL for newer browsers. Only browsers that
// implement an old version of the spec require createURL.
// Should be safe to delete this policy entirely by
// February 2020.
// https://github.com/WICG/trusted-types/pull/204
policy.createURL = function(s) { return s; };
}
}

function ResultsStateBuilder() {
this.topResults = new j$.ResultsNode({}, '', null);
this.currentParent = this.topResults;
Expand Down Expand Up @@ -66,7 +89,7 @@ jasmineRequire.HtmlReporter = function(j$) {
{ className: 'jasmine-banner' },
createDom('a', {
className: 'jasmine-title',
href: 'http://jasmine.github.io/',
href: policy.createURL('http://jasmine.github.io/'),
target: '_blank'
}),
createDom('span', { className: 'jasmine-version' }, j$.version)
Expand Down Expand Up @@ -183,7 +206,7 @@ jasmineRequire.HtmlReporter = function(j$) {
{ className: 'jasmine-bar jasmine-skipped' },
createDom(
'a',
{ href: skippedLink, title: 'Run all specs' },
{ href: policy.createURL(skippedLink), title: 'Run all specs' },
skippedMessage
)
)
Expand Down Expand Up @@ -228,7 +251,7 @@ jasmineRequire.HtmlReporter = function(j$) {
'a',
{
title: 'randomized with seed ' + order.seed,
href: seedHref(order.seed)
href: policy.createURL(seedHref(order.seed))
},
order.seed
)
Expand Down Expand Up @@ -300,7 +323,10 @@ jasmineRequire.HtmlReporter = function(j$) {
createDom('span', {}, 'Spec List | '),
createDom(
'a',
{ className: 'jasmine-failures-menu', href: '#' },
{
className: 'jasmine-failures-menu',
href: policy.createURL('#')
},
'Failures'
)
)
Expand All @@ -311,7 +337,10 @@ jasmineRequire.HtmlReporter = function(j$) {
{ className: 'jasmine-menu jasmine-bar jasmine-failure-list' },
createDom(
'a',
{ className: 'jasmine-spec-list-menu', href: '#' },
{
className: 'jasmine-spec-list-menu',
href: policy.createURL('#')
},
'Spec List'
),
createDom('span', {}, ' | Failures ')
Expand Down Expand Up @@ -385,7 +414,7 @@ jasmineRequire.HtmlReporter = function(j$) {
},
createDom(
'a',
{ href: specHref(resultNode.result) },
{ href: policy.createURL(specHref(resultNode.result)) },
resultNode.result.description
)
)
Expand Down Expand Up @@ -421,7 +450,7 @@ jasmineRequire.HtmlReporter = function(j$) {
},
createDom(
'a',
{ href: specHref(resultNode.result) },
{ href: policy.createURL(specHref(resultNode.result)) },
specDescription
)
)
Expand Down Expand Up @@ -549,7 +578,10 @@ jasmineRequire.HtmlReporter = function(j$) {
{ className: 'jasmine-description' },
createDom(
'a',
{ title: result.description, href: specHref(result) },
{
title: result.description,
href: policy.createURL(specHref(result))
},
result.description
)
);
Expand All @@ -559,7 +591,7 @@ jasmineRequire.HtmlReporter = function(j$) {
wrapper.insertBefore(createTextNode(' > '), wrapper.firstChild);
suiteLink = createDom(
'a',
{ href: suiteHref(suite) },
{ href: policy.createURL(suiteHref(suite)) },
suite.result.description
);
wrapper.insertBefore(suiteLink, wrapper.firstChild);
Expand Down

0 comments on commit c8d07e2

Please sign in to comment.