xast-namespaces

Attach namespace information to a xast XML element tree.

Usage no npm install needed!

<script type="module">
  import xastNamespaces from 'https://cdn.skypack.dev/xast-namespaces';
</script>

README

xast-namespaces

Attach namespace information to a xast XML element tree.

build status codecov npm prettier jest

Installation

npm install xast-namespaces

Usage

import { attachNamespaces } from 'xast-namespaces';

const tree = {
  type: 'element',
  name: 'ns:parent',
  attributes: {
    'xmlns:ns': 'https://ns.example',
  },
  children: [
    {
      type: 'element',
      name: 'ns:child',
      attributes: {
        'ns:attr': 'value',
      },
      children: [],
    },
  ],
};

const result = attachNamespaces(tree);
assert.deepStrictEqual(result, {
  type: 'element',
  name: 'ns:parent',
  namespace: 'ns',
  namespaceURI: 'https://ns.example',
  localName: 'parent',
  namespaces: {
    ns: 'https://ns.example',
  },
  attributes: {
    'xmlns:ns': 'https://ns.example',
  },
  namespacedAttributes: [
    {
      type: 'attribute',
      name: 'xmlns:ns',
      namespace: 'xmlns',
      namespaceURI: undefined,
      localName: 'ns',
      value: 'https://ns.example',
    },
  ],
  children: [
    {
      type: 'element',
      name: 'ns:child',
      namespace: 'ns',
      namespaceURI: 'https://ns.example',
      localName: 'child',
      namespaces: {
        ns: 'https://ns.example',
      },
      attributes: {
        'ns:attr': 'value',
      },
      namespacedAttributes: [
        {
          type: 'attribute',
          name: 'ns:attr',
          namespace: 'ns',
          namespaceURI: 'https://ns.example',
          localName: 'attr',
          value: 'value',
        },
      ],
      children: [],
    },
  ],
});