Bug 1730096 part 1: Add AccAttributes::Equal to compare attributes in two instances. r=eeejay

This will be used later to check whether text attributes are different between two Accessibles.
I added this as a method instead of operator== because callers always hold a pointer to AccAttributes, and *a1 == *a2 is weird and will probably lead to mistakes.
This required adding operator== for our value structs.

Differential Revision: https://phabricator.services.mozilla.com/D129467
This commit is contained in:
James Teh 2021-11-01 23:27:37 +00:00
parent 146d799d27
commit 39c9ee45b8
2 changed files with 46 additions and 0 deletions

View File

@ -71,3 +71,27 @@ void AccAttributes::Update(AccAttributes* aOther) {
iter.Remove(); iter.Remove();
} }
} }
bool AccAttributes::Equal(const AccAttributes* aOther) const {
if (Count() != aOther->Count()) {
return false;
}
for (auto iter = mData.ConstIter(); !iter.Done(); iter.Next()) {
const auto otherEntry = aOther->mData.Lookup(iter.Key());
if (iter.Data().is<UniquePtr<nsString>>()) {
// Because we store nsString in a UniquePtr, we must handle it specially
// so we compare the string and not the pointer.
if (!otherEntry->is<UniquePtr<nsString>>()) {
return false;
}
const auto& thisStr = iter.Data().as<UniquePtr<nsString>>();
const auto& otherStr = otherEntry->as<UniquePtr<nsString>>();
if (*thisStr != *otherStr) {
return false;
}
} else if (!otherEntry || iter.Data() != otherEntry.Data()) {
return false;
}
}
return true;
}

View File

@ -29,10 +29,22 @@ namespace a11y {
struct FontSize { struct FontSize {
int32_t mValue; int32_t mValue;
bool operator==(const FontSize& aOther) const {
return mValue == aOther.mValue;
}
bool operator!=(const FontSize& aOther) const {
return mValue != aOther.mValue;
}
}; };
struct Color { struct Color {
nscolor mValue; nscolor mValue;
bool operator==(const Color& aOther) const { return mValue == aOther.mValue; }
bool operator!=(const Color& aOther) const { return mValue != aOther.mValue; }
}; };
// A special type. If an entry has a value of this type, it instructs the // A special type. If an entry has a value of this type, it instructs the
@ -40,6 +52,10 @@ struct Color {
struct DeleteEntry { struct DeleteEntry {
DeleteEntry() : mValue(true) {} DeleteEntry() : mValue(true) {}
bool mValue; bool mValue;
bool operator==(const DeleteEntry& aOther) const { return true; }
bool operator!=(const DeleteEntry& aOther) const { return false; }
}; };
class AccAttributes { class AccAttributes {
@ -112,6 +128,12 @@ class AccAttributes {
// will be emptied. // will be emptied.
void Update(AccAttributes* aOther); void Update(AccAttributes* aOther);
/**
* Return true if all the attributes in this instance are equal to all the
* attributes in another instance.
*/
bool Equal(const AccAttributes* aOther) const;
// An entry class for our iterator. // An entry class for our iterator.
class Entry { class Entry {
public: public: