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

dimdom

v0.4.1-beta.1

Published

A small, simple way to represent a DOM - a “diminished DOM”

Downloads

13

Readme

DimDom

[About | Installation | Examples]

About

DimDom is a small javascript library for representing a html DOM structure in a simple, naive manner.

Use it to define HTML or SVG element trees, along with their attributes and style, and then add them to existing documents.

DimDom instances are not bound to a document, so they can be created and modified freely without regard to the document they may ultimately be added to. No need to pass around Document or Node objects to your functions or classes for them to create new elements.

When the time comes to add them to your DOM, simply call a DimDom's create method to create a corresponding Node for a given document, or append a new Node directly to an existing one with the appendTo method.

Advantages over other approaches

Representing new elements as strings of markup makes it difficult to manipulate those elements, such as modifying attributes or adding/removing children. In order to do that, you likely would have to create corresponding elements (via inner/outerHTML, insertAdjacentHTML(), or DocumentFragment) and then manipulate it using the browser DOM api methods.

Handling elements using the standard DOM api and classes can be a bit verbose and cumbersome. Also, you usually have to use the document object to create them. This means you must have knowledge of where your elements will ultimately end up in order to create and use them.

Other third party libraries provide means to create and manipulate elements, but they often contain much more that might be overkill for a particular use case. DimDom has no event handling, no DOM selection, no animation effects, and no ajax helpers. It doesn't really deal with the standard existing DOM at all. It just provides an easy way to define new elements.

Installation

Download the repository and reference the dist/dimdom.js or dist/dimdom.min.js file.

-OR- Via the rawgit cdn at https://cdn.rawgit.com/romancow/dimdom/master/dist/dimdom.min.js

-OR- Using npm with npm install dimdom

Examples

Create a simple HTML element with some inner text:

var elem = new DimDom("div", "This is the inner text.");

Create an HTML element with attributes, style, and some inner text:

var elem = new DimDom("div", {class:"example"}, {color:"SlateGray"}, "This is the inner text.");

Create an HTML element with attributes and children elements:

var elem = new DimDom("div", {class:"example"}, [
    new DimDom("h1", {id:"interesting"}, "An Interesting Header"),
    new DimDom("p", "This is the inner text.")
]);

To specify an element namespace, pass a two item array as the first argument to the constructor. The first item is the namespace, the second should be the element name. For convenience, the standard HTML, SVG, and MathML namespaces can be found at DimDom.NS:

var elem = new DimDom([DimDom.NS.SVG, "svg"], {width:500, height:300},
    new DimDom([DimDom.NS.SVG, "circle"], {cx:100, cy:100, r:25})
);

As a shortcut for creating elements for the preset namespaces:

var elem = new DimDom.SVG("svg", {width:500, height:300},
    new DimDom.SVG("circle", {cx:100, cy:100, r:25})
);

Currently only "xmlns" and "xlink" attribute namespaces are supported and can be specified by separating the namespace prefix and attribute name with :

var elem = new DimDom.SVG("svg", {width:500, height:300, "xmlns:xlink": DimDom.NS.XLink});

var image = new DimDom.SVG("image", {"xlink:href": "sample.png"});

To alter attributes, style, or children after instantiation, use the attributes, styles, and children properties:

var elem = new DimDom("div", {class:"example"});
elem.attributes["id"] = "example1";
elem.styles["margin"] = 0;
elem.children.push(
    new DimDom("h1", "An Interesting Header"),
    new DimDom("p", "This is the inner text.")
);

To get an actual Node object to insert into a document:

var elem = new DimDom("div", "This is the inner text.");
var node = elem.create(document);

var parent = document.getElementById("container");
parent.appendChild(node);

Or create and append the corresponding node in one step:

var parent = document.getElementById("container");
var elem = new DimDom("div", "This is the inner text.");
elem.appendTo(parent);

You can also create a DimDom "collection" to represent a list of DimDom items without a parent:

var collection = new DimDom.Collection([
    new DimDom("p", "Some paragraph text"),
    "Some standalone text",
    document.createElement("div")
]);

A collection can be appended directly to a node:

collection.appendTo(someNode);

Or used as the children for a new DimDom instance:

var parent = new DimDom("section", collection);