Browse Source

handle regex with pipes in filter parser

dev
Evan You 8 years ago
parent
commit
38b8cfdf96
  1. 13
      src/compiler/parser/filter-parser.js
  2. 15
      test/unit/features/filter/filter.spec.js

13
src/compiler/parser/filter-parser.js

@ -4,6 +4,7 @@ export function parseFilters (exp: string): string {
let inSingle = false
let inDouble = false
let inTemplateString = false
let inRegex = false
let curly = 0
let square = 0
let paren = 0
@ -14,14 +15,13 @@ export function parseFilters (exp: string): string {
prev = c
c = exp.charCodeAt(i)
if (inSingle) {
// check single quote
if (c === 0x27 && prev !== 0x5C) inSingle = !inSingle
if (c === 0x27 && prev !== 0x5C) inSingle = false
} else if (inDouble) {
// check double quote
if (c === 0x22 && prev !== 0x5C) inDouble = !inDouble
if (c === 0x22 && prev !== 0x5C) inDouble = false
} else if (inTemplateString) {
// check template literal
if (c === 0x60 && prev !== 0x5C) inTemplateString = !inTemplateString
if (c === 0x60 && prev !== 0x5C) inTemplateString = false
} else if (inRegex) {
if (c === 0x2f && prev !== 0x5C) inRegex = false
} else if (
c === 0x7C && // pipe
exp.charCodeAt(i + 1) !== 0x7C &&
@ -40,6 +40,7 @@ export function parseFilters (exp: string): string {
case 0x22: inDouble = true; break // "
case 0x27: inSingle = true; break // '
case 0x60: inTemplateString = true; break // `
case 0x2f: inRegex = true; break // /
case 0x28: paren++; break // (
case 0x29: paren--; break // )
case 0x5B: square++; break // [

15
test/unit/features/filter/filter.spec.js

@ -54,6 +54,21 @@ describe('Filters', () => {
expect(vm.$refs.bar).toBe(vm.$el)
})
it('handle regex with pipe', () => {
const vm = new Vue({
template: `<test ref="test" :pattern="/a|b\\// | identity"></test>`,
filters: { identity: v => v },
components: {
test: {
props: ['pattern'],
template: '<div></div>'
}
}
}).$mount()
expect(vm.$refs.test.pattern instanceof RegExp).toBe(true)
expect(vm.$refs.test.pattern.toString()).toBe('/a|b\\//')
})
it('arguments', () => {
const vm = new Vue({
template: `<div>{{ msg | add(a, 3) }}</div>`,

Loading…
Cancel
Save