derby-parsing
Advanced tools
Comparing version 0.4.0 to 0.4.1
140
lib/index.js
@@ -76,3 +76,3 @@ var htmlUtil = require('html-util'); | ||
if (lowerTagName !== 'view' && !viewForTagName(lowerTagName)) { | ||
hooks = hooksFromAttributes(attributes, 'Element'); | ||
hooks = elementHooksFromAttributes(attributes); | ||
} | ||
@@ -285,5 +285,5 @@ var attributesMap = parseAttributes(attributes); | ||
var viewAttributes = viewAttributesFromElement(element); | ||
var hooks = hooksFromAttributes(viewAttributes, 'Component'); | ||
var componentHooks = componentHooksFromAttributes(viewAttributes); | ||
var remaining = element.content || []; | ||
var viewInstance = createDynamicViewInstance(nameAttribute.expression, viewAttributes, hooks); | ||
var viewInstance = createDynamicViewInstance(nameAttribute.expression, viewAttributes, componentHooks.hooks, componentHooks.initHooks); | ||
finishParseViewElement(viewAttributes, remaining, viewInstance); | ||
@@ -308,5 +308,5 @@ } else { | ||
var viewAttributes = viewAttributesFromElement(element); | ||
var hooks = hooksFromAttributes(viewAttributes, 'Component'); | ||
var componentHooks = componentHooksFromAttributes(viewAttributes); | ||
var remaining = parseContentAttributes(element.content, view, viewAttributes); | ||
var viewInstance = new templates.ViewInstance(view.registeredName, viewAttributes, hooks); | ||
var viewInstance = new templates.ViewInstance(view.registeredName, viewAttributes, componentHooks.hooks, componentHooks.initHooks); | ||
finishParseViewElement(viewAttributes, remaining, viewInstance); | ||
@@ -346,3 +346,30 @@ } | ||
function hooksFromAttributes(attributes, type) { | ||
function parseAsAttribute(key, value) { | ||
var expression = createPathExpression(value); | ||
if (!(expression instanceof expressions.PathExpression)) { | ||
throw new Error(key + ' attribute must be a path: ' + key + '="' + value + '"'); | ||
} | ||
return expression.segments; | ||
} | ||
function parseAsObjectAttribute(key, value) { | ||
var expression = createPathExpression(value); | ||
if (!( | ||
expression instanceof expressions.SequenceExpression && | ||
expression.args.length === 2 && | ||
expression.args[0] instanceof expressions.PathExpression | ||
)) { | ||
throw new Error(key + ' attribute requires a path and a key argument: ' + key + '="' + value + '"'); | ||
} | ||
var segments = expression.args[0].segments; | ||
var expression = expression.args[1]; | ||
return {segments: segments, expression: expression}; | ||
} | ||
function parseOnAttribute(key, value) { | ||
// TODO: Argument checking | ||
return createPathExpression(value); | ||
} | ||
function elementHooksFromAttributes(attributes, type) { | ||
if (!attributes) return; | ||
@@ -354,43 +381,28 @@ var hooks = []; | ||
if (key === 'as' || key === 'as-array' || key === 'asArray') { | ||
var expression = createPathExpression(value); | ||
if (!(expression instanceof expressions.PathExpression)) { | ||
if (key === 'asArray') key = 'as-array'; | ||
throw new Error(key + ' attribute must be a path: ' + key + '="' + value + '"'); | ||
} | ||
var Constructor = (key === 'as') ? templates.AsProperty : | ||
(type === 'Component') ? templates.AsArrayComponent : templates.AsArray; | ||
hooks.push(new Constructor(expression.segments)); | ||
// Parse `as` assignments | ||
if (key === 'as') { | ||
var segments = parseAsAttribute(key, value); | ||
hooks.push(new templates.AsProperty(segments)); | ||
delete attributes[key]; | ||
continue; | ||
} | ||
if (key === 'as-object' || key === 'asObject') { | ||
var expression = createPathExpression(value); | ||
if (!( | ||
expression instanceof expressions.SequenceExpression && | ||
expression.args.length === 2 && | ||
expression.args[0] instanceof expressions.PathExpression | ||
)) { | ||
if (key === 'asObject') key = 'as-object'; | ||
throw new Error(key + ' attribute requires a path and a key argument: ' + key + '="' + value + '"'); | ||
} | ||
var Constructor = (type === 'Component') ? templates.AsObjectComponent : templates.AsObject; | ||
hooks.push(new Constructor(expression.args[0].segments, expression.args[1])); | ||
if (key === 'as-array') { | ||
var segments = parseAsAttribute(key, value); | ||
hooks.push(new templates.AsArray(segments)); | ||
delete attributes[key]; | ||
continue; | ||
} | ||
if (key === 'as-object') { | ||
var parsed = parseAsObjectAttribute(key, value); | ||
hooks.push(new templates.AsObject(parsed.segments, parsed.expression)); | ||
delete attributes[key]; | ||
continue; | ||
} | ||
var match, eventName; | ||
if (type === 'Component') { | ||
match = /^on([A-Z_].*)/.exec(key); | ||
eventName = match && match[1].charAt(0).toLowerCase() + match[1].slice(1); | ||
} else { | ||
match = /^on-(.+)/.exec(key); | ||
eventName = match && match[1]; | ||
} | ||
// Parse event listeners | ||
var match = /^on-(.+)/.exec(key); | ||
var eventName = match && match[1]; | ||
if (eventName) { | ||
var expression = createPathExpression(value); | ||
var Constructor = (type === 'Component') ? | ||
templates.ComponentOn : templates.ElementOn; | ||
hooks.push(new Constructor(eventName, expression)); | ||
var expression = parseOnAttribute(key, value); | ||
hooks.push(new templates.ElementOn(eventName, expression)); | ||
delete attributes[key]; | ||
@@ -403,2 +415,46 @@ } | ||
function componentHooksFromAttributes(attributes) { | ||
if (!attributes) return {}; | ||
var hooks = []; | ||
var initHooks = []; | ||
for (var key in attributes) { | ||
var value = attributes[key]; | ||
// Parse `as` assignments | ||
if (key === 'as') { | ||
var segments = parseAsAttribute(key, value); | ||
hooks.push(new templates.AsProperty(segments)); | ||
delete attributes[key]; | ||
continue; | ||
} | ||
if (key === 'asArray') { | ||
var segments = parseAsAttribute('as-array', value); | ||
hooks.push(new templates.AsArrayComponent(segments)); | ||
delete attributes[key]; | ||
continue; | ||
} | ||
if (key === 'asObject') { | ||
var parsed = parseAsObjectAttribute('as-object', value); | ||
hooks.push(new templates.AsObjectComponent(parsed.segments, parsed.expression)); | ||
delete attributes[key]; | ||
continue; | ||
} | ||
// Parse event listeners | ||
var match = /^on([A-Z_].*)/.exec(key); | ||
var eventName = match && match[1].charAt(0).toLowerCase() + match[1].slice(1); | ||
if (eventName) { | ||
var expression = parseOnAttribute(key, value); | ||
initHooks.push(new templates.ComponentOn(eventName, expression)); | ||
delete attributes[key]; | ||
} | ||
} | ||
return { | ||
hooks: (hooks.length) ? hooks : null, | ||
initHooks: (initHooks.length) ? initHooks : null | ||
}; | ||
} | ||
function dashToCamelCase(string) { | ||
@@ -482,3 +538,3 @@ return string.replace(/-./g, function(match) { | ||
var viewAttributes = viewAttributesFromExpression(attributesExpression); | ||
var hooks = hooksFromAttributes(viewAttributes, 'Component'); | ||
var componentHooks = componentHooksFromAttributes(viewAttributes); | ||
@@ -492,5 +548,5 @@ // A ViewInstance has a static name, and a DynamicViewInstance gets its name | ||
findView(name); | ||
viewInstance = new templates.ViewInstance(name, viewAttributes, hooks); | ||
viewInstance = new templates.ViewInstance(name, viewAttributes, componentHooks.hooks, componentHooks.initHooks); | ||
} else { | ||
viewInstance = createDynamicViewInstance(nameExpression, viewAttributes, hooks); | ||
viewInstance = createDynamicViewInstance(nameExpression, viewAttributes, componentHooks.hooks, componentHooks.initHooks); | ||
} | ||
@@ -497,0 +553,0 @@ parseNode.content.push(viewInstance); |
{ | ||
"name": "derby-parsing", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Add HTML template parsing to Derby", | ||
@@ -22,3 +22,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"derby-templates": "^0.3.0", | ||
"derby-templates": "^0.3.3", | ||
"esprima-derby": "^0.1.0", | ||
@@ -25,0 +25,0 @@ "html-util": "^0.2.0" |
74325
1912
Updatedderby-templates@^0.3.3