From a785c5284972db75afca35e4d2cd5cbfe36b3a35 Mon Sep 17 00:00:00 2001 From: Michael Holroyd Date: Tue, 3 Dec 2013 18:08:44 -0500 Subject: [PATCH] Ignore colons inside of url(.*) when parsing css Parsing by css is totally wrong, but this will at least not break on `url(http://whatever);` anymore! Closes issue #315. --- lib/api/css.js | 9 +++++---- test/api.css.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/api/css.js b/lib/api/css.js index a230dfe0cf..a91a18e86d 100644 --- a/lib/api/css.js +++ b/lib/api/css.js @@ -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; }, {}); } diff --git a/test/api.css.js b/test/api.css.js index 564d95a125..6e1ce4b6b0 100644 --- a/test/api.css.js +++ b/test/api.css.js @@ -27,6 +27,21 @@ describe('$(...)', function() { expect(el.attr('style')).to.equal('margin: 0;'); }); + it('(prop): should not mangle embedded urls', function() { + var el = $('
  • '); + expect(el.css('background-image')).to.equal('url(http://example.com/img.png)'); + }); + + it('(prop): should ignore blank properties', function() { + var el = $('
  • '); + expect(el.css()).to.eql({color:"#aaa"}); + }); + + it('(prop): should ignore blank values', function() { + var el = $('
  • '); + expect(el.css()).to.eql({position:"absolute"}); + }); + describe('(prop, function):', function() { beforeEach(function() { this.$el = $('
    ');