Commit 182500b6 authored by Orsi Batiz's avatar Orsi Batiz Committed by Commit Bot

GetString added in TrustedHTML

Function String GetString(StringOrTrustedHTML, const Document*, ExceptionState&)
has been added to trusted_html.cc
All other callsites (setInnerHTML, setOuterHTML, setAttribute in element.cc,
parseFromString in dom_parser.cc and createContextualFragment in range.cc,
setInnerHTML in shadow_root.cc) have been updated to call this function

Bug: 739170
Change-Id: Ie84462cd233669e55f2b76e685d5d3545198ee59
Reviewed-on: https://chromium-review.googlesource.com/1142768
Commit-Queue: Orsolya Bernadett Batiz <orsibatiz@google.com>
Reviewed-by: default avatarMike West <mkwst@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577450}
parent 86c9de0f
......@@ -1581,19 +1581,11 @@ void Element::SetSynchronizedLazyAttribute(const QualifiedName& name,
void Element::setAttribute(const QualifiedName& name,
const StringOrTrustedHTML& stringOrHTML,
ExceptionState& exception_state) {
DCHECK(stringOrHTML.IsString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
if (stringOrHTML.IsString() && GetDocument().RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedHTML` assignment.");
return;
String valueString =
TrustedHTML::GetString(stringOrHTML, &GetDocument(), exception_state);
if (!exception_state.HadException()) {
setAttribute(name, AtomicString(valueString));
}
String valueString = stringOrHTML.IsString()
? stringOrHTML.GetAsString()
: stringOrHTML.GetAsTrustedHTML()->toString();
setAttribute(name, AtomicString(valueString));
}
void Element::setAttribute(const QualifiedName& name,
......@@ -3472,20 +3464,11 @@ void Element::SetInnerHTMLFromString(const String& html) {
void Element::setInnerHTML(const StringOrTrustedHTML& string_or_html,
ExceptionState& exception_state) {
DCHECK(string_or_html.IsString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
if (string_or_html.IsString() && GetDocument().RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedHTML` assignment.");
return;
String html =
TrustedHTML::GetString(string_or_html, &GetDocument(), exception_state);
if (!exception_state.HadException()) {
SetInnerHTMLFromString(html, exception_state);
}
String html = string_or_html.IsString()
? string_or_html.GetAsString()
: string_or_html.GetAsTrustedHTML()->toString();
SetInnerHTMLFromString(html, exception_state);
}
void Element::setInnerHTML(const StringOrTrustedHTML& string_or_html) {
......@@ -3529,20 +3512,11 @@ void Element::SetOuterHTMLFromString(const String& html,
void Element::setOuterHTML(const StringOrTrustedHTML& string_or_html,
ExceptionState& exception_state) {
DCHECK(string_or_html.IsString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
if (string_or_html.IsString() && GetDocument().RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedHTML` assignment.");
return;
String html =
TrustedHTML::GetString(string_or_html, &GetDocument(), exception_state);
if (!exception_state.HadException()) {
SetOuterHTMLFromString(html, exception_state);
}
String html = string_or_html.IsString()
? string_or_html.GetAsString()
: string_or_html.GetAsTrustedHTML()->toString();
SetOuterHTMLFromString(html, exception_state);
}
Node* Element::InsertAdjacent(const String& where,
......@@ -3689,20 +3663,11 @@ void Element::insertAdjacentHTML(const String& where,
void Element::insertAdjacentHTML(const String& where,
const StringOrTrustedHTML& string_or_html,
ExceptionState& exception_state) {
DCHECK(string_or_html.IsString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
if (string_or_html.IsString() && GetDocument().RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedHTML` assignment.");
return;
String markup =
TrustedHTML::GetString(string_or_html, &GetDocument(), exception_state);
if (!exception_state.HadException()) {
insertAdjacentHTML(where, markup, exception_state);
}
String markup = string_or_html.IsString()
? string_or_html.GetAsString()
: string_or_html.GetAsTrustedHTML()->toString();
insertAdjacentHTML(where, markup, exception_state);
}
void Element::setPointerCapture(int pointer_id,
......
......@@ -975,23 +975,16 @@ DocumentFragment* Range::createContextualFragment(
// Algorithm:
// http://domparsing.spec.whatwg.org/#extensions-to-the-range-interface
DCHECK(string_or_html.IsString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
DCHECK(!string_or_html.IsNull());
Document& document = start_.Container().GetDocument();
if (string_or_html.IsString() && document.RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedHTML` assignment.");
return nullptr;
String markup =
TrustedHTML::GetString(string_or_html, &document, exception_state);
if (!exception_state.HadException()) {
return createContextualFragmentFromString(markup, exception_state);
}
String markup = string_or_html.IsString()
? string_or_html.GetAsString()
: string_or_html.GetAsTrustedHTML()->toString();
return createContextualFragmentFromString(markup, exception_state);
return nullptr;
}
DocumentFragment* Range::createContextualFragmentFromString(
......
......@@ -129,20 +129,11 @@ void ShadowRoot::SetInnerHTMLFromString(const String& markup,
void ShadowRoot::setInnerHTML(const StringOrTrustedHTML& stringOrHtml,
ExceptionState& exception_state) {
DCHECK(stringOrHtml.IsString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
if (stringOrHtml.IsString() && GetDocument().RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedHTML` assignment.");
return;
String html =
TrustedHTML::GetString(stringOrHtml, &GetDocument(), exception_state);
if (!exception_state.HadException()) {
SetInnerHTMLFromString(html, exception_state);
}
String html = stringOrHtml.IsString()
? stringOrHtml.GetAsString()
: stringOrHtml.GetAsTrustedHTML()->toString();
SetInnerHTMLFromString(html, exception_state);
}
void ShadowRoot::RecalcStyle(StyleRecalcChange change) {
......
......@@ -4,7 +4,10 @@
#include "third_party/blink/renderer/core/trustedtypes/trusted_html.h"
#include "third_party/blink/renderer/bindings/core/v8/string_or_trusted_html.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
namespace blink {
......@@ -30,6 +33,25 @@ TrustedHTML* TrustedHTML::unsafelyCreate(ScriptState* script_state,
return TrustedHTML::Create(html);
}
String TrustedHTML::GetString(StringOrTrustedHTML stringOrHTML,
const Document* doc,
ExceptionState& exception_state) {
DCHECK(stringOrHTML.IsString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
DCHECK(!stringOrHTML.IsNull());
if (!stringOrHTML.IsTrustedHTML() && doc && doc->RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedHTML` assignment.");
return g_empty_string;
}
String markup = stringOrHTML.IsString()
? stringOrHTML.GetAsString()
: stringOrHTML.GetAsTrustedHTML()->toString();
return markup;
}
String TrustedHTML::toString() const {
return html_;
}
......
......@@ -12,7 +12,10 @@
namespace blink {
class Document;
class ExceptionState;
class ScriptState;
class StringOrTrustedHTML;
class CORE_EXPORT TrustedHTML final : public ScriptWrappable {
DEFINE_WRAPPERTYPEINFO();
......@@ -26,6 +29,7 @@ class CORE_EXPORT TrustedHTML final : public ScriptWrappable {
String toString() const;
static TrustedHTML* escape(ScriptState*, const String& html);
static TrustedHTML* unsafelyCreate(ScriptState*, const String& html);
static String GetString(StringOrTrustedHTML, const Document*, ExceptionState&);
private:
TrustedHTML(const String& html);
......
......@@ -32,21 +32,12 @@ namespace blink {
Document* DOMParser::parseFromString(const StringOrTrustedHTML& stringOrHTML,
const String& type,
ExceptionState& exception_state) {
DCHECK(stringOrHTML.IsString() ||
RuntimeEnabledFeatures::TrustedDOMTypesEnabled());
DCHECK(!stringOrHTML.IsNull());
if (context_document_ && stringOrHTML.IsString() &&
context_document_->RequireTrustedTypes()) {
exception_state.ThrowTypeError(
"This document requires `TrustedHTML` assignment.");
return nullptr;
String value =
TrustedHTML::GetString(stringOrHTML, context_document_, exception_state);
if (!exception_state.HadException()) {
return parseFromStringInternal(value, type);
}
String valueString = stringOrHTML.IsString()
? stringOrHTML.GetAsString()
: stringOrHTML.GetAsTrustedHTML()->toString();
return parseFromStringInternal(valueString, type);
return nullptr;
}
Document* DOMParser::parseFromStringInternal(const String& str,
......
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