diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..13203c3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ + +node_modules/ + +*.bak +*~ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a96b06a --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +test: + ./node_modules/.bin/mocha --reporter spec + +.PHONY: test diff --git a/README.md b/README.md index 8499aa4..8033897 100644 --- a/README.md +++ b/README.md @@ -1,96 +1,105 @@ -# Pure JavaScript HTML5 Parser # +# Neutron HTML5 Parser # - -A working demo can be seen [here](http://htmlpreview.github.io/?https://github.com/blowsie/Pure-JavaScript-HTML-Parser/blob/master/demo.html). +Here is a small pure-JavaScript HTML5 parser that can run on browsers as well as NodeJS with [jsdom](https://github.com/tmpvar/jsdom). _Credit goes to John Resig for his [code](http://ejohn.org/blog/pure-javascript-html-parser/) written back in 2008 and Erik Arvidsson for his [code](http://erik.eae.net/simplehtmlparser/simplehtmlparser.js) written prior to that._ This code has been updated to work with HTML 5 to fix several problems. +## Use case +For parsing templates on both client and server side. +This library may soon be used internally in [htmlizer](https://github.com/Munawwar/htmlizer). +For only server-side use case, you may like to use [htmlparser2](https://github.com/fb55/htmlparser2) or [high5](https://github.com/fb55/high5). Note: DOCTYPE gets ignored by htmlparser2. -## 4 Libraries in One! ## - -### A SAX-style API ### - -Handles tag, text, and comments with callbacks. For example, let’s say you wanted to implement a simple HTML to XML serialization scheme – you could do so using the following: +For only client-side use case, you can look into jQuery.parseHTML() or native DOMParser (IE10+). - var results = ""; - - HTMLParser("
hello world", { - start: function( tag, attrs, unary ) { - results += "<" + tag; - - for ( var i = 0; i < attrs.length; i++ ) - results += " " + attrs[i].name + '="' + attrs[i].escaped + '"'; - - results += ">"; - }, - end: function( tag ) { - results += "" + tag + ">"; - }, - chars: function( text ) { - results += text; - }, - comment: function( text ) { - results += ""; - } - }); - - results == '
hello world
" +## Usage -### XML Serializer ### +Add htmlparser.js to head tag or require with nodejs. -Now, there’s no need to worry about implementing the above, since it’s included directly in the library, as well. Just feed in HTML and it spits back an XML string. +### DOM Builder ### - var results = HTMLtoXML("Data: ") - results == '
Data:
' + //Returns DocumentFragment. + var documentfragment = HTMLtoDOM("Hello World");
+ //If doctype is given then returns HTMLDocument
+ var doc = HTMLtoDOM("