Skip to content

Commit 130ba50

Browse files
committed
1 parent 447292a commit 130ba50

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

lib/csscss.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
require "csscss/parser/border_width"
2525
require "csscss/parser/border_color"
2626
require "csscss/parser/border_style"
27+
require "csscss/parser/border_side"
2728

lib/csscss/parser/border_side.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
module Csscss
2+
module Parser
3+
module BorderSide
4+
extend Parser::Base
5+
6+
class Parser < Parslet::Parser
7+
include Common
8+
9+
attr_reader :side
10+
def initialize(side)
11+
@side = side.to_sym
12+
end
13+
14+
rule(:border_width) { BorderWidth::Parser.new.border_width_side }
15+
rule(:border_style) { BorderStyle::Parser.new.border_style_side }
16+
rule(:border_color) { BorderColor::Parser.new.border_color_side }
17+
18+
rule(:border_side) {
19+
(
20+
symbol("inherit") >> eof | (
21+
border_width.maybe.as(:width) >>
22+
border_style.maybe.as(:style) >>
23+
border_color.maybe.as(:color)
24+
).as(side)
25+
).as(:border_side)
26+
}
27+
28+
root(:border_side)
29+
end
30+
31+
class Transformer < Parslet::Transform
32+
extend Color::Transformer
33+
34+
class << self
35+
def transform_top(context); transform_side("top", context); end
36+
def transform_right(context); transform_side("right", context); end
37+
def transform_bottom(context); transform_side("bottom", context); end
38+
def transform_left(context); transform_side("left", context); end
39+
40+
def transform_side(side, context)
41+
width = context[:width]
42+
style = context[:style]
43+
color = context[:color]
44+
45+
[].tap do |declarations|
46+
declarations << Declaration.from_parser("border-#{side}-width", width) if width
47+
declarations << Declaration.from_parser("border-#{side}-style", style) if style
48+
declarations << Declaration.from_parser("border-#{side}-color", color.value) if color
49+
end
50+
end
51+
end
52+
53+
rule(border_side: simple(:inherit)) {[]}
54+
55+
[:top, :right, :bottom, :left].each do |side|
56+
rule(border_side: {
57+
side => {
58+
width:simple(:width),
59+
style:simple(:style),
60+
color:simple(:color)
61+
}
62+
}, &method("transform_#{side}"))
63+
end
64+
65+
end
66+
end
67+
end
68+
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require "test_helper"
2+
3+
module Csscss::Parser
4+
module BorderSide
5+
describe self do
6+
include CommonParserTests
7+
8+
before do
9+
@parser = Parser.new("top")
10+
end
11+
12+
def trans_top(s)
13+
Transformer.new.apply(@parser.parse(s))
14+
end
15+
alias_method :trans, :trans_top
16+
17+
def trans_bottom(s)
18+
Transformer.new.apply(Parser.new("bottom").parse(s))
19+
end
20+
21+
it "converts shorthand rules to longhand" do
22+
trans_top("thin").must_equal([
23+
dec("border-top-width", "thin")
24+
])
25+
26+
trans_bottom("rgb(1, 2, 3)").must_equal([
27+
dec("border-bottom-color", "rgb(1, 2, 3)")
28+
])
29+
30+
trans_top("thin solid #fff").must_equal([
31+
dec("border-top-width", "thin"),
32+
dec("border-top-style", "solid"),
33+
dec("border-top-color", "#fff")
34+
])
35+
36+
trans_bottom("thin solid #fff").must_equal([
37+
dec("border-bottom-width", "thin"),
38+
dec("border-bottom-style", "solid"),
39+
dec("border-bottom-color", "#fff")
40+
])
41+
end
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)