can-view-parser
Parses html and magic tags.
API
can-view-parser function
Parse HTML and mustache tokens.
parse(html, handler, [returnIntermediate])
Parse an html string:
var parser = require("can-view-parser");
var html = '<h1><span bob="phillips"></span><span bob="meyers"></span>' +
'</h1>';
var bobs = {};
var curAttr;
parser(html, {
attrStart: function(attrName){
curAttr = attrName;
},
attrValue: function(value){
bobs[curAttr] = value;
}
});
for(var first in bobs) {
var last = bobs[first];
console.log("Hello", first, last);
}
- html
{String|Object}
: A mustache and html string to parse or an intermediate object the represents a previous parsing. - handler
{Object}
: An object of callbacks. - returnIntermediate
{Boolean}
: If true, returns a JS object representation of the parsing.
{Object}
ParseHandler An object consisting of callback functions that handle stages in the parsing process.
Object
start(tagName, unary)
Called when parsing a tag begins.
- tagName
{String}
: The name of the tag. - unary
{Boolean}
: If this tag is unary (has no closing tag).
end(tagName, unary)
Called at the end of parsing a tag.
- tagName
{String}
: The name of the tag. - unary
{Boolean}
: If this tag is unary (has no closing tag).
close(tagName)
Called when a closing tag is found. If no closing tag exists for this tag (because it is self-closing) this function will not be called.
- tagName
{String}
: The name of the tag.
attrStart(attrName)
Called when an attribute is found on an element.
- attrName
{String}
: The name of the attribute.
attrEnd(attrName)
Called at the end of parsing an attribute; after the attrStart and attrValue functions have been called.
- attrName
{String}
: The name of the attribute.
attrValue(value)
Called when an attribute's value has been found.
- value
{String}
: The value discovered associated with an attribute.
chars(value)
Called when CharacterData is found within a tag.
- value
{String}
: The character data within the tag.
comment(value)
Called when a Comment is found within a tag.
- value
{String}
: The Comment within the tag.
done()
Called at the end of parsing the template.
Contributing
Making a Build
To make a build of the distributables into dist/
in the cloned repository run
npm install
node build
Running the tests
Tests can run in the browser by opening a webserver and visiting the test.html
page.
Automated tests that run the tests from the command line in Firefox can be run with
npm test