This is an automated email from the ASF dual-hosted git repository.

thiagoelg pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-tools.git


The following commit(s) were added to refs/heads/main by this push:
     new 07adb455ae5 kie-issues#2301: BPMN Editor: Opening faulty XML will add 
a <parsererror> element to the top-level (#3570)
07adb455ae5 is described below

commit 07adb455ae5286f19ba33bde1f43472d2961db87
Author: Rajalakshmy S <[email protected]>
AuthorDate: Fri May 29 07:02:49 2026 +0530

    kie-issues#2301: BPMN Editor: Opening faulty XML will add a <parsererror> 
element to the top-level (#3570)
    
    Co-authored-by: Rajalakshmy S <[email protected]>
    Co-authored-by: Thiago Lugli <[email protected]>
---
 packages/xml-parser-ts/src/index.ts      | 21 +++++++++++++++++++++
 packages/xml-parser-ts/src/node/index.ts | 18 ++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/packages/xml-parser-ts/src/index.ts 
b/packages/xml-parser-ts/src/index.ts
index 76ca0fc0970..ba16db073f2 100644
--- a/packages/xml-parser-ts/src/index.ts
+++ b/packages/xml-parser-ts/src/index.ts
@@ -53,6 +53,27 @@ export const domParser = {
     // console.time("parsing dom took (DOMParser)");
     const domdoc = new DOMParser().parseFromString(xml.toString(), 
"application/xml");
     // console.timeEnd("parsing dom took (DOMParser)");
+
+    // console.time("parsing dom took (DOMParser) parsererror");
+    const parsererrorElements = domdoc.querySelectorAll("parsererror");
+    if (parsererrorElements.length > 0) {
+      let hasRealError = false;
+      for (let i = 0; i < parsererrorElements.length; i++) {
+        const element = parsererrorElements[i];
+        if (!hasRealError && element.textContent?.trim()) {
+          hasRealError = true;
+        }
+        element.parentNode?.removeChild(element);
+      }
+
+      console.warn("XML parsing error detected and filtered. Document may be 
incomplete.");
+      if (hasRealError && xml && xml.toString().trim() !== "") {
+        // console.timeEnd("parsing dom took (DOMParser) parsererror");
+        throw new Error("XML parsing error detected. The XML structure is 
invalid and cannot be parsed.");
+      }
+    }
+    // console.timeEnd("parsing dom took (DOMParser) parsererror");
+
     return domdoc;
   },
 };
diff --git a/packages/xml-parser-ts/src/node/index.ts 
b/packages/xml-parser-ts/src/node/index.ts
index cb7f2894f02..26bbdae47eb 100644
--- a/packages/xml-parser-ts/src/node/index.ts
+++ b/packages/xml-parser-ts/src/node/index.ts
@@ -28,6 +28,24 @@ index.domParser.getDomDocument = (xml: string | Buffer) => {
   // console.time("parsing dom took (jsdom)");
   const domdoc = new jsdom.JSDOM(xml, { contentType: "application/xml" 
}).window.document;
   // console.timeEnd("parsing dom took (jsdom)");
+
+  const parsererrorElements = domdoc.querySelectorAll("parsererror");
+  if (parsererrorElements.length > 0) {
+    let hasRealError = false;
+    for (let i = 0; i < parsererrorElements.length; i++) {
+      const element = parsererrorElements[i];
+      if (!hasRealError && element.textContent?.trim()) {
+        hasRealError = true;
+      }
+      element.parentNode?.removeChild(element);
+    }
+
+    console.warn("XML parsing error detected and filtered. Document may be 
incomplete.");
+    if (hasRealError && xml && xml.toString().trim() !== "") {
+      throw new Error("XML parsing error detected. The XML structure is 
invalid and cannot be parsed.");
+    }
+  }
+
   return domdoc;
 };
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to