Skip to content

Commit

Permalink
[Sanitizer API] Add dropElements to config.
Browse files Browse the repository at this point in the history
Bug: 1116418
Change-Id: If435e43ced3287398982aa2bc9719bc5498a40ff
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2401162
Commit-Queue: Yifan Luo <[email protected]>
Reviewed-by: Mike West <[email protected]>
Reviewed-by: Daniel Vogelheim <[email protected]>
Cr-Commit-Position: refs/heads/master@{#806614}
  • Loading branch information
iVanlIsh authored and chromium-wpt-export-bot committed Sep 14, 2020
1 parent a667025 commit 26e72a7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 32 deletions.
4 changes: 3 additions & 1 deletion interfaces/sanitizer-api.tentative.idl
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
[CallWith=ScriptState, RaisesException] DOMString sanitizeToString(DOMString input);
};

dictionary SanitizerConfig {};
dictionary SanitizerConfig {
sequence<DOMString>? dropElements;
};
55 changes: 42 additions & 13 deletions sanitizer-api/sanitizer-config.tentative.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,81 @@

<body>
<script>
const default_option ={dropElements: null};
test(t => {
let s = new Sanitizer();
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, {});
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator without config.");

test(t => {
let s = new Sanitizer({});
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, {});
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with empty config.");

test(t => {
let s = new Sanitizer(null);
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, {});
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with null as config.");

test(t => {
let s = new Sanitizer(undefined);
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, {});
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with undefined as config.");

test(t => {
let s = new Sanitizer({testConfig: [1,2,3], attr: ["test", "i", "am"]});
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, {});
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with config ignore unknown values.");

test(t => {
let options = {};
let options = {dropElements: ["div"]};
let s = new Sanitizer(options);
assert_true(s instanceof Sanitizer);
assert_object_equals(s.creationOptions, {});
assert_object_equals(s.creationOptions, {dropElements: ["DIV"]});

options.testConfig = [1,2,3];
assert_object_equals(s.creationOptions, {});
options.dropElements.push("test");
assert_object_equals(s.creationOptions, {dropElements: ["DIV"]});

s.creationOptions = {testConfig: [1,2,3]};
assert_object_equals(s.creationOptions, {});
s.creationOptions = {dropElements: ["test", "t"]};
assert_object_equals(s.creationOptions, {dropElements: ["DIV"]});

s.creationOptions['testConfig'] = [1,2,3];
assert_object_equals(s.creationOptions, {});
s.creationOptions['dropElements'] = [1,2,3];
assert_object_equals(s.creationOptions, {dropElements: ["DIV"]});
}, "SanitizerAPI config is not editable.");

test(t => {
let s = new Sanitizer({dropElements: []});
assert_true(s instanceof Sanitizer);
assert_equals(s.sanitizeToString("<div>balabala<i>test</i></div>"), "<div>balabala<i>test</i></div>");
}, "SanitizerAPI creator with config {dropElements: []}.")

test(t => {
let s = new Sanitizer({dropElements: null});
assert_true(s instanceof Sanitizer);
assert_true(s.creationOptions instanceof Object);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with config {dropElements: null}.")

test(t => {
let s = new Sanitizer({dropElements: undefined});
assert_true(s instanceof Sanitizer);
assert_true(s.creationOptions instanceof Object);
assert_object_equals(s.creationOptions, default_option);
}, "SanitizerAPI creator with config {dropElements: undefined}.");

test(t => {
assert_throws_js(TypeError, _ => {let s = new Sanitizer({dropElements: 123})});
}, "SanitizerAPI creator with config {dropElements: 123}.");

test(t => {
assert_throws_js(TypeError, _ => {let s = new Sanitizer({dropElements: "div"})});
}, "SanitizerAPI creator with config {dropElements: div}.");

</script>
</body>
</html>
5 changes: 4 additions & 1 deletion sanitizer-api/sanitizer-sanitize.tentative.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
d.appendChild(fragment);
return d.innerHTML;
}
const s = new Sanitizer({});

testcases.forEach(c => test(t => {
let s = new Sanitizer(c.config_input);
assert_true(s.creationOptions instanceof Object);
assert_object_equals(s.creationOptions, c.config_value);

fragment = s.sanitize(c.value);
assert_true(fragment instanceof DocumentFragment);
assert_equals(getString(fragment), c.result);
Expand Down
5 changes: 3 additions & 2 deletions sanitizer-api/sanitizer-sanitizeToString.tentative.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

<body>
<script>
const s = new Sanitizer({});

testcases.forEach(c => test(t => {
let s = new Sanitizer(c.config_input);
assert_true(s.creationOptions instanceof Object);
assert_object_equals(s.creationOptions, c.config_value);
assert_equals(s.sanitizeToString(c.value), c.result);
}, "SanitizerAPI sanitizeToString function for " + c.message));
</script>
Expand Down
37 changes: 22 additions & 15 deletions sanitizer-api/support/testcases.sub.js

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

0 comments on commit 26e72a7

Please sign in to comment.