Other attributes removed when writing data into an attribute #20

Merged
merged 3 commits into from Dec 12, 2011

Conversation

Projects
None yet
2 participants
Contributor

robashton commented Dec 7, 2011

edit

Okay, so the bug was actually a bit less edge casey - given any element with any attributes in it, if you choose an attribute as a target for writing into, the other attributes after that one will be removed from the element.

Original

I don't know if this is desired behaviour or not, so I've not tried to fix it - but I figured a pull request with a test would be the best way of describing the behaviour.

Given input that looks like this

<img src="" data-bind="url"></img>

With a configuration that looks like this

{ "url": ["data-bind", "src"]}

That is, as I understand it: "Take where 'data-bind' value is 'url' and replace the attribute 'src' with the value of 'url'

I get the output

<img src="http://example.com"></img>

Where I'd expect

<img src="http://example.com" data-bind="url"></img>

In my own templates I'm using class based matching, and the classes are used for styling as well as binding (that is, the designer doesn't care about binding).

Is this a bug or not? I'm happy to fix it if it is.

Contributor

robashton commented Dec 7, 2011

Oh and ignore what I've done with data-bind, I just did that because I wanted to see how your test harness worked.

Contributor

robashton commented Dec 12, 2011

Okay, so I've added a fix for this as on further use I decided it was definitely a bug.

The problem was with the regex which attempted to find the attribute being filled

new RegExp(map[key][1] + '\\s*=\\s*["|\'](.*)["|\']')

The problem is this will match all content until the final " or ' that is found in the expression

new RegExp(map[key][1] + '\\s*=\\s*["|\'](([^"\']*))["|\']')

Will only match up until the first " or ' that is found.

Clearly this will still fall over on edge cases like

<a href="#" onclick="alert('hello world');">

But that should just serve as a reminder to people not to do such things.

@0x00A 0x00A pushed a commit that referenced this pull request Dec 12, 2011

Paolo Fragomeni Merge pull request #20 from robashton/master
Other attributes removed when writing data into an attribute
2482486

@0x00A 0x00A merged commit 2482486 into flatiron:master Dec 12, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment