class Hatemile::Util::Html::NokogiriLib::NokogiriHTMLDOMElement

The NokogiriHTMLDOMElement class is official implementation of HTMLDOMElement interface for the Nokogiri library.

Constants

SELF_CLOSING_TAGS

Tags that are self closing.

Public Class Methods

new(element) click to toggle source

Initializes a new object that encapsulate the Nokogiri element.

@param element [Nokogiri::XML::Node] The Nokogiri element.

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 53
def initialize(element)
  Hatemile::Helper.require_not_nil(element)
  Hatemile::Helper.require_valid_type(element, Nokogiri::XML::Node)

  @data = element
  init(element, self)
end

Public Instance Methods

==(other) click to toggle source

Compare if two elements object reference the same element.

@param other [Hatemile::Util::Html::HTMLDOMElement] The other

object.

@return [Boolean] True if the object reference the same element or

false if not.
# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 293
def ==(other)
  return false if other.nil?
  return false unless other.is_a?(HTMLDOMElement)
  get_data == other.get_data
end
append_element(element) click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#append_element

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 99
def append_element(element)
  @data.add_child(element.get_data)
  self
end
append_text(text) click to toggle source

@see Hatemile::Util::Html::HTMLDOMNode#append_text

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 154
def append_text(text)
  @data.add_child(Nokogiri::XML::Text.new(text, @data.document))
  self
end
clone_element() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#clone_element

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 206
def clone_element
  NokogiriHTMLDOMElement.new(@data.clone)
end
get_attribute(name) click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#get_attribute

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 69
def get_attribute(name)
  @data.get_attribute(name)
end
get_children() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#get_children

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 123
def get_children
  array = []
  @data.children.each do |child|
    array.push(NokogiriHTMLDOMElement.new(child)) if child.element?
    array.push(NokogiriHTMLDOMTextNode.new(child)) if child.text?
  end
  array
end
get_children_elements() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#get_children_elements

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 113
def get_children_elements
  array = []
  @data.element_children.each do |child|
    array.push(NokogiriHTMLDOMElement.new(child))
  end
  array
end
get_first_element_child() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#get_first_element_child

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 212
def get_first_element_child
  return nil unless has_children_elements?
  get_children_elements.first
end
get_first_node_child() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#get_first_node_child

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 226
def get_first_node_child
  return nil unless has_children?
  get_children.first
end
get_inner_html() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#get_inner_html

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 183
def get_inner_html
  html = ''
  @data.children.each do |child|
    html += to_string(child)
  end
  html
end
get_last_element_child() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#get_last_element_child

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 219
def get_last_element_child
  return nil unless has_children_elements?
  get_children_elements.last
end
get_last_node_child() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#get_last_node_child

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 233
def get_last_node_child
  return nil unless has_children?
  get_children.last
end
get_outer_html() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#get_outer_html

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 193
def get_outer_html
  to_string(@data)
end
get_tag_name() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#get_tag_name

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 63
def get_tag_name
  @data.name.upcase
end
has_attribute?(name) click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#has_attribute?

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 87
def has_attribute?(name)
  !@data.attributes[name].nil?
end
has_attributes?() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#has_attributes?

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 93
def has_attributes?
  !@data.attributes.empty?
end
has_children?() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#has_children?

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 174
def has_children?
  @data.children.each do |child|
    return true if child.element? || child.text?
  end
  false
end
has_children_elements?() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#has_children_elements?

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 168
def has_children_elements?
  @data.element_children.empty? == false
end
normalize() click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#normalize

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 134
def normalize
  return self unless has_children?

  last = nil
  get_children.each do |child|
    if child.is_a?(NokogiriHTMLDOMElement)
      child.normalize
    elsif child.is_a?(NokogiriHTMLDOMTextNode) &&
          last.is_a?(NokogiriHTMLDOMTextNode)
      child.prepend_text(last.get_text_content)
      last.remove_node
    end
    last = child
  end

  self
end
prepend_element(element) click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#prepend_element

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 106
def prepend_element(element)
  @data.prepend_child(element.get_data)
  self
end
prepend_text(text) click to toggle source

@see Hatemile::Util::Html::HTMLDOMNode#prepend_text

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 161
def prepend_text(text)
  @data.prepend_child(Nokogiri::XML::Text.new(text, @data.document))
  self
end
remove_attribute(name) click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#remove_attribute

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 81
def remove_attribute(name)
  @data.remove_attribute(name) if has_attribute?(name)
end
self_closing_tag?(tag) click to toggle source

Returns if the tag is self closing.

@param tag [String] The element tag. @return [Boolean] True if the tag is self closing or false if not.

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 282
def self_closing_tag?(tag)
  SELF_CLOSING_TAGS.include?(tag.downcase)
end
set_attribute(name, value) click to toggle source

@see Hatemile::Util::Html::HTMLDOMElement#set_attribute

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 75
def set_attribute(name, value)
  @data.set_attribute(name, value)
end
set_data(data) click to toggle source

@see Hatemile::Util::Html::HTMLDOMNode#set_data

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 199
def set_data(data)
  @data = data
  set_node(data)
end
to_string(node) click to toggle source

Convert a Nokogiri Node to a HTML code.

@param node [Nokogiri::XML::Node] The Nokogiri Node. @return [String] The HTML code of the Nokogiri Node.

# File lib/hatemile/util/html/nokogiri/nokogiri_html_dom_element.rb, line 243
def to_string(node)
  string = ''
  if node.element?
    string += "<#{node.name.downcase}"
    node.attributes.each do |attribute, value|
      string += " #{attribute}=\"#{value}\""
    end
    string += if node.children.empty? && self_closing_tag?(node.name)
                ' />'
              else
                '>'
              end
  elsif node.comment?
    string += node.to_s
  elsif node.cdata?
    string += node.to_s
  elsif node.html?
    document = node.to_s
    string += document.split("\n")[0] + "\n"
  elsif node.text?
    string += node.text
  end

  node.children.each do |child|
    string += to_string(child)
  end

  if node.element? &&
     !(node.children.empty? && self_closing_tag?(node.name))
    string += "</#{node.name.downcase}>"
  end
  string
end