diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2017-12-13 01:20:24 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-12-13 01:20:24 +0000 |
commit | 94ed4456bfe64ea045be24c66966d814b73fa7ac (patch) | |
tree | 37c2f025b678ce47ef925904d5615c33cd3c656c /fxjs/xfa/cjx_object.cpp | |
parent | 5548984944785d80daa03d98e92698309a3bc604 (diff) | |
download | pdfium-94ed4456bfe64ea045be24c66966d814b73fa7ac.tar.xz |
Move remaining Script_ methods to CJX_Object
The CJX_Node isn't the root of the CJX hierarchy. This causes issues
now that CJX_Object has child objects which don't inherit from CJX_Node.
This CL moves Script_* from CJX_Node to CJX_Object.
Change-Id: I9f063c9d796896ba34be802a8ef8f108911ed56c
Reviewed-on: https://pdfium-review.googlesource.com/20972
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'fxjs/xfa/cjx_object.cpp')
-rw-r--r-- | fxjs/xfa/cjx_object.cpp | 488 |
1 files changed, 488 insertions, 0 deletions
diff --git a/fxjs/xfa/cjx_object.cpp b/fxjs/xfa/cjx_object.cpp index 2c6588824f..04d053ce38 100644 --- a/fxjs/xfa/cjx_object.cpp +++ b/fxjs/xfa/cjx_object.cpp @@ -18,9 +18,11 @@ #include "xfa/fxfa/cxfa_ffnotify.h" #include "xfa/fxfa/cxfa_ffwidget.h" #include "xfa/fxfa/parser/cxfa_document.h" +#include "xfa/fxfa/parser/cxfa_layoutprocessor.h" #include "xfa/fxfa/parser/cxfa_measurement.h" #include "xfa/fxfa/parser/cxfa_node.h" #include "xfa/fxfa/parser/cxfa_object.h" +#include "xfa/fxfa/parser/cxfa_occurdata.h" #include "xfa/fxfa/parser/xfa_utils.h" namespace { @@ -148,6 +150,12 @@ CJS_Return CJX_Object::RunMethod( params); } +void CJX_Object::ThrowTooManyOccurancesException(const WideString& obj) const { + ThrowException( + L"The element [%ls] has violated its allowable number of occurrences.", + obj.c_str()); +} + void CJX_Object::ThrowInvalidPropertyException() const { ThrowException(L"Invalid property set operation."); } @@ -1688,3 +1696,483 @@ int32_t CJX_Object::Subform_and_SubformSet_InstanceIndex() { } return index; } + +void CJX_Object::Script_NodeClass_Ns(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + ThrowInvalidPropertyException(); + return; + } + pValue->SetString( + TryNamespace().value_or(WideString()).UTF8Encode().AsStringView()); +} + +void CJX_Object::Script_NodeClass_Model(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + ThrowInvalidPropertyException(); + return; + } + + pValue->Assign(GetDocument()->GetScriptContext()->GetJSValueFromMap( + ToNode(GetXFAObject())->GetModelNode())); +} + +void CJX_Object::Script_NodeClass_IsContainer(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + ThrowInvalidPropertyException(); + return; + } + + pValue->SetBoolean(GetXFAObject()->IsContainerNode()); +} + +void CJX_Object::Script_NodeClass_IsNull(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + ThrowInvalidPropertyException(); + return; + } + if (ToNode(GetXFAObject())->GetElementType() == XFA_Element::Subform) { + pValue->SetBoolean(false); + return; + } + pValue->SetBoolean(GetContent(false).IsEmpty()); +} + +void CJX_Object::Script_NodeClass_OneOfChild(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + ThrowInvalidPropertyException(); + return; + } + + std::vector<CXFA_Node*> properties = + ToNode(GetXFAObject()) + ->GetNodeList(XFA_NODEFILTER_OneOfProperty, XFA_Element::Unknown); + if (!properties.empty()) { + pValue->Assign(GetDocument()->GetScriptContext()->GetJSValueFromMap( + properties.front())); + } +} + +void CJX_Object::Script_ModelClass_Context(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) {} + +void CJX_Object::Script_ModelClass_AliasNode(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) {} + +void CJX_Object::Script_Delta_CurrentValue(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) {} + +void CJX_Object::Script_Delta_SavedValue(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) {} + +void CJX_Object::Script_Delta_Target(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) {} + +void CJX_Object::Script_Field_Length(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + ThrowInvalidPropertyException(); + return; + } + if (!widget_data_) { + pValue->SetInteger(0); + return; + } + pValue->SetInteger(widget_data_->CountChoiceListItems(true)); +} + +void CJX_Object::Script_Field_EditValue(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (!widget_data_) + return; + + if (bSetting) { + widget_data_->SetValue(XFA_VALUEPICTURE_Edit, pValue->ToWideString()); + return; + } + pValue->SetString(widget_data_->GetValue(XFA_VALUEPICTURE_Edit) + .UTF8Encode() + .AsStringView()); +} + +void CJX_Object::Script_Field_FormattedValue(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (!widget_data_) + return; + + if (bSetting) { + widget_data_->SetValue(XFA_VALUEPICTURE_Display, pValue->ToWideString()); + return; + } + pValue->SetString(widget_data_->GetValue(XFA_VALUEPICTURE_Display) + .UTF8Encode() + .AsStringView()); +} + +void CJX_Object::Script_Field_ParentSubform(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + ThrowInvalidPropertyException(); + return; + } + pValue->SetNull(); +} + +void CJX_Object::Script_Field_SelectedIndex(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (!widget_data_) + return; + + if (!bSetting) { + pValue->SetInteger(widget_data_->GetSelectedItem(0)); + return; + } + + int32_t iIndex = pValue->ToInteger(); + if (iIndex == -1) { + widget_data_->ClearAllSelections(); + return; + } + + widget_data_->SetItemState(iIndex, true, true, true, true); +} + +void CJX_Object::Script_ExclGroup_ErrorText(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) + ThrowInvalidPropertyException(); +} + +void CJX_Object::Script_ExclGroup_DefaultAndRawValue(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (!widget_data_) + return; + + if (bSetting) { + widget_data_->SetSelectedMemberByValue( + pValue->ToWideString().AsStringView(), true, true, true); + return; + } + + WideString wsValue = GetContent(true); + XFA_VERSION curVersion = GetDocument()->GetCurVersionMode(); + if (wsValue.IsEmpty() && curVersion >= XFA_VERSION_300) { + pValue->SetNull(); + return; + } + pValue->SetString(wsValue.UTF8Encode().AsStringView()); +} + +void CJX_Object::Script_ExclGroup_Transient(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) {} + +void CJX_Object::Script_Subform_InstanceManager(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + ThrowInvalidPropertyException(); + return; + } + + WideString wsName = GetCData(XFA_Attribute::Name); + CXFA_Node* pInstanceMgr = nullptr; + for (CXFA_Node* pNode = + ToNode(GetXFAObject())->GetNodeItem(XFA_NODEITEM_PrevSibling); + pNode; pNode = pNode->GetNodeItem(XFA_NODEITEM_PrevSibling)) { + if (pNode->GetElementType() == XFA_Element::InstanceManager) { + WideString wsInstMgrName = pNode->JSNode()->GetCData(XFA_Attribute::Name); + if (wsInstMgrName.GetLength() >= 1 && wsInstMgrName[0] == '_' && + wsInstMgrName.Right(wsInstMgrName.GetLength() - 1) == wsName) { + pInstanceMgr = pNode; + } + break; + } + } + if (!pInstanceMgr) { + pValue->SetNull(); + return; + } + + pValue->Assign( + GetDocument()->GetScriptContext()->GetJSValueFromMap(pInstanceMgr)); +} + +void CJX_Object::Script_Subform_Locale(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + SetCData(XFA_Attribute::Locale, pValue->ToWideString(), true, true); + return; + } + + WideString wsLocaleName; + ToNode(GetXFAObject())->GetLocaleName(wsLocaleName); + pValue->SetString(wsLocaleName.UTF8Encode().AsStringView()); +} + +void CJX_Object::Script_InstanceManager_Max(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + ThrowInvalidPropertyException(); + return; + } + pValue->SetInteger( + CXFA_OccurData(ToNode(GetXFAObject())->GetOccurNode()).GetMax()); +} + +void CJX_Object::Script_InstanceManager_Min(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + ThrowInvalidPropertyException(); + return; + } + pValue->SetInteger( + CXFA_OccurData(ToNode(GetXFAObject())->GetOccurNode()).GetMin()); +} + +void CJX_Object::Script_InstanceManager_Count(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + pValue->SetInteger(ToNode(GetXFAObject())->GetCount()); + return; + } + InstanceManager_SetInstances(pValue->ToInteger()); +} + +void CJX_Object::Script_Occur_Max(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + CXFA_OccurData occurData(ToNode(GetXFAObject())); + if (!bSetting) { + pValue->SetInteger(occurData.GetMax()); + return; + } + occurData.SetMax(pValue->ToInteger()); +} + +void CJX_Object::Script_Occur_Min(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + CXFA_OccurData occurData(ToNode(GetXFAObject())); + if (!bSetting) { + pValue->SetInteger(occurData.GetMin()); + return; + } + occurData.SetMin(pValue->ToInteger()); +} + +void CJX_Object::Script_Form_Checksum(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + SetAttribute(XFA_Attribute::Checksum, pValue->ToWideString().AsStringView(), + false); + return; + } + + pdfium::Optional<WideString> checksum = + TryAttribute(XFA_Attribute::Checksum, false); + pValue->SetString(checksum ? checksum->UTF8Encode().AsStringView() : ""); +} + +void CJX_Object::Script_Packet_Content(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + CFX_XMLNode* pXMLNode = ToNode(GetXFAObject())->GetXMLMappingNode(); + if (bSetting) { + if (pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element) { + CFX_XMLElement* pXMLElement = static_cast<CFX_XMLElement*>(pXMLNode); + pXMLElement->SetTextData(pValue->ToWideString()); + } + return; + } + + WideString wsTextData; + if (pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element) { + CFX_XMLElement* pXMLElement = static_cast<CFX_XMLElement*>(pXMLNode); + wsTextData = pXMLElement->GetTextData(); + } + + pValue->SetString(wsTextData.UTF8Encode().AsStringView()); +} + +void CJX_Object::Script_Source_Db(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) {} + +void CJX_Object::Script_Xfa_This(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) + return; + + CXFA_Object* pThis = GetDocument()->GetScriptContext()->GetThisObject(); + ASSERT(pThis); + pValue->Assign(GetDocument()->GetScriptContext()->GetJSValueFromMap(pThis)); +} + +void CJX_Object::Script_Handler_Version(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) {} + +void CJX_Object::Script_SubmitFormat_Mode(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) {} + +void CJX_Object::Script_Extras_Type(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) {} + +void CJX_Object::Script_Script_Stateless(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) { + if (bSetting) { + ThrowInvalidPropertyException(); + return; + } + pValue->SetString(FX_UTF8Encode(WideStringView(L"0", 1)).AsStringView()); +} + +void CJX_Object::Script_Encrypt_Format(CFXJSE_Value* pValue, + bool bSetting, + XFA_Attribute eAttribute) {} + +pdfium::Optional<WideString> CJX_Object::TryNamespace() { + if (ToNode(GetXFAObject())->IsModelNode() || + ToNode(GetXFAObject())->GetElementType() == XFA_Element::Packet) { + CFX_XMLNode* pXMLNode = ToNode(GetXFAObject())->GetXMLMappingNode(); + if (!pXMLNode || pXMLNode->GetType() != FX_XMLNODE_Element) + return {}; + + return {static_cast<CFX_XMLElement*>(pXMLNode)->GetNamespaceURI()}; + } + + if (ToNode(GetXFAObject())->GetPacketType() != XFA_PacketType::Datasets) + return ToNode(GetXFAObject())->GetModelNode()->JSNode()->TryNamespace(); + + CFX_XMLNode* pXMLNode = ToNode(GetXFAObject())->GetXMLMappingNode(); + if (!pXMLNode || pXMLNode->GetType() != FX_XMLNODE_Element) + return {}; + + if (ToNode(GetXFAObject())->GetElementType() == XFA_Element::DataValue && + GetEnum(XFA_Attribute::Contains) == XFA_AttributeEnum::MetaData) { + WideString wsNamespace; + bool ret = XFA_FDEExtension_ResolveNamespaceQualifier( + static_cast<CFX_XMLElement*>(pXMLNode), + GetCData(XFA_Attribute::QualifiedName), &wsNamespace); + if (!ret) + return {}; + return {wsNamespace}; + } + return {static_cast<CFX_XMLElement*>(pXMLNode)->GetNamespaceURI()}; +} + +int32_t CJX_Object::InstanceManager_SetInstances(int32_t iDesired) { + CXFA_OccurData occurData(ToNode(GetXFAObject())->GetOccurNode()); + if (iDesired < occurData.GetMin()) { + ThrowTooManyOccurancesException(L"min"); + return 1; + } + + int32_t iMax = occurData.GetMax(); + if (iMax >= 0 && iDesired > iMax) { + ThrowTooManyOccurancesException(L"max"); + return 2; + } + + int32_t iCount = ToNode(GetXFAObject())->GetCount(); + if (iDesired == iCount) + return 0; + + if (iDesired < iCount) { + WideString wsInstManagerName = GetCData(XFA_Attribute::Name); + WideString wsInstanceName = WideString( + wsInstManagerName.IsEmpty() + ? wsInstManagerName + : wsInstManagerName.Right(wsInstManagerName.GetLength() - 1)); + uint32_t dInstanceNameHash = + FX_HashCode_GetW(wsInstanceName.AsStringView(), false); + CXFA_Node* pPrevSibling = + iDesired == 0 ? ToNode(GetXFAObject()) + : ToNode(GetXFAObject())->GetItem(iDesired - 1); + while (iCount > iDesired) { + CXFA_Node* pRemoveInstance = + pPrevSibling->GetNodeItem(XFA_NODEITEM_NextSibling); + if (pRemoveInstance->GetElementType() != XFA_Element::Subform && + pRemoveInstance->GetElementType() != XFA_Element::SubformSet) { + continue; + } + if (pRemoveInstance->GetElementType() == XFA_Element::InstanceManager) { + NOTREACHED(); + break; + } + if (pRemoveInstance->GetNameHash() == dInstanceNameHash) { + ToNode(GetXFAObject())->RemoveItem(pRemoveInstance, true); + iCount--; + } + } + } else { + while (iCount < iDesired) { + CXFA_Node* pNewInstance = ToNode(GetXFAObject())->CreateInstance(true); + ToNode(GetXFAObject())->InsertItem(pNewInstance, iCount, iCount, false); + iCount++; + CXFA_FFNotify* pNotify = GetDocument()->GetNotify(); + if (!pNotify) + return 0; + + pNotify->RunNodeInitialize(pNewInstance); + } + } + + CXFA_LayoutProcessor* pLayoutPro = GetDocument()->GetLayoutProcessor(); + if (pLayoutPro) { + pLayoutPro->AddChangedContainer( + ToNode(GetDocument()->GetXFAObject(XFA_HASHCODE_Form))); + } + return 0; +} + +int32_t CJX_Object::InstanceManager_MoveInstance(int32_t iTo, int32_t iFrom) { + int32_t iCount = ToNode(GetXFAObject())->GetCount(); + if (iFrom > iCount || iTo > iCount - 1) { + ThrowIndexOutOfBoundsException(); + return 1; + } + if (iFrom < 0 || iTo < 0 || iFrom == iTo) + return 0; + + CXFA_Node* pMoveInstance = ToNode(GetXFAObject())->GetItem(iFrom); + ToNode(GetXFAObject())->RemoveItem(pMoveInstance, false); + ToNode(GetXFAObject())->InsertItem(pMoveInstance, iTo, iCount - 1, true); + CXFA_LayoutProcessor* pLayoutPro = GetDocument()->GetLayoutProcessor(); + if (pLayoutPro) { + pLayoutPro->AddChangedContainer( + ToNode(GetDocument()->GetXFAObject(XFA_HASHCODE_Form))); + } + return 0; +} |