Skip to content

Commit

Permalink
Ignore colons inside of url(.*) when parsing css
Browse files Browse the repository at this point in the history
Parsing by css is totally wrong, but this will at least not break on `url(http://whatever);` anymore! Closes issue #315.
  • Loading branch information
Meekohi authored and jugglinmike committed Dec 5, 2013
1 parent 37e802c commit a785c52
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/api/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ function parse(styles) {
if (!styles) return {};

return styles
.split(/\s*;\s*/)
.split(';')
.reduce(function(obj, str){
var parts = str.split(/\s*:\s*/);
if (!parts[0]) return obj;
obj[parts[0]] = parts[1];
var n = str.indexOf(":");
// skip if there is no :, or if it is the first/last character
if (n < 1 || n === str.length-1) return obj;
obj[str.slice(0,n).trim()] = str.slice(n+1).trim();
return obj;
}, {});
}
15 changes: 15 additions & 0 deletions test/api.css.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ describe('$(...)', function() {
expect(el.attr('style')).to.equal('margin: 0;');
});

it('(prop): should not mangle embedded urls', function() {
var el = $('<li style="background-image:url(http://example.com/img.png);">');
expect(el.css('background-image')).to.equal('url(http://example.com/img.png)');
});

it('(prop): should ignore blank properties', function() {
var el = $('<li style=":#ccc;color:#aaa;">');
expect(el.css()).to.eql({color:"#aaa"});
});

it('(prop): should ignore blank values', function() {
var el = $('<li style="color:;position:absolute;">');
expect(el.css()).to.eql({position:"absolute"});
});

describe('(prop, function):', function() {
beforeEach(function() {
this.$el = $('<div style="margin: 0;"></div><div style="margin: 0;"></div><div style="margin: 0;">');
Expand Down

0 comments on commit a785c52

Please sign in to comment.