Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "edockit",
"version": "0.3.0",
"version": "0.4.0-dev.0",
"main": "dist/index.cjs.js",
"scripts": {
"test": "jest --silent",
Expand Down
14 changes: 13 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ function createTypescriptPlugin() {
}

function isExternalModule(id) {
return (
id === "fflate" ||
id === "@peculiar/x509" ||
id === "@xmldom/xmldom" ||
id === "xpath" ||
id === "crypto" ||
id.startsWith("node:") ||
id.startsWith("@peculiar/asn1-")
);
}

function isExternalModuleUmd(id) {
return id === "fflate" || id === "@peculiar/x509" || id === "crypto" || id.startsWith("node:");
}

Expand Down Expand Up @@ -73,7 +85,7 @@ const rootUmdBuild = {
target: "es2015",
}),
],
external: isExternalModule,
external: isExternalModuleUmd,
};

export default [moduleBuild, rootUmdBuild];
14 changes: 13 additions & 1 deletion scripts/build-dist.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ function createTypescriptPlugin() {
}

function isExternalModule(id) {
return (
id === "fflate" ||
id === "@peculiar/x509" ||
id === "@xmldom/xmldom" ||
id === "xpath" ||
id === "crypto" ||
id.startsWith("node:") ||
id.startsWith("@peculiar/asn1-")
);
}

function isExternalModuleUmd(id) {
return id === "fflate" || id === "@peculiar/x509" || id === "crypto" || id.startsWith("node:");
}

Expand Down Expand Up @@ -64,7 +76,7 @@ function createRootUmdBuildConfig() {
target: "es2015",
}),
],
external: isExternalModule,
external: isExternalModuleUmd,
output: {
file: packageJson.browser,
format: "umd",
Expand Down
44 changes: 16 additions & 28 deletions src/utils/xmlParser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as xmldom from "@xmldom/xmldom";
import * as xpath from "xpath";

/**
* Recursive DOM traversal to find elements with a given tag name
* (Fallback method when XPath is not available or fails)
Expand Down Expand Up @@ -99,15 +102,7 @@ export function createXMLParser(): XMLParserInterface {
}

// We're in Node.js, so use xmldom
try {
// Import dynamically to avoid bundling issues
const { DOMParser } = require("@xmldom/xmldom");
return new DOMParser();
} catch (e) {
throw new Error(
"XML DOM parser not available. In Node.js environments, please install @xmldom/xmldom package.",
);
}
return new xmldom.DOMParser() as unknown as XMLParserInterface;
}

/**
Expand Down Expand Up @@ -145,12 +140,12 @@ export function queryByXPath(
}
// Node.js environment with xpath module
else {
const xpath = require("xpath");
const xpathLib = xpath;
const nsResolver = createNsResolverForNode(namespaces);

// Use a try-catch here to handle specific XPath issues
try {
const nodes = xpath.select(xpathExpression, parent, nsResolver);
const nodes = xpathLib.select(xpathExpression, parent as any, nsResolver) as any;
return nodes.length > 0 ? nodes[0] : null;
} catch (err: unknown) {
// If we get a namespace error, try a simpler XPath with just local-name()
Expand All @@ -166,7 +161,7 @@ export function queryByXPath(
if (match && match[1]) {
const elementName = match[1];
const simplifiedXPath = `.//*[local-name()='${elementName}']`;
const nodes = xpath.select(simplifiedXPath, parent);
const nodes = xpathLib.select(simplifiedXPath, parent as any) as any;
return nodes.length > 0 ? nodes[0] : null;
}
}
Expand Down Expand Up @@ -219,12 +214,12 @@ export function queryAllByXPath(
}
// Node.js environment with xpath module
else {
const xpath = require("xpath");
const xpathLib = xpath;
const nsResolver = createNsResolverForNode(namespaces);

// Use a try-catch here to handle specific XPath issues
try {
const nodes = xpath.select(xpathExpression, parent, nsResolver);
const nodes = xpathLib.select(xpathExpression, parent as any, nsResolver) as any;
return nodes as Element[];
} catch (err: unknown) {
// If we get a namespace error, try a simpler XPath with just local-name()
Expand All @@ -240,7 +235,7 @@ export function queryAllByXPath(
if (match && match[1]) {
const elementName = match[1];
const simplifiedXPath = `.//*[local-name()='${elementName}']`;
const nodes = xpath.select(simplifiedXPath, parent);
const nodes = xpathLib.select(simplifiedXPath, parent as any) as any;
return nodes as Element[];
}
}
Expand Down Expand Up @@ -327,8 +322,8 @@ export function querySelector(parent: Document | Element, selector: string): Ele

// Then try XPath as a fallback
try {
const xpath = selectorToXPath(selector);
return queryByXPath(parent, xpath);
const xpathExpr = selectorToXPath(selector);
return queryByXPath(parent, xpathExpr);
} catch (e) {
console.warn("XPath query failed, using direct DOM traversal as fallback");
return null;
Expand Down Expand Up @@ -368,8 +363,8 @@ export function querySelectorAll(parent: Document | Element, selector: string):

// Then try XPath as a fallback
try {
const xpath = selectorToXPath(selector);
return queryAllByXPath(parent, xpath);
const xpathExpr = selectorToXPath(selector);
return queryAllByXPath(parent, xpathExpr);
} catch (e) {
console.warn("XPath query failed, using direct DOM traversal as fallback");
return [];
Expand All @@ -385,15 +380,8 @@ export function serializeToXML(node: Node): string {
return new window.XMLSerializer().serializeToString(node);
}

// If we're using xmldom
try {
const { XMLSerializer } = require("@xmldom/xmldom");
return new XMLSerializer().serializeToString(node);
} catch (e) {
throw new Error(
"XML Serializer not available. In Node.js environments, please install @xmldom/xmldom package.",
);
}
// Node.js — use xmldom
return new xmldom.XMLSerializer().serializeToString(node as any);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions web-test-runner.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ const config = {
browsers,

plugins: [
// Stub out Node-only deps before esbuild tries to resolve them
{
name: "stub-node-deps",
resolveImport({ source }) {
if (source === "@xmldom/xmldom" || source === "xpath") {
return `/__node-stub__/${source}`;
}
},
serve(context) {
if (context.path.startsWith("/__node-stub__/")) {
return {
body: "export default {}; export const DOMParser = undefined; export const XMLSerializer = undefined; export const select = undefined;",
type: "js",
};
}
},
},
esbuildPlugin({
ts: true,
tsconfig: "./tsconfig.json",
Expand Down
Loading