derby-parsing
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -157,3 +157,3 @@ var derbyTemplates = require('derby-templates'); | ||
return expression; | ||
} | ||
} | ||
return new expressions.OperatorExpression(operator, [expression]); | ||
@@ -160,0 +160,0 @@ } |
@@ -18,4 +18,4 @@ var htmlUtil = require('html-util'); | ||
// Wrap parsing in a try / catch to add context to message when throwing | ||
var template; | ||
try { | ||
var template; | ||
if (this.string) { | ||
@@ -34,3 +34,3 @@ template = createStringTemplate(this.source, this); | ||
return template; | ||
} | ||
}; | ||
@@ -55,3 +55,3 @@ // Modified and shared among the following parse functions. It's OK for this | ||
parseNode = parseNode.parent; | ||
var last = parseNode.last() | ||
var last = parseNode.last(); | ||
if (last instanceof templates.Element) { | ||
@@ -74,8 +74,11 @@ last.notClosed = true; | ||
function parseHtmlStart(tag, tagName, attributes, selfClosing) { | ||
var hooks; | ||
if (tagName !== 'view' && !viewForTagName(tagName)) { | ||
var hooks = hooksFromAttributes(attributes, 'Element'); | ||
hooks = hooksFromAttributes(attributes, 'Element'); | ||
} | ||
var attributesMap = parseAttributes(attributes); | ||
var namespaceUri = (tagName.toLowerCase() === 'svg') ? | ||
templates.NAMESPACE_URIS.svg : parseNode.namespaceUri; | ||
if (selfClosing || templates.VOID_ELEMENTS[tagName]) { | ||
var element = new templates.Element(tagName, attributesMap, null, hooks, selfClosing); | ||
var element = new templates.Element(tagName, attributesMap, null, hooks, selfClosing, null, namespaceUri); | ||
parseNode.content.push(element); | ||
@@ -85,3 +88,4 @@ parseElementClose(tagName); | ||
parseNode = parseNode.child(); | ||
var element = new templates.Element(tagName, attributesMap, parseNode.content, hooks, selfClosing); | ||
parseNode.namespaceUri = namespaceUri; | ||
var element = new templates.Element(tagName, attributesMap, parseNode.content, hooks, selfClosing, null, namespaceUri); | ||
parseNode.parent.content.push(element); | ||
@@ -97,4 +101,6 @@ } | ||
var value = attributes[key]; | ||
var match = /([^:]+):[^:]/.exec(key); | ||
var nsUri = match && templates.NAMESPACE_URIS[match[1]]; | ||
if (value === '' || typeof value !== 'string') { | ||
attributesMap[key] = new templates.Attribute(value); | ||
attributesMap[key] = new templates.Attribute(value, nsUri); | ||
continue; | ||
@@ -109,12 +115,12 @@ } | ||
attributesMap[key] = | ||
(item instanceof templates.Text) ? new templates.Attribute(item.data) : | ||
(item instanceof templates.Text) ? new templates.Attribute(item.data, nsUri) : | ||
(item instanceof templates.DynamicText) ? | ||
(item.expression instanceof expressions.LiteralExpression) ? | ||
new templates.Attribute(item.expression.value) : | ||
new templates.DynamicAttribute(item.expression) : | ||
new templates.DynamicAttribute(item); | ||
new templates.Attribute(item.expression.value, nsUri) : | ||
new templates.DynamicAttribute(item.expression, nsUri) : | ||
new templates.DynamicAttribute(item, nsUri); | ||
} else if (parseNode.content.length > 1) { | ||
var template = new templates.Template(parseNode.content); | ||
attributesMap[key] = new templates.DynamicAttribute(template); | ||
attributesMap[key] = new templates.DynamicAttribute(template, nsUri); | ||
@@ -342,4 +348,4 @@ } else { | ||
var expression = createPathExpression(value); | ||
var constructor = templates[type + 'On']; | ||
hooks.push(new constructor(eventName, expression)); | ||
var Constructor = templates[type + 'On']; | ||
hooks.push(new Constructor(eventName, expression)); | ||
delete attributes[key]; | ||
@@ -397,3 +403,4 @@ } | ||
function parseAttributeElement(element, name, viewAttributes) { | ||
viewAttributes[name] = new templates.ParentWrapper( | ||
var camelName = dashToCamelCase(name); | ||
viewAttributes[camelName] = new templates.ParentWrapper( | ||
new templates.Template(element.content) | ||
@@ -410,3 +417,5 @@ ); | ||
} | ||
var viewAttribute = viewAttributes[name] || (viewAttributes[name] = []); | ||
var camelName = dashToCamelCase(name); | ||
var viewAttribute = viewAttributes[camelName] || | ||
(viewAttributes[camelName] = []); | ||
viewAttribute.push(item); | ||
@@ -491,2 +500,3 @@ } | ||
this.content = []; | ||
this.namespaceUri = parent && parent.namespaceUri; | ||
} | ||
@@ -589,5 +599,6 @@ ParseNode.prototype.child = function() { | ||
path = source; | ||
var keyword; | ||
do { | ||
match = valueRegExp.exec(path); | ||
var keyword = match[1]; | ||
keyword = match[1]; | ||
path = match[2]; | ||
@@ -605,4 +616,5 @@ if (keyword === 'unescaped') { | ||
// Wrap parsing in a try / catch to add context to message when throwing | ||
var expression; | ||
try { | ||
var expression = (path) ? | ||
expression = (path) ? | ||
createPathExpression(path) : | ||
@@ -609,0 +621,0 @@ new expressions.Expression(); |
@@ -26,3 +26,3 @@ var EventEmitter = require('events').EventEmitter; | ||
if (hasListenerFor(template, 'submit')) { | ||
addListener(template, 'submit', '$preventDefault($event)') | ||
addListener(template, 'submit', '$preventDefault($event)'); | ||
} | ||
@@ -29,0 +29,0 @@ }); |
{ | ||
"name": "derby-parsing", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Add HTML template parsing to Derby", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "mocha --recursive test --timeout 5000 --bail --colors --reporter spec --debug" | ||
"test": "./node_modules/.bin/mocha && ./node_modules/.bin/jshint lib/*.js test/*.js" | ||
}, | ||
@@ -27,4 +27,6 @@ "repository": { | ||
"devDependencies": { | ||
"expect.js": "~0.2.0" | ||
"expect.js": "~0.3.1", | ||
"jshint": "~2.4.4", | ||
"mocha": "~1.17.1" | ||
} | ||
} |
@@ -268,3 +268,3 @@ var expect = require('expect.js'); | ||
expect(create('""').get()).equal(''); | ||
expect(create("'Howdy'").get()).equal('Howdy'); | ||
expect(create('\'Howdy\'').get()).equal('Howdy'); | ||
// Regular Expressions | ||
@@ -300,3 +300,3 @@ var re = create('/([0-9]+)/').get(); | ||
expect(create('false || null').get()).equal(null); | ||
expect(create('"" && 3').get()).equal(""); | ||
expect(create('"" && 3').get()).equal(''); | ||
expect(create('1 + 1').get()).equal(2); | ||
@@ -337,3 +337,3 @@ expect(create('4 - 3').get()).equal(1); | ||
var expression = create('(_page.key === "green") ? _page.colors.green.name : "Other"'); | ||
expect(expression.get(context)).to.equal("Green"); | ||
expect(expression.get(context)).to.equal('Green'); | ||
}); | ||
@@ -340,0 +340,0 @@ |
@@ -46,3 +46,3 @@ var expect = require('expect.js'); | ||
, 'page missing end body and html tags': '<!DOCTYPE html><html><head><title></title></head><body><p></p>' | ||
} | ||
}; | ||
@@ -327,2 +327,20 @@ for (var name in literalTests) { | ||
it('views can define custom child attribute tags with dashes', function() { | ||
var views = new templates.Views(); | ||
context.meta.views = views; | ||
views.register('body' | ||
, '<view name="section">' + | ||
'<main-title><b>Hi</b></main-title>' + | ||
'More text' + | ||
'</view>' | ||
); | ||
views.register('section' | ||
, '<h3>{{@mainTitle}}</h3>' + | ||
'<div>{{@content}}</div>' | ||
, {attributes: 'main-title'} | ||
); | ||
var view = views.find('body'); | ||
expect(view.get(context)).equal('<h3><b>Hi</b></h3><div>More text</div>'); | ||
}); | ||
it('views support generic attribute tags', function() { | ||
@@ -329,0 +347,0 @@ var views = new templates.Views(); |
@@ -39,3 +39,3 @@ var expect = require('expect.js'); | ||
it('gets always truthy value for else block', function() { | ||
parsing.createExpression('else') | ||
parsing.createExpression('else'); | ||
expect(parsing.createExpression('else').truthy()).equal(true); | ||
@@ -42,0 +42,0 @@ }); |
66236
12
1714
3