SPB Git

Repository Public Research preview

xml.mjs 3.0 KB 71 lines javascript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : LLM API toolkits8 * File             : xlsx/skill/excel-model-workbook/scripts/lib/xml.mjs9 * Module           : xlsx10 * Repository       : https://git.spboucher.ai/llm-api-toolkits11 * Created          : 2026-11-1012 * Last Modified    : 2026-11-1013 *14 * Author           : Simon-Pierre Boucher15 * Position         : Professor of Finance16 * Institution      : Université du Québec en Outaouais (UQO)17 * Organization     : SP Boucher18 * Website          : https://spboucher.ai19 * Git              : https://git.spboucher.ai20 *21 * Preferred Contact: dev@spboucher.ai22 * Development      : dev@spboucher.ai23 * Support          : support@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142/** Tiny XML scanner, sufficient for SpreadsheetML parts. Namespace prefixes are dropped from tag names. */43const TOKEN = /<!--[\s\S]*?-->|<\?[\s\S]*?\?>|<!\[CDATA\[([\s\S]*?)\]\]>|<(\/?)([A-Za-z_][\w:.-]*)((?:\s+[\w:.-]+\s*=\s*(?:"[^"]*"|'[^']*'))*)\s*(\/?)>|([^<]+)/g;44const ATTR = /([\w:.-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;45const ENT = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };4647export function decode(s) {48  return s.indexOf("&") === -1 ? s : s.replace(/&(#x[0-9a-fA-F]+|#\d+|\w+);/g, (m, e) => (e[0] === "#" ? String.fromCodePoint(e[1] === "x" ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10)) : ENT[e] ?? m));49}50const local = (n) => { const i = n.indexOf(":"); return i === -1 ? n : n.slice(i + 1); };51function attrs(src) {52  const o = {};53  if (!src) return o;54  ATTR.lastIndex = 0;55  for (let m; (m = ATTR.exec(src)); ) o[local(m[1])] = decode(m[2] ?? m[3]);56  return o;57}5859/** Calls open(name, attrs, selfClosing), text(string) and close(name) in document order. */60export function scan(xml, { open, text, close }) {61  TOKEN.lastIndex = 0;62  for (let m; (m = TOKEN.exec(xml)); ) {63    if (m[3]) {64      const name = local(m[3]);65      if (m[2]) close?.(name);66      else { open?.(name, attrs(m[4]), m[5] === "/"); if (m[5] === "/") close?.(name); }67    } else if (m[1] !== undefined) text?.(m[1]);68    else if (m[6] !== undefined) text?.(decode(m[6]));69  }70}71
Files main
Menu

Theme