7#include "fxjs/xfa/cjx_node.h"
13#include "core/fxcrt/cfx_memorystream.h"
14#include "core/fxcrt/cfx_read_only_string_stream.h"
15#include "core/fxcrt/fx_codepage.h"
16#include "core/fxcrt/xml/cfx_xmldocument.h"
17#include "core/fxcrt/xml/cfx_xmlelement.h"
18#include "core/fxcrt/xml/cfx_xmlparser.h"
20#include "fxjs/js_resources.h"
21#include "fxjs/xfa/cfxjse_engine.h"
22#include "third_party/base/containers/span.h"
23#include "v8/include/v8-object.h"
24#include "xfa/fxfa/cxfa_eventparam.h"
25#include "xfa/fxfa/cxfa_ffdoc.h"
26#include "xfa/fxfa/cxfa_ffnotify.h"
27#include "xfa/fxfa/parser/cxfa_document.h"
28#include "xfa/fxfa/parser/cxfa_document_builder.h"
29#include "xfa/fxfa/parser/cxfa_node.h"
30#include "xfa/fxfa/parser/xfa_basic_data.h"
31#include "xfa/fxfa/parser/xfa_utils.h"
35enum class EventAppliesTo : uint8_t {
40 kFieldOrExclusion = 4,
46struct ExecEventParaInfo {
49 EventAppliesTo m_validFlags;
53#define PARA(a, b, c, d) a, c, EventAppliesTo::d
54const ExecEventParaInfo kExecEventParaInfoTable[] = {
75const ExecEventParaInfo* GetExecEventParaInfoByName(
76 WideStringView wsEventName) {
77 if (wsEventName.IsEmpty())
80 uint32_t uHash = FX_HashCode_GetW(wsEventName);
81 auto* result =
std::lower_bound(
82 std::begin(kExecEventParaInfoTable),
std::end(kExecEventParaInfoTable),
83 uHash, [](
const ExecEventParaInfo& iter,
const uint16_t& hash) {
84 return iter.m_uHash < hash;
86 if (result !=
std::end(kExecEventParaInfoTable) && result->m_uHash == uHash)
94 {
"applyXSL", applyXSL_static},
95 {
"assignNode", assignNode_static},
96 {
"clone", clone_static},
97 {
"getAttribute", getAttribute_static},
98 {
"getElement", getElement_static},
99 {
"isPropertySpecified", isPropertySpecified_static},
100 {
"loadXML", loadXML_static},
101 {
"saveFilteredXML", saveFilteredXML_static},
102 {
"saveXML", saveXML_static},
103 {
"setAttribute", setAttribute_static},
104 {
"setElement", setElement_static}};
117 pdfium::span<v8::Local<v8::Value>> params) {
118 if (params.size() != 1)
126 pdfium::span<v8::Local<v8::Value>> params) {
127 if (params.empty() || params.size() > 3)
135 pdfium::span<v8::Local<v8::Value>> params) {
136 if (params.size() != 1)
139 CXFA_Node* pCloneNode = GetXFANode()->Clone(runtime->ToBoolean(params[0]));
144 pdfium::span<v8::Local<v8::Value>> params) {
145 if (params.size() != 1)
148 WideString expression = runtime->ToWideString(params[0]);
154 pdfium::span<v8::Local<v8::Value>> params) {
155 if (params.empty() || params.size() > 2)
158 WideString expression = runtime->ToWideString(params[0]);
159 int32_t iValue = params.size() >= 2 ? runtime->ToInt32(params[1]) : 0;
160 XFA_Element eElement = XFA_GetElementByName(expression.AsStringView());
173 pdfium::span<v8::Local<v8::Value>> params) {
174 if (params.empty() || params.size() > 3)
177 WideString expression = runtime->ToWideString(params[0]);
178 absl::optional<XFA_ATTRIBUTEINFO> attr =
179 XFA_GetAttributeByName(expression.AsStringView());
180 if (attr.has_value() && HasAttribute(attr.value().attribute))
183 XFA_Element eType = XFA_GetElementByName(expression.AsStringView());
187 bool bParent = params.size() < 2 || runtime->ToBoolean(params[1]);
188 int32_t iIndex = params.size() == 3 ? runtime->ToInt32(params[2]) : 0;
189 bool bHas = !!GetOrCreateProperty<
CXFA_Node>(iIndex, eType);
190 if (!bHas && bParent &&
GetXFANode()->GetParent()) {
192 auto* jsnode =
GetXFANode()->GetParent()->JSObject();
193 bHas = jsnode->HasAttribute(attr.value().attribute) ||
194 !!jsnode->GetOrCreateProperty<CXFA_Node>(iIndex, eType);
200 pdfium::span<v8::Local<v8::Value>> params) {
201 if (params.empty() || params.size() > 3)
204 ByteString expression = runtime->ToByteString(params[0]);
208 bool bIgnoreRoot =
true;
209 if (params.size() >= 2)
210 bIgnoreRoot = runtime->ToBoolean(params[1]);
212 bool bOverwrite =
false;
213 if (params.size() >= 3)
214 bOverwrite = runtime->ToBoolean(params[2]);
217 pdfium::MakeRetain<CFX_ReadOnlyStringStream>(
std::move(expression));
219 CFX_XMLParser parser(stream);
220 std::unique_ptr<CFX_XMLDocument> xml_doc = parser.Parse();
227 GetXFANode()->GetDocument()->GetNotify()->GetFFDoc()->GetXMLDocument();
240 WideString
(wsContentType
));
248 clone = pThisXMLRoot
->Clone(top_xml_doc
);
250 clone = top_xml_doc->CreateNode<CFX_XMLElement>(
253 pFakeXMLRoot = clone;
257 CFX_XMLNode* pXMLChild = pXMLNode->GetFirstChild();
259 CFX_XMLNode* pXMLSibling = pXMLChild->GetNextSibling();
260 pXMLNode->RemoveChild(pXMLChild);
261 pFakeXMLRoot->AppendLastChild(pXMLChild);
262 pXMLChild = pXMLSibling;
265 pXMLNode->RemoveSelfIfParented();
266 pFakeXMLRoot->AppendLastChild(pXMLNode);
276 CXFA_Node* pNewChild = pFakeRoot->GetFirstChild();
279 CXFA_Node* pItem = pNewChild->GetNextSibling();
281 GetXFANode()->InsertChildAndNotify(index++, pNewChild);
287 CXFA_Node* pItem = pChild->GetNextSibling();
288 GetXFANode()->RemoveChildAndNotify(pChild,
true);
296 GetXFANode()->SetXMLMappingNode(pFakeXMLRoot);
298 if (pTempXMLNode && !pTempXMLNode->GetParent())
299 pFakeXMLRoot = pTempXMLNode;
301 pFakeXMLRoot =
nullptr;
305 CXFA_Node* pChild = pFakeRoot->GetFirstChild();
307 CXFA_Node* pItem = pChild->GetNextSibling();
309 GetXFANode()->InsertChildAndNotify(pChild,
nullptr);
325 pdfium::span<v8::Local<v8::Value>> params) {
331 pdfium::span<v8::Local<v8::Value>> params) {
332 if (params.size() > 1)
335 if (params.size() == 1 &&
336 !runtime->ToWideString(params[0]).EqualsASCII(
"pretty")) {
342 ByteString bsXMLHeader =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
353 runtime->NewString(bsXMLHeader.AsStringView())
);
356 XFA_DataExporter_DealWithDataGroupNode(
GetXFANode());
359 auto pMemoryStream = pdfium::MakeRetain<CFX_MemoryStream>();
360 pMemoryStream->WriteString(bsXMLHeader.AsStringView());
363 XFA_DataExporter_RegenerateFormFile(
GetXFANode(), pMemoryStream,
true);
369 runtime->NewString(ByteStringView(pMemoryStream->GetSpan()))
);
373 pdfium::span<v8::Local<v8::Value>> params) {
374 if (params.size() != 2)
379 WideString attributeValue = runtime->ToWideString(params[0]);
380 WideString attribute = runtime->ToWideString(params[1]);
388 pdfium::span<v8::Local<v8::Value>> params) {
389 if (params.size() != 1 && params.size() != 2)
397 v8::Local<v8::Value>* pValue,
404 *pValue = fxv8::NewStringHelper(
405 pIsolate, TryNamespace().value_or(WideString()).ToUTF8().AsStringView());
409 v8::Local<v8::Value>* pValue,
418 *pValue = fxv8::NewNullHelper(pIsolate);
422 GetDocument()->GetScriptContext()->GetOrCreateJSBindingFromMap(pModel);
425void CJX_Node::isContainer(
v8::Isolate* pIsolate,
426 v8::Local<v8::Value>* pValue,
433 *pValue = fxv8::NewBooleanHelper(pIsolate,
GetXFANode()->IsContainerNode());
437 v8::Local<v8::Value>* pValue,
445 *pValue = fxv8::NewBooleanHelper(pIsolate,
false);
448 *pValue = fxv8::NewBooleanHelper(pIsolate,
GetContent(
false).IsEmpty());
451void CJX_Node::oneOfChild(
v8::Isolate* pIsolate,
452 v8::Local<v8::Value>* pValue,
460 std::vector<CXFA_Node*> properties =
461 GetXFANode()->GetNodeListWithFilter(XFA_NodeFilter::kOneOfProperty);
462 if (!properties.empty()) {
463 *pValue =
GetDocument()->GetScriptContext()->GetOrCreateJSBindingFromMap(
474 const ExecEventParaInfo* eventParaInfo =
475 GetExecEventParaInfoByName(wsEventName);
479 switch (eventParaInfo->m_validFlags) {
480 case EventAppliesTo::kNone:
482 case EventAppliesTo::kAll:
483 case EventAppliesTo::kAllNonRecursive:
486 eventParaInfo->m_validFlags == EventAppliesTo::kAll
);
487 case EventAppliesTo::kSubform:
493 case EventAppliesTo::kFieldOrExclusion: {
507 case EventAppliesTo::kField:
513 case EventAppliesTo::kSignature: {
516 if (
GetXFANode()->GetUIChildNode()->GetElementType() !=
523 case EventAppliesTo::kChoiceList: {
526 if (
GetXFANode()->GetUIChildNode()->GetElementType() !=
friend class EventParamScope
void AppendNodesFrom(CFX_XMLDocument *other)
virtual void Save(const RetainPtr< IFX_RetainableWriteStream > &pXMLStream)=0
virtual CFX_XMLNode * Clone(CFX_XMLDocument *doc)=0
virtual Type GetType() const =0
static CJS_Result Success()
static CJS_Result Failure(JSMessage id)
bool DynamicTypeIs(TypeTag eType) const override
CJX_Node(CXFA_Node *node)
XFA_EventError execSingleEventByName(WideStringView wsEventName, XFA_Element eType)
WideString GetAttributeByString(WideStringView attr) const
void DefineMethods(pdfium::span< const CJX_MethodSpec > methods)
WideString GetContent(bool bScriptModify) const
void ThrowInvalidPropertyException(v8::Isolate *pIsolate) const
CXFA_Document * GetDocument() const
void SetAttributeByString(WideStringView wsAttr, const WideString &wsValue)
WideString GetCData(XFA_Attribute eAttr) const
CXFA_Node * GetXFANode() const
bool DynamicTypeIs(TypeTag eType) const override
void ConstructXFANode(CXFA_Node *pXFANode, CFX_XMLNode *pXMLNode)
CFX_XMLNode * Build(CFX_XMLDocument *pXML)
CXFA_Node * GetRootNode() const
XFA_EventError ExecEventByDeepFirst(CXFA_Node *pFormNode, XFA_EVENTTYPE eEventType, bool bIsFormReady, bool bRecursive)
CFX_XMLNode * GetXMLMappingNode() const
void RemoveChildAndNotify(CXFA_Node *pNode, bool bNotify)
void InsertChildAndNotify(CXFA_Node *pNode, CXFA_Node *pBeforeNode)
void SetFlag(XFA_NodeFlag dwFlag)
void SetXMLMappingNode(CFX_XMLNode *node)
void SetInitializedFlagAndNotify()
XFA_Element GetElementType() const
WideString(const WideString &other)
static WideString FromASCII(ByteStringView str)
bool XFA_RecognizeRichText(CFX_XMLElement *pRichTextXMLNode)