npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

microxml

v1.0.1

Published

Minimal and fast non-validating XML reader.

Downloads

5

Readme

μ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

yarn add microxml
npm install microxml
import { fast_xml, FastBackend } from 'microxml';

class ExampleBackend implements FastBackend {
    /** A table of entity definitions. */
    defs = new Map([
        ['foo', '"&bar;"'],
        ['bar', '<baz/>'],
    ]);

    /** Handle `<?xml...?>` and `<!DOCTYPE>`. */
    async head(text: string) {
        console.log('prolog %o', text);
    }

    otag(tag: string, attrs: Map<string, string>) {
        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 = `
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE example>
    <test a="&foo;">
        &foo;
    </test>
`;

fast_xml(src, new ExampleBackend());

Features and non-features

  • The fastest¹.
  • The smallest¹ (≈1.5kB minified, no gzip).
  • ~~The smartest.~~
  • ~~The strongest.~~
  • Unlike many others, DOES reparse entity replacements:
    • With x=<b>&y;</b>, y="<c/>":
      • <a>&x;</a><a><b>"<c/>"</b></a>,
      • <a b="&x;" /><a b='<b>"<c/>"</b>'/>.
  • 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 <?xml...?> and <!DOCTYPE> 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 <a b c=d>.
  • Doesn’t handle references without the trailing semicolon &ampwtf.
  • Doesn’t handle tags without the name <></>.
  • Doesn’t handle tags like <script> and <style>.
  • Doesn’t handle void tags differently.
  • Doesn’t read streaming inputs.
  • Doesn’t report source locations.
  • Doesn’t report errors.
  • Doesn’t trim nor collapse whitespace.
    • But merges adjacent text chunks.
  • Silently ignores comments and processing instructions.
  • Silently ignores undefined entities.
  • Silently ignores text before the first tag.
  • Silently ignores text after the last tag.
  • Silently aborts at EOF-terminated attributes and attribute lists.
  • Silently aborts at expansion of unterminated attribute lists.

  1. [↑], [↑] Probably.

API

fast_xml(src, impl)

Read an XML document using the provided implementation.

Arguments:

  • src: string — the XML document source string.
  • impl: FastBackend — the backend to use.

Return:

  • Promise<void> — a promise that resolves on error or document end.

FastBackend

A backend that provides entities table and token hooks.

All the properties and hooks are assumed to be mutable.

All the hooks are called as methods, so it’s safe to use this in them.

defs

The entity definitions table.

Type:

  • Map<string, string>,
  • undefined.

Keys are entity names without leading & and trailing ;.

Values are replacements, that are allowed to include markup and other references. When an entity is referenced in the markup mode, its replacement will be reparsed as markup with both tags, comments, entity references, etc. handled as usual. When an entity is referenced in an attribute value, everything except other references is ignored, including ["'] delimiters that normally terminate the attribute value.

When filling the table from <!DOCTYPE> and/or external DTDs, be careful to expand numeric references and parametric entities %...; before adding entries to the table.

The table is never consulted for numeric and predefined entities:

  • &#...;,
  • &#x...;,
  • &lt;,
  • &gt;,
  • &amp;,
  • &apos;,
  • &quot;.

The table is assumed to be mutable, so it’s safe to update or completely replace it anytime you want.

head(head)

XML prolog hook.

Triggered even if there is no prolog, or it doesn’t include the <?xml...?> or <!DOCTYPE> declaration.

Use it to parse the XML declaration and doctype.

The hook can return promise we’ll await, so you can do some async stuff here.

Arguments:

  • head: string — the XML prolog text.

Return:

  • any — anything you want, possibly awaitable.

otag(tag, attrs)

Opening tag hook. Also triggered for void tags.

Arguments:

  • tag: string — the tag name.
  • attrs: Map<string, string> — the attributes map.

Return:

  • any — the return value is ignored.

ctag(tag)

Closing tag hook. For void tags, triggered immediately after the otag hook.

Arguments:

  • tag: string — the tag name.

Return:

  • any — the return value is ignored.

text(text)

Plain text hook.

Triggered immediately before otag or ctag with all pending plain text and <![CDATA[...]]> chunks merged, and only if the merged text is non-empty.

For example, when parsing a document like <a>b<!--x-->c<?y?>d<![CDATA[e]]>f</a>, we’ll only trigger text once with the bcdef argument.

Arguments:

  • text: string — the plain text string.

Return:

  • any — the return value is ignored.

License

MIT © 2023 Yuri Zemskov