npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

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

About

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

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

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

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

Open Software & Tools

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

© 2024 – Pkg Stats / Ryan Hefner

@mojule/vdom

v1.0.0

Published

A virtual dom built on @mojule/tree

Downloads

20

Readme

vdom

A virtual dom built on mojule tree and dom-plugins

Lets you use the mojule tree API, plugins etc. to work with HTML nodes on the server and in the browser

Important: this documentation describes a previous version and is out of date - additionally, we are currently using JSDOM in derived packages rather than VDOM for performance reasons - however the tests are up to date and show correct usage

Install

npm install @mojule/vdom

Examples

Server

const vdom = require( '@mojule/vdom' )

const {
  document, documentType, text, comment, documentFragment, element,
  html, head, body, meta, title, div, p, strong, input
} = vdom.h

const dom =
  document(
    documentType('html'),
    html(
      head(
        meta({charset:'utf-8'}),
        title('Hello World!')
      ),
      body(
        comment('Whose line is it anyway?'),
        div({id:'main'},
          p('The quick brown fox jumps over the ',strong('lazy dog')),
          input({type:'text',name:'firstName',placeholder:'Alex'})
        ),
        comment('Fragment not (usually) necessary but make sure it works'),
        documentFragment(
          comment('Text not necessary but etc.'),
          p(text('lol '),'wut')
        ),
        comment('But what if it is not in the spec?'),
        element('customtag',{class:'kk'},
          p('OK that works for me')
        )
      )
    )
  )

const lol = dom.querySelector( 'p:not( #main > p )' )

lol.append( text( ' ok' ) )

console.log( dom.stringify() )

Note, the following has been prettified, stringify does not output any whitespace:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Hello World!</title>
  </head>
  <body>
    <!--Whose line is it anyway?-->
    <div id="main">
      <p>The quick brown fox jumps over the <strong>lazy dog</strong></p>
      <input type="text" name="firstName" placeholder="Alex" />
    </div>
    <!--Fragment not (usually) necessary but make sure it works-->
    <!--Text not necessary but etc.-->
    <p>lol wut ok</p>
    <!--But what if it is not in the spec?-->
    <customtag class="kk">
      <p>OK that works for me</p>
    </customtag>
  </body>
</html>

Browser

Via browserify or similar:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Vdom morphdom</title>
  </head>
  <body>
    <div id="main"></div>
    <script src="main.js"></script>
  </body>
</html>
const vdom = require( '@mojule/vdom' )

const {
  div, p, strong, input, comment, documentFragment
} = vdom.h

const vmain = div(
  { id: 'main', class: 'container' },

  documentFragment(
    comment( 'Hello' )
  ),

  p( 'The quick brown fox jumps over the ', strong( 'lazy dog' ) ),

  input( { type:'text', name:'firstName', placeholder:'Alex' } )
)

const main = document.querySelector( '#main' )

vmain.morphdom( main )

console.log( main.outerHTML )

API reference

First, see the references for tree-factory, tree and dom-plugins, as vdom is built on those so has all of their API methods.

Additionally, vdom adds or overrides the following plugins:

accepts

Overrides base accepts so that HTML nodes can only accept nodes according to the HTML spec, for example disallows adding a div to a span and so forth.

Stricter than the real browser DOM, which will let you do things like that and then try to apply error correction. We may allow turning this off with a loose: true option in future.

const span = vdom.createElement( 'span' )
const div = vdom.createElement( 'div' )

console.log( span.accepts( div ) ) // false

span.append( div ) // throws

actualize

Takes a vdom node and turns it into a browser DOM node (or compatible, like JSDOM) - just calls node.vnode() under the hood

const span = vdom.createElement( 'span' )

const spanEl = span.actualize( document )

// 'SPAN'
console.log( spanEl.tagName )

categories

Returns an array of content categories (as defined by the HTML spec) for a node

const div = vdom.createElement( 'div' )

console.log( div.categories() )
[ "flow content", "palpable content" ]

createState

Extends the default createState to also allow the creation of nodes from HTML strings

const div = vdom( '<div></div>' )

h

Adds a hyper-script-like interface to vdom, stored on vdom.h

A convenience API for easily creating nested nodes

See the examples above.

isEmpty

Overrides default isEmpty implementation to use the definitions from the HTML spec - note that isEmpty === true means that a node cannot have children, not that a node does not have children.

const div = vdom.createElement( 'div' )
const img = vdom.createElement( 'img' )

console.log( div.isEmpty() ) // false
console.log( img.isEmpty() ) // true

isModules

isMetadata

Returns true if the node is a metadata element, as defined by the spec

const div = vdom.createElement( 'div' )
const meta = vdom.createElement( 'meta' )

console.log( div.isMetadata() ) // false
console.log( meta.isMetadata() ) // true

isInline

Returns true if the node's categories include 'phrasing content'

const div = vdom.createElement( 'div' )
const span = vdom.createElement( 'span' )
const text = vdom.createText( 'hello' )

console.log( div.isInline() ) // false
console.log( span.isInline() ) // true
console.log( text.isInline() ) // true

isEmbedded

Returns true if the node's categories include 'embedded content'

const div = vdom.createElement( 'div' )
const img = vdom.createElement( 'img' )

console.log( div.isEmbedded() ) // false
console.log( img.isEmbedded() ) // true

isBlock

Returns true if the node's categories contain 'flow content' and do not include 'phrasing content'

const div = vdom.createElement( 'div' )
const span = vdom.createElement( 'span' )

console.log( div.isBlock() ) // true
console.log( span.isBlock() ) // false

isContainer

Returns true if the node is not an empty node

const div = vdom.createElement( 'div' )
const img = vdom.createElement( 'img' )

console.log( div.isContainer() ) // true
console.log( img.isContainer() ) // false

morphdom

Allows you to patch a vdom node into the real DOM, via morphdom

See the browser example above.

virtualize

Takes a real DOM (or compatible, JSDOM etc) node and turns it into a vdom node

const divEl = document.querySelector( 'div' )

const div = vdom.virtualize( divEl )

// do vdom stuff with div

vnode

Returns a virtual node representation of the current node, suitable for use with morphdom but potentially also with other tools that require a more DOM-like interface

const div = vdom.createElement( 'div' )
const vdiv = div.vnode()

console.log( vdiv.tagName ) // 'div'