derby-parsing
Advanced tools
Comparing version 0.5.1 to 0.6.0
@@ -329,10 +329,32 @@ var htmlUtil = require('html-util'); | ||
function finishParseViewElement(viewAttributes, remaining, viewInstance) { | ||
if (!viewAttributes.hasOwnProperty('content') && remaining.length) { | ||
var template = new templates.Template(remaining); | ||
viewAttributes.content = (viewAttributes.within) ? template : | ||
new templates.ParentWrapper(template); | ||
} | ||
setContentAttribute(viewAttributes, remaining); | ||
parseNode.content.push(viewInstance); | ||
} | ||
function setContentAttribute(attributes, content) { | ||
if (attributes.hasOwnProperty('content')) return; | ||
if (!content.length) return; | ||
attributes.content = attributeValueFromContent(content, attributes.within); | ||
} | ||
function attributeValueFromContent(content, isWithin) { | ||
// Optimize common cases where content can be a literal or a single expression | ||
if (content.length === 1) { | ||
var item = content[0]; | ||
if (item instanceof templates.Text) { | ||
return item.data; | ||
} | ||
if (item instanceof templates.DynamicText) { | ||
var expression = item.expression; | ||
if (expression instanceof expressions.LiteralExpression) { | ||
return expression.value; | ||
} | ||
return (isWithin) ? expression : new expressions.ViewParentExpression(expression); | ||
} | ||
} | ||
// Otherwise, wrap a template as needed for the context | ||
var template = new templates.Template(content); | ||
return (isWithin) ? template : new templates.ViewParent(template); | ||
} | ||
function viewAttributesFromElement(element) { | ||
@@ -345,5 +367,5 @@ var viewAttributes = {}; | ||
(attribute.expression instanceof templates.Template) ? | ||
new templates.ParentWrapper(attribute.expression) : | ||
new templates.ViewParent(attribute.expression) : | ||
(attribute.expression instanceof expressions.Expression) ? | ||
new templates.ParentWrapper(new templates.DynamicText(attribute.expression), attribute.expression) : | ||
new expressions.ViewParentExpression(attribute.expression) : | ||
attribute.data; | ||
@@ -513,5 +535,4 @@ } | ||
var camelName = dashToCamelCase(name); | ||
var content = new templates.Template(element.content); | ||
viewAttributes[camelName] = (element.attributes && element.attributes.within) ? | ||
content : new templates.ParentWrapper(content); | ||
var isWithin = element.attributes && element.attributes.within; | ||
viewAttributes[camelName] = attributeValueFromContent(element.content, isWithin); | ||
} | ||
@@ -521,7 +542,3 @@ | ||
var item = viewAttributesFromElement(element); | ||
if (!item.hasOwnProperty('content') && element.content.length) { | ||
item.content = new templates.ParentWrapper( | ||
new templates.Template(element.content) | ||
); | ||
} | ||
setContentAttribute(item, element.content); | ||
var camelName = dashToCamelCase(name); | ||
@@ -571,3 +588,3 @@ var viewAttribute = viewAttributes[camelName] || | ||
(value instanceof expressions.Expression) ? | ||
new templates.ParentWrapper(new templates.DynamicText(value), value) : | ||
new expressions.ViewParentExpression(value) : | ||
value; | ||
@@ -574,0 +591,0 @@ } |
{ | ||
"name": "derby-parsing", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "Add HTML template parsing to Derby", | ||
@@ -22,3 +22,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"derby-templates": "^0.4.0", | ||
"derby-templates": "^0.5.0", | ||
"esprima-derby": "^0.1.0", | ||
@@ -25,0 +25,0 @@ "html-util": "^0.2.0" |
@@ -525,2 +525,52 @@ var expect = require('expect.js'); | ||
it('view usage supports "within" attribute on child tags to use context from inside view', function() { | ||
var views = new templates.Views(); | ||
context.meta.views = views; | ||
views.register('body' | ||
, '<view is="custom-list" items="{{[\'item A\', \'item B\']}}">' + | ||
'<item-content within><b>{{#item}}</b></item-content>' + | ||
'</view>' | ||
); | ||
views.register('custom-list' | ||
, '<ul>' + | ||
'{{each @items as #item}}' + | ||
'<li>{{@itemContent}}</li>' + | ||
'{{/each}}' + | ||
'</ul>' | ||
, {attributes: 'item-content'} | ||
); | ||
var view = views.find('body'); | ||
expect(view.get(context)).equal('<ul><li><b>item A</b></li><li><b>item B</b></li></ul>'); | ||
}); | ||
it('view usage supports "within" attribute on child array tags to use context from inside view', function() { | ||
var views = new templates.Views(); | ||
context.meta.views = views; | ||
views.register('body' | ||
, '<view is="custom-table" items="{{[\'item A\', \'item BB\']}}">' + | ||
'<row-cell within>Text: {{#item}}</row-cell>' + | ||
'<row-cell within>Length: {{#item.length}}</row-cell>' + | ||
'</view>' | ||
); | ||
views.register('custom-table' | ||
, '<table>' + | ||
'{{each @items as #item}}' + | ||
'<tr>' + | ||
'{{each @rowCells as #rowCell}}' + | ||
'<td>{{#rowCell.content}}</td>' + | ||
'{{/each}}' + | ||
'</tr>' + | ||
'{{/each}}' + | ||
'</table>' | ||
, {arrays: 'row-cell/rowCells'} | ||
); | ||
var view = views.find('body'); | ||
expect(view.get(context)).equal( | ||
'<table>' + | ||
'<tr><td>Text: item A</td><td>Length: 6</td></tr>' + | ||
'<tr><td>Text: item BB</td><td>Length: 7</td></tr>' + | ||
'</table>' | ||
); | ||
}); | ||
it('HTML content escapes a literal view attribute', function() { | ||
@@ -527,0 +577,0 @@ var views = new templates.Views(); |
80041
2042
+ Addedderby-templates@0.5.0(transitive)
+ Addedsaddle@0.8.0(transitive)
- Removedderby-templates@0.4.5(transitive)
- Removedsaddle@0.7.3(transitive)
Updatedderby-templates@^0.5.0