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

Commit b795eb6

Browse files
committed
v0.0.13
1 parent 0b5ea8f commit b795eb6

File tree

5 files changed

+86
-53
lines changed

5 files changed

+86
-53
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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,29 @@ When `true`, output will be formatted for increased legibility.
5252
### options.normalizeWhitespace
5353
Type: `Boolean`
5454
Default value: `true`
55-
When `true`, multiple whitespace characters will be replaced with a single space. For more info, see [handlebars-html-parser](https://github.com/stevenvachon/handlebars-html-parser).
55+
See [handlebars-html-parser](https://github.com/stevenvachon/handlebars-html-parser).
5656

5757
### options.useDomMethods
5858
Type: `Boolean`
5959
Default value: `true`
6060
When `true`, available `React.DOM` convenience functions will be used instead of `React.createElement()`.
6161

62+
### options.xmlMode
63+
Type: `Boolean`
64+
Default value: `false`
65+
See [handlebars-html-parser](https://github.com/stevenvachon/handlebars-html-parser).
66+
6267

6368
## Roadmap Features
64-
* support `<script type="text/x-handlebars-template">`, etc
65-
* add `convertHbsComments` option for converting Handlebars comments to HTML comments
66-
* add `ignoreHtmlComments` option when React supports such ([react#2810](https://github.com/facebook/react/issues/2810))
69+
* support `<template>`
70+
* `convertHbsComments` to JavaScript block comments (or HTML comments?)
71+
* `convertHtmlComments` to JavaScript block comments
72+
* `ignoreComments` option when React supports such ([react#2810](https://github.com/facebook/react/issues/2810))
73+
* `trimWhitespace` option to remove spaces between elements (`<tag/> a word <tag/>` to `<tag/>a word<tag/>`)?
6774

6875

6976
## Changelog
70-
* 0.0.1–0.0.12 pre-releases
77+
* 0.0.1–0.0.13 pre-releases
7178

7279

7380
[npm-image]: https://img.shields.io/npm/v/handlebars-react.svg

index.js

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,71 +15,40 @@ var defaultOptions =
1515
normalizeWhitespace: true,
1616
prefix: "",
1717
suffix: "",
18-
useDomMethods: true
18+
useDomMethods: true,
19+
xmlMode: false
1920
};
2021

2122

2223

23-
/*var NodeType =
24-
{
25-
HBS_EXPRESSION: "hbsExpression",
26-
27-
HBS_EXPRESSION_END: "hbsExpressionEnd",
28-
HBS_EXPRESSION_HASH_PARAM: "hbsExpressionHashParam",
29-
HBS_EXPRESSION_PARAM: "hbsExpressionParam",
30-
HBS_EXPRESSION_PATH: "hbsExpressionPath",
31-
HBS_EXPRESSION_START: "hbsExpressionStart",
32-
HBS_TAG_END: "hbsTagEnd",
33-
HBS_TAG_START: "hbsTagStart",
34-
35-
HTML_ATTR_END: "htmlAttrEnd",
36-
HTML_ATTR_NAME_END: "htmlAttrNameEnd",
37-
HTML_ATTR_NAME_START: "htmlAttrNameStart",
38-
HTML_ATTR_START: "htmlAttrStart",
39-
HTML_ATTR_VALUE_END: "htmlAttrValueEnd",
40-
HTML_ATTR_VALUE_START: "htmlAttrValueStart",
41-
HTML_COMMENT_END: "htmlCommentEnd",
42-
HTML_COMMENT_START: "htmlCommentStart",
43-
HTML_TAG_END: "htmlTagEnd",
44-
HTML_TAG_NAME_END: "htmlTagNameEnd",
45-
HTML_TAG_NAME_START: "htmlTagNameStart",
46-
HTML_TAG_START: "htmlTagStart",
47-
48-
TEXT: "text"
49-
};*/
50-
51-
52-
5324
function compiler(options)
5425
{
5526
this.options = objectAssign({}, defaultOptions, options);
5627

5728
this.parser = new HandlebarsHtmlParser(
5829
{
59-
normalizeWhitespace: this.options.normalizeWhitespace
30+
normalizeWhitespace: this.options.normalizeWhitespace,
31+
xmlMode: this.options.xmlMode
6032
});
6133
}
6234

6335

6436

6537
compiler.prototype.compile = function(str)
6638
{
39+
var nodeStack,parserState;
40+
6741
var compilerState =
6842
{
6943
// React.DOM… or React.createElement per element in stack
7044
// Stack indexed by parent tag depth -- first index is a "document" node (root/top-level nodes container)
7145
areDomMethods: [false]
7246
};
7347

74-
var nodeStack = this.parser.parse(str);
7548
var options = this.options;
76-
var parserState;
7749
var result = [];
7850

79-
//console.log(nodeStack);
80-
//console.log(str);
81-
82-
HandlebarsHtmlParser.each(nodeStack, function(node, state)
51+
nodeStack = this.parser.parse(str, function(node, state)
8352
{
8453
// Parent scope access
8554
parserState = state;
@@ -114,6 +83,16 @@ compiler.prototype.compile = function(str)
11483
}
11584

11685

86+
case HandlebarsHtmlParser.type.HBS_TAG_END:
87+
{
88+
break;
89+
}
90+
case HandlebarsHtmlParser.type.HBS_TAG_START:
91+
{
92+
break;
93+
}
94+
95+
11796
case HandlebarsHtmlParser.type.HTML_ATTR_END:
11897
{
11998
break;
@@ -256,19 +235,25 @@ compiler.prototype.compile = function(str)
256235
}
257236
else if (parserState.isAttributeValue === true)
258237
{
259-
if (parserState.isStyleAttribute === false)
238+
// TODO :: support `href="javscript:code()"`
239+
/*if (parserState.isEventAttribute === true)
260240
{
261-
// React.createElement("tag", {"attr":"value"
262-
// React.DOM.tag({"attr":"value"
263-
// TODO :: run transformScript if script event attribute
264-
result.push( safeString(node.value) );
241+
// React.createElement("tag", {"onsomething":"code()"
242+
// React.DOM.tag({"onsomething":"code()"
243+
result.push( transformScript(node.value, options) );
265244
}
266-
else
245+
else*/ if (parserState.isStyleAttribute === true)
267246
{
268247
// React.createElement("tag", {"style":{…}
269248
// React.DOM.tag({"style":{…}
270249
result.push( transformInlineStyles(node.value, options) );
271250
}
251+
else
252+
{
253+
// React.createElement("tag", {"attr":"value"
254+
// React.DOM.tag({"attr":"value"
255+
result.push( safeString(node.value) );
256+
}
272257
}
273258
}
274259
}
@@ -330,6 +315,8 @@ compiler.prototype.compile = function(str)
330315
}
331316
}
332317

318+
//console.log(str);
319+
//console.log(nodeStack);
333320
//console.log(result);
334321
result = finalize(result, options);
335322
//console.log(result);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "handlebars-react",
33
"description": "Compile Handlebars templates to React.",
4-
"version": "0.0.12",
4+
"version": "0.0.13",
55
"license": "MIT",
66
"homepage": "https://github.com/stevenvachon/handlebars-react",
77
"author": {

test/test.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ describe("Basic HTML", function()
177177
{
178178
it("should support text content with special characters", function(done)
179179
{
180-
var result = new compiler( options() ).compile('<tag>"text©&copy;"</tag>');
181-
var expectedResult = 'React.createElement("tag",null,"\\\"text©©\\\"")';
180+
var result = new compiler( options() ).compile('<tag>"text©&copy;&nbsp;"</tag>');
181+
var expectedResult = 'React.createElement("tag",null,"\\\"text©© \\\"")';
182182

183183
expect(result).to.equal(expectedResult);
184184
done();
@@ -197,6 +197,29 @@ describe("Basic HTML", function()
197197

198198

199199

200+
it("should support unrecognized <script> tags", function(done)
201+
{
202+
var result = new compiler( options() ).compile('<script type="text/template"><tag attr=\"value\">text</tag></script>');
203+
var expectedResult = 'React.createElement("script",{"type":"text/template"},"<tag attr=\\\"value\\\">text</tag>")';
204+
205+
expect(result).to.equal(expectedResult);
206+
done();
207+
});
208+
209+
210+
211+
// TODO :: parse as text?
212+
it.skip("should support <template> tags", function(done)
213+
{
214+
var result = new compiler( options() ).compile('<template><tag attr=\"value\">text</tag></template>');
215+
var expectedResult = 'React.createElement("template",null,"<tag attr=\\\"value\\\">text</tag>")';
216+
217+
expect(result).to.equal(expectedResult);
218+
done();
219+
});
220+
221+
222+
200223
it("should support <style> tags", function(done)
201224
{
202225
var result = new compiler( options() ).compile('<style>html { background-color:gray }</style>');
@@ -246,5 +269,21 @@ describe("Basic HTML", function()
246269
expect(result).to.equal(expectedResult);
247270
done();
248271
});
272+
273+
274+
275+
it("normalizeWhitespace = true", function(done)
276+
{
277+
var result = new compiler( options({ normalizeWhitespace:true }) ).compile('<tag>text©&copy; &nbsp; </tag>');
278+
var expectedResult = 'React.createElement("tag",null,"text©©   ")'; // non-breaking space remains
279+
280+
expect(result).to.equal(expectedResult);
281+
done();
282+
});
283+
284+
285+
286+
// `multipleTopLevelNodes` is tested above
287+
// `useDomMethods` is tested above
249288
});
250289
});

0 commit comments

Comments
 (0)