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

xml-literals

v0.1.1

Published

Portable XML literal support for Javascript

Downloads

1,564

Readme

xml-literals(1) -- XML literals in Javascript

INSTALLING

To install xml-literals:

npm install xml-literals

INTRODUCTION

xml-literals adds support for XML literals to Javascript by means of simple code transformations. If you are familiar with the E4X spec, this could be considered "E4X: The Good Parts". Since it is based on desugaring syntax, you can use XML literals in any Javascript environment (Internet Explorer 6.0, Chrome 10, whatever). This library focuses on adding support to NodeJS, but I'll include some hints as to how to get this to work in a browser.

Here's an example of an XML literal:

	var anchor = <a href={href}>Hello</a>;

This kind of syntax is more concise than something like:

	var anchor = document.createElement('a');
	anchor.href = href;
	anchor.appendChild(document.createTextNode('Hello'));

And safer (and more flexible) than something like:

	var anchor = '<a href="' +href +'">Hello</a>';

All literals which appear in your source code will go through an XMLEnvironment which defines how the literal should be interpreted. This environment should interpret the literal into a constructor for some other DOM. See the documentation in lib/environment.js for more information on XMLEnvironment. js-xml-literals includes two environments for you to get started with. However if you want to implement something interesting like element decomposition you will need to learn how to create your own environment.

GETTING STARTED

The first step to get XML literals working in your project is to register which file extensions should be transformed. If you want to allow XML literals in any Javascript file you would do this:

	require('xml-literals').register('js');

This tells NodeJS to preprocess all *.js files with the XML literals transformation.

Unfortunately you won't be able to use XML literals in the file where you invoked the registration, so this kind of thing won't work:

init.js:

	require('xml-literals').register('js');
	// WON'T WORK!!
	var test = <span>Hello</span>;

After registering a file extension you must setup the XMLEnvironment. If you want to use the included simple-html-dom (recommended) you would do this:

	var SimpleHTMLDOMXMLEnvironment = require('xml-literals/simple-html-dom');
	XMLEnvironment.set(new SimpleHTMLDOMXMLEnvironment);

Then just require() your main script and it will have XML literals enabled. simple-html-dom works just like the DOM in your broswer, however it's simplified. Only basic DOM manipulation and querying functions are available. If you are looking for a more full-featured server-side DOM I recommend taking a look at jsdom, however you will incur a large performance penalty here. simple-html-dom nodes include a toString() method, so you can convert your DOM to string to send to the browser easily.

A quick example is included. Hello World

IN THE BROWSER

If you want to get this running in your browser, include both lib/environment.js and lib/dom-environment.js. Then run this: XMLEnvironment.set(new DOMXMLEnvironment(document));.

This step is probably the hardest. You have to run all your client-side JS through the xml-literals desugaring function before sending it to the browser. The transformation function can be resolved via require('xml-literals/lib/desugar/desugar').desugar. It's up to you how to implement this. Easiest is probably to integrate it into your minification pipeline.

After you get that setup you can do magic like this:

	document.body.appendChild(<div><a href={uri}>It's Magic!</a></div>);