Options
All
  • Public
  • Public/Protected
  • All
Menu

Class xjs_Document

Wrapper for a Document.

see

https://www.w3.org/TR/dom/#document

Hierarchy

Implements

Index

Constructors

constructor

Accessors

node

  • get node(): Document

Methods

append

  • append(...contents: Content[]): this

empty

  • empty(): this

exe

  • exe(executable: function): this
  • deprecated

    DEPRECATED: Use .run() instead. Execute a function acting on this node, and then return this node.

    Simplifies chaining when performing void tasks, especially tasks that have not been defined in this implementation. Note that this function is not asynchronous, and does not accept asynchronous arguments.

    Parameters

    • executable: function

      the function to call in the context of this

        • (this: this): void
        • Parameters

          • this: this

          Returns void

    Returns this

    this

ifElse

  • ifElse(condition: any, onResolve: function, onReject?: function): this
  • deprecated

    DEPRECATED: Use .select() instead. Execute a function based on a condition.

    Given a condition, execute one function upon passing, or execute another upon failure. Note that this function is not asynchronous, and does not accept asynchronous arguments.

    Parameters

    • condition: any

      the condition to evaluate; will be converted to boolean

    • onResolve: function

      the executable to call (in the context of this) if the condition is truthy

        • (this: this): void
        • Parameters

          • this: this

          Returns void

    • Default value onReject: function = () => {}

      the executable to call (in the context of this) if the condition is falsy

        • (this: this): void
        • Parameters

          • this: this

          Returns void

    Returns this

    this

importLinks

  • importLinks(dirpath: string): this
  • Replace all link[rel~="import"][data-import] elements with contents from their documents.

    This method finds all link[rel~="import"][data-import]s in this document, and then replaces those links with a DocumentFragment holding some contents. These contents depend on the value set for data-import:

    • if [data-import="document"], then the replaced contents will be the contents of the link’s imported document itself
    • if [data-import="template"], then the replaced contents will be the contents of the first template descendant in the link’s imported document
    • if the [data-import] attribute value is neither "document" nor "template", or if it is absent, then the link element is completely ignored and left as-is

    Note: If HTMLLinkElement#import is supported (by the browser or jsdom), then when [data-import="document"] is set, the appended contents will instead be a Document object, as defined by HTML Imports, rather than a DocumentFragment object.

    Note: Document#querySelectorAll does not traverse inside <template> elements, so any <link> elements inside <template> elements will be left untouched. To modify those, you will need to call this method on that <template>’s contents (another DocumentFragment).

    In the example below, The link[rel="import"] in this document has [data-import="template"], and so is replaced with the contents of template#sect-template in x-linked-doc.tpl.html---namely, a DocumentFragment containing only the section element. However, if the link had had [data-import="document"], then the replaced content would consist of a DocumentFragment containing the entirety of x-linked-doc.tpl.html, including both the h1 along with the template#sect-template.

    // x-linked-doc.tpl.html:
    <h1>top-level hed</h1>
    <template id="sect-template">
      <section>
        <h2>section hed</h2>
        <p>a graf</p>
      </section>
    </template>
    
    // main.js:
    // assume `this` is an `xjs.Document` instance.
    this.innerHTML() === `
      <link rel="import" data-import="document" href="./x-linked-doc.tpl.html"/>
    `
    
    // This call will work as intended.
    this.importLinks(__dirname)

    Parameters

    • dirpath: string

      the absolute path to the directory of the template file containing the link element

    Returns this

    this

importLinksAsync

  • importLinksAsync(dirpath: string): Promise<this>

innerHTML

  • innerHTML(): string
  • Get the "innerHTML" of this document.

    This method should be used only if this document is an HTML Document, has doctype html, and whose root element is the <html> element (an instance of HTMLHtmlElement).

    You should only use this method if you do not have access to the original DOM object that this Document belongs to. Otherwise, you should use dom.serialize().

    throws

    {ReferenceError} if this document does not contain an <html> element

    Returns string

    a string serialization of this HTML Document

prepend

  • prepend(...contents: Content[]): this

querySelector

querySelectorAll

run

  • run(callback: function): this
  • Execute a callback acting on this node, and then return this node.

    Simplifies chaining when performing void tasks, especially tasks that have not been defined in this implementation. Note that this function is not asynchronous, and does not accept asynchronous arguments.

    Parameters

    • callback: function

      the function to call, taking this as its only argument, and returning void

        • (arg: this): void
        • Parameters

          • arg: this

          Returns void

    Returns this

    this

select

  • select(condition: unknown, onTrue: function, onFalse?: function): this
  • Execute a function based on a condition.

    Given a condition, execute one function upon passing, or execute another upon failure. Note that this function is not asynchronous, and does not accept asynchronous arguments.

    Parameters

    • condition: unknown

      the condition to evaluate; will be converted to boolean

    • onTrue: function

      the callback to call (with this as its parameter) if the condition is truthy

        • (arg: this): void
        • Parameters

          • arg: this

          Returns void

    • Default value onFalse: function = () => {}

      the callback to call (with this as its parameter) if the condition is falsy

        • (arg: this): void
        • Parameters

          • arg: this

          Returns void

    Returns this

    this

textContent

  • textContent(): string | null
  • textContent(text: string): this

trimInner

  • trimInner(): this
  • Remove all inner whitespace text nodes from this node, and return it.

    let div = document.createElement('div')
    div.innerHTML = `
      <h1>
        <em>Hello </em>
        <b>Worl d</b>
      </h1>
    `
    let trimmed_div = new xjs.Node(div).trimInner().node
    return div.innerHTML === `
      <h1>
        <em>Hello </em>
        <b>Worl d</b>
      </h1>
    `
      && trimmed_div.innerHTML = `<h1><em>Hello </em><b>Worl d</b></h1>`

    Returns this

    this

Static fromFile

  • Read an HTML file and return a document with its contents.

    The Document object will be wrapped in an xjs.Document object. To access the actual document, call {@link xjs_Document#node}.

    Parameters

    • filepath: string

      the path to the file

    Returns Promise<xjs_Document>

    the document, wrapped

Static fromFileSync

Static fromString

  • Read an HTML string and return a document with its contents.

    The Document object will be wrapped in an xjs.Document object. To access the actual document, call {@link xjs_Document#node}.

    Parameters

    • str: string

      a string of markup

    Returns xjs_Document

    the document, wrapped

Generated using TypeDoc