Skip to content

Latest commit

 

History

History
180 lines (99 loc) · 3.96 KB

readme.md

File metadata and controls

180 lines (99 loc) · 3.96 KB

can-view-parser

Build Status

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);
}
  1. html {String|Object}: A mustache and html string to parse or an intermediate object the represents a previous parsing.
  2. handler {Object}: An object of callbacks.
  3. returnIntermediate {Boolean}: If true, returns a JS object representation of the parsing.

ParseHandler {Object}

An object consisting of callback functions that handle stages in the parsing process.

Object

start(tagName, unary)

Called when parsing a tag begins.

  1. tagName {String}: The name of the tag.
  2. unary {Boolean}: If this tag is unary (has no closing tag).

end(tagName, unary)

Called at the end of parsing a tag.

  1. tagName {String}: The name of the tag.
  2. 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.

  1. tagName {String}: The name of the tag.

attrStart(attrName)

Called when an attribute is found on an element.

  1. 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.

  1. attrName {String}: The name of the attribute.

attrValue(value)

Called when an attribute's value has been found.

  1. value {String}: The value discovered associated with an attribute.

chars(value)

Called when CharacterData is found within a tag.

  1. value {String}: The character data within the tag.

comment(value)

Called when a Comment is found within a tag.

  1. 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