Class: SilkLayout::HTML::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/silk_layout/html/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag:, attributes:, children:, text: nil, parent: nil) ⇒ Node

Returns a new instance of Node.



10
11
12
13
14
15
16
# File 'lib/silk_layout/html/node.rb', line 10

def initialize(tag:, attributes:, children:, text: nil, parent: nil)
  @tag = tag
  @attributes = attributes
  @children = children
  @text = text
  @parent = parent
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/silk_layout/html/node.rb', line 6

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



7
8
9
# File 'lib/silk_layout/html/node.rb', line 7

def children
  @children
end

#computed_styleObject

Returns the value of attribute computed_style.



8
9
10
# File 'lib/silk_layout/html/node.rb', line 8

def computed_style
  @computed_style
end

#parentObject

Returns the value of attribute parent.



7
8
9
# File 'lib/silk_layout/html/node.rb', line 7

def parent
  @parent
end

#tagObject (readonly)

Returns the value of attribute tag.



6
7
8
# File 'lib/silk_layout/html/node.rb', line 6

def tag
  @tag
end

#textObject (readonly)

Returns the value of attribute text.



6
7
8
# File 'lib/silk_layout/html/node.rb', line 6

def text
  @text
end

Class Method Details

.build_element_node(node, parent) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/silk_layout/html/node.rb', line 43

def self.build_element_node(node, parent)
  element = new(
    tag: node.name,
    attributes: node.attributes.transform_values(&:value),
    children: [],
    parent: parent
  )

  element.children = node.children.map { |child| from_nokogiri(child, element) }.compact
  element
end

.build_text_node(node, parent) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/silk_layout/html/node.rb', line 30

def self.build_text_node(node, parent)
  text = node.text.to_s
  return nil if text.empty?

  new(
    tag: nil,
    attributes: {},
    children: [],
    text: text,
    parent: parent
  )
end

.from_nokogiri(node, parent = nil) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/silk_layout/html/node.rb', line 22

def self.from_nokogiri(node, parent = nil)
  if node.text?
    build_text_node(node, parent)
  else
    build_element_node(node, parent)
  end
end

Instance Method Details

#element?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/silk_layout/html/node.rb', line 18

def element?
  !tag.nil?
end