css_parser
css_parser copied to clipboard
Fix parsing of multiline URL values for rule sets
Currently, parsing doesn't work properly for CSS rules with multiline URL values. Here's what happens:
font_face = "@font-face {\n src: url(https://example.com/font.woff2), \n url(https://example.com/font.woff); } \n"
CssParser::Parser.new.tap { |parser| parser.add_block!(font_face) }.to_s
# => "@font-face {\n://example.com/font.woff2),url(https: //example.com/font.woff);\n}\n"
This is due to the regex here: https://github.com/premailer/css_parser/blob/57647747cb8b9eb517b0e70949b3e23b777645f5/lib/css_parser/rule_set.rb#L501
Amending this regex to /(.[^:]*)\s*:\s*(?m:(.+))(;?\s*\Z)/i
(which essentially allows multiline capturing of a CSS rule's value capture group) fixes this issue.
I'm not a regex pro so I'm not sure if this is the best solution. Thoughts?