derby-parsing
Advanced tools
Comparing version 0.7.3 to 0.7.4
@@ -757,8 +757,8 @@ var htmlUtil = require('html-util'); | ||
var match = blockRegExp.exec(source); | ||
var path; | ||
var path, as, keyAs; | ||
if (match) { | ||
meta.blockType = match[1]; | ||
path = match[2]; | ||
meta.as = match[3]; | ||
meta.keyAs = match[4]; | ||
as = match[3]; | ||
keyAs = match[4]; | ||
@@ -802,2 +802,8 @@ // The blocks `else`, `unbound`, and `bound` may not have a path or alias | ||
new expressions.Expression(); | ||
if (as) { | ||
meta.as = parseAlias(as); | ||
} | ||
if (keyAs) { | ||
meta.keyAs = parseAlias(keyAs); | ||
} | ||
} catch (err) { | ||
@@ -822,1 +828,15 @@ var message = '\n\nWithin expression: ' + source; | ||
} | ||
function parseAlias(source) { | ||
// Try parsing into a path expression. This throws on invalid expressions. | ||
var expression = createPathExpression(source); | ||
// Verify that it's an AliasPathExpression with no segments, i.e. that | ||
// it has the format "#IDENTIFIER". | ||
if (expression instanceof expressions.AliasPathExpression) { | ||
if (expression.segments.length === 0) { | ||
return expression.alias; | ||
} | ||
throw new Error('Alias must not have dots or brackets: ' + source); | ||
} | ||
throw new Error('Alias must be an identifier starting with "#": ' + source); | ||
} |
{ | ||
"name": "derby-parsing", | ||
"version": "0.7.3", | ||
"version": "0.7.4", | ||
"description": "Add HTML template parsing to Derby", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,6 +0,25 @@ | ||
Work in progress... | ||
derby-html-parser | ||
derby-parsing | ||
=============== | ||
Add HTML-based template parsing to Derby | ||
This module contains the HTML-based template parsing for [DerbyJS](https://github.com/derbyjs/derby). Given a template source string, it produces parsed `Template`s and `Expression`s as defined in [derbyjs/derby-templates](https://github.com/derbyjs/derby-templates). | ||
## Installation | ||
```shell | ||
npm install derby-parsing | ||
``` | ||
## Example usage | ||
```javascript | ||
var derbyParsing = require('derby-parsing'); | ||
var templateSource = '<title>{{_page.title}}</title>'; | ||
var template = derbyParsing.createTemplate(templateSource); | ||
``` | ||
## Tests | ||
```shell | ||
npm test | ||
``` |
@@ -400,3 +400,3 @@ var expect = require('expect.js'); | ||
]); | ||
expect(expression.get(blockContext)).to.eql('light Green'); | ||
expect(expression.get(blockContext)).a(templates.Template); | ||
}); | ||
@@ -472,3 +472,3 @@ | ||
]); | ||
expect(expression.get(blockContext)).to.eql('light Green'); | ||
expect(expression.get(blockContext)).a(templates.Template); | ||
}); | ||
@@ -603,11 +603,5 @@ | ||
describe('template values', function() { | ||
// TODO: We aren't testing the returned value from get for comparision. | ||
// Currently templates are not rendered when returned from attribute | ||
// expressions in particular. Perhaps this should change for | ||
// consistency, but right now derby relies on it working this way when | ||
// it sets the value of attributes on the model for a component | ||
it('gets function template attribute dependencies', function() { | ||
var attributes = { | ||
color: createTemplate('{{passThrough(_page.colors.green)}}') | ||
color: createTemplate('light{{_page.colors.green.name}}') | ||
}; | ||
@@ -617,4 +611,5 @@ var viewContext = context.viewChild(view, attributes); | ||
expect(expression.dependencies(viewContext)).to.eql([ | ||
['_page', 'colors', 'green', '*'] | ||
['_page', 'colors', 'green', 'name'] | ||
]); | ||
expect(expression.get(viewContext)).a(templates.Template); | ||
}); | ||
@@ -624,9 +619,10 @@ | ||
var attributes = { | ||
color: createTemplate('{{passThrough(_page.colors.green)}}') | ||
color: createTemplate('light{{_page.colors.green.name}}') | ||
}; | ||
var viewContext = context.viewChild(view, attributes); | ||
var expression = createExpression('@color.name'); | ||
var expression = createExpression('@color.length'); | ||
expect(expression.dependencies(viewContext)).to.eql([ | ||
['_page', 'colors', 'green', '*'] | ||
['_page', 'colors', 'green', 'name'] | ||
]); | ||
expect(expression.get(viewContext)).equal(10); | ||
}); | ||
@@ -645,2 +641,3 @@ | ||
]); | ||
expect(expression.get(viewContext)).a(templates.Template); | ||
}); | ||
@@ -659,2 +656,3 @@ | ||
]); | ||
expect(expression.get(viewContext)).equal(11); | ||
}); | ||
@@ -661,0 +659,0 @@ }); |
@@ -102,2 +102,37 @@ var expect = require('expect.js'); | ||
it('with block, valid alias', function() { | ||
test('{{with _page.greeting as #greeting}}{{#greeting}}{{/with}}', 'Howdy!'); | ||
}); | ||
describe('with block, invalid alias throws during parsing', function() { | ||
it('no pound sign at start of alias', function() { | ||
var source = '{{with _page.greeting as greeting}}{{/with}}'; | ||
expect(function() { | ||
var template = parsing.createTemplate(source); | ||
console.log(template.content[0]); | ||
}).to.throwException(/Alias must be an identifier starting with "#"/); | ||
}); | ||
it('trailing parenthesis in alias', function() { | ||
var source = '{{with _page.greeting as #greeting)}}{{/with}}'; | ||
expect(function() { | ||
var template = parsing.createTemplate(source); | ||
}).to.throwException(/Unexpected token \)/); | ||
}); | ||
it('brackets in alias', function() { | ||
var source = '{{with _page.greeting as #greeting[0]}}{{/with}}'; | ||
expect(function() { | ||
var template = parsing.createTemplate(source); | ||
}).to.throwException(/Alias must not have dots or brackets/); | ||
}); | ||
it('dots in alias', function() { | ||
var source = '{{with _page.greeting as #greeting.a}}{{/with}}'; | ||
expect(function() { | ||
var template = parsing.createTemplate(source); | ||
}).to.throwException(/Alias must not have dots or brackets/); | ||
}); | ||
}); | ||
it('if block', function() { | ||
@@ -182,2 +217,60 @@ test('{{if _page.yep}}yes{{/if}}', 'yes'); | ||
}); | ||
describe('each block, invalid alias throws during parsing', function() { | ||
it('no pound sign at start of alias', function() { | ||
var source = '{{each _page.letters as letter}}{{/each}}'; | ||
expect(function() { | ||
var template = parsing.createTemplate(source); | ||
}).to.throwException(/Alias must be an identifier starting with "#"/); | ||
}); | ||
it('trailing parenthesis in alias', function() { | ||
var source = '{{each _page.letters as #letter)}}{{/each}}'; | ||
expect(function() { | ||
var template = parsing.createTemplate(source); | ||
}).to.throwException(/Unexpected token \)/); | ||
}); | ||
it('brackets in alias', function() { | ||
var source = '{{each _page.letters as #letter[0]}}{{/each}}'; | ||
expect(function() { | ||
var template = parsing.createTemplate(source); | ||
}).to.throwException(/Alias must not have dots or brackets/); | ||
}); | ||
it('dots in alias', function() { | ||
var source = '{{each _page.letters as #letter.a}}{{/each}}'; | ||
expect(function() { | ||
var template = parsing.createTemplate(source); | ||
}).to.throwException(/Alias must not have dots or brackets/); | ||
}); | ||
it('no pound sign at start of index alias', function() { | ||
var source = '{{each _page.letters as #letter, index}}{{/each}}'; | ||
expect(function() { | ||
var template = parsing.createTemplate(source); | ||
}).to.throwException(/Alias must be an identifier starting with "#"/); | ||
}); | ||
it('trailing parenthesis in index alias', function() { | ||
var source = '{{each _page.letters as #letter, #index)}}{{/each}}'; | ||
expect(function() { | ||
var template = parsing.createTemplate(source); | ||
}).to.throwException(/Unexpected token \)/); | ||
}); | ||
it('brackets in index alias', function() { | ||
var source = '{{each _page.letters as #letter, #index[0]}}{{/each}}'; | ||
expect(function() { | ||
var template = parsing.createTemplate(source); | ||
}).to.throwException(/Alias must not have dots or brackets/); | ||
}); | ||
it('dots in index alias', function() { | ||
var source = '{{each _page.letters as #letter, #index.a}}{{/each}}'; | ||
expect(function() { | ||
var template = parsing.createTemplate(source); | ||
}).to.throwException(/Alias must not have dots or brackets/); | ||
}); | ||
}); | ||
}); | ||
@@ -184,0 +277,0 @@ |
Sorry, the diff of this file is not supported yet
107125
2717
26