Skip to content
This repository was archived by the owner on Feb 23, 2025. It is now read-only.

Commit ffd9764

Browse files
committed
v0.0.12
1 parent fe935d9 commit ffd9764

File tree

8 files changed

+188
-112
lines changed

8 files changed

+188
-112
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ language: node_js
22
node_js:
33
- "0.10"
44
- "0.12"
5-
- "iojs"
5+
- "4"
66
script: npm test

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ For more information on the strengths, weaknesses and implementation details of
4343

4444
## Options
4545

46+
### options.convertHbsComments
47+
Type: `Boolean`
48+
Default value: `false`
49+
When `true`, Handlebars comments will be converted to HTML comments.
50+
4651
### options.ignoreHbsComments
4752
Type: `Boolean`
4853
Default value: `false`
@@ -60,7 +65,8 @@ When `true`, will replace multiple standard whitespace characters (line breaks,
6065

6166
* Does not affect "special" whitespace chars such as ` `, etc.
6267
* Does not affect text within `<pre>`,`<script>`,`<style>` tags
63-
* Does not affect HTML's rendered appearance
68+
* Does not affect text within "dynamic" tags (`<{{tag}}>`)
69+
* Does not affect HTML's rendered appearance (unless using CSS `white-space:pre`)
6470

6571
### options.xmlMode
6672
Type: `Boolean`
@@ -81,12 +87,11 @@ As of v1.5.0, it is not at all "forgiving", in that it will parse `<{{tag}}>asdf
8187
* add support for `{{#if}}`,`{{else}}`,`{{else if}}`,`{{#unless}}`
8288
* add support for `{{#with}}`,`{{#each}}`,`{{@index}}`,`{{@key}}`,`{{this}}`,`{{.}}`,`{{undefined}}`,`{{null}}`,`{{true}}`,`{{1}}`
8389
* add support for `{{> partial}}`
84-
* `options.convertHbsComments` for converting Handlebars comments to HTML comments
8590
* `options.mustacheOnly` that disables helpers, expressions and whitespace control? would have to provide parse errors
8691

8792

8893
## Changelog
89-
* 0.0.1–0.0.11 pre-releases
94+
* 0.0.1–0.0.12 pre-releases
9095

9196

9297
[npm-image]: https://img.shields.io/npm/v/handlebars-html-parser.svg

lib/each.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function addTop(stack, value)
1212

1313

1414

15-
function each(program, callback)
15+
function each(program, options, callback)
1616
{
1717
var i;
1818
var numNodes = program.length;
@@ -39,13 +39,13 @@ function each(program, callback)
3939

4040
for (i=0; i<numNodes; i++)
4141
{
42-
eachNode(program[i], state, callback);
42+
eachNode(program[i], state, options, callback);
4343
}
4444
}
4545

4646

4747

48-
function eachNode(node, state, callback)
48+
function eachNode(node, state, options, callback)
4949
{
5050
var callbackInvoked = false;
5151

@@ -85,6 +85,14 @@ function eachNode(node, state, callback)
8585
}
8686
case NodeType.HBS_TAG_START:
8787
{
88+
/*if (state.isTag === false)
89+
{
90+
if (node.closing !== true)
91+
{
92+
incrementTop(state.hbsCounts);
93+
}
94+
}*/
95+
8896
break;
8997
}
9098

@@ -152,6 +160,7 @@ function eachNode(node, state, callback)
152160
{
153161
removeTop(state.attrCounts);
154162
removeTop(state.childCounts);
163+
//removeTop(state.hbsCounts);
155164

156165
state.isWithinScriptTag = false;
157166
state.isWithinStyleTag = false;
@@ -174,6 +183,7 @@ function eachNode(node, state, callback)
174183

175184
addTop(state.attrCounts);
176185
addTop(state.childCounts);
186+
//addTop(state.hbsCounts);
177187
}
178188

179189
break;

lib/index.js

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,71 +9,69 @@ var objectAssign = require("object-assign");
99

1010
var defaultOptions =
1111
{
12-
// Undocumented
13-
// TODO :: this isn't cloned
14-
htmlParser:
15-
{
16-
decodeEntities: true,
17-
lowerCaseAttributeNames: true,
18-
lowerCaseTags: true,
19-
recognizeSelfClosing: true
20-
},
21-
22-
// Documented
12+
convertHbsComments: false,
2313
ignoreHbsComments: false,
2414
ignoreHtmlComments: false,
2515
normalizeWhitespace: false,
2616
xmlMode: false
2717
};
2818

19+
var htmlParserOptions =
20+
{
21+
decodeEntities: true,
22+
lowerCaseAttributeNames: true, // TODO :: what about inline SVG?
23+
lowerCaseTags: true, // TODO :: what about inline SVG?
24+
recognizeSelfClosing: true,
25+
xmlMode: false
26+
};
27+
2928

3029

3130
function parser(options)
3231
{
3332
this.options = objectAssign({}, defaultOptions, options);
3433

35-
this.options.htmlParser.xmlMode = this.options.xmlMode;
34+
this.options.htmlParser = objectAssign({}, htmlParserOptions);
3635

3736
if (this.options.xmlMode === true)
3837
{
39-
this.options.htmlParser.lowerCaseAttributeNames = true;
40-
this.options.htmlParser.lowerCaseTags = true;
38+
this.options.htmlParser.lowerCaseAttributeNames = false;
39+
this.options.htmlParser.lowerCaseTags = false;
40+
this.options.htmlParser.xmlMode = true;
4141
}
42+
43+
// Convenience
44+
this.type = parser.type;
4245
}
4346

4447

4548

46-
parser.each = each;
4749
parser.type = NodeType;
4850

4951

5052

5153
/*
52-
Build a linear program of HTML pieces and Handlebars expressions.
54+
Run a provided function once per element in a linear program containing
55+
HTML pieces and Handlebars expressions.
5356
*/
54-
parser.prototype.parse = function(input)
57+
parser.prototype.parse = function(input, callback)
5558
{
5659
//devlog.basic(input);
5760

58-
this.orgProgram = handlebars.parse(input);
59-
this.newProgram = [];
60-
61-
// Used for iterating through Handlebars statements within `orgProgram`
62-
//this.orgProgramCurrent = null;
63-
64-
// Convenience
65-
this.type = parser.type;
61+
var orgProgram = handlebars.parse(input);
62+
//devlog(orgProgram);
6663

6764
// Start with root-level (no children) program
68-
parseHtml(this, this.orgProgram);
69-
70-
//devlog(this.orgProgram);
65+
var newProgram = parseHtml([], orgProgram, this.options);
66+
//devlog(newProgram);
7167

72-
// TODO :: loop through newProgram in test
73-
//devlog(this.newProgram);
68+
if (typeof callback === "function")
69+
{
70+
each(newProgram, this.options, callback);
71+
}
7472

75-
// ?
76-
return this.newProgram;
73+
// For testing and curious minds
74+
return newProgram;
7775
};
7876

7977

0 commit comments

Comments
 (0)