summaryrefslogtreecommitdiff
path: root/fxjs/cjx_node.cpp
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2017-11-14 21:19:44 +0000
committerChromium commit bot <commit-bot@chromium.org>2017-11-14 21:19:44 +0000
commitaee28693976cc246eea8230a999906802e52cab7 (patch)
tree612e6dbc9b79c978208a623138f70f881acb539f /fxjs/cjx_node.cpp
parent5a423ef8708e61d43f1556ab09c2e09f496d700d (diff)
downloadpdfium-aee28693976cc246eea8230a999906802e52cab7.tar.xz
Cleanup XFA default value code
This CL adds helper methods to CXFA_Node to retrieve the default values for attributes with the correct data types. Change-Id: I644435b4b430819f1060a95fa4fffe4ba2826cfe Reviewed-on: https://pdfium-review.googlesource.com/18450 Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'fxjs/cjx_node.cpp')
-rw-r--r--fxjs/cjx_node.cpp131
1 files changed, 53 insertions, 78 deletions
diff --git a/fxjs/cjx_node.cpp b/fxjs/cjx_node.cpp
index bde131aa09..0af65464c8 100644
--- a/fxjs/cjx_node.cpp
+++ b/fxjs/cjx_node.cpp
@@ -278,12 +278,7 @@ bool CJX_Node::GetAttribute(XFA_Attribute eAttr,
return true;
}
case XFA_AttributeType::CData: {
- WideStringView wsValueC;
- if (!TryCData(pAttr->eName, wsValueC, bUseDefault))
- return false;
-
- wsValue = wsValueC;
- return true;
+ return TryCData(pAttr->eName, wsValue, bUseDefault);
}
case XFA_AttributeType::Boolean: {
bool bValue;
@@ -365,7 +360,7 @@ int32_t CJX_Node::InstanceManager_SetInstances(int32_t iDesired) {
return 0;
if (iDesired < iCount) {
- WideStringView wsInstManagerName = GetCData(XFA_Attribute::Name);
+ WideString wsInstManagerName = GetCData(XFA_Attribute::Name);
WideString wsInstanceName = WideString(
wsInstManagerName.IsEmpty()
? wsInstManagerName
@@ -761,7 +756,7 @@ void CJX_Node::Script_NodeClass_LoadXML(CFXJSE_Arguments* pArguments) {
}
CXFA_Node* pFakeRoot = GetXFANode()->Clone(false);
- WideStringView wsContentType = GetCData(XFA_Attribute::ContentType);
+ WideString wsContentType = GetCData(XFA_Attribute::ContentType);
if (!wsContentType.IsEmpty()) {
pFakeRoot->JSNode()->SetCData(XFA_Attribute::ContentType,
WideString(wsContentType), false, false);
@@ -2265,13 +2260,12 @@ void CJX_Node::Script_Subform_InstanceManager(CFXJSE_Value* pValue,
return;
}
- WideStringView wsName = GetCData(XFA_Attribute::Name);
+ WideString wsName = GetCData(XFA_Attribute::Name);
CXFA_Node* pInstanceMgr = nullptr;
for (CXFA_Node* pNode = GetXFANode()->GetNodeItem(XFA_NODEITEM_PrevSibling);
pNode; pNode = pNode->GetNodeItem(XFA_NODEITEM_PrevSibling)) {
if (pNode->GetElementType() == XFA_Element::InstanceManager) {
- WideStringView wsInstMgrName =
- pNode->JSNode()->GetCData(XFA_Attribute::Name);
+ WideString wsInstMgrName = pNode->JSNode()->GetCData(XFA_Attribute::Name);
if (wsInstMgrName.GetLength() >= 1 && wsInstMgrName[0] == '_' &&
wsInstMgrName.Right(wsInstMgrName.GetLength() - 1) == wsName) {
pInstanceMgr = pNode;
@@ -3009,10 +3003,19 @@ void CJX_Node::Script_Encrypt_Format(CFXJSE_Value* pValue,
bool CJX_Node::TryBoolean(XFA_Attribute eAttr, bool& bValue, bool bUseDefault) {
void* pValue = nullptr;
- if (!GetValue(eAttr, XFA_AttributeType::Boolean, bUseDefault, pValue))
+ void* pKey = GetMapKey_Element(GetXFANode()->GetElementType(), eAttr);
+ if (GetMapModuleValue(pKey, pValue)) {
+ bValue = !!pValue;
+ return true;
+ }
+ if (!bUseDefault)
return false;
- bValue = !!pValue;
+ pdfium::Optional<bool> ret = GetXFANode()->GetDefaultBoolean(eAttr);
+ if (!ret)
+ return false;
+
+ bValue = *ret;
return true;
}
@@ -3039,22 +3042,40 @@ int32_t CJX_Node::GetInteger(XFA_Attribute eAttr) {
bool CJX_Node::TryInteger(XFA_Attribute eAttr,
int32_t& iValue,
bool bUseDefault) {
+ void* pKey = GetMapKey_Element(GetXFANode()->GetElementType(), eAttr);
void* pValue = nullptr;
- if (!GetValue(eAttr, XFA_AttributeType::Integer, bUseDefault, pValue))
+ if (GetMapModuleValue(pKey, pValue)) {
+ iValue = (int32_t)(uintptr_t)pValue;
+ return true;
+ }
+ if (!bUseDefault)
+ return false;
+
+ pdfium::Optional<int32_t> ret = GetXFANode()->GetDefaultInteger(eAttr);
+ if (!ret)
return false;
- iValue = (int32_t)(uintptr_t)pValue;
+ iValue = *ret;
return true;
}
bool CJX_Node::TryEnum(XFA_Attribute eAttr,
XFA_ATTRIBUTEENUM& eValue,
bool bUseDefault) {
+ void* pKey = GetMapKey_Element(GetXFANode()->GetElementType(), eAttr);
void* pValue = nullptr;
- if (!GetValue(eAttr, XFA_AttributeType::Enum, bUseDefault, pValue))
+ if (GetMapModuleValue(pKey, pValue)) {
+ eValue = (XFA_ATTRIBUTEENUM)(uintptr_t)pValue;
+ return true;
+ }
+ if (!bUseDefault)
return false;
- eValue = (XFA_ATTRIBUTEENUM)(uintptr_t)pValue;
+ pdfium::Optional<XFA_ATTRIBUTEENUM> ret = GetXFANode()->GetDefaultEnum(eAttr);
+ if (!ret)
+ return false;
+
+ eValue = *ret;
return true;
}
@@ -3091,14 +3112,14 @@ bool CJX_Node::TryMeasure(XFA_Attribute eAttr,
memcpy(&mValue, pValue, sizeof(mValue));
return true;
}
- if (bUseDefault &&
- XFA_GetAttributeDefaultValue(pValue, GetXFANode()->GetElementType(),
- eAttr, XFA_AttributeType::Measure,
- GetXFANode()->GetPacketID())) {
- memcpy(&mValue, pValue, sizeof(mValue));
- return true;
- }
- return false;
+ if (!bUseDefault)
+ return false;
+
+ pdfium::Optional<CXFA_Measurement> measure =
+ GetXFANode()->GetDefaultMeasurement(eAttr);
+ if (measure)
+ mValue = *measure;
+ return !!measure;
}
CXFA_Measurement CJX_Node::GetMeasure(XFA_Attribute eAttr) const {
@@ -3106,9 +3127,9 @@ CXFA_Measurement CJX_Node::GetMeasure(XFA_Attribute eAttr) const {
return TryMeasure(eAttr, mValue, true) ? mValue : CXFA_Measurement();
}
-WideStringView CJX_Node::GetCData(XFA_Attribute eAttr) {
- WideStringView wsValue;
- return TryCData(eAttr, wsValue, true) ? wsValue : WideStringView();
+WideString CJX_Node::GetCData(XFA_Attribute eAttr) {
+ WideString wsValue;
+ return TryCData(eAttr, wsValue, true) ? wsValue : WideString();
}
bool CJX_Node::SetCData(XFA_Attribute eAttr,
@@ -3258,41 +3279,10 @@ bool CJX_Node::TryCData(XFA_Attribute eAttr,
if (!bUseDefault)
return false;
- void* pValue = nullptr;
- if (XFA_GetAttributeDefaultValue(pValue, GetXFANode()->GetElementType(),
- eAttr, XFA_AttributeType::CData,
- GetXFANode()->GetPacketID())) {
- wsValue = (const wchar_t*)pValue;
- return true;
- }
- return false;
-}
-
-bool CJX_Node::TryCData(XFA_Attribute eAttr,
- WideStringView& wsValue,
- bool bUseDefault) {
- void* pKey = GetMapKey_Element(GetXFANode()->GetElementType(), eAttr);
- if (eAttr == XFA_Attribute::Value) {
- WideString* pStr = (WideString*)GetUserData(pKey, true);
- if (pStr) {
- wsValue = pStr->AsStringView();
- return true;
- }
- } else {
- if (GetMapModuleString(pKey, wsValue))
- return true;
- }
- if (!bUseDefault)
- return false;
-
- void* pValue = nullptr;
- if (XFA_GetAttributeDefaultValue(pValue, GetXFANode()->GetElementType(),
- eAttr, XFA_AttributeType::CData,
- GetXFANode()->GetPacketID())) {
- wsValue = (WideStringView)(const wchar_t*)pValue;
- return true;
- }
- return false;
+ pdfium::Optional<WideString> str = GetXFANode()->GetDefaultCData(eAttr);
+ if (str)
+ wsValue = *str;
+ return !!str;
}
bool CJX_Node::SetObject(XFA_Attribute eAttr,
@@ -3352,21 +3342,6 @@ bool CJX_Node::SetValue(XFA_Attribute eAttr,
return true;
}
-bool CJX_Node::GetValue(XFA_Attribute eAttr,
- XFA_AttributeType eType,
- bool bUseDefault,
- void*& pValue) {
- void* pKey = GetMapKey_Element(GetXFANode()->GetElementType(), eAttr);
- if (GetMapModuleValue(pKey, pValue))
- return true;
- if (!bUseDefault)
- return false;
-
- return XFA_GetAttributeDefaultValue(pValue, GetXFANode()->GetElementType(),
- eAttr, eType,
- GetXFANode()->GetPacketID());
-}
-
void* CJX_Node::GetUserData(void* pKey, bool bProtoAlso) {
void* pData;
return TryUserData(pKey, pData, bProtoAlso) ? pData : nullptr;