Skip to content
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

Report regexp to split formatters. Fixed #432 #562

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
41 changes: 41 additions & 0 deletions spec/rivets/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@ describe('Rivets.Binding', function() {
binding.binder.routine.should.equal(rivets.binders.text)
})

describe('with formatters', function() {
it('register all formatters', function() {

valueInput = document.createElement('input')
valueInput.setAttribute('type','text')
valueInput.setAttribute('data-value', "obj.name | awesome | radical | totally")

view = rivets.bind(valueInput, {obj: { name: 'nothing' }})
binding = view.bindings[0]

binding.formatters.should.be.eql(['awesome', 'radical', 'totally'])
})

it('allows arguments with pipes', function() {

valueInput = document.createElement('input')
valueInput.setAttribute('type','text')
valueInput.setAttribute('data-value', "obj.name | awesome | totally 'arg | with || pipes'")

view = rivets.bind(valueInput, {obj: { name: 'nothing' }})
binding = view.bindings[0]

binding.formatters.should.be.eql(['awesome', "totally 'arg | with || pipes'"])
})
})

describe('bind()', function() {
it('subscribes to the model for changes via the adapter', function() {
sinon.spy(adapter, 'observe')
Expand Down Expand Up @@ -273,6 +299,21 @@ describe('Rivets.Binding', function() {
binding.formattedValue('jacket').should.equal('super awesome jacket')
})
})

describe('with a formatter string with pipes in argument', function() {
beforeEach(function () {

view.formatters.totally = function (value, prefix) {
return prefix + ' totally ' + value
}

binding.formatters.push("totally 'arg | with || pipes'")
})

it('applies the formatter with arguments with pipes', function () {
binding.formattedValue('jacket').should.equal('arg | with || pipes totally jacket')
})
})
})

describe('getValue()', function() {
Expand Down
2 changes: 1 addition & 1 deletion src/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class View {
}

buildBinding(binding, node, type, declaration) {
let pipes = declaration.split('|').map(pipe => {
let pipes = declaration.match(/((?:'[^']*')*(?:(?:[^\|']+(?:'[^']*')*[^\|']*)+|[^\|]+))|^$/g).map(pipe => {
Copy link
Contributor

Choose a reason for hiding this comment

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

replace with .map(pipe => pipe.trim())

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, thanks

return pipe.trim()
})

Expand Down