Nexory commented on code in PR #1880: URL: https://github.com/apache/wicket/pull/1880#discussion_r4546243079
########## wicket-core/src/test/js/trusted-types.js: ########## @@ -0,0 +1,249 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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. + */ + +/*global QUnit: true, MultiSelector: true */ + +Wicket.Event.add(window, 'domready', function() { + "use strict"; + + const { module, test } = QUnit; + + QUnit.config.testTimeout = 3000; + + let logError, errors; + + // builds a node of an Ajax response without a parser, which would need a TrustedHTML itself + const xmlElement = function (tagName, attributes, text) { + const xml = document.implementation.createDocument(null, 'head', null); + const element = xml.createElement(tagName); + for (const name in attributes) { + element.setAttribute(name, attributes[name]); + } + if (text) { + element.appendChild(xml.createTextNode(text)); + } + return element; + }; + + /* + * The page requires Trusted Types for every script sink and allows the policy 'wicket' only, + * so a string that reaches a sink fails the test with a TypeError. + */ + module('Trusted Types', { + beforeEach: function () { + errors = []; + logError = Wicket.Log.error; + Wicket.Log.error = function () { + errors.push(Array.prototype.join.call(arguments, ' ')); + }; + }, + afterEach: function (assert) { + Wicket.Log.error = logError; + assert.deepEqual(errors, [], 'No error is logged'); + } + }); + + test('the page enforces Trusted Types', assert => { + assert.throws(function () { + document.createElement('div').innerHTML = '<b>string</b>'; + }, TypeError); + }); + + test('Wicket.Xml.parse', assert => { + const xml = Wicket.Xml.parse('<ajax-response><evaluate>1</evaluate></ajax-response>'); + + assert.equal(xml.documentElement.tagName, 'ajax-response'); + }); + + test('strings are passed on when the page does not allow the policy', assert => { + const done = assert.async(); + const frame = document.createElement('iframe'); + Wicket.$('qunit-fixture').appendChild(frame); + const frameWindow = frame.contentWindow; + // the frame inherits the policy of the page, which allows the name 'wicket' only once + const taken = frameWindow.trustedTypes.createPolicy('wicket', { + createScriptURL: function (url) { + return url; + } + }); + const sources = Array.prototype.map.call(document.querySelectorAll('script[src*="jquery"], script[src*="wicket-ajax"]'), function (script) { + return script.src; + }); + + const load = function () { + if (sources.length > 0) { + const script = frameWindow.document.createElement('script'); + script.src = taken.createScriptURL(sources.shift()); + script.onload = load; + frameWindow.document.head.appendChild(script); + return; + } + const frameErrors = []; + frameWindow.Wicket.Log.error = function (message) { + frameErrors.push(message); + }; + + assert.throws(function () { + frameWindow.Wicket.Xml.parse('<ajax-response/>'); + }, frameWindow.TypeError); + assert.deepEqual(frameErrors, ["Wicket.TrustedTypes: Cannot create the policy 'wicket'"]); + done(); + }; + load(); + }); + + test('Wicket.DOM.replace', assert => { + Wicket.DOM.replace(Wicket.$('componentToReplace'), '<span id="componentToReplace">new <b>body</b></span>'); + Wicket.DOM.replace(Wicket.$('rowToReplace'), '<tr id="rowToReplace"><td>new cell</td></tr>'); + + assert.equal(Wicket.$('componentToReplace').tagName.toLowerCase(), 'span'); + assert.equal(Wicket.$('componentToReplace').innerHTML, 'new <b>body</b>'); + assert.equal(Wicket.$('rowToReplace').cells[0].textContent, 'new cell'); + }); + + test('Wicket.DOM.replace - markup followed by text', assert => { + Wicket.DOM.replace(Wicket.$('withTextAfter'), '<span id="withTextAfter">new body</span> text'); + + assert.equal(Wicket.$('withTextAfter').tagName.toLowerCase(), 'span'); + assert.equal(Wicket.$('withTextAfter').textContent, 'new body'); + }); + + // jQuery 4.0.0 evaluates the scripts of the markup it inserts itself, from a string + test.if('Wicket.DOM.replace - inline and external scripts in the markup', typeof(jQuery) === 'undefined', assert => { Review Comment: It does now: `trusted-types.js` runs with jQuery 4.0.0 and with the jQuery-free engine, and covers inline and external scripts for `replace` and `add`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
