Bug 1666300 part 2 - Parse into an inert document. r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D93478
This commit is contained in:
Henri Sivonen 2020-10-19 13:42:31 +00:00
Родитель 0931769e02
Коммит 8439bf4064
4 изменённых файлов: 89 добавлений и 22 удалений

Просмотреть файл

@ -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);
@ -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,

Просмотреть файл

@ -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.

Просмотреть файл

@ -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,

Просмотреть файл

@ -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);
rv = nsContentUtils::ParseDocumentHTML(aFromStr, document, false);
if (!document) {
return NS_ERROR_FAILURE;
}
nsresult rv = nsContentUtils::ParseDocumentHTML(aFromStr, document, false);
NS_ENSURE_SUCCESS(rv, rv);
nsTreeSanitizer sanitizer(aFlags);