Store properties/attributes in CXFA_Node as span<>
Change-Id: I653664b2471876e28a2680ba44871c795d6e206e
Reviewed-on: https://pdfium-review.googlesource.com/c/48971
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp
index 334a8c1..f9d26c3 100644
--- a/xfa/fxfa/parser/cxfa_node.cpp
+++ b/xfa/fxfa/parser/cxfa_node.cpp
@@ -839,8 +839,8 @@
pdfium::span<const AttributeData> attributes,
std::unique_ptr<CJX_Object> js_object)
: CXFA_Object(pDoc, oType, eType, std::move(js_object)),
- m_Properties(properties.data()),
- m_Attributes(attributes.data()),
+ m_Properties(properties),
+ m_Attributes(attributes),
m_ValidPackets(validPackets),
m_ePacket(ePacket) {
ASSERT(m_pDocument);
@@ -936,15 +936,10 @@
const CXFA_Node::PropertyData* CXFA_Node::GetPropertyData(
XFA_Element property) const {
- if (m_Properties == nullptr)
- return nullptr;
-
- for (size_t i = 0;; ++i) {
- const PropertyData* data = m_Properties + i;
- if (data->property == XFA_Element::Unknown)
- break;
- if (data->property == property)
- return data;
+ ASSERT(property != XFA_Element::Unknown);
+ for (const auto& prop : m_Properties) {
+ if (prop.property == property)
+ return ∝
}
return nullptr;
}
@@ -964,30 +959,19 @@
}
Optional<XFA_Element> CXFA_Node::GetFirstPropertyWithFlag(uint8_t flag) {
- if (m_Properties == nullptr)
- return {};
-
- for (size_t i = 0;; ++i) {
- const PropertyData* data = m_Properties + i;
- if (data->property == XFA_Element::Unknown)
- break;
- if (data->flags & flag)
- return {data->property};
+ for (const auto& prop : m_Properties) {
+ if (prop.flags & flag)
+ return prop.property;
}
return {};
}
const CXFA_Node::AttributeData* CXFA_Node::GetAttributeData(
XFA_Attribute attr) const {
- if (m_Attributes == nullptr)
- return nullptr;
-
- for (size_t i = 0;; ++i) {
- const AttributeData* cur_attr = &m_Attributes[i];
- if (cur_attr->attribute == XFA_Attribute::Unknown)
- break;
- if (cur_attr->attribute == attr)
- return cur_attr;
+ ASSERT(attr != XFA_Attribute::Unknown);
+ for (const auto& cur_attr : m_Attributes) {
+ if (cur_attr.attribute == attr)
+ return &cur_attr;
}
return nullptr;
}
@@ -996,11 +980,9 @@
return !!GetAttributeData(attr);
}
-// Note: This Method assumes that i is a valid index ....
XFA_Attribute CXFA_Node::GetAttribute(size_t i) const {
- if (m_Attributes == nullptr)
- return XFA_Attribute::Unknown;
- return m_Attributes[i].attribute;
+ return i < m_Attributes.size() ? m_Attributes[i].attribute
+ : XFA_Attribute::Unknown;
}
XFA_AttributeType CXFA_Node::GetAttributeType(XFA_Attribute type) const {
@@ -1056,15 +1038,14 @@
if (!bFilterOneOfProperties || !nodes.empty())
return nodes;
- if (m_Properties == nullptr)
- return nodes;
Optional<XFA_Element> property =
GetFirstPropertyWithFlag(XFA_PROPERTYFLAG_DefaultOneOf);
- if (!property)
+ if (!property.has_value())
return nodes;
- CXFA_Node* pNewNode = m_pDocument->CreateNode(GetPacketType(), *property);
+ CXFA_Node* pNewNode =
+ m_pDocument->CreateNode(GetPacketType(), property.value());
if (pNewNode) {
InsertChild(pNewNode, nullptr);
pNewNode->SetFlagAndNotify(XFA_NodeFlag_Initialized);
diff --git a/xfa/fxfa/parser/cxfa_node.h b/xfa/fxfa/parser/cxfa_node.h
index fc9a357..405a5d5 100644
--- a/xfa/fxfa/parser/cxfa_node.h
+++ b/xfa/fxfa/parser/cxfa_node.h
@@ -98,9 +98,11 @@
void SendAttributeChangeMessage(XFA_Attribute eAttribute, bool bScriptModify);
bool HasAttribute(XFA_Attribute attr) const;
- XFA_Attribute GetAttribute(size_t i) const;
XFA_AttributeType GetAttributeType(XFA_Attribute type) const;
+ // Note: returns XFA_Attribute::Unknown for invalid indicies.
+ XFA_Attribute GetAttribute(size_t i) const;
+
XFA_PacketType GetPacketType() const { return m_ePacket; }
void SetFlag(uint32_t dwFlag);
@@ -467,8 +469,8 @@
CXFA_Event* event,
CXFA_EventParam* pEventParam);
- const PropertyData* const m_Properties;
- const AttributeData* const m_Attributes;
+ pdfium::span<const PropertyData> const m_Properties;
+ pdfium::span<const AttributeData> const m_Attributes;
const uint32_t m_ValidPackets;
// These members are responsible for building the CXFA_Node tree. Node