Skip to content

Commit

Permalink
Bug 1712140 - Part 4: Add parseHTMLUnsafe and setHTMLUnsafe methods. …
Browse files Browse the repository at this point in the history
…r=dom-core,webidl,hsivonen

Differential Revision: https://phabricator.services.mozilla.com/D193676
  • Loading branch information
avandolder committed Dec 5, 2023
1 parent 65c76f6 commit c57aaf1
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 0 deletions.
27 changes: 27 additions & 0 deletions dom/base/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18941,4 +18941,31 @@ bool Document::AllowsDeclarativeShadowRoots() const {
return mAllowDeclarativeShadowRoots;
}

/* static */
already_AddRefed<Document> Document::ParseHTMLUnsafe(GlobalObject& aGlobal,
const nsAString& aHTML) {
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), "about:blank");
if (!uri) {
return nullptr;
}

nsCOMPtr<Document> doc;
nsresult rv =
NS_NewHTMLDocument(getter_AddRefs(doc), aGlobal.GetSubjectPrincipal(),
aGlobal.GetSubjectPrincipal());
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}

doc->SetAllowDeclarativeShadowRoots(true);
doc->SetDocumentURI(uri);
rv = nsContentUtils::ParseDocumentHTML(aHTML, doc, false);
if (NS_WARN_IF(NS_FAILED(rv))) {
return nullptr;
}

return doc.forget();
}

} // namespace mozilla::dom
3 changes: 3 additions & 0 deletions dom/base/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -5335,6 +5335,9 @@ class Document : public nsINode,
void LoadEventFired();

RadioGroupContainer& OwnedRadioGroupContainer();

static already_AddRefed<Document> ParseHTMLUnsafe(GlobalObject& aGlobal,
const nsAString& aHTML);
};

NS_DEFINE_STATIC_IID_ACCESSOR(Document, NS_IDOCUMENT_IID)
Expand Down
4 changes: 4 additions & 0 deletions dom/base/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5045,4 +5045,8 @@ EditorBase* Element::GetEditorWithoutCreation() const {
return docShell ? docShell->GetHTMLEditorInternal() : nullptr;
}

void Element::SetHTMLUnsafe(const nsAString& aHTML) {
nsContentUtils::SetHTMLUnsafe(this, this, aHTML);
}

} // namespace mozilla::dom
3 changes: 3 additions & 0 deletions dom/base/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -2087,6 +2087,9 @@ class Element : public FragmentOrElement {

virtual bool Translate() const;

MOZ_CAN_RUN_SCRIPT
virtual void SetHTMLUnsafe(const nsAString& aHTML);

protected:
enum class ReparseAttributes { No, Yes };
/**
Expand Down
5 changes: 5 additions & 0 deletions dom/base/ShadowRoot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,3 +877,8 @@ nsresult ShadowRoot::Clone(dom::NodeInfo* aNodeInfo, nsINode** aResult) const {
*aResult = nullptr;
return NS_ERROR_DOM_NOT_SUPPORTED_ERR;
}

void ShadowRoot::SetHTMLUnsafe(const nsAString& aHTML) {
RefPtr<Element> host = GetHost();
nsContentUtils::SetHTMLUnsafe(this, host, aHTML);
}
3 changes: 3 additions & 0 deletions dom/base/ShadowRoot.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ class ShadowRoot final : public DocumentFragment, public DocumentOrShadowRoot {

bool IsClonable() const { return mIsClonable == Clonable::Yes; }

MOZ_CAN_RUN_SCRIPT
void SetHTMLUnsafe(const nsAString& aHTML);

protected:
// FIXME(emilio): This will need to become more fine-grained.
void ApplicableRulesChanged();
Expand Down
5 changes: 5 additions & 0 deletions dom/html/HTMLTemplateElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@ bool HTMLTemplateElement::ParseAttribute(int32_t aNamespaceID,
aMaybeScriptedPrincipal, aResult);
}

void HTMLTemplateElement::SetHTMLUnsafe(const nsAString& aHTML) {
RefPtr<DocumentFragment> content = mContent;
nsContentUtils::SetHTMLUnsafe(content, this, aHTML);
}

} // namespace mozilla::dom
3 changes: 3 additions & 0 deletions dom/html/HTMLTemplateElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class HTMLTemplateElement final : public nsGenericHTMLElement {
IgnoredErrorResult());
}

MOZ_CAN_RUN_SCRIPT
void SetHTMLUnsafe(const nsAString& aHTML) final;

protected:
virtual ~HTMLTemplateElement();

Expand Down
2 changes: 2 additions & 0 deletions dom/webidl/Document.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ interface Document : Node {

// https://html.spec.whatwg.org/multipage/dom.html#the-document-object
partial interface Document {
static Document parseHTMLUnsafe(DOMString html);

[PutForwards=href, LegacyUnforgeable] readonly attribute Location? location;
[SetterThrows] attribute DOMString domain;
readonly attribute DOMString referrer;
Expand Down
4 changes: 4 additions & 0 deletions dom/webidl/Element.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,7 @@ partial interface Element {
[SecureContext, UseCounter, Throws, Pref="dom.security.setHTML.enabled"]
undefined setHTML(DOMString aInnerHTML, optional SetHTMLOptions options = {});
};

partial interface Element {
undefined setHTMLUnsafe(DOMString html);
};
4 changes: 4 additions & 0 deletions dom/webidl/ShadowRoot.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ interface ShadowRoot : DocumentFragment
boolean isUAWidget();
};

partial interface ShadowRoot {
undefined setHTMLUnsafe(DOMString html);
};

ShadowRoot includes DocumentOrShadowRoot;

0 comments on commit c57aaf1

Please sign in to comment.