From b8b349798b0eafd0562e602586f62b9871858927 Mon Sep 17 00:00:00 2001 From: Marina Miyaoka Date: Wed, 11 Jan 2023 22:58:11 +0300 Subject: [PATCH] Initial commit --- .gitignore | 6 + .npmignore | 13 + LICENSE.md | 18 + README.md | 229 ++ etc/build.bash | 14 + etc/mjs-resolver.cjs | 13 + etc/tsconfig.json | 90 + jest.config.mjs | 9 + package-lock.json | 6492 ++++++++++++++++++++++++++++++++++++++++++ package.json | 54 + prettier.config.cjs | 51 + rollup.config.mjs | 29 + src/index.mts | 413 +++ src/tsconfig.json | 13 + test/index.mts | 342 +++ test/tsconfig.json | 17 + tsconfig.json | 12 + 17 files changed, 7815 insertions(+) create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 LICENSE.md create mode 100644 README.md create mode 100755 etc/build.bash create mode 100644 etc/mjs-resolver.cjs create mode 100644 etc/tsconfig.json create mode 100644 jest.config.mjs create mode 100644 package-lock.json create mode 100644 package.json create mode 100755 prettier.config.cjs create mode 100644 rollup.config.mjs create mode 100644 src/index.mts create mode 100644 src/tsconfig.json create mode 100644 test/index.mts create mode 100644 test/tsconfig.json create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5332b98 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/.build/ +/.vscode/ +/dist/ +/node_modules/ + +*.tgz diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..46ecc5d --- /dev/null +++ b/.npmignore @@ -0,0 +1,13 @@ +/.build/ +/.vscode/ + +/test/ + +/etc/build.bash +/etc/mjs-resolver.cjs + +/jest.config.mjs +/prettier.config.cjs +/rollup.config.mjs + +*.tgz diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..0629fd3 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,18 @@ +© 2023 Yuri Zemskov + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the “Software”), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d68de67 --- /dev/null +++ b/README.md @@ -0,0 +1,229 @@ +# μXML + +Minimal and fast non-validating SAX-like XML reader. + +- When to use it: + - You just need to communicate with XML-based API. + - You just need to read XML-based configs or whatever. + - You don’t care of ill-formed or invalid markup. + - You don’t care of comments and processing instructions. + - You don’t care of source locations. +- When **NOT** to use it: + - You need to parse HTML, SVG, JSX, templates, etc. + - You need to validate, debug, or format XML. + - You need to handle comments and/or processing instructions. + - You need to read XML streamingly. + +## Usage + +```bash +yarn add microxml +``` + +```bash +npm install microxml +``` + +```typescript +import { fast_xml, FastBackend } from 'microxml'; + +class ExampleBackend implements FastBackend { + /** A table of entity definitions. */ + defs = new Map([ + ['foo', '"&bar;"'], + ['bar', ''], + ]); + + /** Handle `` and ``. */ + async head(text: string) { + console.log('prolog %o', text); + } + + otag(tag: string, attrs: Map) { + console.log('opening tag %o %o', tag, attrs); + } + + ctag(tag: string) { + console.log('closing tag %o', tag); + } + + text(text: string) { + console.log('text %o', text); + } +} + +const src = ` + + + + &foo; + +`; + +fast_xml(src, new ExampleBackend()); +``` + +## Features and non-features + +- The fastest[¹](#fn1). +- The smallest[¹](#fn1) (≈1.5kB minified, **no gzip**). +- ~~The smartest.~~ +- ~~The strongest.~~ +- Unlike many others, **DOES** reparse entity replacements: + - With `x`=`&y;`, `y`=`""`: + - `&x;`≡`""`, + - ``≡``. +- May or may not explode in your face at ill-formed code. +- May or may not explode in your face at invalid code. +- Doesn’t parse `` and `` declarations. + - But the `async head(text: string)` hook may do the trick. +- Doesn’t parse HTML. +- Doesn’t parse SVG. +- Doesn’t parse JSX. +- Doesn’t parse templates. +- Doesn’t handle boolean and unquoted attributes ``. +- Doesn’t handle references without the trailing semicolon `&wtf`. +- Doesn’t handle tags without the name `<>`. +- Doesn’t handle tags like `