Class: Html::Merge::NodeWrapper
- Inherits:
-
Object
- Object
- Html::Merge::NodeWrapper
- Defined in:
- lib/html/merge/node_wrapper.rb,
sig/html/merge.rbs
Overview
Parser-derived identity, attributes, semantics, and source range for an HTML element.
Constant Summary collapse
- ELEMENT_TYPES =
%w[element script_element style_element].freeze
Instance Attribute Summary collapse
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#parent ⇒ NodeWrapper?
readonly
Returns the value of attribute parent.
-
#source ⇒ String
readonly
Returns the value of attribute source.
Instance Method Summary collapse
- #attributes ⇒ Hash[String, String | true]
- #element? ⇒ Boolean
- #end_byte ⇒ Integer
- #explicit_end_tag? ⇒ Boolean
- #explicit_id ⇒ String, ...
-
#initialize(node, source:, parent: nil) ⇒ NodeWrapper
constructor
A new instance of NodeWrapper.
- #self_closing? ⇒ Boolean
- #semantic_tree ⇒ Object
- #source_text ⇒ String
- #start_byte ⇒ Integer
- #tag_name ⇒ String?
Constructor Details
#initialize(node, source:, parent: nil) ⇒ NodeWrapper
Returns a new instance of NodeWrapper.
11 12 13 14 15 |
# File 'lib/html/merge/node_wrapper.rb', line 11 def initialize(node, source:, parent: nil) @node = node @source = source @parent = parent end |
Instance Attribute Details
#node ⇒ Object (readonly)
Returns the value of attribute node.
9 10 11 |
# File 'lib/html/merge/node_wrapper.rb', line 9 def node @node end |
#parent ⇒ NodeWrapper? (readonly)
Returns the value of attribute parent.
9 10 11 |
# File 'lib/html/merge/node_wrapper.rb', line 9 def parent @parent end |
#source ⇒ String (readonly)
Returns the value of attribute source.
9 10 11 |
# File 'lib/html/merge/node_wrapper.rb', line 9 def source @source end |
Instance Method Details
#attributes ⇒ Hash[String, String | true]
31 32 33 34 35 36 37 |
# File 'lib/html/merge/node_wrapper.rb', line 31 def attributes return {} unless start_node start_node.children.select { |child| child.type == "attribute" }.to_h do |attribute| attribute_pair(attribute) end.freeze end |
#element? ⇒ Boolean
17 |
# File 'lib/html/merge/node_wrapper.rb', line 17 def element? = ELEMENT_TYPES.include?(node.type) |
#end_byte ⇒ Integer
20 21 22 23 24 |
# File 'lib/html/merge/node_wrapper.rb', line 20 def end_byte return start_node.end_byte if !explicit_end_tag? && start_node node.end_byte end |
#explicit_end_tag? ⇒ Boolean
28 |
# File 'lib/html/merge/node_wrapper.rb', line 28 def explicit_end_tag? = node.children.any? { |child| child.type == "end_tag" } |
#explicit_id ⇒ String, ...
39 |
# File 'lib/html/merge/node_wrapper.rb', line 39 def explicit_id = attributes["id"] |
#self_closing? ⇒ Boolean
29 |
# File 'lib/html/merge/node_wrapper.rb', line 29 def self_closing? = start_node&.type == "self_closing_tag" |
#semantic_tree ⇒ Object
41 42 43 |
# File 'lib/html/merge/node_wrapper.rb', line 41 def semantic_tree semantic_node(node) end |
#source_text ⇒ String
26 |
# File 'lib/html/merge/node_wrapper.rb', line 26 def source_text = source.byteslice(start_byte...end_byte).to_s |
#start_byte ⇒ Integer
18 |
# File 'lib/html/merge/node_wrapper.rb', line 18 def start_byte = node.start_byte |
#tag_name ⇒ String?
27 |
# File 'lib/html/merge/node_wrapper.rb', line 27 def tag_name = node_text(first_descendant(start_node, "tag_name"))&.downcase |