From 0cec8819ca4af49c26174fa43942db4e91738a68 Mon Sep 17 00:00:00 2001 From: Sophia Antipenko Date: Sat, 25 Feb 2017 00:02:07 +0200 Subject: [PATCH] Fix spaces handling inside url() (#8) --- lib/css-parser.js | 2 +- test/css-parser-test.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/css-parser.js b/lib/css-parser.js index 723e23c..b64035a 100644 --- a/lib/css-parser.js +++ b/lib/css-parser.js @@ -1,6 +1,6 @@ var embeddedRegexp = /data:(.*?);base64,/; var commentRegexp = /\/\*([\s\S]*?)\*\//g; -var urlsRegexp = /((?:@import\s+)?url\s*\(['"]?)(\S*?)(['"]?\s*\))|(@import\s+['"]?)([^;'"]+)/ig; +var urlsRegexp = /((?:@import\s+)?url\s*\(\s*['"]?)(.*?)(['"]?\s*\))|(@import\s+['"]?)([^;'"]+)/ig; function isEmbedded (src) { return embeddedRegexp.test(src); diff --git a/test/css-parser-test.js b/test/css-parser-test.js index 9b592a5..5c2bb53 100644 --- a/test/css-parser-test.js +++ b/test/css-parser-test.js @@ -58,6 +58,30 @@ describe('Parse css urls', function(){ urls.should.have.length(0); }); + it('should handle urls with spaces inside brackets', function () { + var text1 = '.image { background: url( "1.css"); } '; + var urls1 = parseCssUrls(text1); + urls1.should.be.instanceof(Array).and.have.lengthOf(1); + urls1.should.containEql('1.css'); + + var text2 = '.image { background: url( "2.css"); } '; + var urls2 = parseCssUrls(text2); + urls2.should.be.instanceof(Array).and.have.lengthOf(1); + urls2.should.containEql('2.css'); + + var text3 = ".image { background: url('3.css' ); } "; + var urls3 = parseCssUrls(text3); + urls3.should.be.instanceof(Array).and.have.lengthOf(1); + urls3.should.containEql('3.css'); + }); + + it('should handle urls with spaces inside quotes', function() { + var text = '.image { background: url(" a.css"); } '; + var urls = parseCssUrls(text); + urls.should.be.instanceof(Array).and.have.lengthOf(1); + urls.should.containEql(' a.css'); + }); + describe('comments', function() { it('should ignore comments and return empty array if there are only comments in text', function(){ var text = '\