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

@caldwell/jsml

v1.1.5

Published

a compact representation of HTML using Javascript arrays and objects.

Downloads

21

Readme

JSML

What is JSML?

JSML is a compact representation of HTML using Javascript arrays and objects. JSML is also a small jQuery plug-in that allows you to construct DOM elements from these structured Javascript objects. Here's a quick example:

$("#my-element").jsml(["a", { href: "http://example.com" }, "This is a " ])

That finds the div identified by "my-element" and replaces its contents with:

This is a <link>

...except that there's never an actual HTML string created. JSML uses 'document.createElement()', 'document.createTextNode()' and 'document.appendChild()' to build up the DOM representation directly which means that you never have to worry about quoting HTML entities which helps prevent XSS (https://secure.wikimedia.org/wikipedia/en/wiki/Cross_site_scripting[cross site scripting]) attacks by being secure by default.

Here's a slightly more complicated example:

$("#my-element").jsml(["ul", ["li", { style: { backgroundColor: "red" } }, "First item"], ["li", ["img", { src: "myimage.png", title: "A title"}, { alt: "Some Alt-text" }], "Second item"], $("raw html item")])

Additional things to notice:

  • The basic idea is that new arrays signal new elements. The first item in an array defines what type of element. The rest of the items in an array are either attributes or child elements.

  • Objects (enclosed in "'{}'") let you define an element's attributes. You can have multiple objects in the array--they simply accumulate. If you have duplicate keys in the objects then the last one in the array wins.

  • As a special case you can drill down into the style with a nested object (though it's not necessarily the best thing to do).

  • Style and attribute names are the standard Javascript equivalents ("border-top" is "borderTop", "class" is "className").

  • You can use jQuery to drop in unquoted HTML strings if you absolutely need to.

Advanced Usage

JSML has special support for 'map' functions (Underscore's is shown here):

$("#my-element").jsml(["ul", _.map([1,2,3], function(num) { return ["li", "Item "+num] })]);

If you're paying attention you'll notice that the structure passed to the 'jsml()' function looks like this:

["ul", [["li", "Item 1"], ["li", "Item 2"], ["li", "Item 3"]]]

...which has extra array around the list items unlike the standard syntax:

["ul", ["li", "Item 1"], ["li", "Item 2"], ["li", "Item 3"]]

JSML automatically flattens this extra level of arrays to make using 'map()' convenient. This is also useful for passing in an array of uniform items in a JSML structure (which means there's usually no need to use 'concat()').

var a = []; a.push(["li", "Item 1"], ["li", "Item 2"]); $("#my-element").jsml(["ul", a]);

You can also pass DOM element objects and jQuery objects and those will be appended:

var li = document.createElement("li"); li.appendChild(document.createTextNode("Standard DOM element")); $("#my-element").jsml(["ul", li, $("jQuery element")]);

If you wish to create detached DOM elements you can call either '$.fn.jsml.make()' which returns a jQuery wrapped object, or, '$.fn.jsml.dom()' which returns a standard DOM element object.

How Should I Pronounce This?

There are 3 ways to pronounce it, depending on what you think of the
project. If you think it's cool you can call it "Jay-Smile", since its
compact representation and XSS resistance makes you happy. If you are
neutral, you can just call it "Jay Ess Emm Ell". If you hate it, feel free
to call it "Jay-Smell" (and make sure to really sneer when you say it).

Author, Copyright, and License

Copyright © 2009-2012 by David Caldwell [email protected] and Jim Radford [email protected]

The JSML source code is licensed under the Mozilla Public License. It is available here: http://mozilla.org/MPL/2.0/