Bug 1482044 [wpt PR 12372] - Added new class TrustedScript to TrustedTypes, a=testonly
authorMaja Kabus <kabusm@google.com>
Wed, 22 Aug 2018 13:19:58 +0000
changeset 488201 5fa65bc6ba492cd700a92322b035286465501d13
parent 488200 fb5f92fb339bedd97e7f19e6e4649c80bfe92f32
child 488202 0435e5afe9d2f4dc150c4ae64751b0ea751311b4
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerstestonly
bugs1482044, 12372, 739170, 1168494, 584197
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1482044 [wpt PR 12372] - Added new class TrustedScript to TrustedTypes, a=testonly Automatic update from web-platform-testsAdded new class TrustedScript to TrustedTypes New class TrustedScript added as a C++ implementation of JS TrustedScript. Bug: 739170 Change-Id: Ife70f8838aefa6bb4aac45f09639464c19de6e1b Reviewed-on: https://chromium-review.googlesource.com/1168494 Commit-Queue: Maja Kabus <kabusm@google.com> Reviewed-by: Mike West <mkwst@chromium.org> Cr-Commit-Position: refs/heads/master@{#584197} -- wpt-commits: fd1fcca70c03cea6e89a75e507c2ce5042494725 wpt-pr: 12372
testing/web-platform/meta/MANIFEST.json
testing/web-platform/tests/trusted-types/TrustedTypePolicyFactory-createPolicy.tentative.html
testing/web-platform/tests/trusted-types/support/helper.sub.js
--- a/testing/web-platform/meta/MANIFEST.json
+++ b/testing/web-platform/meta/MANIFEST.json
@@ -644253,17 +644253,17 @@
    "d0743949b6a122d8bd0adf7b1ed0181f0c51429d",
    "support"
   ],
   "trusted-types/Range-createContextualFragment.tentative.html": [
    "3d45b33486d3971c0c58180fa4034dbfae18f135",
    "testharness"
   ],
   "trusted-types/TrustedTypePolicyFactory-createPolicy.tentative.html": [
-   "d6f6b355c6acfa3de2049c8105c008c90edae11c",
+   "76e6d130b05dfba00911ad42eb7a162cd29b222e",
    "testharness"
   ],
   "trusted-types/Window-open.tentative.html": [
    "c005fbba143f66a9540deebba7988fdea9661558",
    "testharness"
   ],
   "trusted-types/Window-trustedTypes.tentative.html": [
    "ef4487749dd0c12a00bd3ab42c1353467a6eeb8f",
@@ -644305,17 +644305,17 @@
    "2afa2572c350071b791ee280bce0a1e5135dc2aa",
    "testharness"
   ],
   "trusted-types/block-string-assignment-to-Window-open.tentative.html": [
    "f5712295d30d7b1d680ad6753dd401d21c0409f9",
    "testharness"
   ],
   "trusted-types/support/helper.sub.js": [
-   "07e8902a168838f6ae71895b63051d1d6cc3b462",
+   "b5435917bec607c97eaa5d75ee7fa2752999cb0a",
    "support"
   ],
   "uievents/META.yml": [
    "2f1ec58efec10e0dd6374aac05cb926c8cffa3f1",
    "support"
   ],
   "uievents/README.md": [
    "065c0c78848af60244a860c2d785fa48c66e5ef5",
--- a/testing/web-platform/tests/trusted-types/TrustedTypePolicyFactory-createPolicy.tentative.html
+++ b/testing/web-platform/tests/trusted-types/TrustedTypePolicyFactory-createPolicy.tentative.html
@@ -79,16 +79,87 @@
 
   async_test(t => {
     window.trustedTypes.createPolicy('SomeName', null)
         .then(t.step_func_done(p => {
             assert_equals(p.createHTML('whatever'), null);
     }));
   }, "html - calling undefined callback");
 
+  //Script tests
+  function createScriptTest(policy, expectedScript, t) {
+    let p = window.trustedTypes.createPolicy('SomeName', policy)
+        .then(t.step_func_done(p => {
+            assert_true(p.createScript('whatever') instanceof TrustedScript);
+            assert_equals(p.createScript('whatever') + "", expectedScript);
+    }));
+  }
+
+  async_test(t => {
+    createScriptTest( { createScript: s => s }, 'whatever', t);
+  }, "script = identity function");
+
+  async_test(t => {
+    createScriptTest( { createScript: s => null }, "null", t);
+  }, "script = null");
+
+  var Scriptstr = 'well, ';
+  async_test(t => {
+    createScriptTest( { createScript: s => Scriptstr + s }, Scriptstr + 'whatever', t);
+  }, "script = string + global string");
+
+  var Scriptx = 'global';
+  async_test(t => {
+    let p = window.trustedTypes.createPolicy('SomeName', {
+        createScript: s => { Scriptx = s; return s; }
+      })
+      .then(t.step_func_done(p => {
+        assert_true(p.createScript('whatever') instanceof TrustedScript);
+        assert_equals(p.createScript('whatever') + "", 'whatever');
+        assert_equals(Scriptx, 'whatever');
+    }));
+  }, "script = identity function, global string changed");
+
+  async_test(t => {
+    let p = window.trustedTypes.createPolicy('SomeName', {
+        createScript: s => { throw new Error(); }
+      })
+      .then(t.step_func_done(p => {
+        assert_throws(new Error(), _ => {
+          p.createScript('whatever');
+        });
+    }));
+  }, "script = callback that throws");
+
+  var obj = {
+    "foo": "well,"
+  }
+
+  function getScript(s) {
+    return this.foo + " " + s;
+  }
+
+  async_test(t => {
+    createScriptTest( {
+      createScript: getScript.bind(obj)},
+      'well, whatever', t);
+  }, "script = this bound to an object");
+
+  var foo = "well,";
+  async_test(t => {
+    createScriptTest( { createScript: s => getScript(s) }, 'well, whatever', t);
+  }, "script = this without bind");
+
+  async_test(t => {
+    let p = window.trustedTypes.createPolicy('SomeName', null)
+        .then(t.step_func_done(p => {
+            assert_equals(p.createScript('whatever'), null);
+    }));
+  }, "script - calling undefined callback");
+
   //ScriptURL tests
   function createScriptURLTest(policy, expectedScriptURL, t) {
     window.trustedTypes.createPolicy('SomeName', policy)
         .then(t.step_func_done(p => {
             assert_true(p.createScriptURL(INPUTS.SCRIPTURL) instanceof TrustedScriptURL);
             assert_equals(p.createScriptURL(INPUTS.SCRIPTURL) + "", expectedScriptURL);
     }));
   }
--- a/testing/web-platform/tests/trusted-types/support/helper.sub.js
+++ b/testing/web-platform/tests/trusted-types/support/helper.sub.js
@@ -1,54 +1,73 @@
 var INPUTS = {
   HTML: "Hi, I want to be transformed!",
+  SCRIPT: "Hi, I want to be transformed!",
   SCRIPTURL: "http://this.is.a.scripturl.test/",
   URL: "http://hello.i.am.an.url/"
 };
 
 var RESULTS = {
   HTML: "Quack, I want to be a duck!",
+  SCRIPT: "Meow, I want to be a cat!",
   SCRIPTURL: "http://this.is.a.successful.test/",
   URL: "http://hooray.i.am.successfully.transformed/"
 };
 
 function createHTMLJS(html) {
   return html.replace("Hi", "Quack")
       .replace("transformed", "a duck");
 }
 
+function createScriptJS(script) {
+  return script.replace("Hi", "Meow")
+      .replace("transformed", "a cat");
+}
+
 function createScriptURLJS(scripturl) {
   return scripturl.replace("scripturl", "successful");
 }
 
 function createURLJS(url) {
   return url.replace("hello", "hooray")
       .replace("an.url", "successfully.transformed");
 }
 
 function createHTML_policy(win) {
   return win.trustedTypes.createPolicy('SomeName', { createHTML: createHTMLJS });
 }
 
+function createScript_policy(win) {
+  return win.trustedTypes.createPolicy('SomeName', { createScript: createScriptJS });
+}
+
 function createScriptURL_policy(win) {
   return win.trustedTypes.createPolicy('SomeName', { createScriptURL: createScriptURLJS });
 }
 
 function createURL_policy(win) {
   return win.trustedTypes.createPolicy('SomeName', { createURL: createURLJS });
 }
 
 function assert_element_accepts_trusted_html(win, t, tag, attribute, expected) {
   createHTML_policy(win)
       .then(t.step_func_done(p => {
           let html = p.createHTML(INPUTS.HTML);
           assert_element_accepts_trusted_type(tag, attribute, html, expected);
       }));
 }
 
+function assert_element_accepts_trusted_script(win, t, tag, attribute, expected) {
+  createScript_policy(win)
+      .then(t.step_func_done(p => {
+          let script = p.createScript(INPUTS.SCRIPT);
+          assert_element_accepts_trusted_type(tag, attribute, script, expected);
+      }));
+}
+
 function assert_element_accepts_trusted_script_url(win, t, tag, attribute, expected) {
   createScriptURL_policy(win)
       .then(t.step_func_done(p => {
           let scripturl = p.createScriptURL(INPUTS.SCRIPTURL);
           assert_element_accepts_trusted_type(tag, attribute, scripturl, expected);
       }));
 }