Open
2,324 changes: 2,063 additions & 261 deletions API.md

Large diffs are not rendered by default.

41 changes: 27 additions & 14 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ There are some limitations:

- Comments are discarded.
- Processing instructions are discarded.
- Namespace prefixes are preserved but otherwise ignored.
- Namespace prefixes are preserved on every node; full xmlns validation is opt-in via the `ns` parse option.
- Doctypes are discarded.
- Nested doctypes are not supported and will cause errors.
- Nodes have a limited interface.
Expand All@@ -34,10 +34,10 @@ $ npm i @borgar/simple-xml
import { parseXML } from '@borgar/simple-xml';

const dom = parseXML('<root><node>Lorem ipsum</node></root>');
console.log(dom.getElementsByTagName('node').textContent);
console.log(dom.getElementsByTagName('node')[0].textContent);
```

The parse function accepts two arguments, the first is an XML source. The second is an options object. There are two options:
The parse function accepts two arguments, the first is an XML source. The second is an options object. There are three options:

Allow normally forbidden unquoted attributes with `laxAttr`:

Expand All@@ -51,7 +51,13 @@ Allow normally forbidden "rootless" documents with `emptyDoc`:
parseXML('', { emptyDoc: true });
```

As well as a parse method, the package exports Node classes: Node, Element, Document, TextNode, and CDataNode. They are pretty much what you expect but with an incomplete or altered set of DOM API functions.
Validate `xmlns` declarations and element/attribute prefixes against them with `ns`. With this flag, an unknown prefix or a re-bound prefix throws a `NamespaceError`:

```js
parseXML('<root xmlns:x="urn:example"><x:node /></root>', { ns: true });
```

As well as a parse method, the package exports the node classes (`Node`, `Element`, `Document`, `DocumentFragment`, `TextNode`, `CDataNode`), serialization helpers (`prettyPrint`, `simplePrint`, `escapeXML`), the `isElement` type guard, and an error hierarchy rooted at `XMLError` (`ParserError`, `NamespaceError`, `HierarchyError`, `NotFoundError`). The node classes are pretty much what you'd expect but with an "incomplete" or altered set of DOM API functions.

[See the API documentation.](API.md)

Expand All@@ -68,20 +74,24 @@ As well as a parse method, the package exports Node classes: Node, Element, Docu
* `.nodeType`
A node type number (e.g. `Element.ELEMENT_NODE === 1`)

* `.ns`
A namespace identifier (`foo` in the case of `<foo:bar>`)
* `.prefix`
The namespace prefix of the element, or `null` if it has none (`foo` in the case of `<foo:bar>`).

* `.localName`
The tag name with any namespace prefix stripped (`bar` in the case of `<foo:bar>`).

* `.fullName`
A full tag name with identifier (`foo:bar` in the case of `<foo:bar>`)
The full tag name including the prefix (`foo:bar` in the case of `<foo:bar>`).

**Attributes**

* `.getAttribute( attrName )`
* `.setAttribute( attrName, attrValue )`
* `.hasAttribute( attrName )`
* `.hasAttributes()`
* `.removeAttribute( attrName )`

Attributes are not stored as attribute nodes in a list, but rather they are a simple `{ name: value }` style object on the node.
* `.attributes`
A `NamedNodeMap` of `Attr` nodes — iterable, indexable by name (`el.attributes.foo` returns the `Attr`), and with DOM-style `getNamedItem` / `setNamedItem` / `removeNamedItem` methods.

**Tree & traversal**

Expand All@@ -105,6 +115,9 @@ Attributes are not stored as attribute nodes in a list, but rather they are a si
* `.getElementsByTagName( tagName )`
Lists all Elements in the target's subtree, traversal order, that have `.tagName` equal to the argument. Function is case-sensitive.

* `.querySelector( cssSelector )`
Returns the first Element in the target's subtree, traversal order, that matches the supplied CSS selector, or `null` if none match.

* `.querySelectorAll( cssSelector )`
Lists all Elements in the target's subtree, traversal order, that match the supplied CSS selector. Function should be case-sensitive but may be case insensitive for some.

Expand All@@ -123,19 +136,19 @@ It will parse to a document which has a `.root` node looking roughly like this:
{
nodeType: 1,
nodeName: 'TAG',
ns: 'x',
prefix: 'x',
localName: 'tag',
tagName: 'tag',
fullName: 'x:tag',
attr: { 'foo': 'bar' }
attributes: NamedNodeMap { /* Attr { name: 'foo', value: 'bar' } */ },
parentNode: null,
childNodes = [
childNodes: [
{
nodeName: '#text',
nodeType: 3,
value: 'Text content',
data: 'Text content',
parentNode: {...},
}
]
}
```

47 changes: 47 additions & 0 deletions lib/Attr.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
import { Node } from './Node.js';
import { ATTRIBUTE_NODE } from './constants.js';
import { splitTagName } from './splitTagName.ts';

/**
* A class describing an attribute.
*
* @augments Node
*/
export class Attr extends Node {
/** The namespace prefix of the element, or null' if no prefix is specified. */
prefix: string | null;
/** The name of the tag for the given element, excluding any namespace prefix. */
localName: string;

/** The node's name. */
name: string;
/** The node's data value. */
value: string;

/**
* Constructs a new Attr instance.
*
* @param name The name of the attribute.
* @param value The data of the attribute.
*/
constructor (name: string, value: any) {
super();

const [ prefix, localName ] = splitTagName(name);
this.prefix = prefix;
this.localName = localName;
this.name = localName;
this.nodeName = localName;

this.value = String(value);

this.nodeType = ATTRIBUTE_NODE;
}

/** The full name of the tag for the given element, including a namespace prefix. */
get fullName () {
return this.prefix
? this.prefix + ':' + this.localName
: this.localName;
}
}
4 changes: 4 additions & 0 deletions lib/CreateChildArgument.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import type { DocumentFragment } from './DocumentFragment.ts';
import type { Node } from './Node.ts';

export type CreateChildArgument = Node | DocumentFragment | string | boolean | number | null | undefined;
133 changes: 124 additions & 9 deletions lib/Document.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,10 +2,17 @@ import { JsonML, type JsonMLElement } from './JsonML.js';
import { Node } from './Node.js';
import { Element } from './Element.js';
import { appendChild } from './appendChild.js';
import { DOCUMENT_NODE } from './constants.js';
import { DOCUMENT_NODE, XML_DECLARATION } from './constants.js';
import { domQuery } from './domQuery/index.js';
import { findAll } from './findAll.js';
import { isElement } from './isElement.ts';
import { DocumentFragment } from './DocumentFragment.ts';
import { NSMap } from './NSMap.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';
import type { CreateChildArgument } from './CreateChildArgument.ts';
import type { XMLAttr } from './XMLAttr.ts';
import { HierarchyError, NamespaceError } from './errors.js';

/**
* This class describes an XML document.
Expand All@@ -14,6 +21,8 @@ import { isElement } from './isElement.ts';
*/
export class Document extends Node {
root: Element | null = null;
/** @ignore */
namespaces = new NSMap();

/**
* Constructs a new Document instance.
Expand All@@ -25,6 +34,31 @@ export class Document extends Node {
this.nodeType = DOCUMENT_NODE;
}

/**
* Attach a namespace to the document.
*
* @param namespaceURI The namespace URI to attach.
* @param [prefix] Prefix to use on elements belonging to the namespace.
*/
attachNS (namespaceURI: string, prefix = ''): (name: string, attr?: XMLAttr | null, ...children: (CreateChildArgument | CreateChildArgument[])[]) => Element {
this.namespaces.add(namespaceURI, prefix);
this._updateNS();

// Return a new create function bound to the namespace
return this.createElementNS.bind(this, namespaceURI);
}

/** @ignore */
private _updateNS () {
// ensure that namespaces exist on the root node
if (this.root) {
for (const [ namespaceURI, prefix ] of this.namespaces.list()) {
const key = 'xmlns' + (prefix ? ':' + prefix : '');
this.root.setAttribute(key, namespaceURI);
}
}
}

// overwrites super
get textContent () {
return this.root ? this.root.textContent : '';
Expand All@@ -37,6 +71,58 @@ export class Document extends Node {
return this.childNodes.filter(isElement);
}

/**
* Create a new element node.
*
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElement = (
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const element = new Element(qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Create a new element node associated with a given namespace.
*
* @param namespaceURI The namespaceURI to associate with the element.
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElementNS = (
namespaceURI: string,
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const ns = this.namespaces.get(namespaceURI);
if (ns == null) {
throw new NamespaceError('Unknown namespace ' + namespaceURI);
}
const element = new Element(ns ? ns + ':' + qualifiedName : qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Return all descendant elements that have the specified tag name.
*
Expand All@@ -45,7 +131,7 @@ export class Document extends Node {
*/
getElementsByTagName (tagName: string): Element[] {
if (!tagName) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return findAll(this, tagName, []);
}
Expand All@@ -58,7 +144,7 @@ export class Document extends Node {
*/
querySelector (selector: string): Element | null {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector)[0] || null;
}
Expand All@@ -71,18 +157,32 @@ export class Document extends Node {
*/
querySelectorAll (selector: string): Element[] {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector);
}

// overwrites super
appendChild (node: Element): Element {
if (this.root) {
throw new Error('A document may only have one child/root element.');
appendChild<T extends Node | DocumentFragment> (node: T): T {
if (this.root || (node instanceof DocumentFragment && node.childNodes.length > 1)) {
throw new HierarchyError('A document must have only one child element.');
}
let root: Element;
if (node instanceof DocumentFragment) {
if (!(node.childNodes[0] instanceof Element)) {
throw new HierarchyError('Document root node must be an Element');
}
root = node.childNodes[0];
}
else if (node instanceof Element) {
root = node;
}
appendChild(this, node);
this.root = node;
else {
throw new HierarchyError('Document root node must be an Element');
}
appendChild(this, root);
this.root = root;
this._updateNS();
return node;
}

Expand All@@ -94,4 +194,19 @@ export class Document extends Node {
toJS (): JsonMLElement | [] {
return this.root ? JsonML(this.root) : [];
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
if (!(this.root instanceof Element)) {
throw new HierarchyError('root element is missing');
}
return `${XML_DECLARATION}\n` + (
pretty ? prettyPrint(this.root) : simplePrint(this.root)
);
}
}
43 changes: 43 additions & 0 deletions lib/DocumentFragment.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
import { appendChild } from './appendChild.ts';
import { DOCUMENT_FRAGMENT_NODE } from './constants.ts';
import type { Node } from './Node.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';

/**
* A class describing a DocumentFragment.
*/
export class DocumentFragment {
/** The immediate children contained in the fragment. */
childNodes: Node[] = [];
/** A numerical node type identifier. */
nodeType: number = DOCUMENT_FRAGMENT_NODE;

/**
* Appends a child node into the document fragment.
*
* @param node The new child node
* @returns The same node that was passed in.
*/
appendChild<T extends Node | DocumentFragment> (node: T): T {
appendChild(this, node);
return node;
}

/** @ignore */
toString (): string {
return '#document-fragment';
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
return pretty
? prettyPrint(this)
: simplePrint(this);
}
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Open
2,324 changes: 2,063 additions & 261 deletions API.md

Large diffs are not rendered by default.

41 changes: 27 additions & 14 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ There are some limitations:

- Comments are discarded.
- Processing instructions are discarded.
- Namespace prefixes are preserved but otherwise ignored.
- Namespace prefixes are preserved on every node; full xmlns validation is opt-in via the `ns` parse option.
- Doctypes are discarded.
- Nested doctypes are not supported and will cause errors.
- Nodes have a limited interface.
Expand All@@ -34,10 +34,10 @@ $ npm i @borgar/simple-xml
import { parseXML } from '@borgar/simple-xml';

const dom = parseXML('<root><node>Lorem ipsum</node></root>');
console.log(dom.getElementsByTagName('node').textContent);
console.log(dom.getElementsByTagName('node')[0].textContent);
```

The parse function accepts two arguments, the first is an XML source. The second is an options object. There are two options:
The parse function accepts two arguments, the first is an XML source. The second is an options object. There are three options:

Allow normally forbidden unquoted attributes with `laxAttr`:

Expand All@@ -51,7 +51,13 @@ Allow normally forbidden "rootless" documents with `emptyDoc`:
parseXML('', { emptyDoc: true });
```

As well as a parse method, the package exports Node classes: Node, Element, Document, TextNode, and CDataNode. They are pretty much what you expect but with an incomplete or altered set of DOM API functions.
Validate `xmlns` declarations and element/attribute prefixes against them with `ns`. With this flag, an unknown prefix or a re-bound prefix throws a `NamespaceError`:

```js
parseXML('<root xmlns:x="urn:example"><x:node /></root>', { ns: true });
```

As well as a parse method, the package exports the node classes (`Node`, `Element`, `Document`, `DocumentFragment`, `TextNode`, `CDataNode`), serialization helpers (`prettyPrint`, `simplePrint`, `escapeXML`), the `isElement` type guard, and an error hierarchy rooted at `XMLError` (`ParserError`, `NamespaceError`, `HierarchyError`, `NotFoundError`). The node classes are pretty much what you'd expect but with an "incomplete" or altered set of DOM API functions.

[See the API documentation.](API.md)

Expand All@@ -68,20 +74,24 @@ As well as a parse method, the package exports Node classes: Node, Element, Docu
* `.nodeType`
A node type number (e.g. `Element.ELEMENT_NODE === 1`)

* `.ns`
A namespace identifier (`foo` in the case of `<foo:bar>`)
* `.prefix`
The namespace prefix of the element, or `null` if it has none (`foo` in the case of `<foo:bar>`).

* `.localName`
The tag name with any namespace prefix stripped (`bar` in the case of `<foo:bar>`).

* `.fullName`
A full tag name with identifier (`foo:bar` in the case of `<foo:bar>`)
The full tag name including the prefix (`foo:bar` in the case of `<foo:bar>`).

**Attributes**

* `.getAttribute( attrName )`
* `.setAttribute( attrName, attrValue )`
* `.hasAttribute( attrName )`
* `.hasAttributes()`
* `.removeAttribute( attrName )`

Attributes are not stored as attribute nodes in a list, but rather they are a simple `{ name: value }` style object on the node.
* `.attributes`
A `NamedNodeMap` of `Attr` nodes — iterable, indexable by name (`el.attributes.foo` returns the `Attr`), and with DOM-style `getNamedItem` / `setNamedItem` / `removeNamedItem` methods.

**Tree & traversal**

Expand All@@ -105,6 +115,9 @@ Attributes are not stored as attribute nodes in a list, but rather they are a si
* `.getElementsByTagName( tagName )`
Lists all Elements in the target's subtree, traversal order, that have `.tagName` equal to the argument. Function is case-sensitive.

* `.querySelector( cssSelector )`
Returns the first Element in the target's subtree, traversal order, that matches the supplied CSS selector, or `null` if none match.

* `.querySelectorAll( cssSelector )`
Lists all Elements in the target's subtree, traversal order, that match the supplied CSS selector. Function should be case-sensitive but may be case insensitive for some.

Expand All@@ -123,19 +136,19 @@ It will parse to a document which has a `.root` node looking roughly like this:
{
nodeType: 1,
nodeName: 'TAG',
ns: 'x',
prefix: 'x',
localName: 'tag',
tagName: 'tag',
fullName: 'x:tag',
attr: { 'foo': 'bar' }
attributes: NamedNodeMap { /* Attr { name: 'foo', value: 'bar' } */ },
parentNode: null,
childNodes = [
childNodes: [
{
nodeName: '#text',
nodeType: 3,
value: 'Text content',
data: 'Text content',
parentNode: {...},
}
]
}
```

47 changes: 47 additions & 0 deletions lib/Attr.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
import { Node } from './Node.js';
import { ATTRIBUTE_NODE } from './constants.js';
import { splitTagName } from './splitTagName.ts';

/**
* A class describing an attribute.
*
* @augments Node
*/
export class Attr extends Node {
/** The namespace prefix of the element, or null' if no prefix is specified. */
prefix: string | null;
/** The name of the tag for the given element, excluding any namespace prefix. */
localName: string;

/** The node's name. */
name: string;
/** The node's data value. */
value: string;

/**
* Constructs a new Attr instance.
*
* @param name The name of the attribute.
* @param value The data of the attribute.
*/
constructor (name: string, value: any) {
super();

const [ prefix, localName ] = splitTagName(name);
this.prefix = prefix;
this.localName = localName;
this.name = localName;
this.nodeName = localName;

this.value = String(value);

this.nodeType = ATTRIBUTE_NODE;
}

/** The full name of the tag for the given element, including a namespace prefix. */
get fullName () {
return this.prefix
? this.prefix + ':' + this.localName
: this.localName;
}
}
4 changes: 4 additions & 0 deletions lib/CreateChildArgument.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import type { DocumentFragment } from './DocumentFragment.ts';
import type { Node } from './Node.ts';

export type CreateChildArgument = Node | DocumentFragment | string | boolean | number | null | undefined;
133 changes: 124 additions & 9 deletions lib/Document.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,10 +2,17 @@ import { JsonML, type JsonMLElement } from './JsonML.js';
import { Node } from './Node.js';
import { Element } from './Element.js';
import { appendChild } from './appendChild.js';
import { DOCUMENT_NODE } from './constants.js';
import { DOCUMENT_NODE, XML_DECLARATION } from './constants.js';
import { domQuery } from './domQuery/index.js';
import { findAll } from './findAll.js';
import { isElement } from './isElement.ts';
import { DocumentFragment } from './DocumentFragment.ts';
import { NSMap } from './NSMap.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';
import type { CreateChildArgument } from './CreateChildArgument.ts';
import type { XMLAttr } from './XMLAttr.ts';
import { HierarchyError, NamespaceError } from './errors.js';

/**
* This class describes an XML document.
Expand All@@ -14,6 +21,8 @@ import { isElement } from './isElement.ts';
*/
export class Document extends Node {
root: Element | null = null;
/** @ignore */
namespaces = new NSMap();

/**
* Constructs a new Document instance.
Expand All@@ -25,6 +34,31 @@ export class Document extends Node {
this.nodeType = DOCUMENT_NODE;
}

/**
* Attach a namespace to the document.
*
* @param namespaceURI The namespace URI to attach.
* @param [prefix] Prefix to use on elements belonging to the namespace.
*/
attachNS (namespaceURI: string, prefix = ''): (name: string, attr?: XMLAttr | null, ...children: (CreateChildArgument | CreateChildArgument[])[]) => Element {
this.namespaces.add(namespaceURI, prefix);
this._updateNS();

// Return a new create function bound to the namespace
return this.createElementNS.bind(this, namespaceURI);
}

/** @ignore */
private _updateNS () {
// ensure that namespaces exist on the root node
if (this.root) {
for (const [ namespaceURI, prefix ] of this.namespaces.list()) {
const key = 'xmlns' + (prefix ? ':' + prefix : '');
this.root.setAttribute(key, namespaceURI);
}
}
}

// overwrites super
get textContent () {
return this.root ? this.root.textContent : '';
Expand All@@ -37,6 +71,58 @@ export class Document extends Node {
return this.childNodes.filter(isElement);
}

/**
* Create a new element node.
*
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElement = (
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const element = new Element(qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Create a new element node associated with a given namespace.
*
* @param namespaceURI The namespaceURI to associate with the element.
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElementNS = (
namespaceURI: string,
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const ns = this.namespaces.get(namespaceURI);
if (ns == null) {
throw new NamespaceError('Unknown namespace ' + namespaceURI);
}
const element = new Element(ns ? ns + ':' + qualifiedName : qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Return all descendant elements that have the specified tag name.
*
Expand All@@ -45,7 +131,7 @@ export class Document extends Node {
*/
getElementsByTagName (tagName: string): Element[] {
if (!tagName) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return findAll(this, tagName, []);
}
Expand All@@ -58,7 +144,7 @@ export class Document extends Node {
*/
querySelector (selector: string): Element | null {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector)[0] || null;
}
Expand All@@ -71,18 +157,32 @@ export class Document extends Node {
*/
querySelectorAll (selector: string): Element[] {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector);
}

// overwrites super
appendChild (node: Element): Element {
if (this.root) {
throw new Error('A document may only have one child/root element.');
appendChild<T extends Node | DocumentFragment> (node: T): T {
if (this.root || (node instanceof DocumentFragment && node.childNodes.length > 1)) {
throw new HierarchyError('A document must have only one child element.');
}
let root: Element;
if (node instanceof DocumentFragment) {
if (!(node.childNodes[0] instanceof Element)) {
throw new HierarchyError('Document root node must be an Element');
}
root = node.childNodes[0];
}
else if (node instanceof Element) {
root = node;
}
appendChild(this, node);
this.root = node;
else {
throw new HierarchyError('Document root node must be an Element');
}
appendChild(this, root);
this.root = root;
this._updateNS();
return node;
}

Expand All@@ -94,4 +194,19 @@ export class Document extends Node {
toJS (): JsonMLElement | [] {
return this.root ? JsonML(this.root) : [];
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
if (!(this.root instanceof Element)) {
throw new HierarchyError('root element is missing');
}
return `${XML_DECLARATION}\n` + (
pretty ? prettyPrint(this.root) : simplePrint(this.root)
);
}
}
43 changes: 43 additions & 0 deletions lib/DocumentFragment.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
import { appendChild } from './appendChild.ts';
import { DOCUMENT_FRAGMENT_NODE } from './constants.ts';
import type { Node } from './Node.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';

/**
* A class describing a DocumentFragment.
*/
export class DocumentFragment {
/** The immediate children contained in the fragment. */
childNodes: Node[] = [];
/** A numerical node type identifier. */
nodeType: number = DOCUMENT_FRAGMENT_NODE;

/**
* Appends a child node into the document fragment.
*
* @param node The new child node
* @returns The same node that was passed in.
*/
appendChild<T extends Node | DocumentFragment> (node: T): T {
appendChild(this, node);
return node;
}

/** @ignore */
toString (): string {
return '#document-fragment';
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
return pretty
? prettyPrint(this)
: simplePrint(this);
}
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Open
2,324 changes: 2,063 additions & 261 deletions API.md

Large diffs are not rendered by default.

41 changes: 27 additions & 14 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ There are some limitations:

- Comments are discarded.
- Processing instructions are discarded.
- Namespace prefixes are preserved but otherwise ignored.
- Namespace prefixes are preserved on every node; full xmlns validation is opt-in via the `ns` parse option.
- Doctypes are discarded.
- Nested doctypes are not supported and will cause errors.
- Nodes have a limited interface.
Expand All@@ -34,10 +34,10 @@ $ npm i @borgar/simple-xml
import { parseXML } from '@borgar/simple-xml';

const dom = parseXML('<root><node>Lorem ipsum</node></root>');
console.log(dom.getElementsByTagName('node').textContent);
console.log(dom.getElementsByTagName('node')[0].textContent);
```

The parse function accepts two arguments, the first is an XML source. The second is an options object. There are two options:
The parse function accepts two arguments, the first is an XML source. The second is an options object. There are three options:

Allow normally forbidden unquoted attributes with `laxAttr`:

Expand All@@ -51,7 +51,13 @@ Allow normally forbidden "rootless" documents with `emptyDoc`:
parseXML('', { emptyDoc: true });
```

As well as a parse method, the package exports Node classes: Node, Element, Document, TextNode, and CDataNode. They are pretty much what you expect but with an incomplete or altered set of DOM API functions.
Validate `xmlns` declarations and element/attribute prefixes against them with `ns`. With this flag, an unknown prefix or a re-bound prefix throws a `NamespaceError`:

```js
parseXML('<root xmlns:x="urn:example"><x:node /></root>', { ns: true });
```

As well as a parse method, the package exports the node classes (`Node`, `Element`, `Document`, `DocumentFragment`, `TextNode`, `CDataNode`), serialization helpers (`prettyPrint`, `simplePrint`, `escapeXML`), the `isElement` type guard, and an error hierarchy rooted at `XMLError` (`ParserError`, `NamespaceError`, `HierarchyError`, `NotFoundError`). The node classes are pretty much what you'd expect but with an "incomplete" or altered set of DOM API functions.

[See the API documentation.](API.md)

Expand All@@ -68,20 +74,24 @@ As well as a parse method, the package exports Node classes: Node, Element, Docu
* `.nodeType`
A node type number (e.g. `Element.ELEMENT_NODE === 1`)

* `.ns`
A namespace identifier (`foo` in the case of `<foo:bar>`)
* `.prefix`
The namespace prefix of the element, or `null` if it has none (`foo` in the case of `<foo:bar>`).

* `.localName`
The tag name with any namespace prefix stripped (`bar` in the case of `<foo:bar>`).

* `.fullName`
A full tag name with identifier (`foo:bar` in the case of `<foo:bar>`)
The full tag name including the prefix (`foo:bar` in the case of `<foo:bar>`).

**Attributes**

* `.getAttribute( attrName )`
* `.setAttribute( attrName, attrValue )`
* `.hasAttribute( attrName )`
* `.hasAttributes()`
* `.removeAttribute( attrName )`

Attributes are not stored as attribute nodes in a list, but rather they are a simple `{ name: value }` style object on the node.
* `.attributes`
A `NamedNodeMap` of `Attr` nodes — iterable, indexable by name (`el.attributes.foo` returns the `Attr`), and with DOM-style `getNamedItem` / `setNamedItem` / `removeNamedItem` methods.

**Tree & traversal**

Expand All@@ -105,6 +115,9 @@ Attributes are not stored as attribute nodes in a list, but rather they are a si
* `.getElementsByTagName( tagName )`
Lists all Elements in the target's subtree, traversal order, that have `.tagName` equal to the argument. Function is case-sensitive.

* `.querySelector( cssSelector )`
Returns the first Element in the target's subtree, traversal order, that matches the supplied CSS selector, or `null` if none match.

* `.querySelectorAll( cssSelector )`
Lists all Elements in the target's subtree, traversal order, that match the supplied CSS selector. Function should be case-sensitive but may be case insensitive for some.

Expand All@@ -123,19 +136,19 @@ It will parse to a document which has a `.root` node looking roughly like this:
{
nodeType: 1,
nodeName: 'TAG',
ns: 'x',
prefix: 'x',
localName: 'tag',
tagName: 'tag',
fullName: 'x:tag',
attr: { 'foo': 'bar' }
attributes: NamedNodeMap { /* Attr { name: 'foo', value: 'bar' } */ },
parentNode: null,
childNodes = [
childNodes: [
{
nodeName: '#text',
nodeType: 3,
value: 'Text content',
data: 'Text content',
parentNode: {...},
}
]
}
```

47 changes: 47 additions & 0 deletions lib/Attr.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
import { Node } from './Node.js';
import { ATTRIBUTE_NODE } from './constants.js';
import { splitTagName } from './splitTagName.ts';

/**
* A class describing an attribute.
*
* @augments Node
*/
export class Attr extends Node {
/** The namespace prefix of the element, or null' if no prefix is specified. */
prefix: string | null;
/** The name of the tag for the given element, excluding any namespace prefix. */
localName: string;

/** The node's name. */
name: string;
/** The node's data value. */
value: string;

/**
* Constructs a new Attr instance.
*
* @param name The name of the attribute.
* @param value The data of the attribute.
*/
constructor (name: string, value: any) {
super();

const [ prefix, localName ] = splitTagName(name);
this.prefix = prefix;
this.localName = localName;
this.name = localName;
this.nodeName = localName;

this.value = String(value);

this.nodeType = ATTRIBUTE_NODE;
}

/** The full name of the tag for the given element, including a namespace prefix. */
get fullName () {
return this.prefix
? this.prefix + ':' + this.localName
: this.localName;
}
}
4 changes: 4 additions & 0 deletions lib/CreateChildArgument.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import type { DocumentFragment } from './DocumentFragment.ts';
import type { Node } from './Node.ts';

export type CreateChildArgument = Node | DocumentFragment | string | boolean | number | null | undefined;
133 changes: 124 additions & 9 deletions lib/Document.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,10 +2,17 @@ import { JsonML, type JsonMLElement } from './JsonML.js';
import { Node } from './Node.js';
import { Element } from './Element.js';
import { appendChild } from './appendChild.js';
import { DOCUMENT_NODE } from './constants.js';
import { DOCUMENT_NODE, XML_DECLARATION } from './constants.js';
import { domQuery } from './domQuery/index.js';
import { findAll } from './findAll.js';
import { isElement } from './isElement.ts';
import { DocumentFragment } from './DocumentFragment.ts';
import { NSMap } from './NSMap.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';
import type { CreateChildArgument } from './CreateChildArgument.ts';
import type { XMLAttr } from './XMLAttr.ts';
import { HierarchyError, NamespaceError } from './errors.js';

/**
* This class describes an XML document.
Expand All@@ -14,6 +21,8 @@ import { isElement } from './isElement.ts';
*/
export class Document extends Node {
root: Element | null = null;
/** @ignore */
namespaces = new NSMap();

/**
* Constructs a new Document instance.
Expand All@@ -25,6 +34,31 @@ export class Document extends Node {
this.nodeType = DOCUMENT_NODE;
}

/**
* Attach a namespace to the document.
*
* @param namespaceURI The namespace URI to attach.
* @param [prefix] Prefix to use on elements belonging to the namespace.
*/
attachNS (namespaceURI: string, prefix = ''): (name: string, attr?: XMLAttr | null, ...children: (CreateChildArgument | CreateChildArgument[])[]) => Element {
this.namespaces.add(namespaceURI, prefix);
this._updateNS();

// Return a new create function bound to the namespace
return this.createElementNS.bind(this, namespaceURI);
}

/** @ignore */
private _updateNS () {
// ensure that namespaces exist on the root node
if (this.root) {
for (const [ namespaceURI, prefix ] of this.namespaces.list()) {
const key = 'xmlns' + (prefix ? ':' + prefix : '');
this.root.setAttribute(key, namespaceURI);
}
}
}

// overwrites super
get textContent () {
return this.root ? this.root.textContent : '';
Expand All@@ -37,6 +71,58 @@ export class Document extends Node {
return this.childNodes.filter(isElement);
}

/**
* Create a new element node.
*
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElement = (
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const element = new Element(qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Create a new element node associated with a given namespace.
*
* @param namespaceURI The namespaceURI to associate with the element.
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElementNS = (
namespaceURI: string,
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const ns = this.namespaces.get(namespaceURI);
if (ns == null) {
throw new NamespaceError('Unknown namespace ' + namespaceURI);
}
const element = new Element(ns ? ns + ':' + qualifiedName : qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Return all descendant elements that have the specified tag name.
*
Expand All@@ -45,7 +131,7 @@ export class Document extends Node {
*/
getElementsByTagName (tagName: string): Element[] {
if (!tagName) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return findAll(this, tagName, []);
}
Expand All@@ -58,7 +144,7 @@ export class Document extends Node {
*/
querySelector (selector: string): Element | null {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector)[0] || null;
}
Expand All@@ -71,18 +157,32 @@ export class Document extends Node {
*/
querySelectorAll (selector: string): Element[] {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector);
}

// overwrites super
appendChild (node: Element): Element {
if (this.root) {
throw new Error('A document may only have one child/root element.');
appendChild<T extends Node | DocumentFragment> (node: T): T {
if (this.root || (node instanceof DocumentFragment && node.childNodes.length > 1)) {
throw new HierarchyError('A document must have only one child element.');
}
let root: Element;
if (node instanceof DocumentFragment) {
if (!(node.childNodes[0] instanceof Element)) {
throw new HierarchyError('Document root node must be an Element');
}
root = node.childNodes[0];
}
else if (node instanceof Element) {
root = node;
}
appendChild(this, node);
this.root = node;
else {
throw new HierarchyError('Document root node must be an Element');
}
appendChild(this, root);
this.root = root;
this._updateNS();
return node;
}

Expand All@@ -94,4 +194,19 @@ export class Document extends Node {
toJS (): JsonMLElement | [] {
return this.root ? JsonML(this.root) : [];
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
if (!(this.root instanceof Element)) {
throw new HierarchyError('root element is missing');
}
return `${XML_DECLARATION}\n` + (
pretty ? prettyPrint(this.root) : simplePrint(this.root)
);
}
}
43 changes: 43 additions & 0 deletions lib/DocumentFragment.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
import { appendChild } from './appendChild.ts';
import { DOCUMENT_FRAGMENT_NODE } from './constants.ts';
import type { Node } from './Node.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';

/**
* A class describing a DocumentFragment.
*/
export class DocumentFragment {
/** The immediate children contained in the fragment. */
childNodes: Node[] = [];
/** A numerical node type identifier. */
nodeType: number = DOCUMENT_FRAGMENT_NODE;

/**
* Appends a child node into the document fragment.
*
* @param node The new child node
* @returns The same node that was passed in.
*/
appendChild<T extends Node | DocumentFragment> (node: T): T {
appendChild(this, node);
return node;
}

/** @ignore */
toString (): string {
return '#document-fragment';
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
return pretty
? prettyPrint(this)
: simplePrint(this);
}
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Open
2,324 changes: 2,063 additions & 261 deletions API.md

Large diffs are not rendered by default.

41 changes: 27 additions & 14 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ There are some limitations:

- Comments are discarded.
- Processing instructions are discarded.
- Namespace prefixes are preserved but otherwise ignored.
- Namespace prefixes are preserved on every node; full xmlns validation is opt-in via the `ns` parse option.
- Doctypes are discarded.
- Nested doctypes are not supported and will cause errors.
- Nodes have a limited interface.
Expand All@@ -34,10 +34,10 @@ $ npm i @borgar/simple-xml
import { parseXML } from '@borgar/simple-xml';

const dom = parseXML('<root><node>Lorem ipsum</node></root>');
console.log(dom.getElementsByTagName('node').textContent);
console.log(dom.getElementsByTagName('node')[0].textContent);
```

The parse function accepts two arguments, the first is an XML source. The second is an options object. There are two options:
The parse function accepts two arguments, the first is an XML source. The second is an options object. There are three options:

Allow normally forbidden unquoted attributes with `laxAttr`:

Expand All@@ -51,7 +51,13 @@ Allow normally forbidden "rootless" documents with `emptyDoc`:
parseXML('', { emptyDoc: true });
```

As well as a parse method, the package exports Node classes: Node, Element, Document, TextNode, and CDataNode. They are pretty much what you expect but with an incomplete or altered set of DOM API functions.
Validate `xmlns` declarations and element/attribute prefixes against them with `ns`. With this flag, an unknown prefix or a re-bound prefix throws a `NamespaceError`:

```js
parseXML('<root xmlns:x="urn:example"><x:node /></root>', { ns: true });
```

As well as a parse method, the package exports the node classes (`Node`, `Element`, `Document`, `DocumentFragment`, `TextNode`, `CDataNode`), serialization helpers (`prettyPrint`, `simplePrint`, `escapeXML`), the `isElement` type guard, and an error hierarchy rooted at `XMLError` (`ParserError`, `NamespaceError`, `HierarchyError`, `NotFoundError`). The node classes are pretty much what you'd expect but with an "incomplete" or altered set of DOM API functions.

[See the API documentation.](API.md)

Expand All@@ -68,20 +74,24 @@ As well as a parse method, the package exports Node classes: Node, Element, Docu
* `.nodeType`
A node type number (e.g. `Element.ELEMENT_NODE === 1`)

* `.ns`
A namespace identifier (`foo` in the case of `<foo:bar>`)
* `.prefix`
The namespace prefix of the element, or `null` if it has none (`foo` in the case of `<foo:bar>`).

* `.localName`
The tag name with any namespace prefix stripped (`bar` in the case of `<foo:bar>`).

* `.fullName`
A full tag name with identifier (`foo:bar` in the case of `<foo:bar>`)
The full tag name including the prefix (`foo:bar` in the case of `<foo:bar>`).

**Attributes**

* `.getAttribute( attrName )`
* `.setAttribute( attrName, attrValue )`
* `.hasAttribute( attrName )`
* `.hasAttributes()`
* `.removeAttribute( attrName )`

Attributes are not stored as attribute nodes in a list, but rather they are a simple `{ name: value }` style object on the node.
* `.attributes`
A `NamedNodeMap` of `Attr` nodes — iterable, indexable by name (`el.attributes.foo` returns the `Attr`), and with DOM-style `getNamedItem` / `setNamedItem` / `removeNamedItem` methods.

**Tree & traversal**

Expand All@@ -105,6 +115,9 @@ Attributes are not stored as attribute nodes in a list, but rather they are a si
* `.getElementsByTagName( tagName )`
Lists all Elements in the target's subtree, traversal order, that have `.tagName` equal to the argument. Function is case-sensitive.

* `.querySelector( cssSelector )`
Returns the first Element in the target's subtree, traversal order, that matches the supplied CSS selector, or `null` if none match.

* `.querySelectorAll( cssSelector )`
Lists all Elements in the target's subtree, traversal order, that match the supplied CSS selector. Function should be case-sensitive but may be case insensitive for some.

Expand All@@ -123,19 +136,19 @@ It will parse to a document which has a `.root` node looking roughly like this:
{
nodeType: 1,
nodeName: 'TAG',
ns: 'x',
prefix: 'x',
localName: 'tag',
tagName: 'tag',
fullName: 'x:tag',
attr: { 'foo': 'bar' }
attributes: NamedNodeMap { /* Attr { name: 'foo', value: 'bar' } */ },
parentNode: null,
childNodes = [
childNodes: [
{
nodeName: '#text',
nodeType: 3,
value: 'Text content',
data: 'Text content',
parentNode: {...},
}
]
}
```

47 changes: 47 additions & 0 deletions lib/Attr.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
import { Node } from './Node.js';
import { ATTRIBUTE_NODE } from './constants.js';
import { splitTagName } from './splitTagName.ts';

/**
* A class describing an attribute.
*
* @augments Node
*/
export class Attr extends Node {
/** The namespace prefix of the element, or null' if no prefix is specified. */
prefix: string | null;
/** The name of the tag for the given element, excluding any namespace prefix. */
localName: string;

/** The node's name. */
name: string;
/** The node's data value. */
value: string;

/**
* Constructs a new Attr instance.
*
* @param name The name of the attribute.
* @param value The data of the attribute.
*/
constructor (name: string, value: any) {
super();

const [ prefix, localName ] = splitTagName(name);
this.prefix = prefix;
this.localName = localName;
this.name = localName;
this.nodeName = localName;

this.value = String(value);

this.nodeType = ATTRIBUTE_NODE;
}

/** The full name of the tag for the given element, including a namespace prefix. */
get fullName () {
return this.prefix
? this.prefix + ':' + this.localName
: this.localName;
}
}
4 changes: 4 additions & 0 deletions lib/CreateChildArgument.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import type { DocumentFragment } from './DocumentFragment.ts';
import type { Node } from './Node.ts';

export type CreateChildArgument = Node | DocumentFragment | string | boolean | number | null | undefined;
133 changes: 124 additions & 9 deletions lib/Document.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,10 +2,17 @@ import { JsonML, type JsonMLElement } from './JsonML.js';
import { Node } from './Node.js';
import { Element } from './Element.js';
import { appendChild } from './appendChild.js';
import { DOCUMENT_NODE } from './constants.js';
import { DOCUMENT_NODE, XML_DECLARATION } from './constants.js';
import { domQuery } from './domQuery/index.js';
import { findAll } from './findAll.js';
import { isElement } from './isElement.ts';
import { DocumentFragment } from './DocumentFragment.ts';
import { NSMap } from './NSMap.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';
import type { CreateChildArgument } from './CreateChildArgument.ts';
import type { XMLAttr } from './XMLAttr.ts';
import { HierarchyError, NamespaceError } from './errors.js';

/**
* This class describes an XML document.
Expand All@@ -14,6 +21,8 @@ import { isElement } from './isElement.ts';
*/
export class Document extends Node {
root: Element | null = null;
/** @ignore */
namespaces = new NSMap();

/**
* Constructs a new Document instance.
Expand All@@ -25,6 +34,31 @@ export class Document extends Node {
this.nodeType = DOCUMENT_NODE;
}

/**
* Attach a namespace to the document.
*
* @param namespaceURI The namespace URI to attach.
* @param [prefix] Prefix to use on elements belonging to the namespace.
*/
attachNS (namespaceURI: string, prefix = ''): (name: string, attr?: XMLAttr | null, ...children: (CreateChildArgument | CreateChildArgument[])[]) => Element {
this.namespaces.add(namespaceURI, prefix);
this._updateNS();

// Return a new create function bound to the namespace
return this.createElementNS.bind(this, namespaceURI);
}

/** @ignore */
private _updateNS () {
// ensure that namespaces exist on the root node
if (this.root) {
for (const [ namespaceURI, prefix ] of this.namespaces.list()) {
const key = 'xmlns' + (prefix ? ':' + prefix : '');
this.root.setAttribute(key, namespaceURI);
}
}
}

// overwrites super
get textContent () {
return this.root ? this.root.textContent : '';
Expand All@@ -37,6 +71,58 @@ export class Document extends Node {
return this.childNodes.filter(isElement);
}

/**
* Create a new element node.
*
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElement = (
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const element = new Element(qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Create a new element node associated with a given namespace.
*
* @param namespaceURI The namespaceURI to associate with the element.
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElementNS = (
namespaceURI: string,
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const ns = this.namespaces.get(namespaceURI);
if (ns == null) {
throw new NamespaceError('Unknown namespace ' + namespaceURI);
}
const element = new Element(ns ? ns + ':' + qualifiedName : qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Return all descendant elements that have the specified tag name.
*
Expand All@@ -45,7 +131,7 @@ export class Document extends Node {
*/
getElementsByTagName (tagName: string): Element[] {
if (!tagName) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return findAll(this, tagName, []);
}
Expand All@@ -58,7 +144,7 @@ export class Document extends Node {
*/
querySelector (selector: string): Element | null {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector)[0] || null;
}
Expand All@@ -71,18 +157,32 @@ export class Document extends Node {
*/
querySelectorAll (selector: string): Element[] {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector);
}

// overwrites super
appendChild (node: Element): Element {
if (this.root) {
throw new Error('A document may only have one child/root element.');
appendChild<T extends Node | DocumentFragment> (node: T): T {
if (this.root || (node instanceof DocumentFragment && node.childNodes.length > 1)) {
throw new HierarchyError('A document must have only one child element.');
}
let root: Element;
if (node instanceof DocumentFragment) {
if (!(node.childNodes[0] instanceof Element)) {
throw new HierarchyError('Document root node must be an Element');
}
root = node.childNodes[0];
}
else if (node instanceof Element) {
root = node;
}
appendChild(this, node);
this.root = node;
else {
throw new HierarchyError('Document root node must be an Element');
}
appendChild(this, root);
this.root = root;
this._updateNS();
return node;
}

Expand All@@ -94,4 +194,19 @@ export class Document extends Node {
toJS (): JsonMLElement | [] {
return this.root ? JsonML(this.root) : [];
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
if (!(this.root instanceof Element)) {
throw new HierarchyError('root element is missing');
}
return `${XML_DECLARATION}\n` + (
pretty ? prettyPrint(this.root) : simplePrint(this.root)
);
}
}
43 changes: 43 additions & 0 deletions lib/DocumentFragment.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
import { appendChild } from './appendChild.ts';
import { DOCUMENT_FRAGMENT_NODE } from './constants.ts';
import type { Node } from './Node.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';

/**
* A class describing a DocumentFragment.
*/
export class DocumentFragment {
/** The immediate children contained in the fragment. */
childNodes: Node[] = [];
/** A numerical node type identifier. */
nodeType: number = DOCUMENT_FRAGMENT_NODE;

/**
* Appends a child node into the document fragment.
*
* @param node The new child node
* @returns The same node that was passed in.
*/
appendChild<T extends Node | DocumentFragment> (node: T): T {
appendChild(this, node);
return node;
}

/** @ignore */
toString (): string {
return '#document-fragment';
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
return pretty
? prettyPrint(this)
: simplePrint(this);
}
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Open
2,324 changes: 2,063 additions & 261 deletions API.md

Large diffs are not rendered by default.

41 changes: 27 additions & 14 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ There are some limitations:

- Comments are discarded.
- Processing instructions are discarded.
- Namespace prefixes are preserved but otherwise ignored.
- Namespace prefixes are preserved on every node; full xmlns validation is opt-in via the `ns` parse option.
- Doctypes are discarded.
- Nested doctypes are not supported and will cause errors.
- Nodes have a limited interface.
Expand All@@ -34,10 +34,10 @@ $ npm i @borgar/simple-xml
import { parseXML } from '@borgar/simple-xml';

const dom = parseXML('<root><node>Lorem ipsum</node></root>');
console.log(dom.getElementsByTagName('node').textContent);
console.log(dom.getElementsByTagName('node')[0].textContent);
```

The parse function accepts two arguments, the first is an XML source. The second is an options object. There are two options:
The parse function accepts two arguments, the first is an XML source. The second is an options object. There are three options:

Allow normally forbidden unquoted attributes with `laxAttr`:

Expand All@@ -51,7 +51,13 @@ Allow normally forbidden "rootless" documents with `emptyDoc`:
parseXML('', { emptyDoc: true });
```

As well as a parse method, the package exports Node classes: Node, Element, Document, TextNode, and CDataNode. They are pretty much what you expect but with an incomplete or altered set of DOM API functions.
Validate `xmlns` declarations and element/attribute prefixes against them with `ns`. With this flag, an unknown prefix or a re-bound prefix throws a `NamespaceError`:

```js
parseXML('<root xmlns:x="urn:example"><x:node /></root>', { ns: true });
```

As well as a parse method, the package exports the node classes (`Node`, `Element`, `Document`, `DocumentFragment`, `TextNode`, `CDataNode`), serialization helpers (`prettyPrint`, `simplePrint`, `escapeXML`), the `isElement` type guard, and an error hierarchy rooted at `XMLError` (`ParserError`, `NamespaceError`, `HierarchyError`, `NotFoundError`). The node classes are pretty much what you'd expect but with an "incomplete" or altered set of DOM API functions.

[See the API documentation.](API.md)

Expand All@@ -68,20 +74,24 @@ As well as a parse method, the package exports Node classes: Node, Element, Docu
* `.nodeType`
A node type number (e.g. `Element.ELEMENT_NODE === 1`)

* `.ns`
A namespace identifier (`foo` in the case of `<foo:bar>`)
* `.prefix`
The namespace prefix of the element, or `null` if it has none (`foo` in the case of `<foo:bar>`).

* `.localName`
The tag name with any namespace prefix stripped (`bar` in the case of `<foo:bar>`).

* `.fullName`
A full tag name with identifier (`foo:bar` in the case of `<foo:bar>`)
The full tag name including the prefix (`foo:bar` in the case of `<foo:bar>`).

**Attributes**

* `.getAttribute( attrName )`
* `.setAttribute( attrName, attrValue )`
* `.hasAttribute( attrName )`
* `.hasAttributes()`
* `.removeAttribute( attrName )`

Attributes are not stored as attribute nodes in a list, but rather they are a simple `{ name: value }` style object on the node.
* `.attributes`
A `NamedNodeMap` of `Attr` nodes — iterable, indexable by name (`el.attributes.foo` returns the `Attr`), and with DOM-style `getNamedItem` / `setNamedItem` / `removeNamedItem` methods.

**Tree & traversal**

Expand All@@ -105,6 +115,9 @@ Attributes are not stored as attribute nodes in a list, but rather they are a si
* `.getElementsByTagName( tagName )`
Lists all Elements in the target's subtree, traversal order, that have `.tagName` equal to the argument. Function is case-sensitive.

* `.querySelector( cssSelector )`
Returns the first Element in the target's subtree, traversal order, that matches the supplied CSS selector, or `null` if none match.

* `.querySelectorAll( cssSelector )`
Lists all Elements in the target's subtree, traversal order, that match the supplied CSS selector. Function should be case-sensitive but may be case insensitive for some.

Expand All@@ -123,19 +136,19 @@ It will parse to a document which has a `.root` node looking roughly like this:
{
nodeType: 1,
nodeName: 'TAG',
ns: 'x',
prefix: 'x',
localName: 'tag',
tagName: 'tag',
fullName: 'x:tag',
attr: { 'foo': 'bar' }
attributes: NamedNodeMap { /* Attr { name: 'foo', value: 'bar' } */ },
parentNode: null,
childNodes = [
childNodes: [
{
nodeName: '#text',
nodeType: 3,
value: 'Text content',
data: 'Text content',
parentNode: {...},
}
]
}
```

47 changes: 47 additions & 0 deletions lib/Attr.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
import { Node } from './Node.js';
import { ATTRIBUTE_NODE } from './constants.js';
import { splitTagName } from './splitTagName.ts';

/**
* A class describing an attribute.
*
* @augments Node
*/
export class Attr extends Node {
/** The namespace prefix of the element, or null' if no prefix is specified. */
prefix: string | null;
/** The name of the tag for the given element, excluding any namespace prefix. */
localName: string;

/** The node's name. */
name: string;
/** The node's data value. */
value: string;

/**
* Constructs a new Attr instance.
*
* @param name The name of the attribute.
* @param value The data of the attribute.
*/
constructor (name: string, value: any) {
super();

const [ prefix, localName ] = splitTagName(name);
this.prefix = prefix;
this.localName = localName;
this.name = localName;
this.nodeName = localName;

this.value = String(value);

this.nodeType = ATTRIBUTE_NODE;
}

/** The full name of the tag for the given element, including a namespace prefix. */
get fullName () {
return this.prefix
? this.prefix + ':' + this.localName
: this.localName;
}
}
4 changes: 4 additions & 0 deletions lib/CreateChildArgument.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import type { DocumentFragment } from './DocumentFragment.ts';
import type { Node } from './Node.ts';

export type CreateChildArgument = Node | DocumentFragment | string | boolean | number | null | undefined;
133 changes: 124 additions & 9 deletions lib/Document.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,10 +2,17 @@ import { JsonML, type JsonMLElement } from './JsonML.js';
import { Node } from './Node.js';
import { Element } from './Element.js';
import { appendChild } from './appendChild.js';
import { DOCUMENT_NODE } from './constants.js';
import { DOCUMENT_NODE, XML_DECLARATION } from './constants.js';
import { domQuery } from './domQuery/index.js';
import { findAll } from './findAll.js';
import { isElement } from './isElement.ts';
import { DocumentFragment } from './DocumentFragment.ts';
import { NSMap } from './NSMap.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';
import type { CreateChildArgument } from './CreateChildArgument.ts';
import type { XMLAttr } from './XMLAttr.ts';
import { HierarchyError, NamespaceError } from './errors.js';

/**
* This class describes an XML document.
Expand All@@ -14,6 +21,8 @@ import { isElement } from './isElement.ts';
*/
export class Document extends Node {
root: Element | null = null;
/** @ignore */
namespaces = new NSMap();

/**
* Constructs a new Document instance.
Expand All@@ -25,6 +34,31 @@ export class Document extends Node {
this.nodeType = DOCUMENT_NODE;
}

/**
* Attach a namespace to the document.
*
* @param namespaceURI The namespace URI to attach.
* @param [prefix] Prefix to use on elements belonging to the namespace.
*/
attachNS (namespaceURI: string, prefix = ''): (name: string, attr?: XMLAttr | null, ...children: (CreateChildArgument | CreateChildArgument[])[]) => Element {
this.namespaces.add(namespaceURI, prefix);
this._updateNS();

// Return a new create function bound to the namespace
return this.createElementNS.bind(this, namespaceURI);
}

/** @ignore */
private _updateNS () {
// ensure that namespaces exist on the root node
if (this.root) {
for (const [ namespaceURI, prefix ] of this.namespaces.list()) {
const key = 'xmlns' + (prefix ? ':' + prefix : '');
this.root.setAttribute(key, namespaceURI);
}
}
}

// overwrites super
get textContent () {
return this.root ? this.root.textContent : '';
Expand All@@ -37,6 +71,58 @@ export class Document extends Node {
return this.childNodes.filter(isElement);
}

/**
* Create a new element node.
*
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElement = (
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const element = new Element(qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Create a new element node associated with a given namespace.
*
* @param namespaceURI The namespaceURI to associate with the element.
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElementNS = (
namespaceURI: string,
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const ns = this.namespaces.get(namespaceURI);
if (ns == null) {
throw new NamespaceError('Unknown namespace ' + namespaceURI);
}
const element = new Element(ns ? ns + ':' + qualifiedName : qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Return all descendant elements that have the specified tag name.
*
Expand All@@ -45,7 +131,7 @@ export class Document extends Node {
*/
getElementsByTagName (tagName: string): Element[] {
if (!tagName) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return findAll(this, tagName, []);
}
Expand All@@ -58,7 +144,7 @@ export class Document extends Node {
*/
querySelector (selector: string): Element | null {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector)[0] || null;
}
Expand All@@ -71,18 +157,32 @@ export class Document extends Node {
*/
querySelectorAll (selector: string): Element[] {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector);
}

// overwrites super
appendChild (node: Element): Element {
if (this.root) {
throw new Error('A document may only have one child/root element.');
appendChild<T extends Node | DocumentFragment> (node: T): T {
if (this.root || (node instanceof DocumentFragment && node.childNodes.length > 1)) {
throw new HierarchyError('A document must have only one child element.');
}
let root: Element;
if (node instanceof DocumentFragment) {
if (!(node.childNodes[0] instanceof Element)) {
throw new HierarchyError('Document root node must be an Element');
}
root = node.childNodes[0];
}
else if (node instanceof Element) {
root = node;
}
appendChild(this, node);
this.root = node;
else {
throw new HierarchyError('Document root node must be an Element');
}
appendChild(this, root);
this.root = root;
this._updateNS();
return node;
}

Expand All@@ -94,4 +194,19 @@ export class Document extends Node {
toJS (): JsonMLElement | [] {
return this.root ? JsonML(this.root) : [];
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
if (!(this.root instanceof Element)) {
throw new HierarchyError('root element is missing');
}
return `${XML_DECLARATION}\n` + (
pretty ? prettyPrint(this.root) : simplePrint(this.root)
);
}
}
43 changes: 43 additions & 0 deletions lib/DocumentFragment.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
import { appendChild } from './appendChild.ts';
import { DOCUMENT_FRAGMENT_NODE } from './constants.ts';
import type { Node } from './Node.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';

/**
* A class describing a DocumentFragment.
*/
export class DocumentFragment {
/** The immediate children contained in the fragment. */
childNodes: Node[] = [];
/** A numerical node type identifier. */
nodeType: number = DOCUMENT_FRAGMENT_NODE;

/**
* Appends a child node into the document fragment.
*
* @param node The new child node
* @returns The same node that was passed in.
*/
appendChild<T extends Node | DocumentFragment> (node: T): T {
appendChild(this, node);
return node;
}

/** @ignore */
toString (): string {
return '#document-fragment';
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
return pretty
? prettyPrint(this)
: simplePrint(this);
}
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Open
2,324 changes: 2,063 additions & 261 deletions API.md

Large diffs are not rendered by default.

41 changes: 27 additions & 14 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ There are some limitations:

- Comments are discarded.
- Processing instructions are discarded.
- Namespace prefixes are preserved but otherwise ignored.
- Namespace prefixes are preserved on every node; full xmlns validation is opt-in via the `ns` parse option.
- Doctypes are discarded.
- Nested doctypes are not supported and will cause errors.
- Nodes have a limited interface.
Expand All@@ -34,10 +34,10 @@ $ npm i @borgar/simple-xml
import { parseXML } from '@borgar/simple-xml';

const dom = parseXML('<root><node>Lorem ipsum</node></root>');
console.log(dom.getElementsByTagName('node').textContent);
console.log(dom.getElementsByTagName('node')[0].textContent);
```

The parse function accepts two arguments, the first is an XML source. The second is an options object. There are two options:
The parse function accepts two arguments, the first is an XML source. The second is an options object. There are three options:

Allow normally forbidden unquoted attributes with `laxAttr`:

Expand All@@ -51,7 +51,13 @@ Allow normally forbidden "rootless" documents with `emptyDoc`:
parseXML('', { emptyDoc: true });
```

As well as a parse method, the package exports Node classes: Node, Element, Document, TextNode, and CDataNode. They are pretty much what you expect but with an incomplete or altered set of DOM API functions.
Validate `xmlns` declarations and element/attribute prefixes against them with `ns`. With this flag, an unknown prefix or a re-bound prefix throws a `NamespaceError`:

```js
parseXML('<root xmlns:x="urn:example"><x:node /></root>', { ns: true });
```

As well as a parse method, the package exports the node classes (`Node`, `Element`, `Document`, `DocumentFragment`, `TextNode`, `CDataNode`), serialization helpers (`prettyPrint`, `simplePrint`, `escapeXML`), the `isElement` type guard, and an error hierarchy rooted at `XMLError` (`ParserError`, `NamespaceError`, `HierarchyError`, `NotFoundError`). The node classes are pretty much what you'd expect but with an "incomplete" or altered set of DOM API functions.

[See the API documentation.](API.md)

Expand All@@ -68,20 +74,24 @@ As well as a parse method, the package exports Node classes: Node, Element, Docu
* `.nodeType`
A node type number (e.g. `Element.ELEMENT_NODE === 1`)

* `.ns`
A namespace identifier (`foo` in the case of `<foo:bar>`)
* `.prefix`
The namespace prefix of the element, or `null` if it has none (`foo` in the case of `<foo:bar>`).

* `.localName`
The tag name with any namespace prefix stripped (`bar` in the case of `<foo:bar>`).

* `.fullName`
A full tag name with identifier (`foo:bar` in the case of `<foo:bar>`)
The full tag name including the prefix (`foo:bar` in the case of `<foo:bar>`).

**Attributes**

* `.getAttribute( attrName )`
* `.setAttribute( attrName, attrValue )`
* `.hasAttribute( attrName )`
* `.hasAttributes()`
* `.removeAttribute( attrName )`

Attributes are not stored as attribute nodes in a list, but rather they are a simple `{ name: value }` style object on the node.
* `.attributes`
A `NamedNodeMap` of `Attr` nodes — iterable, indexable by name (`el.attributes.foo` returns the `Attr`), and with DOM-style `getNamedItem` / `setNamedItem` / `removeNamedItem` methods.

**Tree & traversal**

Expand All@@ -105,6 +115,9 @@ Attributes are not stored as attribute nodes in a list, but rather they are a si
* `.getElementsByTagName( tagName )`
Lists all Elements in the target's subtree, traversal order, that have `.tagName` equal to the argument. Function is case-sensitive.

* `.querySelector( cssSelector )`
Returns the first Element in the target's subtree, traversal order, that matches the supplied CSS selector, or `null` if none match.

* `.querySelectorAll( cssSelector )`
Lists all Elements in the target's subtree, traversal order, that match the supplied CSS selector. Function should be case-sensitive but may be case insensitive for some.

Expand All@@ -123,19 +136,19 @@ It will parse to a document which has a `.root` node looking roughly like this:
{
nodeType: 1,
nodeName: 'TAG',
ns: 'x',
prefix: 'x',
localName: 'tag',
tagName: 'tag',
fullName: 'x:tag',
attr: { 'foo': 'bar' }
attributes: NamedNodeMap { /* Attr { name: 'foo', value: 'bar' } */ },
parentNode: null,
childNodes = [
childNodes: [
{
nodeName: '#text',
nodeType: 3,
value: 'Text content',
data: 'Text content',
parentNode: {...},
}
]
}
```

47 changes: 47 additions & 0 deletions lib/Attr.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
import { Node } from './Node.js';
import { ATTRIBUTE_NODE } from './constants.js';
import { splitTagName } from './splitTagName.ts';

/**
* A class describing an attribute.
*
* @augments Node
*/
export class Attr extends Node {
/** The namespace prefix of the element, or null' if no prefix is specified. */
prefix: string | null;
/** The name of the tag for the given element, excluding any namespace prefix. */
localName: string;

/** The node's name. */
name: string;
/** The node's data value. */
value: string;

/**
* Constructs a new Attr instance.
*
* @param name The name of the attribute.
* @param value The data of the attribute.
*/
constructor (name: string, value: any) {
super();

const [ prefix, localName ] = splitTagName(name);
this.prefix = prefix;
this.localName = localName;
this.name = localName;
this.nodeName = localName;

this.value = String(value);

this.nodeType = ATTRIBUTE_NODE;
}

/** The full name of the tag for the given element, including a namespace prefix. */
get fullName () {
return this.prefix
? this.prefix + ':' + this.localName
: this.localName;
}
}
4 changes: 4 additions & 0 deletions lib/CreateChildArgument.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import type { DocumentFragment } from './DocumentFragment.ts';
import type { Node } from './Node.ts';

export type CreateChildArgument = Node | DocumentFragment | string | boolean | number | null | undefined;
133 changes: 124 additions & 9 deletions lib/Document.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,10 +2,17 @@ import { JsonML, type JsonMLElement } from './JsonML.js';
import { Node } from './Node.js';
import { Element } from './Element.js';
import { appendChild } from './appendChild.js';
import { DOCUMENT_NODE } from './constants.js';
import { DOCUMENT_NODE, XML_DECLARATION } from './constants.js';
import { domQuery } from './domQuery/index.js';
import { findAll } from './findAll.js';
import { isElement } from './isElement.ts';
import { DocumentFragment } from './DocumentFragment.ts';
import { NSMap } from './NSMap.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';
import type { CreateChildArgument } from './CreateChildArgument.ts';
import type { XMLAttr } from './XMLAttr.ts';
import { HierarchyError, NamespaceError } from './errors.js';

/**
* This class describes an XML document.
Expand All@@ -14,6 +21,8 @@ import { isElement } from './isElement.ts';
*/
export class Document extends Node {
root: Element | null = null;
/** @ignore */
namespaces = new NSMap();

/**
* Constructs a new Document instance.
Expand All@@ -25,6 +34,31 @@ export class Document extends Node {
this.nodeType = DOCUMENT_NODE;
}

/**
* Attach a namespace to the document.
*
* @param namespaceURI The namespace URI to attach.
* @param [prefix] Prefix to use on elements belonging to the namespace.
*/
attachNS (namespaceURI: string, prefix = ''): (name: string, attr?: XMLAttr | null, ...children: (CreateChildArgument | CreateChildArgument[])[]) => Element {
this.namespaces.add(namespaceURI, prefix);
this._updateNS();

// Return a new create function bound to the namespace
return this.createElementNS.bind(this, namespaceURI);
}

/** @ignore */
private _updateNS () {
// ensure that namespaces exist on the root node
if (this.root) {
for (const [ namespaceURI, prefix ] of this.namespaces.list()) {
const key = 'xmlns' + (prefix ? ':' + prefix : '');
this.root.setAttribute(key, namespaceURI);
}
}
}

// overwrites super
get textContent () {
return this.root ? this.root.textContent : '';
Expand All@@ -37,6 +71,58 @@ export class Document extends Node {
return this.childNodes.filter(isElement);
}

/**
* Create a new element node.
*
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElement = (
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const element = new Element(qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Create a new element node associated with a given namespace.
*
* @param namespaceURI The namespaceURI to associate with the element.
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElementNS = (
namespaceURI: string,
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const ns = this.namespaces.get(namespaceURI);
if (ns == null) {
throw new NamespaceError('Unknown namespace ' + namespaceURI);
}
const element = new Element(ns ? ns + ':' + qualifiedName : qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Return all descendant elements that have the specified tag name.
*
Expand All@@ -45,7 +131,7 @@ export class Document extends Node {
*/
getElementsByTagName (tagName: string): Element[] {
if (!tagName) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return findAll(this, tagName, []);
}
Expand All@@ -58,7 +144,7 @@ export class Document extends Node {
*/
querySelector (selector: string): Element | null {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector)[0] || null;
}
Expand All@@ -71,18 +157,32 @@ export class Document extends Node {
*/
querySelectorAll (selector: string): Element[] {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector);
}

// overwrites super
appendChild (node: Element): Element {
if (this.root) {
throw new Error('A document may only have one child/root element.');
appendChild<T extends Node | DocumentFragment> (node: T): T {
if (this.root || (node instanceof DocumentFragment && node.childNodes.length > 1)) {
throw new HierarchyError('A document must have only one child element.');
}
let root: Element;
if (node instanceof DocumentFragment) {
if (!(node.childNodes[0] instanceof Element)) {
throw new HierarchyError('Document root node must be an Element');
}
root = node.childNodes[0];
}
else if (node instanceof Element) {
root = node;
}
appendChild(this, node);
this.root = node;
else {
throw new HierarchyError('Document root node must be an Element');
}
appendChild(this, root);
this.root = root;
this._updateNS();
return node;
}

Expand All@@ -94,4 +194,19 @@ export class Document extends Node {
toJS (): JsonMLElement | [] {
return this.root ? JsonML(this.root) : [];
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
if (!(this.root instanceof Element)) {
throw new HierarchyError('root element is missing');
}
return `${XML_DECLARATION}\n` + (
pretty ? prettyPrint(this.root) : simplePrint(this.root)
);
}
}
43 changes: 43 additions & 0 deletions lib/DocumentFragment.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
import { appendChild } from './appendChild.ts';
import { DOCUMENT_FRAGMENT_NODE } from './constants.ts';
import type { Node } from './Node.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';

/**
* A class describing a DocumentFragment.
*/
export class DocumentFragment {
/** The immediate children contained in the fragment. */
childNodes: Node[] = [];
/** A numerical node type identifier. */
nodeType: number = DOCUMENT_FRAGMENT_NODE;

/**
* Appends a child node into the document fragment.
*
* @param node The new child node
* @returns The same node that was passed in.
*/
appendChild<T extends Node | DocumentFragment> (node: T): T {
appendChild(this, node);
return node;
}

/** @ignore */
toString (): string {
return '#document-fragment';
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
return pretty
? prettyPrint(this)
: simplePrint(this);
}
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Open
2,324 changes: 2,063 additions & 261 deletions API.md

Large diffs are not rendered by default.

41 changes: 27 additions & 14 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ There are some limitations:

- Comments are discarded.
- Processing instructions are discarded.
- Namespace prefixes are preserved but otherwise ignored.
- Namespace prefixes are preserved on every node; full xmlns validation is opt-in via the `ns` parse option.
- Doctypes are discarded.
- Nested doctypes are not supported and will cause errors.
- Nodes have a limited interface.
Expand All@@ -34,10 +34,10 @@ $ npm i @borgar/simple-xml
import { parseXML } from '@borgar/simple-xml';

const dom = parseXML('<root><node>Lorem ipsum</node></root>');
console.log(dom.getElementsByTagName('node').textContent);
console.log(dom.getElementsByTagName('node')[0].textContent);
```

The parse function accepts two arguments, the first is an XML source. The second is an options object. There are two options:
The parse function accepts two arguments, the first is an XML source. The second is an options object. There are three options:

Allow normally forbidden unquoted attributes with `laxAttr`:

Expand All@@ -51,7 +51,13 @@ Allow normally forbidden "rootless" documents with `emptyDoc`:
parseXML('', { emptyDoc: true });
```

As well as a parse method, the package exports Node classes: Node, Element, Document, TextNode, and CDataNode. They are pretty much what you expect but with an incomplete or altered set of DOM API functions.
Validate `xmlns` declarations and element/attribute prefixes against them with `ns`. With this flag, an unknown prefix or a re-bound prefix throws a `NamespaceError`:

```js
parseXML('<root xmlns:x="urn:example"><x:node /></root>', { ns: true });
```

As well as a parse method, the package exports the node classes (`Node`, `Element`, `Document`, `DocumentFragment`, `TextNode`, `CDataNode`), serialization helpers (`prettyPrint`, `simplePrint`, `escapeXML`), the `isElement` type guard, and an error hierarchy rooted at `XMLError` (`ParserError`, `NamespaceError`, `HierarchyError`, `NotFoundError`). The node classes are pretty much what you'd expect but with an "incomplete" or altered set of DOM API functions.

[See the API documentation.](API.md)

Expand All@@ -68,20 +74,24 @@ As well as a parse method, the package exports Node classes: Node, Element, Docu
* `.nodeType`
A node type number (e.g. `Element.ELEMENT_NODE === 1`)

* `.ns`
A namespace identifier (`foo` in the case of `<foo:bar>`)
* `.prefix`
The namespace prefix of the element, or `null` if it has none (`foo` in the case of `<foo:bar>`).

* `.localName`
The tag name with any namespace prefix stripped (`bar` in the case of `<foo:bar>`).

* `.fullName`
A full tag name with identifier (`foo:bar` in the case of `<foo:bar>`)
The full tag name including the prefix (`foo:bar` in the case of `<foo:bar>`).

**Attributes**

* `.getAttribute( attrName )`
* `.setAttribute( attrName, attrValue )`
* `.hasAttribute( attrName )`
* `.hasAttributes()`
* `.removeAttribute( attrName )`

Attributes are not stored as attribute nodes in a list, but rather they are a simple `{ name: value }` style object on the node.
* `.attributes`
A `NamedNodeMap` of `Attr` nodes — iterable, indexable by name (`el.attributes.foo` returns the `Attr`), and with DOM-style `getNamedItem` / `setNamedItem` / `removeNamedItem` methods.

**Tree & traversal**

Expand All@@ -105,6 +115,9 @@ Attributes are not stored as attribute nodes in a list, but rather they are a si
* `.getElementsByTagName( tagName )`
Lists all Elements in the target's subtree, traversal order, that have `.tagName` equal to the argument. Function is case-sensitive.

* `.querySelector( cssSelector )`
Returns the first Element in the target's subtree, traversal order, that matches the supplied CSS selector, or `null` if none match.

* `.querySelectorAll( cssSelector )`
Lists all Elements in the target's subtree, traversal order, that match the supplied CSS selector. Function should be case-sensitive but may be case insensitive for some.

Expand All@@ -123,19 +136,19 @@ It will parse to a document which has a `.root` node looking roughly like this:
{
nodeType: 1,
nodeName: 'TAG',
ns: 'x',
prefix: 'x',
localName: 'tag',
tagName: 'tag',
fullName: 'x:tag',
attr: { 'foo': 'bar' }
attributes: NamedNodeMap { /* Attr { name: 'foo', value: 'bar' } */ },
parentNode: null,
childNodes = [
childNodes: [
{
nodeName: '#text',
nodeType: 3,
value: 'Text content',
data: 'Text content',
parentNode: {...},
}
]
}
```

47 changes: 47 additions & 0 deletions lib/Attr.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
import { Node } from './Node.js';
import { ATTRIBUTE_NODE } from './constants.js';
import { splitTagName } from './splitTagName.ts';

/**
* A class describing an attribute.
*
* @augments Node
*/
export class Attr extends Node {
/** The namespace prefix of the element, or null' if no prefix is specified. */
prefix: string | null;
/** The name of the tag for the given element, excluding any namespace prefix. */
localName: string;

/** The node's name. */
name: string;
/** The node's data value. */
value: string;

/**
* Constructs a new Attr instance.
*
* @param name The name of the attribute.
* @param value The data of the attribute.
*/
constructor (name: string, value: any) {
super();

const [ prefix, localName ] = splitTagName(name);
this.prefix = prefix;
this.localName = localName;
this.name = localName;
this.nodeName = localName;

this.value = String(value);

this.nodeType = ATTRIBUTE_NODE;
}

/** The full name of the tag for the given element, including a namespace prefix. */
get fullName () {
return this.prefix
? this.prefix + ':' + this.localName
: this.localName;
}
}
4 changes: 4 additions & 0 deletions lib/CreateChildArgument.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import type { DocumentFragment } from './DocumentFragment.ts';
import type { Node } from './Node.ts';

export type CreateChildArgument = Node | DocumentFragment | string | boolean | number | null | undefined;
133 changes: 124 additions & 9 deletions lib/Document.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,10 +2,17 @@ import { JsonML, type JsonMLElement } from './JsonML.js';
import { Node } from './Node.js';
import { Element } from './Element.js';
import { appendChild } from './appendChild.js';
import { DOCUMENT_NODE } from './constants.js';
import { DOCUMENT_NODE, XML_DECLARATION } from './constants.js';
import { domQuery } from './domQuery/index.js';
import { findAll } from './findAll.js';
import { isElement } from './isElement.ts';
import { DocumentFragment } from './DocumentFragment.ts';
import { NSMap } from './NSMap.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';
import type { CreateChildArgument } from './CreateChildArgument.ts';
import type { XMLAttr } from './XMLAttr.ts';
import { HierarchyError, NamespaceError } from './errors.js';

/**
* This class describes an XML document.
Expand All@@ -14,6 +21,8 @@ import { isElement } from './isElement.ts';
*/
export class Document extends Node {
root: Element | null = null;
/** @ignore */
namespaces = new NSMap();

/**
* Constructs a new Document instance.
Expand All@@ -25,6 +34,31 @@ export class Document extends Node {
this.nodeType = DOCUMENT_NODE;
}

/**
* Attach a namespace to the document.
*
* @param namespaceURI The namespace URI to attach.
* @param [prefix] Prefix to use on elements belonging to the namespace.
*/
attachNS (namespaceURI: string, prefix = ''): (name: string, attr?: XMLAttr | null, ...children: (CreateChildArgument | CreateChildArgument[])[]) => Element {
this.namespaces.add(namespaceURI, prefix);
this._updateNS();

// Return a new create function bound to the namespace
return this.createElementNS.bind(this, namespaceURI);
}

/** @ignore */
private _updateNS () {
// ensure that namespaces exist on the root node
if (this.root) {
for (const [ namespaceURI, prefix ] of this.namespaces.list()) {
const key = 'xmlns' + (prefix ? ':' + prefix : '');
this.root.setAttribute(key, namespaceURI);
}
}
}

// overwrites super
get textContent () {
return this.root ? this.root.textContent : '';
Expand All@@ -37,6 +71,58 @@ export class Document extends Node {
return this.childNodes.filter(isElement);
}

/**
* Create a new element node.
*
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElement = (
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const element = new Element(qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Create a new element node associated with a given namespace.
*
* @param namespaceURI The namespaceURI to associate with the element.
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElementNS = (
namespaceURI: string,
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const ns = this.namespaces.get(namespaceURI);
if (ns == null) {
throw new NamespaceError('Unknown namespace ' + namespaceURI);
}
const element = new Element(ns ? ns + ':' + qualifiedName : qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Return all descendant elements that have the specified tag name.
*
Expand All@@ -45,7 +131,7 @@ export class Document extends Node {
*/
getElementsByTagName (tagName: string): Element[] {
if (!tagName) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return findAll(this, tagName, []);
}
Expand All@@ -58,7 +144,7 @@ export class Document extends Node {
*/
querySelector (selector: string): Element | null {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector)[0] || null;
}
Expand All@@ -71,18 +157,32 @@ export class Document extends Node {
*/
querySelectorAll (selector: string): Element[] {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector);
}

// overwrites super
appendChild (node: Element): Element {
if (this.root) {
throw new Error('A document may only have one child/root element.');
appendChild<T extends Node | DocumentFragment> (node: T): T {
if (this.root || (node instanceof DocumentFragment && node.childNodes.length > 1)) {
throw new HierarchyError('A document must have only one child element.');
}
let root: Element;
if (node instanceof DocumentFragment) {
if (!(node.childNodes[0] instanceof Element)) {
throw new HierarchyError('Document root node must be an Element');
}
root = node.childNodes[0];
}
else if (node instanceof Element) {
root = node;
}
appendChild(this, node);
this.root = node;
else {
throw new HierarchyError('Document root node must be an Element');
}
appendChild(this, root);
this.root = root;
this._updateNS();
return node;
}

Expand All@@ -94,4 +194,19 @@ export class Document extends Node {
toJS (): JsonMLElement | [] {
return this.root ? JsonML(this.root) : [];
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
if (!(this.root instanceof Element)) {
throw new HierarchyError('root element is missing');
}
return `${XML_DECLARATION}\n` + (
pretty ? prettyPrint(this.root) : simplePrint(this.root)
);
}
}
43 changes: 43 additions & 0 deletions lib/DocumentFragment.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
import { appendChild } from './appendChild.ts';
import { DOCUMENT_FRAGMENT_NODE } from './constants.ts';
import type { Node } from './Node.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';

/**
* A class describing a DocumentFragment.
*/
export class DocumentFragment {
/** The immediate children contained in the fragment. */
childNodes: Node[] = [];
/** A numerical node type identifier. */
nodeType: number = DOCUMENT_FRAGMENT_NODE;

/**
* Appends a child node into the document fragment.
*
* @param node The new child node
* @returns The same node that was passed in.
*/
appendChild<T extends Node | DocumentFragment> (node: T): T {
appendChild(this, node);
return node;
}

/** @ignore */
toString (): string {
return '#document-fragment';
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
return pretty
? prettyPrint(this)
: simplePrint(this);
}
}
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Open
2,324 changes: 2,063 additions & 261 deletions API.md

Large diffs are not rendered by default.

41 changes: 27 additions & 14 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,7 @@ There are some limitations:

- Comments are discarded.
- Processing instructions are discarded.
- Namespace prefixes are preserved but otherwise ignored.
- Namespace prefixes are preserved on every node; full xmlns validation is opt-in via the `ns` parse option.
- Doctypes are discarded.
- Nested doctypes are not supported and will cause errors.
- Nodes have a limited interface.
Expand All@@ -34,10 +34,10 @@ $ npm i @borgar/simple-xml
import { parseXML } from '@borgar/simple-xml';

const dom = parseXML('<root><node>Lorem ipsum</node></root>');
console.log(dom.getElementsByTagName('node').textContent);
console.log(dom.getElementsByTagName('node')[0].textContent);
```

The parse function accepts two arguments, the first is an XML source. The second is an options object. There are two options:
The parse function accepts two arguments, the first is an XML source. The second is an options object. There are three options:

Allow normally forbidden unquoted attributes with `laxAttr`:

Expand All@@ -51,7 +51,13 @@ Allow normally forbidden "rootless" documents with `emptyDoc`:
parseXML('', { emptyDoc: true });
```

As well as a parse method, the package exports Node classes: Node, Element, Document, TextNode, and CDataNode. They are pretty much what you expect but with an incomplete or altered set of DOM API functions.
Validate `xmlns` declarations and element/attribute prefixes against them with `ns`. With this flag, an unknown prefix or a re-bound prefix throws a `NamespaceError`:

```js
parseXML('<root xmlns:x="urn:example"><x:node /></root>', { ns: true });
```

As well as a parse method, the package exports the node classes (`Node`, `Element`, `Document`, `DocumentFragment`, `TextNode`, `CDataNode`), serialization helpers (`prettyPrint`, `simplePrint`, `escapeXML`), the `isElement` type guard, and an error hierarchy rooted at `XMLError` (`ParserError`, `NamespaceError`, `HierarchyError`, `NotFoundError`). The node classes are pretty much what you'd expect but with an "incomplete" or altered set of DOM API functions.

[See the API documentation.](API.md)

Expand All@@ -68,20 +74,24 @@ As well as a parse method, the package exports Node classes: Node, Element, Docu
* `.nodeType`
A node type number (e.g. `Element.ELEMENT_NODE === 1`)

* `.ns`
A namespace identifier (`foo` in the case of `<foo:bar>`)
* `.prefix`
The namespace prefix of the element, or `null` if it has none (`foo` in the case of `<foo:bar>`).

* `.localName`
The tag name with any namespace prefix stripped (`bar` in the case of `<foo:bar>`).

* `.fullName`
A full tag name with identifier (`foo:bar` in the case of `<foo:bar>`)
The full tag name including the prefix (`foo:bar` in the case of `<foo:bar>`).

**Attributes**

* `.getAttribute( attrName )`
* `.setAttribute( attrName, attrValue )`
* `.hasAttribute( attrName )`
* `.hasAttributes()`
* `.removeAttribute( attrName )`

Attributes are not stored as attribute nodes in a list, but rather they are a simple `{ name: value }` style object on the node.
* `.attributes`
A `NamedNodeMap` of `Attr` nodes — iterable, indexable by name (`el.attributes.foo` returns the `Attr`), and with DOM-style `getNamedItem` / `setNamedItem` / `removeNamedItem` methods.

**Tree & traversal**

Expand All@@ -105,6 +115,9 @@ Attributes are not stored as attribute nodes in a list, but rather they are a si
* `.getElementsByTagName( tagName )`
Lists all Elements in the target's subtree, traversal order, that have `.tagName` equal to the argument. Function is case-sensitive.

* `.querySelector( cssSelector )`
Returns the first Element in the target's subtree, traversal order, that matches the supplied CSS selector, or `null` if none match.

* `.querySelectorAll( cssSelector )`
Lists all Elements in the target's subtree, traversal order, that match the supplied CSS selector. Function should be case-sensitive but may be case insensitive for some.

Expand All@@ -123,19 +136,19 @@ It will parse to a document which has a `.root` node looking roughly like this:
{
nodeType: 1,
nodeName: 'TAG',
ns: 'x',
prefix: 'x',
localName: 'tag',
tagName: 'tag',
fullName: 'x:tag',
attr: { 'foo': 'bar' }
attributes: NamedNodeMap { /* Attr { name: 'foo', value: 'bar' } */ },
parentNode: null,
childNodes = [
childNodes: [
{
nodeName: '#text',
nodeType: 3,
value: 'Text content',
data: 'Text content',
parentNode: {...},
}
]
}
```

47 changes: 47 additions & 0 deletions lib/Attr.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
import { Node } from './Node.js';
import { ATTRIBUTE_NODE } from './constants.js';
import { splitTagName } from './splitTagName.ts';

/**
* A class describing an attribute.
*
* @augments Node
*/
export class Attr extends Node {
/** The namespace prefix of the element, or null' if no prefix is specified. */
prefix: string | null;
/** The name of the tag for the given element, excluding any namespace prefix. */
localName: string;

/** The node's name. */
name: string;
/** The node's data value. */
value: string;

/**
* Constructs a new Attr instance.
*
* @param name The name of the attribute.
* @param value The data of the attribute.
*/
constructor (name: string, value: any) {
super();

const [ prefix, localName ] = splitTagName(name);
this.prefix = prefix;
this.localName = localName;
this.name = localName;
this.nodeName = localName;

this.value = String(value);

this.nodeType = ATTRIBUTE_NODE;
}

/** The full name of the tag for the given element, including a namespace prefix. */
get fullName () {
return this.prefix
? this.prefix + ':' + this.localName
: this.localName;
}
}
4 changes: 4 additions & 0 deletions lib/CreateChildArgument.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
import type { DocumentFragment } from './DocumentFragment.ts';
import type { Node } from './Node.ts';

export type CreateChildArgument = Node | DocumentFragment | string | boolean | number | null | undefined;
133 changes: 124 additions & 9 deletions lib/Document.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,10 +2,17 @@ import { JsonML, type JsonMLElement } from './JsonML.js';
import { Node } from './Node.js';
import { Element } from './Element.js';
import { appendChild } from './appendChild.js';
import { DOCUMENT_NODE } from './constants.js';
import { DOCUMENT_NODE, XML_DECLARATION } from './constants.js';
import { domQuery } from './domQuery/index.js';
import { findAll } from './findAll.js';
import { isElement } from './isElement.ts';
import { DocumentFragment } from './DocumentFragment.ts';
import { NSMap } from './NSMap.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';
import type { CreateChildArgument } from './CreateChildArgument.ts';
import type { XMLAttr } from './XMLAttr.ts';
import { HierarchyError, NamespaceError } from './errors.js';

/**
* This class describes an XML document.
Expand All@@ -14,6 +21,8 @@ import { isElement } from './isElement.ts';
*/
export class Document extends Node {
root: Element | null = null;
/** @ignore */
namespaces = new NSMap();

/**
* Constructs a new Document instance.
Expand All@@ -25,6 +34,31 @@ export class Document extends Node {
this.nodeType = DOCUMENT_NODE;
}

/**
* Attach a namespace to the document.
*
* @param namespaceURI The namespace URI to attach.
* @param [prefix] Prefix to use on elements belonging to the namespace.
*/
attachNS (namespaceURI: string, prefix = ''): (name: string, attr?: XMLAttr | null, ...children: (CreateChildArgument | CreateChildArgument[])[]) => Element {
this.namespaces.add(namespaceURI, prefix);
this._updateNS();

// Return a new create function bound to the namespace
return this.createElementNS.bind(this, namespaceURI);
}

/** @ignore */
private _updateNS () {
// ensure that namespaces exist on the root node
if (this.root) {
for (const [ namespaceURI, prefix ] of this.namespaces.list()) {
const key = 'xmlns' + (prefix ? ':' + prefix : '');
this.root.setAttribute(key, namespaceURI);
}
}
}

// overwrites super
get textContent () {
return this.root ? this.root.textContent : '';
Expand All@@ -37,6 +71,58 @@ export class Document extends Node {
return this.childNodes.filter(isElement);
}

/**
* Create a new element node.
*
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElement = (
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const element = new Element(qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Create a new element node associated with a given namespace.
*
* @param namespaceURI The namespaceURI to associate with the element.
* @param qualifiedName The local tagName of the element.
* @param attr A record of attributes to assign to the new element.
* If the value is null or undefined, the attribute will be omitted.
* @param children Nodes to insert as children.
* Strings will be converted to TextNodes and arrays will be flattened.
* @returns A new Element instance.
*/
createElementNS = (
namespaceURI: string,
qualifiedName: string,
attr: XMLAttr | null | undefined,
...children: (CreateChildArgument | CreateChildArgument[])[]
): Element => {
const ns = this.namespaces.get(namespaceURI);
if (ns == null) {
throw new NamespaceError('Unknown namespace ' + namespaceURI);
}
const element = new Element(ns ? ns + ':' + qualifiedName : qualifiedName);
element.setAttrValues(attr ?? null);
for (const child of children) {
element.append(child);
}
return element;
};

/**
* Return all descendant elements that have the specified tag name.
*
Expand All@@ -45,7 +131,7 @@ export class Document extends Node {
*/
getElementsByTagName (tagName: string): Element[] {
if (!tagName) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return findAll(this, tagName, []);
}
Expand All@@ -58,7 +144,7 @@ export class Document extends Node {
*/
querySelector (selector: string): Element | null {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector)[0] || null;
}
Expand All@@ -71,18 +157,32 @@ export class Document extends Node {
*/
querySelectorAll (selector: string): Element[] {
if (!selector) {
throw new Error('1 argument required, but 0 present.');
throw new TypeError('1 argument required, but 0 present.');
}
return domQuery(this, selector);
}

// overwrites super
appendChild (node: Element): Element {
if (this.root) {
throw new Error('A document may only have one child/root element.');
appendChild<T extends Node | DocumentFragment> (node: T): T {
if (this.root || (node instanceof DocumentFragment && node.childNodes.length > 1)) {
throw new HierarchyError('A document must have only one child element.');
}
let root: Element;
if (node instanceof DocumentFragment) {
if (!(node.childNodes[0] instanceof Element)) {
throw new HierarchyError('Document root node must be an Element');
}
root = node.childNodes[0];
}
else if (node instanceof Element) {
root = node;
}
appendChild(this, node);
this.root = node;
else {
throw new HierarchyError('Document root node must be an Element');
}
appendChild(this, root);
this.root = root;
this._updateNS();
return node;
}

Expand All@@ -94,4 +194,19 @@ export class Document extends Node {
toJS (): JsonMLElement | [] {
return this.root ? JsonML(this.root) : [];
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
if (!(this.root instanceof Element)) {
throw new HierarchyError('root element is missing');
}
return `${XML_DECLARATION}\n` + (
pretty ? prettyPrint(this.root) : simplePrint(this.root)
);
}
}
43 changes: 43 additions & 0 deletions lib/DocumentFragment.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
import { appendChild } from './appendChild.ts';
import { DOCUMENT_FRAGMENT_NODE } from './constants.ts';
import type { Node } from './Node.ts';
import { prettyPrint } from './prettyPrint.ts';
import { simplePrint } from './simplePrint.ts';

/**
* A class describing a DocumentFragment.
*/
export class DocumentFragment {
/** The immediate children contained in the fragment. */
childNodes: Node[] = [];
/** A numerical node type identifier. */
nodeType: number = DOCUMENT_FRAGMENT_NODE;

/**
* Appends a child node into the document fragment.
*
* @param node The new child node
* @returns The same node that was passed in.
*/
appendChild<T extends Node | DocumentFragment> (node: T): T {
appendChild(this, node);
return node;
}

/** @ignore */
toString (): string {
return '#document-fragment';
}

/**
* Print the document as a string.
*
* @param pretty Apply automatic linebreaks and indentation to the output.
* @returns The document as an XML string.
*/
print (pretty = false): string {
return pretty
? prettyPrint(this)
: simplePrint(this);
}
}
Loading