mv whiteSpaceCtrl to a separate file

This commit is contained in:
harttle
2017-10-30 00:36:21 +08:00
parent 8c77fc5a3d
commit 502426f621
2 changed files with 79 additions and 73 deletions
+35
View File
@@ -0,0 +1,35 @@
const _ = require('./util/underscore.js')
function whiteSpaceCtrl (tokens, options) {
options = _.assign({ greedy: true }, options)
var inRaw = false
tokens.forEach((token, i) => {
if (!inRaw && (token.trim_left || options.trim_left)) {
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)) {
trimRight(tokens[i + 1], options.greedy)
}
})
}
function trimLeft (token, greedy) {
if (!token || token.type !== 'html') return
var rLeft = greedy ? /\s+$/g : /[\t\r ]*$/g
token.value = token.value.replace(rLeft, '')
}
function trimRight (token, greedy) {
if (!token || token.type !== 'html') return
var rRight = greedy ? /^\s+/g : /^[\t\r ]*\n?/g
token.value = token.value.replace(rRight, '')
}
module.exports = whiteSpaceCtrl