0.1.0 • Published 10 years ago

fasthtml v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 years ago

FastHTML Build Status Coverage Status

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 - HTML string to parse
  • elementNodeConstructor - function returning an ElementNode
  • textNodeConstructor - function returning a TextNode

ElementNode(tagName, attributes)

  • tagName - string tagName of the ElementNode
  • attributes - object attribtues of the ElementNode

TextNode(textContent)

  • textString - string contents of the TextNode

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.

0.1.0

10 years ago

0.0.1

10 years ago