feature: separate options for value/tag trimming, working on #17

This commit is contained in:
harttle
2017-10-30 01:46:31 +08:00
parent 502426f621
commit e8fa8d32f5
15 changed files with 236 additions and 147 deletions
+6 -6
View File
@@ -55,8 +55,8 @@ module.exports = function (Tag, Filter) {
var tpl = null
if (token.type === 'tag') {
tpl = parseTag(token, tokens)
} else if (token.type === 'output') {
tpl = parseOutput(token.value)
} else if (token.type === 'value') {
tpl = parseValue(token.value)
} else { // token.type === 'html'
tpl = token
}
@@ -72,9 +72,9 @@ module.exports = function (Tag, Filter) {
return Tag.construct(token, tokens)
}
function parseOutput (str) {
function parseValue (str) {
var match = lexical.matchValue(str)
assert(match, `illegal output string: ${str}`)
assert(match, `illegal value string: ${str}`)
var initial = match[0]
str = str.substr(match.index + match[0].length)
@@ -85,7 +85,7 @@ module.exports = function (Tag, Filter) {
}
return {
type: 'output',
type: 'value',
initial: initial,
filters: filters.map(str => Filter.construct(str))
}
@@ -100,6 +100,6 @@ module.exports = function (Tag, Filter) {
parse,
parseTag,
parseStream,
parseOutput
parseValue
}
}
+4 -4
View File
@@ -27,9 +27,9 @@ var render = {
if (template.type === 'tag') {
return this.renderTag(template, scope)
.then(partial => partial === undefined ? '' : partial)
} else if (template.type === 'output') {
} else if (template.type === 'value') {
return Promise.resolve()
.then(() => this.evalOutput(template, scope))
.then(() => this.evalValue(template, scope))
.then(partial => partial === undefined ? '' : stringify(partial))
} else { // template.type === 'html'
return Promise.resolve(template.value)
@@ -47,8 +47,8 @@ var render = {
return template.render(scope)
},
evalOutput: function (template, scope) {
assert(scope, 'unable to evalOutput: scope undefined')
evalValue: function (template, scope) {
assert(scope, 'unable to evalValue: scope undefined')
return template.filters.reduce(
(prev, filter) => filter.render(prev, scope),
Syntax.evalExp(template.initial, scope))
+3 -3
View File
@@ -19,7 +19,7 @@ function parse (input, file, options) {
}
tokens.push(match[1]
? parseTagToken(match[1], match[2].trim(), match.index)
: parseOutputToken(match[3], match[4].trim(), match.index))
: parseValueToken(match[3], match[4].trim(), match.index))
}
if (input.length > lastMatchEnd) {
tokens.push(parseHTMLToken(lastMatchEnd, input.length))
@@ -48,9 +48,9 @@ function parse (input, file, options) {
return token
}
function parseOutputToken (raw, value, pos) {
function parseValueToken (raw, value, pos) {
return {
type: 'output',
type: 'value',
line: lineNumber.get(pos),
trim_left: raw.slice(0, 3) === '{{-',
trim_right: raw.slice(-3) === '-}}',
+15 -2
View File
@@ -5,22 +5,35 @@ function whiteSpaceCtrl (tokens, options) {
var inRaw = false
tokens.forEach((token, i) => {
if (!inRaw && (token.trim_left || options.trim_left)) {
if (shouldTrimLeft(token, inRaw, options)) {
trimLeft(tokens[i - 1], options.greedy)
}
if (token.type === 'tag' && token.name === 'raw') inRaw = true
if (token.type === 'tag' && token.name === 'endraw') inRaw = false
if (!inRaw && (token.trim_right || options.trim_right)) {
if (shouldTrimRight(token, inRaw, options)) {
trimRight(tokens[i + 1], options.greedy)
}
})
}
function shouldTrimLeft (token, inRaw, options) {
if (inRaw) return false
if (token.type === 'tag') return token.trim_left || options.trim_tag_left
if (token.type === 'value') return token.trim_left || options.trim_value_left
}
function shouldTrimRight (token, inRaw, options) {
if (inRaw) return false
if (token.type === 'tag') return token.trim_right || options.trim_tag_right
if (token.type === 'value') return token.trim_right || options.trim_value_right
}
function trimLeft (token, greedy) {
if (!token || token.type !== 'html') return
console.log('trimming', token.value)
var rLeft = greedy ? /\s+$/g : /[\t\r ]*$/g
token.value = token.value.replace(rLeft, '')
}