derby-parsing
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -40,2 +40,3 @@ var htmlUtil = require('html-util'); | ||
function createTemplate(source, view) { | ||
source = escapeBraced(source); | ||
parseNode = new ParseNode(view); | ||
@@ -66,4 +67,5 @@ htmlUtil.parse(source, { | ||
function createStringTemplate(source, view) { | ||
source = escapeBraced(source); | ||
parseNode = new ParseNode(view); | ||
parseText(source, parseTextLiteral, parseTextExpression); | ||
parseText(source, parseTextLiteral, parseTextExpression, 'string'); | ||
return new templates.Template(parseNode.content); | ||
@@ -113,3 +115,3 @@ } | ||
parseNode = parseNode.child(); | ||
parseText(htmlUtil.unescapeEntities(value), parseTextLiteral, parseTextExpression); | ||
parseText(value, parseTextLiteral, parseTextExpression, 'attribute'); | ||
@@ -173,4 +175,4 @@ if (parseNode.content.length === 1) { | ||
function parseHtmlText(data, isRawText) { | ||
var unescaped = (isRawText) ? data : htmlUtil.unescapeEntities(data); | ||
parseText(unescaped, parseTextLiteral, parseTextExpression, 'html'); | ||
var environment = (isRawText) ? 'string' : 'html'; | ||
parseText(data, parseTextLiteral, parseTextExpression, environment); | ||
} | ||
@@ -211,3 +213,4 @@ | ||
function parseTextExpression(expression, environment) { | ||
function parseTextExpression(source, environment) { | ||
var expression = createExpression(source); | ||
if (expression.meta.blockType) { | ||
@@ -517,2 +520,29 @@ parseBlockExpression(expression); | ||
function escapeBraced(source) { | ||
var out = ''; | ||
parseText(source, onLiteral, onExpression, 'string'); | ||
function onLiteral(text) { | ||
out += text; | ||
} | ||
function onExpression(text) { | ||
var escaped = text.replace(/[&<]/g, function(match) { | ||
return (match === '&') ? '&' : '<'; | ||
}); | ||
out += '{{' + escaped + '}}'; | ||
} | ||
return out; | ||
} | ||
function unescapeBraced(source) { | ||
return source.replace(/(?:&|<)/g, function(match) { | ||
return (match === '&') ? '&' : '<'; | ||
}); | ||
} | ||
function unescapeTextLiteral(text, environment) { | ||
return (environment === 'html' || environment === 'attribute') ? | ||
htmlUtil.unescapeEntities(text) : | ||
text; | ||
} | ||
function parseText(data, onLiteral, onExpression, environment) { | ||
@@ -527,3 +557,4 @@ var current = data; | ||
if (start === -1) { | ||
onLiteral(current); | ||
var unescapedCurrent = unescapeTextLiteral(current, environment); | ||
onLiteral(unescapedCurrent); | ||
return; | ||
@@ -537,3 +568,4 @@ } | ||
var before = current.slice(0, start); | ||
onLiteral(current.slice(0, start)); | ||
var unescapedBefore = unescapeTextLiteral(before, environment); | ||
onLiteral(unescapedBefore); | ||
} | ||
@@ -543,4 +575,5 @@ | ||
if (inside) { | ||
var expression = createExpression(inside); | ||
onExpression(expression, environment); | ||
var unescapedInside = unescapeBraced(inside); | ||
unescapedInside = unescapeTextLiteral(unescapedInside, environment); | ||
onExpression(unescapedInside, environment); | ||
} | ||
@@ -547,0 +580,0 @@ |
{ | ||
"name": "derby-parsing", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"description": "Add HTML template parsing to Derby", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -189,2 +189,46 @@ var expect = require('expect.js'); | ||
}); | ||
it('less than sign in double braces', function() { | ||
test('{{_page.zero < 0}} {{_page.zero <= 0}}', 'false true'); | ||
}); | ||
it('less than sign in double braces in attribute', function() { | ||
test('<div class="{{_page.zero < 0}} {{_page.zero <= 0}}"></div>', '<div class="false true"></div>'); | ||
}); | ||
it('less than sign in double braces in script tag', function() { | ||
test('<script>{{_page.zero < 0}} {{_page.zero <= 0}}</script>', '<script>false true</script>'); | ||
}); | ||
it('less than sign in string in double braces', function() { | ||
test('{{"<div>"}}', '<div>'); | ||
}); | ||
it('less than sign in string in double braces in attribute', function() { | ||
test('<div class="{{"<div>"}}"></div>', '<div class="<div>"></div>'); | ||
}); | ||
it('less than sign in string in double braces in script tag', function() { | ||
test('<script>\'{{"<div>"}}\'</script>', '<script>\'<div>\'</script>'); | ||
}); | ||
it('amphersand in double braces', function() { | ||
test('{{1 && 2}} < {{_page.zero && 2}}', '2 < 0'); | ||
}); | ||
it('amphersandin double braces in attribute', function() { | ||
test('<div class="{{1 && 2}} < {{_page.zero && 2}}"></div>', '<div class="2 < 0"></div>'); | ||
}); | ||
it('amphersand in double braces in script tag', function() { | ||
test('<script>{{1 && 2}} < {{_page.zero && 2}}</script>', '<script>2 < 0</script>'); | ||
}); | ||
it('braces containing hex escaped literal braces', function() { | ||
test('{{"\\x7b\\x7b"}} {{"\\x7d"}}', '{{ }'); | ||
}); | ||
it('double braces can be escaped with HTML entity', function() { | ||
test('{{ }} { {', '{{ }} { {'); | ||
}); | ||
}); | ||
@@ -191,0 +235,0 @@ |
69922
1799