Skip to content

Commit

Permalink
Bug 1666300 part 2 - Parse into an inert document. r=smaug
Browse files Browse the repository at this point in the history
  • Loading branch information
hsivonen committed Oct 19, 2020
1 parent 0931769 commit 8439bf4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 22 deletions.
67 changes: 57 additions & 10 deletions dom/base/nsContentUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4985,17 +4985,12 @@ nsresult nsContentUtils::ConvertToPlainText(const nsAString& aSourceBuffer,
nsAString& aResultBuffer,
uint32_t aFlags,
uint32_t aWrapCol) {
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), "about:blank");
nsCOMPtr<nsIPrincipal> principal =
NullPrincipal::CreateWithoutOriginAttributes();
RefPtr<Document> document;
nsresult rv =
NS_NewDOMDocument(getter_AddRefs(document), u""_ns, u""_ns, nullptr, uri,
uri, principal, true, nullptr, DocumentFlavorHTML);
NS_ENSURE_SUCCESS(rv, rv);
RefPtr<Document> document = nsContentUtils::CreateInertHTMLDocument(nullptr);
if (!document) {
return NS_ERROR_FAILURE;
}

rv = nsContentUtils::ParseDocumentHTML(
nsresult rv = nsContentUtils::ParseDocumentHTML(
aSourceBuffer, document,
!(aFlags & nsIDocumentEncoder::OutputNoScriptContent));
NS_ENSURE_SUCCESS(rv, rv);
Expand All @@ -5010,6 +5005,58 @@ nsresult nsContentUtils::ConvertToPlainText(const nsAString& aSourceBuffer,
return encoder->EncodeToString(aResultBuffer);
}

/* static */
already_AddRefed<Document> nsContentUtils::CreateInertXMLDocument(
const Document* aTemplate) {
return nsContentUtils::CreateInertDocument(aTemplate, DocumentFlavorXML);
}

/* static */
already_AddRefed<Document> nsContentUtils::CreateInertHTMLDocument(
const Document* aTemplate) {
return nsContentUtils::CreateInertDocument(aTemplate, DocumentFlavorHTML);
}

/* static */
already_AddRefed<Document> nsContentUtils::CreateInertDocument(
const Document* aTemplate, DocumentFlavor aFlavor) {
if (aTemplate) {
bool hasHad = true;
nsIScriptGlobalObject* sgo = aTemplate->GetScriptHandlingObject(hasHad);
NS_ENSURE_TRUE(sgo || !hasHad, nullptr);

nsCOMPtr<Document> doc;
nsresult rv = NS_NewDOMDocument(
getter_AddRefs(doc), u""_ns, u""_ns, nullptr,
aTemplate->GetDocumentURI(), aTemplate->GetDocBaseURI(),
aTemplate->NodePrincipal(), true, sgo, aFlavor);
if (NS_FAILED(rv)) {
return nullptr;
}
return doc.forget();
}
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), "about:blank"_ns);
if (!uri) {
return nullptr;
}

RefPtr<NullPrincipal> nullPrincipal =
NullPrincipal::CreateWithoutOriginAttributes();
if (!nullPrincipal) {
return nullptr;
}

nsCOMPtr<Document> doc;
nsresult rv =
NS_NewDOMDocument(getter_AddRefs(doc), u""_ns, u""_ns, nullptr, uri, uri,
nullPrincipal, true, nullptr, aFlavor);
if (NS_FAILED(rv)) {
return nullptr;
}
return doc.forget();
}

/* static */
nsresult nsContentUtils::SetNodeTextContent(nsIContent* aContent,
const nsAString& aValue,
Expand Down
19 changes: 19 additions & 0 deletions dom/base/nsContentUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,25 @@ class nsContentUtils {
nsAString& aResultBuffer, uint32_t aFlags,
uint32_t aWrapCol);

/**
* Creates a 'loaded-as-data' HTML document that takes that principal,
* script global, and URL from the argument, which may be null.
*/
static already_AddRefed<Document> CreateInertHTMLDocument(
const Document* aTemplate);

/**
* Creates a 'loaded-as-data' XML document that takes that principal,
* script global, and URL from the argument, which may be null.
*/
static already_AddRefed<Document> CreateInertXMLDocument(
const Document* aTemplate);

private:
static already_AddRefed<Document> CreateInertDocument(
const Document* aTemplate, DocumentFlavor aFlavor);

public:
/**
* Sets the text contents of a node by replacing all existing children
* with a single text child.
Expand Down
9 changes: 7 additions & 2 deletions editor/libeditor/HTMLEditorDataTransfer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3538,8 +3538,13 @@ nsresult HTMLEditor::HTMLWithContextInserter::FragmentParser::ParseFragment(
bool aTrustedInput) {
nsAutoScriptBlockerSuppressNodeRemoved autoBlocker;

RefPtr<DocumentFragment> fragment = new (aTargetDocument->NodeInfoManager())
DocumentFragment(aTargetDocument->NodeInfoManager());
nsCOMPtr<Document> doc =
nsContentUtils::CreateInertHTMLDocument(aTargetDocument);
if (!doc) {
return NS_ERROR_FAILURE;
}
RefPtr<DocumentFragment> fragment =
new (doc->NodeInfoManager()) DocumentFragment(doc->NodeInfoManager());
nsresult rv = nsContentUtils::ParseFragmentHTML(
aFragStr, fragment,
aContextLocalName ? aContextLocalName : nsGkAtoms::body,
Expand Down
16 changes: 6 additions & 10 deletions parser/html/nsParserUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,13 @@ nsParserUtils::ConvertToPlainText(const nsAString& aFromStr, uint32_t aFlags,
NS_IMETHODIMP
nsParserUtils::Sanitize(const nsAString& aFromStr, uint32_t aFlags,
nsAString& aToStr) {
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), "about:blank");
nsCOMPtr<nsIPrincipal> principal =
mozilla::NullPrincipal::CreateWithoutOriginAttributes();
RefPtr<Document> document;
nsresult rv =
NS_NewDOMDocument(getter_AddRefs(document), u""_ns, u""_ns, nullptr, uri,
uri, principal, true, nullptr, DocumentFlavorHTML);
NS_ENSURE_SUCCESS(rv, rv);
RefPtr<Document> document = nsContentUtils::CreateInertHTMLDocument(nullptr);

if (!document) {
return NS_ERROR_FAILURE;
}

rv = nsContentUtils::ParseDocumentHTML(aFromStr, document, false);
nsresult rv = nsContentUtils::ParseDocumentHTML(aFromStr, document, false);
NS_ENSURE_SUCCESS(rv, rv);

nsTreeSanitizer sanitizer(aFlags);
Expand Down

0 comments on commit 8439bf4

Please sign in to comment.