From 640d8ffad8536c789103892c7a4e69e5d30172c8 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Wed, 10 Jan 2018 16:28:57 +0000 Subject: Make methods which create nodes more obvious This CL converts the various methods Get methods which take a boolean value to explicit Get* and GetOrCreate* methods to make the usage clearer. Change-Id: I2af68448b1b69b95713e739bf7fe14a4336d2b65 Reviewed-on: https://pdfium-review.googlesource.com/22590 Reviewed-by: Ryan Harrison Commit-Queue: dsinclair --- fxjs/xfa/cjx_node.cpp | 8 +++--- fxjs/xfa/cjx_object.cpp | 67 ++++++++++++++++++++++++++++--------------------- fxjs/xfa/cjx_object.h | 17 +++++++++---- 3 files changed, 54 insertions(+), 38 deletions(-) (limited to 'fxjs/xfa') diff --git a/fxjs/xfa/cjx_node.cpp b/fxjs/xfa/cjx_node.cpp index c0634767cf..581d3281ef 100644 --- a/fxjs/xfa/cjx_node.cpp +++ b/fxjs/xfa/cjx_node.cpp @@ -163,8 +163,8 @@ CJS_Return CJX_Node::getElement( WideString expression = runtime->ToWideString(params[0]); int32_t iValue = params.size() >= 2 ? runtime->ToInt32(params[1]) : 0; - CXFA_Node* pNode = GetProperty( - iValue, CXFA_Node::NameToElement(expression), true); + CXFA_Node* pNode = GetOrCreateProperty( + iValue, CXFA_Node::NameToElement(expression)); CFXJSE_Value* value = GetDocument()->GetScriptContext()->GetJSValueFromMap(pNode); if (!value) @@ -186,12 +186,12 @@ CJS_Return CJX_Node::isPropertySpecified( bool bParent = params.size() < 2 || runtime->ToBoolean(params[1]); int32_t iIndex = params.size() == 3 ? runtime->ToInt32(params[2]) : 0; XFA_Element eType = CXFA_Node::NameToElement(expression); - bool bHas = !!GetProperty(iIndex, eType, true); + bool bHas = !!GetOrCreateProperty(iIndex, eType); if (!bHas && bParent && GetXFANode()->GetParent()) { // Also check on the parent. auto* jsnode = GetXFANode()->GetParent()->JSObject(); bHas = jsnode->HasAttribute(attr) || - !!jsnode->GetProperty(iIndex, eType, true); + !!jsnode->GetOrCreateProperty(iIndex, eType); } return CJS_Return(runtime->NewBoolean(bHas)); } diff --git a/fxjs/xfa/cjx_object.cpp b/fxjs/xfa/cjx_object.cpp index 0528bd5edd..45089f68da 100644 --- a/fxjs/xfa/cjx_object.cpp +++ b/fxjs/xfa/cjx_object.cpp @@ -629,7 +629,7 @@ bool CJX_Object::SetContent(const WideString& wsContent, case XFA_ObjectType::ContainerNode: { if (XFA_FieldIsMultiListBox(ToNode(GetXFAObject()))) { CXFA_Value* pValue = - GetProperty(0, XFA_Element::Value, true); + GetOrCreateProperty(0, XFA_Element::Value); if (!pValue) break; @@ -714,7 +714,7 @@ bool CJX_Object::SetContent(const WideString& wsContent, pNode = ToNode(GetXFAObject()); } else { CXFA_Value* pValue = - GetProperty(0, XFA_Element::Value, true); + GetOrCreateProperty(0, XFA_Element::Value); if (!pValue) break; @@ -908,33 +908,42 @@ Optional CJX_Object::TryNamespace() { return {static_cast(pXMLNode)->GetNamespaceURI()}; } -CXFA_Node* CJX_Object::GetPropertyInternal(int32_t index, - XFA_Element eProperty, - bool bCreateProperty) { - if (index < 0 || - index >= ToNode(GetXFAObject())->PropertyOccuranceCount(eProperty)) { - return nullptr; - } +std::pair CJX_Object::GetPropertyInternal( + int32_t index, + XFA_Element eProperty) const { + const CXFA_Node* xfaNode = ToNode(GetXFAObject()); + if (index < 0 || index >= xfaNode->PropertyOccuranceCount(eProperty)) + return {nullptr, 0}; int32_t iCount = 0; - for (CXFA_Node* pNode = ToNode(GetXFAObject())->GetChildNode(); pNode; + for (CXFA_Node* pNode = xfaNode->GetChildNode(); pNode; pNode = pNode->GetNodeItem(XFA_NODEITEM_NextSibling)) { if (pNode->GetElementType() == eProperty) { iCount++; if (iCount > index) - return pNode; + return {pNode, iCount}; } } - if (!bCreateProperty) + return {nullptr, iCount}; +} + +CXFA_Node* CJX_Object::GetOrCreatePropertyInternal(int32_t index, + XFA_Element eProperty) { + CXFA_Node* xfaNode = ToNode(GetXFAObject()); + if (index < 0 || index >= xfaNode->PropertyOccuranceCount(eProperty)) return nullptr; - if (ToNode(GetXFAObject()) - ->HasPropertyFlags(eProperty, XFA_PROPERTYFLAG_OneOf)) { - for (CXFA_Node* pNode = ToNode(GetXFAObject())->GetChildNode(); pNode; + int32_t iCount = 0; + CXFA_Node* node; + std::tie(node, iCount) = GetPropertyInternal(index, eProperty); + if (node) + return node; + + if (xfaNode->HasPropertyFlags(eProperty, XFA_PROPERTYFLAG_OneOf)) { + for (CXFA_Node* pNode = xfaNode->GetChildNode(); pNode; pNode = pNode->GetNodeItem(XFA_NODEITEM_NextSibling)) { - if (ToNode(GetXFAObject()) - ->HasPropertyFlags(pNode->GetElementType(), - XFA_PROPERTYFLAG_OneOf)) { + if (xfaNode->HasPropertyFlags(pNode->GetElementType(), + XFA_PROPERTYFLAG_OneOf)) { return nullptr; } } @@ -942,11 +951,11 @@ CXFA_Node* CJX_Object::GetPropertyInternal(int32_t index, CXFA_Node* pNewNode = nullptr; for (; iCount <= index; ++iCount) { - pNewNode = GetDocument()->CreateNode( - ToNode(GetXFAObject())->GetPacketType(), eProperty); + pNewNode = GetDocument()->CreateNode(xfaNode->GetPacketType(), eProperty); if (!pNewNode) return nullptr; - ToNode(GetXFAObject())->InsertChild(pNewNode, nullptr); + + xfaNode->InsertChild(pNewNode, nullptr); pNewNode->SetFlag(XFA_NodeFlag_Initialized, true); } return pNewNode; @@ -1304,7 +1313,7 @@ void CJX_Object::Script_Attribute_Integer(CFXJSE_Value* pValue, void CJX_Object::Script_Som_FontColor(CFXJSE_Value* pValue, bool bSetting, XFA_Attribute eAttribute) { - CXFA_Font* font = ToNode(object_.Get())->GetFont(true); + CXFA_Font* font = ToNode(object_.Get())->GetOrCreateFont(); if (!font) return; @@ -1329,8 +1338,8 @@ void CJX_Object::Script_Som_FontColor(CFXJSE_Value* pValue, void CJX_Object::Script_Som_FillColor(CFXJSE_Value* pValue, bool bSetting, XFA_Attribute eAttribute) { - CXFA_Border* border = ToNode(object_.Get())->GetBorder(true); - CXFA_Fill* borderfill = border->GetFill(true); + CXFA_Border* border = ToNode(object_.Get())->GetOrCreateBorder(); + CXFA_Fill* borderfill = border->GetOrCreateFill(); if (!borderfill) return; @@ -1357,7 +1366,7 @@ void CJX_Object::Script_Som_FillColor(CFXJSE_Value* pValue, void CJX_Object::Script_Som_BorderColor(CFXJSE_Value* pValue, bool bSetting, XFA_Attribute eAttribute) { - CXFA_Border* border = ToNode(object_.Get())->GetBorder(true); + CXFA_Border* border = ToNode(object_.Get())->GetOrCreateBorder(); int32_t iSize = border->CountEdges(); if (bSetting) { int32_t r = 0; @@ -1384,7 +1393,7 @@ void CJX_Object::Script_Som_BorderColor(CFXJSE_Value* pValue, void CJX_Object::Script_Som_BorderWidth(CFXJSE_Value* pValue, bool bSetting, XFA_Attribute eAttribute) { - CXFA_Border* border = ToNode(object_.Get())->GetBorder(true); + CXFA_Border* border = ToNode(object_.Get())->GetOrCreateBorder(); if (bSetting) { CXFA_Measurement thickness = border->GetEdge(0)->GetMSThickness(); pValue->SetString(thickness.ToString().UTF8Encode().AsStringView()); @@ -1402,9 +1411,9 @@ void CJX_Object::Script_Som_Message(CFXJSE_Value* pValue, bool bSetting, XFA_SOM_MESSAGETYPE iMessageType) { bool bNew = false; - CXFA_Validate* validate = ToNode(object_.Get())->GetValidate(false); + CXFA_Validate* validate = ToNode(object_.Get())->GetValidate(); if (!validate) { - validate = ToNode(object_.Get())->GetValidate(true); + validate = ToNode(object_.Get())->GetOrCreateValidate(); bNew = true; } @@ -1586,7 +1595,7 @@ void CJX_Object::Script_Som_DataNode(CFXJSE_Value* pValue, void CJX_Object::Script_Som_Mandatory(CFXJSE_Value* pValue, bool bSetting, XFA_Attribute eAttribute) { - CXFA_Validate* validate = ToNode(object_.Get())->GetValidate(true); + CXFA_Validate* validate = ToNode(object_.Get())->GetOrCreateValidate(); if (!validate) return; diff --git a/fxjs/xfa/cjx_object.h b/fxjs/xfa/cjx_object.h index 87a164ff8b..c252b45036 100644 --- a/fxjs/xfa/cjx_object.h +++ b/fxjs/xfa/cjx_object.h @@ -96,8 +96,15 @@ class CJX_Object { WideString GetContent(bool bScriptModify); template - T* GetProperty(int32_t index, XFA_Element eType, bool bCreateProperty) { - return static_cast(GetPropertyInternal(index, eType, bCreateProperty)); + T* GetProperty(int32_t index, XFA_Element eType) const { + CXFA_Node* node; + int32_t count; + std::tie(node, count) = GetPropertyInternal(index, eType); + return static_cast(node); + } + template + T* GetOrCreateProperty(int32_t index, XFA_Element eType) { + return static_cast(GetOrCreatePropertyInternal(index, eType)); } void SetAttributeValue(const WideString& wsValue, @@ -227,9 +234,9 @@ class CJX_Object { bool bSetting, XFA_Attribute eAttribute); - CXFA_Node* GetPropertyInternal(int32_t index, - XFA_Element eType, - bool bCreateProperty); + std::pair GetPropertyInternal(int32_t index, + XFA_Element eType) const; + CXFA_Node* GetOrCreatePropertyInternal(int32_t index, XFA_Element eType); void OnChanged(XFA_Attribute eAttr, bool bNotify, bool bScriptModify); void OnChanging(XFA_Attribute eAttr, bool bNotify); -- cgit v1.2.3