0.1.0 • Published 10 years ago
fasthtml v0.1.0
FastHTML
FastHTML is a single-purpose HTML parser focused on performance. It only copes with sane HTML - see the Things that break section. Based upon node-fast-html-parser by ashi009
Install
npm install --save fasthtml
Performance
Tests via htmlparser-benchmark are on the way.
Docs
Test-documentation generated with mocha's "doc" reporter.
Usage
var parse = require('fasthtml');
var root = parse('<ul id="list"><li>Hello World</li></ul>', createElementNode, createTextNode);
API
parse(HTMLString, elementNodeConstructor, textNodeConstructor)
HTMLString
- HTMLstring
to parseelementNodeConstructor
-function
returning anElementNode
textNodeConstructor
-function
returning aTextNode
ElementNode(tagName, attributes)
tagName
-string
tagName of theElementNode
attributes
-object
attribtues of theElementNode
TextNode(textContent)
textString
-string
contents of theTextNode
Minimal implementation
See createElementNode.js and createTextNode.js in ./test/lib/
Example implementation
var HTMLElement = function HTMLElement(name, attributes) {
this.tagName = name;
this.attributes = attributes;
this.childNodes = [];
if (attributes.id) {
this.id = attributes.id;
}
};
HTMLElement.prototype.appendChild = function (node) {
this.childNodes.push(node);
return node;
};
var TextNode = function TextNode(value) {
this.rawText = value;
}
function createElementNode(tagName, attributes) {
return new HTMLElement(tagName, attributes);
}
function createTextNode(textContent) {
return new TextNode(textContent);
}
var parse = require('fasthtml');
var parsed = parse('<ul id="list"><li>Hello World</li></ul>', createElementNode, createTextNode);
console.log(parsed);
Things that break
FastHTML is designed for maximum performance, therefore it will not
- Try to fix broken markup
- Try to fix slightly broken markup
- Try to do automagic closing stuff
- Be graceful about unescaped HTML strings in inline
script
,style
and HTML attributes
Solutions
</script>
strings in inline script
Use <![CDATA[...]]>
.
HTML strings in attributes
Escape them.