Class: Nokogiri::XML::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/patches/nokogiri.rb

Overview

Monkey patches for Nokogiri::XML::Document

Instance Method Summary collapse

Instance Method Details

#add_all_namespaces!Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/kitchen/patches/nokogiri.rb', line 35

def add_all_namespaces!
  # Nokogiri by default only recognizes the namespaces on the root node.  Collect all
  # namespaces and add them manually.
  return if @all_namespaces_added

  collect_namespaces.each do |namespace, url|
    prefix, name = namespace.split(':')
    next unless prefix == 'xmlns' && name.present?

    root.add_namespace_definition(name, url)
  end

  @all_namespaces_added = true
end

#alphabetize_attributes!Object

Alphabetizes all attributes within the document, useful for comparing one document to another (since attribute order isn’t meaningful)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kitchen/patches/nokogiri.rb', line 19

def alphabetize_attributes!
  traverse do |child|
    next if child.text? || child.document?

    child_attributes = child.attributes
    child_attributes.each do |key, _value|
      child.remove_attribute(key)
    end
    sorted_keys = child_attributes.keys.sort
    sorted_keys.each do |key|
      value = child_attributes[key].to_s
      child[key] = value
    end
  end
end

#inspectObject

Hides the guts of the document when printed out so you don’t get 5MB dumped into your terminal



12
13
14
# File 'lib/kitchen/patches/nokogiri.rb', line 12

def inspect
  'Nokogiri::XML::Document <hidden for brevity>'
end