From 6c970ce23e0a494638991064deb25829bad99e09 Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Fri, 13 Dec 2024 21:05:36 -0500 Subject: [PATCH] fix(xml): remove type dependencies to make http imports work again (#91) --- xml/_types.ts | 7 ++----- xml/parse.ts | 9 ++++----- xml/stringify.ts | 11 +++++------ 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/xml/_types.ts b/xml/_types.ts index d90577f3..f142f9a9 100644 --- a/xml/_types.ts +++ b/xml/_types.ts @@ -1,5 +1,5 @@ -// Imports -import type { Nullable } from "@libs/typing" +/** Nullable type. */ +export type Nullable = T | null /** XML text node. */ export type xml_text = { @@ -44,6 +44,3 @@ export type ReaderSync = { readSync(buffer: Uint8Array): Nullable } /** A laxer type for what can be stringified. We won’t ever create this, but we’ll accept it. */ export type stringifyable = Partial & { "@version": string; "@standalone": string }> - -// Exports -export type { Nullable } diff --git a/xml/parse.ts b/xml/parse.ts index a9dba891..92d72030 100644 --- a/xml/parse.ts +++ b/xml/parse.ts @@ -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()) @@ -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 + const attributes = {} as Record for (const [_, name, __, value] of raw.matchAll(/(?[A-Za-z_][-\w.:]*)=(["'])(?(?:(?!\2).)*)(\2)/g)) { attributes[`@${name}`] = value } @@ -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)["~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)["~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)["#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 }) diff --git a/xml/stringify.ts b/xml/stringify.ts index dc636508..16b1512d 100644 --- a/xml/stringify.ts +++ b/xml/stringify.ts @@ -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. */ @@ -133,7 +132,7 @@ export function comment(text: string): Omit { /** Create XML prolog. */ function xml_prolog(document: xml_document, options: _options): string { - ;(document as rw)["~name"] ??= "xml" + ;(document as Record)["~name"] ??= "xml" return xml_instruction(document, options) } @@ -222,9 +221,9 @@ function xml_children(node: xml_node, options: options): Array { 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, ["~name"]: key } as Record + if (((value as Record)["~name"] as string)?.startsWith("~")) { + child[internal] = (value as Record)["~name"] } return child }