Skip to content

Commit

Permalink
fix(xml): remove type dependencies to make http imports work again (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter authored Dec 14, 2024
1 parent 7d22bc7 commit 6c970ce
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
7 changes: 2 additions & 5 deletions xml/_types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Imports
import type { Nullable } from "@libs/typing"
/** Nullable type. */
export type Nullable<T> = T | null

/** XML text node. */
export type xml_text = {
Expand Down Expand Up @@ -44,6 +44,3 @@ export type ReaderSync = { readSync(buffer: Uint8Array): Nullable<number> }

/** A laxer type for what can be stringified. We won’t ever create this, but we’ll accept it. */
export type stringifyable = Partial<Omit<xml_document, "@version" | "@standalone"> & { "@version": string; "@standalone": string }>

// Exports
export type { Nullable }
9 changes: 4 additions & 5 deletions xml/parse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Imports
import { initSync, JsReader, source, Token, tokenize } from "./wasm_xml_parser/wasm_xml_parser.js"
import type { record, rw } from "@libs/typing"
import type { Nullable, ReaderSync, xml_document, xml_node, xml_text } from "./_types.ts"
export type { Nullable, ReaderSync, xml_document, xml_node, xml_text }
initSync(source())
Expand Down Expand Up @@ -241,7 +240,7 @@ export function parse(content: string | ReaderSync, options?: options): xml_docu

/** Parse xml attributes. */
function xml_attributes(raw: string) {
const attributes = {} as record<string>
const attributes = {} as Record<PropertyKey, string>
for (const [_, name, __, value] of raw.matchAll(/(?<name>[A-Za-z_][-\w.:]*)=(["'])(?<value>(?:(?!\2).)*)(\2)/g)) {
attributes[`@${name}`] = value
}
Expand Down Expand Up @@ -326,17 +325,17 @@ function postprocess(node: xml_node, options: options) {
delete node["#doctype"]
}
if (options?.clean?.instructions) {
;(node as rw)["~children"] = node["~children"].filter((child) => !(child["~name"] in ((node as xml_document)["#instructions"] ?? {})))
;(node as Record<PropertyKey, unknown>)["~children"] = node["~children"].filter((child) => !(child["~name"] in ((node as xml_document)["#instructions"] ?? {})))
delete node["#instructions"]
}
}
// Clean node and enable enumerable properties if required
if (node["~children"]) {
if (options?.clean?.comments) {
;(node as rw)["~children"] = node["~children"].filter((child) => child["~name"] !== "~comment")
;(node as Record<PropertyKey, unknown>)["~children"] = node["~children"].filter((child) => child["~name"] !== "~comment")
}
if (options?.revive?.trim) {
node["~children"].forEach((child) => /^~(?:text|cdata|comment)$/.test(child["~name"]) ? (child as rw)["#text"] = revive(child, "#text", { revive: { trim: node["@xml:space"] !== "preserve" } }) : null)
node["~children"].forEach((child) => /^~(?:text|cdata|comment)$/.test(child["~name"]) ? (child as Record<PropertyKey, unknown>)["#text"] = revive(child, "#text", { revive: { trim: node["@xml:space"] !== "preserve" } }) : null)
}
if (node["~children"].some((child) => (/^~(?:text|cdata)$/.test(child["~name"])) && (child["#text"].trim().length + (node["@xml:space"] === "preserve" ? 1 : 0) * child["#text"].length))) {
Object.defineProperty(node, "#text", { enumerable: true, configurable: true })
Expand Down
11 changes: 5 additions & 6 deletions xml/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Imports
import type { Nullable, record, rw } from "@libs/typing"
import type { stringifyable, xml_document, xml_node, xml_text } from "./_types.ts"
import type { Nullable, stringifyable, xml_document, xml_node, xml_text } from "./_types.ts"
export type { Nullable, stringifyable, xml_document, xml_node, xml_text }

/** XML stringifier options. */
Expand Down Expand Up @@ -133,7 +132,7 @@ export function comment(text: string): Omit<xml_text, "~parent"> {

/** Create XML prolog. */
function xml_prolog(document: xml_document, options: _options): string {
;(document as rw)["~name"] ??= "xml"
;(document as Record<PropertyKey, unknown>)["~name"] ??= "xml"
return xml_instruction(document, options)
}

Expand Down Expand Up @@ -222,9 +221,9 @@ function xml_children(node: xml_node, options: options): Array<xml_node> {
case value === null:
return ({ ["~name"]: key, ["#text"]: "" })
case typeof value === "object": {
const child = { ...value as record, ["~name"]: key } as record
if (((value as record)["~name"] as string)?.startsWith("~")) {
child[internal] = (value as record)["~name"]
const child = { ...value as Record<PropertyKey, unknown>, ["~name"]: key } as Record<PropertyKey, unknown>
if (((value as Record<PropertyKey, unknown>)["~name"] as string)?.startsWith("~")) {
child[internal] = (value as Record<PropertyKey, unknown>)["~name"]
}
return child
}
Expand Down

0 comments on commit 6c970ce

Please sign in to comment.