Skip to content

Commit

Permalink
Bug 1864838 - Update nsTreeSanitizer to use Maybe instead UniquePtr. …
Browse files Browse the repository at this point in the history
…r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D194753
  • Loading branch information
evilpie committed Nov 28, 2023
1 parent 66ec5e2 commit 2a346c5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
62 changes: 32 additions & 30 deletions dom/base/nsTreeSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "mozilla/DeclarationBlock.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/StyleSheetInlines.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/DocumentFragment.h"
#include "mozilla/dom/HTMLFormElement.h"
#include "mozilla/dom/HTMLTemplateElement.h"
Expand Down Expand Up @@ -2479,105 +2478,106 @@ static int32_t ConvertNamespaceString(const nsAString& aNamespace,
return kNameSpaceID_Unknown;
}

UniquePtr<nsTreeSanitizer::ElementNameSet> nsTreeSanitizer::ConvertElements(
nsTreeSanitizer::ElementNameSet nsTreeSanitizer::ConvertElements(
const nsTArray<OwningStringOrSanitizerElementNamespace>& aElements,
mozilla::ErrorResult& aRv) {
auto set = MakeUnique<ElementNameSet>(aElements.Length());
ElementNameSet set(aElements.Length());

for (const auto& entry : aElements) {
if (entry.IsString()) {
RefPtr<nsAtom> nameAtom = NS_AtomizeMainThread(entry.GetAsString());
// The default namespace for elements is HTML.
ElementName elemName(kNameSpaceID_XHTML, std::move(nameAtom));
set->Insert(elemName);
set.Insert(elemName);
} else {
const auto& elemNamespace = entry.GetAsSanitizerElementNamespace();

int32_t namespaceID =
ConvertNamespaceString(elemNamespace.mNamespace, false, aRv);
if (aRv.Failed()) {
return {};
break;
}

RefPtr<nsAtom> nameAtom = NS_AtomizeMainThread(elemNamespace.mName);
ElementName elemName(namespaceID, std::move(nameAtom));
set->Insert(elemName);
set.Insert(elemName);
}
}

return set;
}

UniquePtr<nsTreeSanitizer::ElementsToAttributesMap>
nsTreeSanitizer::ElementsToAttributesMap
nsTreeSanitizer::ConvertElementsWithAttributes(
const nsTArray<OwningStringOrSanitizerElementNamespaceWithAttributes>&
aElements,
mozilla::ErrorResult& aRv) {
auto map = MakeUnique<ElementsToAttributesMap>();
ElementsToAttributesMap map;

for (const auto& entry : aElements) {
if (entry.IsString()) {
RefPtr<nsAtom> nameAtom = NS_AtomizeMainThread(entry.GetAsString());
// The default namespace for elements is HTML.
ElementName elemName(kNameSpaceID_XHTML, std::move(nameAtom));
// No explicit list of attributes to allow/remove.
map->InsertOrUpdate(elemName, ElementWithAttributes{});
map.InsertOrUpdate(elemName, ElementWithAttributes{});
} else {
const auto& elemNamespace =
entry.GetAsSanitizerElementNamespaceWithAttributes();

ElementWithAttributes elemWithAttributes;

if (elemNamespace.mAttributes.WasPassed()) {
elemWithAttributes.mAttributes =
ConvertAttributes(elemNamespace.mAttributes.Value(), aRv);
elemWithAttributes.mAttributes.emplace(
ConvertAttributes(elemNamespace.mAttributes.Value(), aRv));
if (aRv.Failed()) {
return {};
break;
}
}

if (elemNamespace.mRemoveAttributes.WasPassed()) {
elemWithAttributes.mRemoveAttributes =
ConvertAttributes(elemNamespace.mRemoveAttributes.Value(), aRv);
elemWithAttributes.mRemoveAttributes.emplace(
ConvertAttributes(elemNamespace.mRemoveAttributes.Value(), aRv));
if (aRv.Failed()) {
return {};
break;
}
}

int32_t namespaceID =
ConvertNamespaceString(elemNamespace.mNamespace, false, aRv);
if (aRv.Failed()) {
return {};
break;
}

RefPtr<nsAtom> nameAtom = NS_AtomizeMainThread(elemNamespace.mName);
ElementName elemName(namespaceID, std::move(nameAtom));

map->InsertOrUpdate(elemName, std::move(elemWithAttributes));
map.InsertOrUpdate(elemName, std::move(elemWithAttributes));
}
}

return map;
}

UniquePtr<nsTreeSanitizer::AttributeNameSet> nsTreeSanitizer::ConvertAttributes(
nsTreeSanitizer::AttributeNameSet nsTreeSanitizer::ConvertAttributes(
const nsTArray<OwningStringOrSanitizerAttributeNamespace>& aAttributes,
ErrorResult& aRv) {
auto set = MakeUnique<AttributeNameSet>(aAttributes.Length());
AttributeNameSet set(aAttributes.Length());
for (const auto& entry : aAttributes) {
if (entry.IsString()) {
RefPtr<nsAtom> nameAtom = NS_AtomizeMainThread(entry.GetAsString());
// The default namespace for attributes is the "null" namespace.
AttributeName attrName(kNameSpaceID_None, std::move(nameAtom));
set->Insert(attrName);
set.Insert(attrName);
} else {
const auto& attrNamespace = entry.GetAsSanitizerAttributeNamespace();
int32_t namespaceID =
ConvertNamespaceString(attrNamespace.mNamespace, true, aRv);
if (aRv.Failed()) {
return {};
break;
}
RefPtr<nsAtom> attrAtom = NS_AtomizeMainThread(attrNamespace.mName);
AttributeName attrName(namespaceID, std::move(attrAtom));
set->Insert(attrName);
set.Insert(attrName);
}
}
return set;
Expand Down Expand Up @@ -2606,36 +2606,38 @@ void nsTreeSanitizer::WithWebSanitizerOptions(
}

if (aOptions.mElements.WasPassed()) {
mElements = ConvertElementsWithAttributes(aOptions.mElements.Value(), aRv);
mElements.emplace(
ConvertElementsWithAttributes(aOptions.mElements.Value(), aRv));
if (aRv.Failed()) {
return;
}
}

if (aOptions.mRemoveElements.WasPassed()) {
mRemoveElements = ConvertElements(aOptions.mRemoveElements.Value(), aRv);
mRemoveElements.emplace(
ConvertElements(aOptions.mRemoveElements.Value(), aRv));
if (aRv.Failed()) {
return;
}
}

if (aOptions.mReplaceWithChildrenElements.WasPassed()) {
mReplaceWithChildrenElements =
ConvertElements(aOptions.mReplaceWithChildrenElements.Value(), aRv);
mReplaceWithChildrenElements.emplace(
ConvertElements(aOptions.mReplaceWithChildrenElements.Value(), aRv));
if (aRv.Failed()) {
return;
}
}

if (aOptions.mAttributes.WasPassed()) {
mAttributes = ConvertAttributes(aOptions.mAttributes.Value(), aRv);
mAttributes.emplace(ConvertAttributes(aOptions.mAttributes.Value(), aRv));
if (aRv.Failed()) {
return;
}
}

if (aOptions.mRemoveAttributes.WasPassed()) {
mRemoveAttributes =
ConvertAttributes(aOptions.mRemoveAttributes.Value(), aRv);
mRemoveAttributes.emplace(
ConvertAttributes(aOptions.mRemoveAttributes.Value(), aRv));
}
}
23 changes: 11 additions & 12 deletions dom/base/nsTreeSanitizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "nsIPrincipal.h"
#include "nsTArray.h"
#include "nsTHashSet.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Maybe.h"
#include "mozilla/dom/NameSpaceConstants.h"
#include "mozilla/dom/SanitizerBinding.h"

Expand Down Expand Up @@ -179,8 +179,8 @@ class nsTreeSanitizer {

class ElementWithAttributes {
public:
mozilla::UniquePtr<AttributeNameSet> mAttributes;
mozilla::UniquePtr<AttributeNameSet> mRemoveAttributes;
mozilla::Maybe<AttributeNameSet> mAttributes;
mozilla::Maybe<AttributeNameSet> mRemoveAttributes;
};

using ElementsToAttributesMap =
Expand Down Expand Up @@ -299,19 +299,18 @@ class nsTreeSanitizer {
static bool MatchesAttributeName(AttributeNameSet& aNames, int32_t aNamespace,
nsAtom* aLocalName);

static mozilla::UniquePtr<ElementNameSet> ConvertElements(
static ElementNameSet ConvertElements(
const nsTArray<mozilla::dom::OwningStringOrSanitizerElementNamespace>&
aElements,
mozilla::ErrorResult& aRv);

static mozilla::UniquePtr<ElementsToAttributesMap>
ConvertElementsWithAttributes(
static ElementsToAttributesMap ConvertElementsWithAttributes(
const nsTArray<
mozilla::dom::OwningStringOrSanitizerElementNamespaceWithAttributes>&
aElements,
mozilla::ErrorResult& aRv);

static mozilla::UniquePtr<AttributeNameSet> ConvertAttributes(
static AttributeNameSet ConvertAttributes(
const nsTArray<mozilla::dom::OwningStringOrSanitizerAttributeNamespace>&
aAttributes,
mozilla::ErrorResult& aRv);
Expand Down Expand Up @@ -401,19 +400,19 @@ class nsTreeSanitizer {

// An allow-list of elements to keep, with potentially associated lists of
// attributes to keep/remove.
mozilla::UniquePtr<ElementsToAttributesMap> mElements;
mozilla::Maybe<ElementsToAttributesMap> mElements;

// A deny-list of elements to remove. (aka prune)
mozilla::UniquePtr<ElementNameSet> mRemoveElements;
mozilla::Maybe<ElementNameSet> mRemoveElements;

// A deny-list of elements to replace with children. (aka flatten)
mozilla::UniquePtr<ElementNameSet> mReplaceWithChildrenElements;
mozilla::Maybe<ElementNameSet> mReplaceWithChildrenElements;

// An allow-list of attributes to keep.
mozilla::UniquePtr<AttributeNameSet> mAttributes;
mozilla::Maybe<AttributeNameSet> mAttributes;

// A deny-list of attributes to remove.
mozilla::UniquePtr<AttributeNameSet> mRemoveAttributes;
mozilla::Maybe<AttributeNameSet> mRemoveAttributes;
};

#endif // nsTreeSanitizer_h_

0 comments on commit 2a346c5

Please sign in to comment.