-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rebuild CSS values parser to be more standards compliant
CSS values parsing was pretty basic before. The CSS 2 graamar spec doesn't actually spell out exactly how quoting should work, but it's referenced elsewhere in the document, so this commit does its best to implement quote parsing. It also implements escaping as per the spec. Fixes #165
- Loading branch information
Showing
5 changed files
with
160 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module Prawn::SVG::CSS | ||
class ValuesParser | ||
class << self | ||
def parse(values) | ||
result = [] | ||
|
||
while values | ||
value, remainder = parse_next(values) | ||
break unless value | ||
|
||
result << value | ||
values = remainder | ||
end | ||
|
||
result | ||
end | ||
|
||
private | ||
|
||
def parse_next(values) | ||
values = values.strip | ||
return if values.empty? | ||
|
||
if (matches = values.match(/\A([a-z-]+)\(\s*(.+)/i)) | ||
parse_function_call(matches[1].downcase, matches[2]) | ||
else | ||
values.split(/\s+/, 2) | ||
end | ||
end | ||
|
||
# Note this does not support space-separated arguments. | ||
# I don't think CSS 2 has any, but in case it does here is the place to add them. | ||
def parse_function_call(name, rest) | ||
arguments = [] | ||
in_quote = nil | ||
in_escape = false | ||
current = '' | ||
|
||
rest.chars.each.with_index do |char, index| | ||
if in_escape | ||
current << char | ||
in_escape = false | ||
elsif %w[" '].include?(char) | ||
if in_quote == char | ||
in_quote = nil | ||
elsif in_quote.nil? | ||
in_quote = char | ||
else | ||
current << char | ||
end | ||
elsif char == '\\' | ||
in_escape = true | ||
elsif in_quote.nil? && char == ',' | ||
arguments << current.strip | ||
current = '' | ||
elsif in_quote.nil? && char == ')' | ||
arguments << current.strip | ||
return [[name, arguments], rest[index + 1..]] | ||
else | ||
current << char | ||
end | ||
end | ||
|
||
[rest, nil] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Prawn::SVG::CSS::ValuesParser do | ||
it 'parses specified values' do | ||
values = 'hello world url("#myid") no-quote(very good) escaping(")\\")ok") rgb( 1,4, 5 )' | ||
|
||
expect(described_class.parse(values)).to eq [ | ||
'hello', | ||
'world', | ||
['url', ['#myid']], | ||
['no-quote', ['very good']], | ||
['escaping', [')")ok']], | ||
['rgb', %w[1 4 5]] | ||
] | ||
end | ||
end |