123456789_123456789_123456789_123456789_123456789_

Class: Nokogiri::XML::ParseOptions

Relationships & Source Files
Inherits: Object
Defined in: lib/nokogiri/xml/parse_options.rb

Overview

Options that control the parsing behavior for Document, DocumentFragment, ::Nokogiri::HTML4::Document, ::Nokogiri::HTML4::DocumentFragment, ::Nokogiri::XSLT::Stylesheet, and Schema.

These options directly expose libxml2’s parse options, which are all boolean in the sense that an option is “on” or “off”.

💡 Note that ::Nokogiri::HTML5 parsing has a separate, orthogonal set of options due to the nature of the ::Nokogiri::HTML5 specification. See ::Nokogiri::HTML5.

⚠ Not all parse options are supported on JRuby. ::Nokogiri will attempt to invoke the equivalent behavior in Xerces/NekoHTML on JRuby when it’s possible.

Setting and unsetting parse options

You can build your own combinations of parse options by using any of the following methods:

ParseOptions method chaining

Every option has an equivalent method in lowercase. You can chain these methods together to set various combinations.

# Set the HUGE & PEDANTIC options
po = Nokogiri::XML::ParseOptions.new.huge.pedantic
doc = Nokogiri::XML::Document.parse(xml, nil, nil, po)

Every option has an equivalent no{option} method in lowercase. You can call these methods on an instance of ParseOptions to unset the option.

# Set the HUGE & PEDANTIC options
po = Nokogiri::XML::ParseOptions.new.huge.pedantic

# later we want to modify the options
po.nohuge # Unset the HUGE option
po.nopedantic # Unset the PEDANTIC option

💡 Note that some options begin with “no” leading to the logical but perhaps unintuitive double negative:

po.nocdata # Set the NOCDATA parse option
po.nonocdata # Unset the NOCDATA parse option

💡 Note that negation is not available for STRICT, which is itself a negation of all other features.

Using Ruby Blocks

Most parsing methods will accept a block for configuration of parse options, and we recommend chaining the setter methods:

doc = Nokogiri::XML::Document.parse(xml) { |config| config.huge.pedantic }
ParseOptions constants

You can also use the constants declared under Nokogiri::XML::ParseOptions to set various combinations. They are bits in a bitmask, and so can be combined with bitwise operators:

po = Nokogiri::XML::ParseOptions.new(Nokogiri::XML::ParseOptions::HUGE | Nokogiri::XML::ParseOptions::PEDANTIC)
doc = Nokogiri::XML::Document.parse(xml, nil, nil, po)

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(options = STRICT) ⇒ ParseOptions

[ GitHub ]

  
# File 'lib/nokogiri/xml/parse_options.rb', line 165

def initialize(options = STRICT)
  @options = options
end

Instance Attribute Details

#options (rw) Also known as: #to_i

[ GitHub ]

  
# File 'lib/nokogiri/xml/parse_options.rb', line 163

attr_accessor :options

#strict (readonly)

[ GitHub ]

  
# File 'lib/nokogiri/xml/parse_options.rb', line 189

def strict
  @options &= ~RECOVER
  self
end

#strict?Boolean (readonly)

[ GitHub ]

  
# File 'lib/nokogiri/xml/parse_options.rb', line 194

def strict?
  @options & RECOVER == STRICT
end

#to_i (readonly)

Alias for #options.

[ GitHub ]

  
# File 'lib/nokogiri/xml/parse_options.rb', line 202

alias_method :to_i, :options

Instance Method Details

#==(other)

[ GitHub ]

  
# File 'lib/nokogiri/xml/parse_options.rb', line 198

def ==(other)
  other.to_i == to_i
end

#inspect

[ GitHub ]

  
# File 'lib/nokogiri/xml/parse_options.rb', line 204

def inspect
  options = []
  self.class.constants.each do |k|
    options << k.downcase if send(:"#{k.downcase}?")
  end
  super.sub(/>$/, " " + options.join(", ") + ">")
end