Commit 58d3ca9b authored by Yifan Luo's avatar Yifan Luo Committed by Commit Bot

[Sanitizer API] Sanitize to fragment

Bug: 1116418
Change-Id: I617f6cf6565c723a12bda5c942a6153b41ed9f97
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2387760Reviewed-by: default avatarDaniel Vogelheim <vogelheim@chromium.org>
Commit-Queue: Yifan Luo <lyf@chromium.org>
Cr-Commit-Position: refs/heads/master@{#803828}
parent 2ab5162b
...@@ -4,7 +4,11 @@ ...@@ -4,7 +4,11 @@
#include "sanitizer.h" #include "sanitizer.h"
#include "third_party/blink/renderer/core/dom/document_fragment.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/platform/bindings/exception_messages.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h" #include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
namespace blink { namespace blink {
...@@ -21,4 +25,19 @@ String Sanitizer::sanitizeToString(const String& input) { ...@@ -21,4 +25,19 @@ String Sanitizer::sanitizeToString(const String& input) {
return sanitizedString; return sanitizedString;
} }
DocumentFragment* Sanitizer::sanitize(ScriptState* script_state,
const String& input,
ExceptionState& exception_state) {
LocalDOMWindow* window = LocalDOMWindow::From(script_state);
if (!window) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"Cannot find current DOM window.");
return nullptr;
}
Document* document = window->document();
DocumentFragment* fragment = document->createDocumentFragment();
fragment->ParseHTML(input, document->documentElement());
return fragment;
}
} // namespace blink } // namespace blink
...@@ -11,7 +11,9 @@ ...@@ -11,7 +11,9 @@
namespace blink { namespace blink {
class DocumentFragment;
class ExceptionState; class ExceptionState;
class ScriptState;
class MODULES_EXPORT Sanitizer final : public ScriptWrappable { class MODULES_EXPORT Sanitizer final : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
...@@ -22,6 +24,8 @@ class MODULES_EXPORT Sanitizer final : public ScriptWrappable { ...@@ -22,6 +24,8 @@ class MODULES_EXPORT Sanitizer final : public ScriptWrappable {
~Sanitizer() override; ~Sanitizer() override;
String sanitizeToString(const String&); String sanitizeToString(const String&);
DocumentFragment* sanitize(ScriptState*, const String&, ExceptionState&);
}; };
} // namespace blink } // namespace blink
......
...@@ -10,4 +10,5 @@ ...@@ -10,4 +10,5 @@
] interface Sanitizer { ] interface Sanitizer {
[RaisesException] constructor(); [RaisesException] constructor();
DOMString sanitizeToString(DOMString input); DOMString sanitizeToString(DOMString input);
[CallWith=ScriptState, RaisesException] DocumentFragment sanitize(DOMString input);
}; };
...@@ -8,10 +8,34 @@ ...@@ -8,10 +8,34 @@
<body> <body>
<script> <script>
function getString(fragment) {
d = document.createElement("div");
d.appendChild(fragment);
return d.innerHTML;
}
s = new Sanitizer();
test(t => { test(t => {
s = new Sanitizer(); assert_equals(s.sanitizeToString("test"), "test");
assert_equals("test", s.sanitizeToString("test"));
}, "SanitizerAPI exists!"); }, "SanitizerAPI exists!");
test(t => {
fragment = s.sanitize("test");
assert_equals("[object DocumentFragment]", fragment.toString());
assert_equals(getString(fragment), "<head></head><body>test</body>");
}, "SanitizerAPI sanitizeToFragment works!");
test(t => {
fragment = s.sanitize("<b>bla</b>");
assert_equals(fragment.toString(), "[object DocumentFragment]");
assert_equals(getString(fragment), "<head></head><body><b>bla</b></body>");
}, "SanitizerAPI sanitizeToFragment with HTML codes works!");
test(t => {
fragment = s.sanitize("<a<embla");
assert_equals(fragment.toString(), "[object DocumentFragment]");
assert_equals(getString(fragment), "<head></head><body></body>");
}, "SanitizerAPI sanitizeToFragment with broken HTML codes works!");
</script> </script>
</body> </body>
</html> </html>
...@@ -7930,6 +7930,7 @@ interface SVGViewElement : SVGElement ...@@ -7930,6 +7930,7 @@ interface SVGViewElement : SVGElement
interface Sanitizer interface Sanitizer
attribute @@toStringTag attribute @@toStringTag
method constructor method constructor
method sanitize
method sanitizeToString method sanitizeToString
interface Scheduler interface Scheduler
attribute @@toStringTag attribute @@toStringTag
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment