div {…}
TODO: Look into making @rules its own hash-like class TODO: Look into whether selector should be child by default
# File lib/less/engine/nodes/element.rb, line 18
18: def initialize name = "", selector = ''
19: @name = name
20: @set, @imported = [], []
21: @rules = [] # Holds all the nodes under this element's hierarchy
22: @selector = Selector[selector.strip].new # descendant | child | adjacent
23: end
Add an arbitrary node to this element
# File lib/less/engine/nodes/element.rb, line 127
127: def << obj
128: if obj.kind_of? Node::Entity
129: obj.parent = self
130: @rules << obj
131: else
132: raise ArgumentError, "argument can't be a #{obj.class}"
133: end
134: end
# File lib/less/engine/nodes/element.rb, line 96
96: def == other
97: name == other.name
98: end
Select a child element TODO: Implement full selector syntax & merge with descend()
# File lib/less/engine/nodes/element.rb, line 86
86: def [] key
87: case key
88: when Entity
89: @rules.find {|i| i.eql? key }
90: when String
91: @rules.find {|i| i.to_s == key }
92: else raise ArgumentError
93: end
94: end
# File lib/less/engine/nodes/element.rb, line 25
25: def class?; name =~ /^\./ end
Same as above, except with a specific selector TODO: clean this up or implement it differently
# File lib/less/engine/nodes/element.rb, line 113
113: def descend selector, element
114: if selector.is_a? Child
115: s = self[element.name].selector
116: self[element.name] if s.is_a? Child or s.is_a? Descendant
117: elsif selector.is_a? Descendant
118: self[element.name]
119: else
120: self[element.name] if self[element.name].selector.class == selector.class
121: end
122: end
Traverse the whole tree, returning each leaf (recursive)
# File lib/less/engine/nodes/element.rb, line 186
186: def each path = [], &blk
187: elements.each do |element|
188: path << element
189: yield element, path if element.leaf?
190: element.each path, &blk
191: path.pop
192: end
193: self
194: end
# File lib/less/engine/nodes/element.rb, line 80
80: def elements; @rules.select {|r| r.kind_of? Element } end
# File lib/less/engine/nodes/element.rb, line 38
38: def empty?
39: @rules.empty?
40: end
# File lib/less/engine/nodes/element.rb, line 100
100: def eql? other
101: super and self.equiv? other
102: end
# File lib/less/engine/nodes/element.rb, line 104
104: def equiv? other
105: rules.size == other.rules.size and
106: !rules.zip(other.rules).map do |a, b|
107: a.to_css == b.to_css
108: end.include?(false)
109: end
# File lib/less/engine/nodes/element.rb, line 137
137: def first; elements.first end
Group similar rulesets together This is horrible, horrible code, but it’ll have to do until I find a proper way to do it.
# File lib/less/engine/nodes/element.rb, line 50
50: def group
51: elements = self.elements.reject {|e| e.is_a?(Mixin::Def) }
52: return self unless elements.size > 1
53:
54: stack, result, matched = elements.dup, [], false
55:
56: elements.each do
57: e = stack.first
58: result << e unless matched
59:
60: matched = stack[1..1].each do |ee|
61: if e.equiv? ee and e.elements.size == 0
62: self[e].set << ee
63: stack.shift
64: else
65: stack.shift
66: break false
67: end
68: end if stack.size > 1
69: end
70: @rules -= (elements - result)
71: self
72: end
# File lib/less/engine/nodes/element.rb, line 26
26: def id?; name =~ /^#/ end
Accessors for the different nodes in @rules
# File lib/less/engine/nodes/element.rb, line 77
77: def identifiers; @rules.select {|r| r.kind_of? Property } end
# File lib/less/engine/nodes/element.rb, line 196
196: def inspect depth = 0
197: indent = lambda {|i| '. ' * i }
198: put = lambda {|ary| ary.map {|i| indent[ depth + 1 ] + i.inspect } * "\n"}
199:
200: (root?? "\n" : "") + [
201: indent[ depth ] + self.to_s,
202: put[ variables ],
203: put[ properties ],
204: put[ mixins ],
205: elements.map {|i| i.inspect( depth + 1 ) } * "\n"
206: ].reject(&:empty?).join("\n") + "\n" + indent[ depth ]
207: end
# File lib/less/engine/nodes/element.rb, line 136
136: def last; elements.last end
# File lib/less/engine/nodes/element.rb, line 42
42: def leaf?
43: elements.empty?
44: end
# File lib/less/engine/nodes/element.rb, line 81
81: def mixins; @rules.select {|r| r.instance_of? Mixin::Call} end
Find the nearest node in the hierarchy or raise a NameError
# File lib/less/engine/nodes/element.rb, line 174
174: def nearest ident, type = nil
175: ary = type || ident =~ /^[.#]/ ? :elements : :variables
176: path.map do |node|
177: node.send(ary).find {|i| i.to_s == ident }
178: end.compact.first.tap do |result|
179: raise VariableNameError, ("#{ident} in #{self.to_s}") if result.nil? && type != :mixin
180: end
181: end
# File lib/less/engine/nodes/element.rb, line 82
82: def parameters; [] end
# File lib/less/engine/nodes/element.rb, line 78
78: def properties; @rules.select {|r| r.instance_of? Property } end
Top-most node?
# File lib/less/engine/nodes/element.rb, line 34
34: def root?
35: parent.nil?
36: end
# File lib/less/engine/nodes/element.rb, line 29
29: def tag?
30: not id? || class? || universal?
31: end
Entry point for the css conversion
# File lib/less/engine/nodes/element.rb, line 143
143: def to_css path = [], env = nil
144: path << @selector.to_css << name unless root?
145:
146: # puts "to_css env: #{env ? env.variables : "nil"}"
147: content = @rules.select do |r|
148: r.is_a?(Mixin::Call) || r.instance_of?(Property)
149: end.map do |i|
150: ' ' * 2 + i.to_css(env)
151: end.compact.reject(&:empty?) * "\n"
152:
153: content = content.include?("\n") ? "\n#{content}\n" : " #{content.strip} "
154:
155: ruleset = if is_a?(Mixin::Def)
156: content.strip
157: else
158: !content.strip.empty??
159: "#{[path.reject(&:empty?).join.strip,
160: *@set.map(&:name)].uniq * ', '} {#{content}}\n" : ""
161: end
162:
163: ruleset + elements.reject {|e| e.is_a?(Mixin::Def) }.map do |i|
164: i.to_css(path, env)
165: end.reject(&:empty?).join
166:
167: ensure
168: 2.times { path.pop }
169: end
# File lib/less/engine/nodes/element.rb, line 138
138: def to_s; root?? '*' : name end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.