Skip to content

Commit d2100bc

Browse files
committed
Implements derivative declarations
A derivative declaration is one that comes from a parent. Usually parsed from a shorthand declaration.
1 parent be3ce9b commit d2100bc

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed

lib/csscss/types.rb

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,62 @@
11
module Csscss
2-
class Declaration < Struct.new(:property, :value)
2+
class Declaration < Struct.new(:property, :value, :parent)
33
def self.from_csspool(dec)
44
new(dec.property.to_s, dec.expressions.join(" "))
55
end
66

7+
def self.from_parser(property, value)
8+
new(property.to_s, value.to_s.downcase.strip)
9+
end
10+
11+
def derivative?
12+
!parent.nil?
13+
end
14+
15+
def without_parent
16+
if derivative?
17+
dup.tap do |duped|
18+
duped.parent = nil
19+
end
20+
else
21+
self
22+
end
23+
end
24+
25+
def ==(other)
26+
if derivative?
27+
without_parent == other
28+
else
29+
super(other.respond_to?(:without_parent) ? other.without_parent : other)
30+
end
31+
end
32+
33+
def hash
34+
derivative? ? without_parent.hash : super
35+
end
36+
37+
def eql?(other)
38+
hash == other.hash
39+
end
40+
741
def <=>(other)
842
property <=> other.property
943
end
1044

45+
def >(other)
46+
self == other.parent
47+
end
48+
49+
def <(other)
50+
other > self
51+
end
52+
1153
def to_s
12-
"#{property}: #{value}"
54+
base = "#{property}: #{value}"
55+
if parent
56+
"#{base} (parent: #{parent})"
57+
else
58+
base
59+
end
1360
end
1461

1562
def inspect

test/csscss/types_test.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require "test_helper"
2+
3+
module Csscss
4+
describe Declaration do
5+
it "< and > checks parent" do
6+
dec1 = Declaration.new("background", "#fff")
7+
dec2 = Declaration.new("background", "#fff")
8+
dec3 = Declaration.new("background-color", "#fff", dec1)
9+
dec4 = Declaration.new("background-color", "#fff", nil)
10+
11+
dec1.must_be :>, dec3
12+
dec3.must_be :<, dec1
13+
14+
dec1.wont_be :>, dec2
15+
dec1.wont_be :>, dec4
16+
dec4.wont_be :>, dec1
17+
end
18+
19+
it "is a derivative if it has parent" do
20+
dec1 = Declaration.new("background", "#fff")
21+
dec1.wont_be :derivative?
22+
Declaration.new("background-color", "#fff", dec1).must_be :derivative?
23+
end
24+
25+
it "ignores parents when checking equality" do
26+
dec1 = Declaration.new("background", "#fff")
27+
dec2 = Declaration.new("background-color", "#fff", dec1)
28+
dec3 = Declaration.new("background-color", "#fff", nil)
29+
30+
dec1.wont_equal dec2
31+
dec2.wont_equal dec1
32+
33+
dec2.must_equal dec3
34+
dec3.must_equal dec2
35+
36+
dec2.hash.must_equal dec3.hash
37+
dec3.hash.must_equal dec2.hash
38+
dec3.hash.wont_equal dec1.hash
39+
40+
dec2.must_be :eql?, dec3
41+
dec3.must_be :eql?, dec2
42+
dec2.wont_be :eql?, dec1
43+
end
44+
45+
it "derivatives are handled correctly in a hash" do
46+
dec1 = Declaration.new("background", "#fff")
47+
dec2 = Declaration.new("background-color", "#fff", dec1)
48+
dec3 = Declaration.new("background-color", "#fff", nil)
49+
50+
h = {}
51+
h[dec2] = false
52+
h[dec3] = true
53+
54+
h.keys.size.must_equal 1
55+
h[dec2].must_equal true
56+
h[dec3].must_equal true
57+
end
58+
end
59+
end

0 commit comments

Comments
 (0)