Class: Html::Merge::NodeWrapper

Inherits:
Object
  • Object
show all
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 =

Returns:

  • (Array[String])
%w[element script_element style_element].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, source:, parent: nil) ⇒ NodeWrapper

Returns a new instance of NodeWrapper.

Parameters:

  • node (Object)
  • source: (String)
  • parent: (NodeWrapper, nil) (defaults to: nil)


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

#nodeObject (readonly)

Returns the value of attribute node.

Returns:

  • (Object)


9
10
11
# File 'lib/html/merge/node_wrapper.rb', line 9

def node
  @node
end

#parentNodeWrapper? (readonly)

Returns the value of attribute parent.

Returns:



9
10
11
# File 'lib/html/merge/node_wrapper.rb', line 9

def parent
  @parent
end

#sourceString (readonly)

Returns the value of attribute source.

Returns:

  • (String)


9
10
11
# File 'lib/html/merge/node_wrapper.rb', line 9

def source
  @source
end

Instance Method Details

#attributesHash[String, String | true]

Returns:

  • (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

Returns:

  • (Boolean)


17
# File 'lib/html/merge/node_wrapper.rb', line 17

def element? = ELEMENT_TYPES.include?(node.type)

#end_byteInteger

Returns:

  • (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

Returns:

  • (Boolean)


28
# File 'lib/html/merge/node_wrapper.rb', line 28

def explicit_end_tag? = node.children.any? { |child| child.type == "end_tag" }

#explicit_idString, ...

Returns:

  • (String, true, nil)


39
# File 'lib/html/merge/node_wrapper.rb', line 39

def explicit_id = attributes["id"]

#self_closing?Boolean

Returns:

  • (Boolean)


29
# File 'lib/html/merge/node_wrapper.rb', line 29

def self_closing? = start_node&.type == "self_closing_tag"

#semantic_treeObject

Returns:

  • (Object)


41
42
43
# File 'lib/html/merge/node_wrapper.rb', line 41

def semantic_tree
  semantic_node(node)
end

#source_textString

Returns:

  • (String)


26
# File 'lib/html/merge/node_wrapper.rb', line 26

def source_text = source.byteslice(start_byte...end_byte).to_s

#start_byteInteger

Returns:

  • (Integer)


18
# File 'lib/html/merge/node_wrapper.rb', line 18

def start_byte = node.start_byte

#tag_nameString?

Returns:

  • (String, nil)


27
# File 'lib/html/merge/node_wrapper.rb', line 27

def tag_name = node_text(first_descendant(start_node, "tag_name"))&.downcase