Class: Parselly::Node
- Inherits:
-
Object
- Object
- Parselly::Node
- Includes:
- Enumerable
- Defined in:
- lib/parselly/node.rb
Overview
Represents a node in the Abstract Syntax Tree (AST) for CSS selectors.
Defined Under Namespace
Classes: ChildList
Constant Summary collapse
- SIMPLE_SELECTOR_TYPES =
Set[ :type_selector, :universal_selector, :id_selector, :class_selector, :attribute_selector, :pseudo_class, :pseudo_function, :pseudo_element, :pseudo_element_function ].freeze
- COMBINATOR_TYPES =
{ child_combinator: '>', adjacent_combinator: '+', sibling_combinator: '~', descendant_combinator: ' ', column_combinator: '||' }.freeze
- SPECIFICITY_ZERO_PSEUDO_FUNCTIONS =
Set['where'].freeze
- SPECIFICITY_MAX_ARGUMENT_PSEUDO_FUNCTIONS =
Set['is', 'not', 'has'].freeze
- NTH_PSEUDO_FUNCTIONS =
Set['nth-child', 'nth-last-child', 'nth-of-type', 'nth-last-of-type', 'nth-col', 'nth-last-col'].freeze
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#modifier ⇒ Object
Returns the value of attribute modifier.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#position ⇒ Object
Returns the value of attribute position.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#quote ⇒ Object
Returns the value of attribute quote.
-
#raw_value ⇒ Object
Returns the value of attribute raw_value.
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #add_child(node) ⇒ Object
- #ancestors ⇒ Object
- #as_json ⇒ Object
- #attribute? ⇒ Boolean
- #attribute_selectors ⇒ Object
- #attributes ⇒ Object
- #classes ⇒ Object
- #combinators ⇒ Object
- #complex_selector? ⇒ Boolean
- #compound_selector? ⇒ Boolean
- #deconstruct_keys(keys) ⇒ Object
- #descendants ⇒ Object
- #dup_tree ⇒ Object (also: #deep_dup)
- #each ⇒ Object
- #find_all(type) ⇒ Object
- #freeze_tree ⇒ Object
- #id ⇒ Object
- #id? ⇒ Boolean
- #ids ⇒ Object
-
#initialize(type, value = nil, position = {}, raw_value: nil, line: nil, column: nil, offset: nil, start_line: nil, start_column: nil, start_offset: nil, end_line: nil, end_column: nil, end_offset: nil, namespace: nil, quote: nil, modifier: nil, prefix: nil) ⇒ Node
constructor
A new instance of Node.
- #insert_after(reference_child, new_child) ⇒ Object
- #insert_before(reference_child, new_child) ⇒ Object
- #insert_child(index, node) ⇒ Object
- #inspect ⇒ Object
- #pseudo_class_names ⇒ Object
- #pseudo_classes ⇒ Object
- #pseudo_element_names ⇒ Object
- #pseudo_function_names ⇒ Object
- #remove_child(node_or_index) ⇒ Object
- #replace_child(index, new_node) ⇒ Object
- #selector_list? ⇒ Boolean
- #siblings ⇒ Object
- #specificity ⇒ Object
- #to_h ⇒ Object
- #to_selector(mode: :normalized) ⇒ Object
- #to_tree(indent = 0) ⇒ Object
- #type_names ⇒ Object
- #type_selector? ⇒ Boolean
- #type_selectors ⇒ Object
Constructor Details
#initialize(type, value = nil, position = {}, raw_value: nil, line: nil, column: nil, offset: nil, start_line: nil, start_column: nil, start_offset: nil, end_line: nil, end_column: nil, end_offset: nil, namespace: nil, quote: nil, modifier: nil, prefix: nil) ⇒ Node
Returns a new instance of Node.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/parselly/node.rb', line 100 def initialize(type, value = nil, position = {}, raw_value: nil, line: nil, column: nil, offset: nil, start_line: nil, start_column: nil, start_offset: nil, end_line: nil, end_column: nil, end_offset: nil, namespace: nil, quote: nil, modifier: nil, prefix: nil) @type = type @value = value @raw_value = raw_value.nil? ? value : raw_value @children = ChildList.new(self) @parent = nil @namespace = namespace @quote = quote @modifier = modifier @prefix = prefix unless position.nil? || position.is_a?(Hash) raise ArgumentError, 'position must be a Hash' end resolved_position = position ? position.dup : {} resolved_position[:line] = line unless line.nil? resolved_position[:column] = column unless column.nil? resolved_position[:offset] = offset unless offset.nil? resolved_position[:start_line] = start_line unless start_line.nil? resolved_position[:start_column] = start_column unless start_column.nil? resolved_position[:start_offset] = start_offset unless start_offset.nil? resolved_position[:end_line] = end_line unless end_line.nil? resolved_position[:end_column] = end_column unless end_column.nil? resolved_position[:end_offset] = end_offset unless end_offset.nil? @position = resolved_position @descendants_cache = nil end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
98 99 100 |
# File 'lib/parselly/node.rb', line 98 def children @children end |
#modifier ⇒ Object
Returns the value of attribute modifier.
97 98 99 |
# File 'lib/parselly/node.rb', line 97 def modifier @modifier end |
#namespace ⇒ Object
Returns the value of attribute namespace.
97 98 99 |
# File 'lib/parselly/node.rb', line 97 def namespace @namespace end |
#parent ⇒ Object
Returns the value of attribute parent.
97 98 99 |
# File 'lib/parselly/node.rb', line 97 def parent @parent end |
#position ⇒ Object
Returns the value of attribute position.
97 98 99 |
# File 'lib/parselly/node.rb', line 97 def position @position end |
#prefix ⇒ Object
Returns the value of attribute prefix.
97 98 99 |
# File 'lib/parselly/node.rb', line 97 def prefix @prefix end |
#quote ⇒ Object
Returns the value of attribute quote.
97 98 99 |
# File 'lib/parselly/node.rb', line 97 def quote @quote end |
#raw_value ⇒ Object
Returns the value of attribute raw_value.
97 98 99 |
# File 'lib/parselly/node.rb', line 97 def raw_value @raw_value end |
#type ⇒ Object
Returns the value of attribute type.
97 98 99 |
# File 'lib/parselly/node.rb', line 97 def type @type end |
#value ⇒ Object
Returns the value of attribute value.
97 98 99 |
# File 'lib/parselly/node.rb', line 97 def value @value end |
Instance Method Details
#add_child(node) ⇒ Object
136 137 138 139 140 141 |
# File 'lib/parselly/node.rb', line 136 def add_child(node) return nil if node.nil? @children << node node end |
#ancestors ⇒ Object
182 183 184 185 186 187 188 189 190 |
# File 'lib/parselly/node.rb', line 182 def ancestors result = [] node = parent while node result << node node = node.parent end result end |
#as_json ⇒ Object
424 425 426 |
# File 'lib/parselly/node.rb', line 424 def as_json(*) to_h end |
#attribute? ⇒ Boolean
311 312 313 |
# File 'lib/parselly/node.rb', line 311 def attribute? any? { |node| node.type == :attribute_selector } end |
#attribute_selectors ⇒ Object
319 320 321 |
# File 'lib/parselly/node.rb', line 319 def attribute_selectors attribute_selector_nodes.map { |node| extract_attribute_node(node) } end |
#attributes ⇒ Object
315 316 317 |
# File 'lib/parselly/node.rb', line 315 def attributes attribute_selector_nodes.map { |node| extract_attribute_info(node) } end |
#classes ⇒ Object
307 308 309 |
# File 'lib/parselly/node.rb', line 307 def classes each.with_object([]) { |node, result| result << node.value if node.type == :class_selector } end |
#combinators ⇒ Object
363 364 365 366 367 368 369 |
# File 'lib/parselly/node.rb', line 363 def combinators each.with_object([]) do |node, result| next unless COMBINATOR_TYPES.key?(node.type) result << { type: node.type, value: node.value, position: node.position } end end |
#complex_selector? ⇒ Boolean
375 376 377 |
# File 'lib/parselly/node.rb', line 375 def complex_selector? type == :selector || any? { |node| COMBINATOR_TYPES.key?(node.type) } end |
#compound_selector? ⇒ Boolean
379 380 381 382 383 384 385 386 387 388 |
# File 'lib/parselly/node.rb', line 379 def compound_selector? case type when :selector_list children.size == 1 && children.first.compound_selector? when :simple_selector_sequence children.count { |child| SIMPLE_SELECTOR_TYPES.include?(child.type) } > 1 else false end end |
#deconstruct_keys(keys) ⇒ Object
428 429 430 431 432 433 |
# File 'lib/parselly/node.rb', line 428 def deconstruct_keys(keys) hash = to_h return hash if keys.nil? keys.each_with_object({}) { |key, result| result[key] = hash[key] if hash.key?(key) } end |
#descendants ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/parselly/node.rb', line 192 def descendants return @descendants_cache if @descendants_cache @descendants_cache = [] queue = @children.to_a index = 0 while index < queue.length node = queue[index] @descendants_cache << node queue.concat(node.children) unless node.children.empty? index += 1 end @descendants_cache end |
#dup_tree ⇒ Object Also known as: deep_dup
441 442 443 444 445 446 447 448 449 450 451 452 453 454 |
# File 'lib/parselly/node.rb', line 441 def dup_tree duplicate = self.class.new( type, value, position.dup, raw_value: raw_value, namespace: namespace, quote: quote, modifier: modifier, prefix: prefix ) children.each { |child| duplicate.add_child(child.dup_tree) } duplicate end |
#each ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/parselly/node.rb', line 207 def each return enum_for(:each) unless block_given? stack = [self] until stack.empty? node = stack.pop yield node stack.concat(node.children.reverse) unless node.children.empty? end self end |
#find_all(type) ⇒ Object
220 221 222 |
# File 'lib/parselly/node.rb', line 220 def find_all(type) each.select { |node| node.type == type } end |
#freeze_tree ⇒ Object
435 436 437 438 439 |
# File 'lib/parselly/node.rb', line 435 def freeze_tree children.each(&:freeze_tree) children.freeze freeze end |
#id ⇒ Object
299 300 301 |
# File 'lib/parselly/node.rb', line 299 def id ids.first end |
#id? ⇒ Boolean
295 296 297 |
# File 'lib/parselly/node.rb', line 295 def id? any? { |node| node.type == :id_selector } end |
#ids ⇒ Object
303 304 305 |
# File 'lib/parselly/node.rb', line 303 def ids each.with_object([]) { |node, result| result << node.value if node.type == :id_selector } end |
#insert_after(reference_child, new_child) ⇒ Object
175 176 177 178 179 180 |
# File 'lib/parselly/node.rb', line 175 def insert_after(reference_child, new_child) index = @children.index(reference_child) return nil unless index insert_child(index + 1, new_child) end |
#insert_before(reference_child, new_child) ⇒ Object
168 169 170 171 172 173 |
# File 'lib/parselly/node.rb', line 168 def insert_before(reference_child, new_child) index = @children.index(reference_child) return nil unless index insert_child(index, new_child) end |
#insert_child(index, node) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/parselly/node.rb', line 150 def insert_child(index, node) return nil if node.nil? return nil if index.negative? || index > @children.size @children.insert(index, node) node end |
#inspect ⇒ Object
240 241 242 |
# File 'lib/parselly/node.rb', line 240 def inspect "#<#{self.class.name} type=#{type} value=#{value.inspect} children=#{children.size}>" end |
#pseudo_class_names ⇒ Object
331 332 333 |
# File 'lib/parselly/node.rb', line 331 def pseudo_class_names each.with_object([]) { |node, result| result << node.value if node.type == :pseudo_class } end |
#pseudo_classes ⇒ Object
323 324 325 326 327 328 329 |
# File 'lib/parselly/node.rb', line 323 def pseudo_classes each.with_object([]) do |node, result| if [:pseudo_class, :pseudo_element, :pseudo_function, :pseudo_element_function].include?(node.type) result << node.value end end end |
#pseudo_element_names ⇒ Object
335 336 337 338 339 |
# File 'lib/parselly/node.rb', line 335 def pseudo_element_names each.with_object([]) do |node, result| result << node.value if [:pseudo_element, :pseudo_element_function].include?(node.type) end end |
#pseudo_function_names ⇒ Object
341 342 343 |
# File 'lib/parselly/node.rb', line 341 def pseudo_function_names each.with_object([]) { |node, result| result << node.value if node.type == :pseudo_function } end |
#remove_child(node_or_index) ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/parselly/node.rb', line 158 def remove_child(node_or_index) if node_or_index.is_a?(Integer) return nil if node_or_index.negative? || node_or_index >= @children.size return @children.delete_at(node_or_index) end @children.delete(node_or_index) end |
#replace_child(index, new_node) ⇒ Object
143 144 145 146 147 148 |
# File 'lib/parselly/node.rb', line 143 def replace_child(index, new_node) return nil if new_node.nil? return nil if index.negative? || index >= @children.size @children[index] = new_node end |
#selector_list? ⇒ Boolean
371 372 373 |
# File 'lib/parselly/node.rb', line 371 def selector_list? type == :selector_list end |
#siblings ⇒ Object
224 225 226 227 228 |
# File 'lib/parselly/node.rb', line 224 def siblings return [] unless parent parent.children.reject { |child| child == self } end |
#specificity ⇒ Object
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/parselly/node.rb', line 390 def specificity case type when :selector_list children.map(&:specificity).max || [0, 0, 0] when :selector, :simple_selector_sequence children.reduce([0, 0, 0]) { |sum, child| add_specificity(sum, child.specificity) } when :id_selector [1, 0, 0] when :class_selector, :attribute_selector, :pseudo_class [0, 1, 0] when :type_selector, :pseudo_element, :pseudo_element_function [0, 0, 1] when :pseudo_function pseudo_function_specificity else [0, 0, 0] end end |
#to_h ⇒ Object
409 410 411 412 413 414 415 416 417 418 419 420 421 422 |
# File 'lib/parselly/node.rb', line 409 def to_h hash = { type: type, value: value, raw_value: raw_value, namespace: namespace, quote: quote, modifier: modifier, prefix: prefix, position: position, children: children.map(&:to_h) } hash.delete_if { |key, val| key != :children && (val.nil? || val == {}) } end |
#to_selector(mode: :normalized) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/parselly/node.rb', line 244 def to_selector(mode: :normalized) validate_selector_mode!(mode) case type when :selector_list children.map { |child| child.to_selector(mode: mode) }.join(', ') when :selector build_selector(mode) when :simple_selector_sequence children.map { |child| child.to_selector(mode: mode) }.join when :type_selector, :universal_selector selector_name(mode) when :id_selector "##{selector_identifier(mode)}" when :class_selector ".#{selector_identifier(mode)}" when :attribute_selector build_attribute_selector(mode) when :pseudo_class "#{selector_prefix(mode, ':')}#{selector_identifier(mode)}" when :pseudo_element "#{selector_prefix(mode, '::')}#{selector_identifier(mode)}" when :pseudo_function "#{selector_prefix(mode, ':')}#{selector_identifier(mode)}(#{children.map { |child| child.to_selector(mode: mode) }.join})" when :pseudo_element_function "#{selector_prefix(mode, '::')}#{selector_identifier(mode)}(#{children.map { |child| child.to_selector(mode: mode) }.join})" when :child_combinator ' > ' when :adjacent_combinator ' + ' when :sibling_combinator ' ~ ' when :descendant_combinator ' ' when :column_combinator ' || ' when :nth_selector_argument "#{children[0].to_selector(mode: mode)} of #{children[1].to_selector(mode: mode)}" when :an_plus_b value.to_s when :argument argument_selector(mode) when :attribute, :value, :equal_operator, :includes_operator, :dashmatch_operator, :prefixmatch_operator, :suffixmatch_operator, :substringmatch_operator value.to_s else children.map { |child| child.to_selector(mode: mode) }.join end end |
#to_tree(indent = 0) ⇒ Object
230 231 232 233 234 235 236 237 238 |
# File 'lib/parselly/node.rb', line 230 def to_tree(indent = 0) lines = [] prefix = ' ' * indent pos_info = position.empty? ? '' : " [#{position[:line]}:#{position[:column]}]" lines << "#{prefix}#{type}#{"(#{value.inspect})" if value}#{pos_info}" children.each { |child| lines << child.to_tree(indent + 1) } lines.join("\n") end |
#type_names ⇒ Object
349 350 351 |
# File 'lib/parselly/node.rb', line 349 def type_names each.with_object([]) { |node, result| result << node.value if node.type == :type_selector } end |
#type_selector? ⇒ Boolean
345 346 347 |
# File 'lib/parselly/node.rb', line 345 def type_selector? any? { |node| node.type == :type_selector } end |
#type_selectors ⇒ Object
353 354 355 356 357 358 359 360 361 |
# File 'lib/parselly/node.rb', line 353 def type_selectors each.with_object([]) do |node, result| next unless node.type == :type_selector detail = { name: node.value, raw_name: node.raw_value, position: node.position } detail[:namespace] = node.namespace unless node.namespace.nil? result << detail end end |