Module: Asciidoctor::DocTest::HtmlNormalizer

Defined in:
lib/asciidoctor/doctest/html_normalizer.rb

Overview

Module to be included into Nokogiri::HTML::Document or DocumentFragment to add #normalize! feature.

Examples:

Nokogiri::HTML.parse(str).normalize!
Nokogiri::HTML.fragment(str).normalize!

Instance Method Summary collapse

Instance Method Details

#normalize!Object

Normalizes the HTML document or fragment so it can be easily compared with another HTML.

What does it actually do?

  • sorts element attributes by name

  • sorts inline CSS declarations inside a style attribute by name

  • removes all blank text nodes (i.e. node that contain just whitespaces)

  • strips nonsignificant leading and trailing whitespaces around text

  • strips nonsignificant repeated whitespaces

Returns:

  • (Object)

    self


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/asciidoctor/doctest/html_normalizer.rb', line 32

def normalize!
  traverse do |node|
    case node.type

    when Nokogiri::XML::Node::ELEMENT_NODE
      sort_element_attrs! node
      sort_element_style_attr! node

    when Nokogiri::XML::Node::TEXT_NODE
      # Remove text node that contains whitespaces only.
      if node.blank?
        node.remove

      elsif !preformatted_block? node
        strip_redundant_spaces! node
        strip_spaces_around_text! node
      end
    end
  end
  self
end