Skip to content

filter division expression with parentheses (fix #4838) #4844

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

Merged
merged 5 commits into from
Feb 14, 2017
Merged
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
2 changes: 1 addition & 1 deletion src/compiler/parser/filter-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function parseFilters (exp: string): string {
p = exp.charAt(j)
if (p !== ' ') break
}
if (!p || !/[\w$]/.test(p)) {
if (!p || !/[\w).\]\+\-\_$]/.test(p)) {
inRegex = true
}
}
Expand Down
62 changes: 62 additions & 0 deletions test/unit/features/filter/filter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,68 @@ describe('Filters', () => {
expect(vm.$el.textContent).toBe(String(1 / 4))
})

it('handle division with parenthesis', () => {
const vm = new Vue({
data: { a: 20 },
template: `<div>{{ (a*2) / 5 | double }}</div>`,
filters: { double: v => v * 2 }
}).$mount()
expect(vm.$el.textContent).toBe(String(16))
})

it('handle division with dot', () => {
const vm = new Vue({
template: `<div>{{ 20. / 5 | double }}</div>`,
Copy link
Member

@HerringtonDarkholme HerringtonDarkholme Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about {{ a++ / 5 }} and {{ privateVar__ / 5}}?

Template string or object literal probably does not need to be considered, IMHO,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't considered + and - and also _ I have to include those too

Copy link
Contributor Author

@rixlabs rixlabs Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made a quick test checking every single exception with /[\w).\]\+\-\_$]/ and it's working.
Maybe there is a more generic way to do this

filters: { double: v => v * 2 }
}).$mount()
expect(vm.$el.textContent).toBe(String(8))
})

it('handle division with array values', () => {
const vm = new Vue({
data: { a: [20] },
template: `<div>{{ a[0] / 5 | double }}</div>`,
filters: { double: v => v * 2 }
}).$mount()
expect(vm.$el.textContent).toBe(String(8))
})

it('handle division with hash values', () => {
const vm = new Vue({
data: { a: { n: 20 }},
template: `<div>{{ a['n'] / 5 | double }}</div>`,
filters: { double: v => v * 2 }
}).$mount()
expect(vm.$el.textContent).toBe(String(8))
})

it('handle division with variable++', () => {
const vm = new Vue({
data: { a: 7 },
template: `<div>{{ a++ / 2 | double }}</div>`,
filters: { double: v => v * 2 }
}).$mount()
expect(vm.$el.textContent).toBe(String(7))
})

it('handle division with variable--', () => {
const vm = new Vue({
data: { a: 7 },
template: `<div>{{ a++ / 2 | double }}</div>`,
filters: { double: v => v * 2 }
}).$mount()
expect(vm.$el.textContent).toBe(String(7))
})

it('handle division with variable_', () => {
const vm = new Vue({
data: { a_: 8 },
template: `<div>{{ a_ / 2 | double }}</div>`,
filters: { double: v => v * 2 }
}).$mount()
expect(vm.$el.textContent).toBe(String(8))
})

it('arguments', () => {
const vm = new Vue({
template: `<div>{{ msg | add(a, 3) }}</div>`,
Expand Down