Skip to content

Commit

Permalink
Update to latest specs, add StaticRange and replaceChildren
Browse files Browse the repository at this point in the history
  • Loading branch information
bwrrp committed Feb 14, 2021
1 parent 2a44f34 commit 7566958
Show file tree
Hide file tree
Showing 35 changed files with 1,120 additions and 507 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Fast, tiny, standards-compliant XML DOM implementation for node and the browser.

This is a (partial) implementation of the [DOM living standard][domstandard], as last updated 1 March 2019, and the [DOM Parsing and Serialization W3C Editor's Draft][domparsing], as last updated 11 February 2019. See the 'Features and Limitations' section below for details on what's included and what's not.
This is a (partial) implementation of the [DOM living standard][domstandard], as last updated 27 January 2021, and the [DOM Parsing and Serialization W3C Editor's Draft][domparsing], as last updated 20 April 2020. See the 'Features and Limitations' section below for details on what's included and what's not.

[domstandard]: https://dom.spec.whatwg.org/
[domparsing]: https://w3c.github.io/DOM-Parsing/
Expand Down Expand Up @@ -68,7 +68,7 @@ If you need to parse HTML, see [this example][parse5-example] which shows how to

### CSS Selectors and XPath

This library does not implement CSS selectors, which means no `querySelector` / `querySelectorAll` on `ParentNode` and no `closest` / `matches` / `webkitMatchesSelector` on `Element`.
This library does not implement CSS selectors, which means no `querySelector` / `querySelectorAll` on `ParentNode` and no `closest` / `matches` / `webkitMatchesSelector` on `Element`. This library also does not implement XPath, which means no `XPathResult` / `XPathExpression` / `XPathEvaluator` interfaces and no `createExpression` / `createNSResolver` / `evaluate` on `Document`.

To query a slimdom document using XPath or XQuery, use [FontoXPath][fontoxpath].

Expand All @@ -82,11 +82,11 @@ This implementation offers no special treatment of HTML documents, which means t

This library also does not currently implement events, including the `Event` / `EventTarget` interfaces. It also currently does not contain an implementation of `AbortController` / `AbortSignal`. As these may have wider applications than browser-specific use cases, please file an issue if you have a use for these in your application and would like support for them to be added.

There is currently no support for shadow DOM, so no `Slotable` / `ShadowRoot` interfaces and no `slot` / `attachShadow` / `shadowRoot` on `Element`. Slimdom also does not support the APIs for custom elements using the `is` option on `createElement` / `createElementNS`.
There is currently no support for shadow DOM, so no `Slottable` / `ShadowRoot` interfaces and no `slot` / `attachShadow` / `shadowRoot` on `Element`. Slimdom also does not support the APIs for custom elements using the `is` option on `createElement` / `createElementNS`.

This library has no notion of URLs (`baseURI` on `Node`, and `URL` / `documentURI` / `origin` on `Document`), nor of encodings (`characterSet` / `charset` / `inputEncoding` on `Document`). This library only deals with JavaScript strings, not raw byte streams.
This library has no notion of URLs (`baseURI` on `Node`, and `URL` / `documentURI` on `Document`), nor of encodings (`characterSet` / `charset` / `inputEncoding` on `Document`). This library only deals with JavaScript strings, not raw byte streams.

This library omits properties and methods that exist mainly for web compatibility reasons (`insertAdjacentElement` / `insertAdjacentText` on `Element`, `hasFeature` on `DOMImplementation`, and `specified` on `Attr`).
This library omits properties and methods that exist mainly for web compatibility reasons (`insertAdjacentElement` / `insertAdjacentText` on `Element`, `hasFeature` on `DOMImplementation`, and `specified` on `Attr`). This also includes all interfaces and interface members listed as removed in the [DOM living standard][domstandard].

### Miscellaneous

Expand Down
404 changes: 382 additions & 22 deletions api/slimdom.api.json

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions api/slimdom.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ export class Document extends Node implements NonElementParentNode, ParentNode {
// (undocumented)
prepend(...nodes: (Node | string)[]): void;
// (undocumented)
replaceChildren(...nodes: (Node | string)[]): void;
// (undocumented)
get textContent(): string | null;
set textContent(_newValue: string | null);
}
Expand Down Expand Up @@ -181,6 +183,8 @@ export class DocumentFragment extends Node implements NonElementParentNode, Pare
// (undocumented)
prepend(...nodes: (Node | string)[]): void;
// (undocumented)
replaceChildren(...nodes: (Node | string)[]): void;
// (undocumented)
get textContent(): string | null;
set textContent(newValue: string | null);
}
Expand Down Expand Up @@ -281,6 +285,8 @@ export class Element extends Node implements ParentNode, NonDocumentTypeChildNod
removeAttributeNode(attr: Attr): Attr;
removeAttributeNS(namespace: string | null, localName: string): void;
// (undocumented)
replaceChildren(...nodes: (Node | string)[]): void;
// (undocumented)
replaceWith(...nodes: (Node | string)[]): void;
setAttribute(qualifiedName: string, value: string): void;
setAttributeNode(attr: Attr): Attr | null;
Expand Down Expand Up @@ -463,6 +469,22 @@ export class Range implements AbstractRange {
// @public
export function serializeToWellFormedString(root: Node): string;

// @public
export class StaticRange implements AbstractRange {
// Warning: (ae-forgotten-export) The symbol "StaticRangeInit" needs to be exported by the entry point index.d.ts
constructor(init: StaticRangeInit);
// (undocumented)
readonly collapsed: boolean;
// (undocumented)
readonly endContainer: Node;
// (undocumented)
readonly endOffset: number;
// (undocumented)
readonly startContainer: Node;
// (undocumented)
readonly startOffset: number;
}

// @public
export class Text extends CharacterData {
constructor(data?: string);
Expand Down
16 changes: 8 additions & 8 deletions src/Attr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class Attr extends Node {
public set nodeValue(newValue: string | null) {
newValue = treatNullAsEmptyString(newValue);

// Set an existing attribute value with context object and new value.
// Set an existing attribute value with this and new value.
setExistingAttributeValue(this, newValue);
}

Expand All @@ -42,7 +42,7 @@ export default class Attr extends Node {
public set textContent(newValue: string | null) {
newValue = treatNullAsEmptyString(newValue);

// Set an existing attribute value with context object and new value.
// Set an existing attribute value with this and new value.
setExistingAttributeValue(this, newValue);
}

Expand All @@ -52,7 +52,7 @@ export default class Attr extends Node {
// 1. If namespace is null or the empty string, then return null.
// (not necessary due to recursion)

// 2. Switch on the context object:
// 2. Switch on this:
// Attr - Return the result of locating a namespace prefix for its element, if its element
// is non-null, and null otherwise.
if (this.ownerElement !== null) {
Expand All @@ -68,7 +68,7 @@ export default class Attr extends Node {
// 1. If prefix is the empty string, then set it to null.
// (not necessary due to recursion)

// 2. Return the result of running locate a namespace for the context object using prefix.
// 2. Return the result of running locate a namespace for this using prefix.

// To locate a namespace for a node using prefix, switch on node: Attr
// 1. If its element is null, then return null.
Expand Down Expand Up @@ -128,11 +128,11 @@ export default class Attr extends Node {
}

/**
* (non-standard) Creates a copy of the context object, not including its children.
* (non-standard) Creates a copy of this, not including its children.
*
* @param document - The node document to associate with the copy
*
* @returns A shallow copy of the context object
* @returns A shallow copy of this
*/
public _copy(document: Document): Attr {
// Set copy’s namespace, namespace prefix, local name, and value, to those of node.
Expand Down Expand Up @@ -164,7 +164,7 @@ function setExistingAttributeValue(attribute: Attr, value: string) {
if (element === null) {
(attribute as any)._value = value;
} else {
// 2. Otherwise, change attribute from attribute’s element to value.
changeAttribute(attribute, element, value);
// 2. Otherwise, change attribute to value.
changeAttribute(attribute, value);
}
}
4 changes: 2 additions & 2 deletions src/CDATASection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ export default class CDATASection extends Text {
}

/**
* (non-standard) Creates a copy of the context object, not including its children.
* (non-standard) Creates a copy of this, not including its children.
*
* @param document - The node document to associate with the copy
*
* @returns A shallow copy of the context object
* @returns A shallow copy of this
*/
public _copy(document: Document): CDATASection {
// Set copy’s data, to that of node.
Expand Down
15 changes: 7 additions & 8 deletions src/CharacterData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default abstract class CharacterData
public set nodeValue(newValue: string | null) {
newValue = treatNullAsEmptyString(newValue);

// Set an existing attribute value with context object and new value.
// Set an existing attribute value with this and new value.
replaceData(this, 0, this.length, newValue);
}

Expand All @@ -45,7 +45,7 @@ export default abstract class CharacterData
public set textContent(newValue: string | null) {
newValue = treatNullAsEmptyString(newValue);

// Set an existing attribute value with context object and new value.
// Set an existing attribute value with this and new value.
replaceData(this, 0, this.length, newValue);
}

Expand All @@ -55,7 +55,7 @@ export default abstract class CharacterData
// 1. If namespace is null or the empty string, then return null.
// (not necessary due to recursion)

// 2. Switch on the context object:
// 2. Switch on this:
// Any other node - Return the result of locating a namespace prefix for its parent element,
// if its parent element is non-null, and null otherwise.
const parentElement = this.parentElement;
Expand All @@ -72,7 +72,7 @@ export default abstract class CharacterData
// 1. If prefix is the empty string, then set it to null.
// (not necessary due to recursion)

// 2. Return the result of running locate a namespace for the context object using prefix.
// 2. Return the result of running locate a namespace for this using prefix.

// To locate a namespace for a node using prefix, switch on node: Any other node
// 1. If its parent element is null, then return null.
Expand Down Expand Up @@ -129,7 +129,7 @@ export default abstract class CharacterData
// [TreatNullAs=EmptyString]
newValue = treatNullAsEmptyString(newValue);

// replace data with node context object, offset 0, count context object’s length, and data
// replace data with node this, offset 0, count this’s length, and data
// new value.
replaceData(this, 0, this.length, newValue);
}
Expand Down Expand Up @@ -285,9 +285,8 @@ export function replaceData(
}
});

// 12. If node is a Text node and its parent is not null, run the child text content change
// steps for node’s parent.
// (child text content change steps not implemented)
// 12. If node's parent is non-null, then run the children changed steps for node’s parent.
// (children changed steps not implemented)
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export default class Comment extends CharacterData {
}

/**
* (non-standard) Creates a copy of the context object, not including its children.
* (non-standard) Creates a copy of this, not including its children.
*
* @param document - The node document to associate with the copy
*
* @returns A shallow copy of the context object
* @returns A shallow copy of this
*/
public _copy(document: Document): Comment {
// Set copy’s data, to that of node.
Expand Down
6 changes: 3 additions & 3 deletions src/DOMImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class DOMImplementation {

// 2. Return a new doctype, with qualifiedName as its name, publicId as its public ID, and
// systemId as its system ID, and with its node document set to the associated document of
// the context object.
// this.
const context = getContext(this._document);
const doctype = new context.DocumentType(qualifiedName, publicId, systemId);
doctype.ownerDocument = this._document;
Expand Down Expand Up @@ -97,7 +97,7 @@ export default class DOMImplementation {
document.appendChild(element);
}

// 6. document’s origin is context object’s associated document’s origin.
// 6. document’s origin is this’s associated document’s origin.
// (origin not implemented)

// 7. document’s content type is determined by namespace:
Expand Down Expand Up @@ -159,7 +159,7 @@ export default class DOMImplementation {
// the html element created earlier.
htmlElement.appendChild(createElement(doc, 'body', HTML_NAMESPACE));

// 8. doc’s origin is context object’s associated document’s origin.
// 8. doc’s origin is this’s associated document’s origin.
// (origin not implemented)

// 9. Return doc.
Expand Down
Loading

0 comments on commit 7566958

Please sign in to comment.