Skip to content

Allow white spaces inside object literal. #3214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/parsers/directive.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { toNumber, stripQuotes } from '../util/index'
import Cache from '../cache'
import { warn } from '../util/index'

const cache = new Cache(1000)
const filterTokenRE = /[^\s'"]+|'[^']*'|"[^"]*"/g
const filterTokenRE = /(\[.*\])|[^\s'"]+|'[^']*'|"[^"]*"/g
const reservedArgRE = /^in$|^-?\d+/

/**
Expand All @@ -25,7 +26,39 @@ function pushFilter () {
var tokens = exp.match(filterTokenRE)
filter.name = tokens[0]
if (tokens.length > 1) {
filter.args = tokens.slice(1).map(processFilterArg)
tokens.slice(1).map(function (token) {
if (filter.args === undefined) {
filter.args = []
}

if (token[0] === '[') {
try {
/* eslint-disable no-eval */
window.eval(token).map(function (arg) {
/* eslint-disable no-eval */
filter.args.push(processFilterArg(JSON.stringify(arg).replace(/"/g, '')))
})
} catch (e) {
if (process.env.NODE_ENV !== 'production') {
/* istanbul ignore if */
if (e.toString().match(/unsafe-eval|CSP/)) {
warn(
'It seems you are using the default build of Vue.js in an environment ' +
'with Content Security Policy that prohibits unsafe-eval. ' +
'Use the CSP-compliant build instead: ' +
'http://vuejs.org/guide/installation.html#CSP-compliant-build'
)
} else {
warn(
'Invalid filter argument: ' + token
)
}
}
}
} else {
filter.args.push(processFilterArg(token))
}
})
}
}
if (filter) {
Expand Down
24 changes: 24 additions & 0 deletions test/unit/specs/parsers/directive_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ describe('Directive Parser', function () {
expect(res.filters[0].args).toBeUndefined()
})

it('object literal with mixed spaces', function () {
var res1 = parse('t | f [{u: true}]')
var res2 = parse('t | f [{u : true}]')
var res3 = parse('t | f [{ u: true }]')
var res4 = parse('t | f [{u: true }]')
var res5 = parse('t | f [{ u:true}]')

expect(res1.filters[0].args[0].value).toBe('{u:true}')
expect(res2.filters[0].args[0].value).toBe('{u:true}')
expect(res3.filters[0].args[0].value).toBe('{u:true}')
expect(res4.filters[0].args[0].value).toBe('{u:true}')
expect(res5.filters[0].args[0].value).toBe('{u:true}')

var res6 = parse('t | f1 [[1, 2, 3], { a: 1 }, { u : true, b: [1, 2] }] | f2 \'abc\'')
expect(res6.filters[0].args[0].value).toBe('[1,2,3]')
expect(res6.filters[0].args[1].value).toBe('{a:1}')
expect(res6.filters[0].args[2].value).toBe('{u:true,b:[1,2]}')
expect(res6.filters[1].args[0].value).toBe('abc')

// Try out broken filter
parse('t | f1 [[1, 2, 3],')
expect('Invalid filter argument: [[1, 2, 3]').toHaveBeenWarned()
})

it('escape string', function () {
var res = parse("'a\\'b' | test")
expect(res.expression).toBe("'a\\'b'")
Expand Down