Parse options for passing to Nokogiri.XML or Nokogiri.HTML

Building combinations of parse options

You can build your own combinations of these parse options by using any of the following methods: Note: All examples attempt to set the RECOVER & NOENT options. All examples use Ruby 2 optional parameter syntax.

Ruby's bitwise operators

You can use the Ruby bitwise operators to set various combinations.

Nokogiri.XML('<content>Chapter 1</content', options: Nokogiri::XML::ParseOptions.new((1 << 0) | (1 << 1)))
Method chaining

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

Nokogiri.XML('<content>Chapter 1</content', options: Nokogiri::XML::ParseOptions.new.recover.noent)
Using Ruby Blocks

You can also setup parse combinations in the block passed to Nokogiri.XML or Nokogiri.HTML

Nokogiri.XML('<content>Chapter 1</content') {|config| config.recover.noent}

Removing particular parse options

You can also remove options from an instance of ParseOptions dynamically. Every option has an equivalent no{option} method in lowercase. You can call these methods on an instance of ParseOptions to remove the option. Note that this is not available for STRICT.

# Setting the RECOVER & NOENT options...
options = Nokogiri::XML::ParseOptions.new.recover.noent
# later...
options.norecover # Removes the Nokogiri::XML::ParseOptions::RECOVER option
options.nonoent # Removes the Nokogiri::XML::ParseOptions::NOENT option
Methods
I
N
S
Constants
STRICT = 0
 

Strict parsing

RECOVER = 1 << 0
 

Recover from errors

NOENT = 1 << 1
 

Substitute entities

DTDLOAD = 1 << 2
 

Load external subsets

DTDATTR = 1 << 3
 

Default DTD attributes

DTDVALID = 1 << 4
 

validate with the DTD

NOERROR = 1 << 5
 

suppress error reports

NOWARNING = 1 << 6
 

suppress warning reports

PEDANTIC = 1 << 7
 

pedantic error reporting

NOBLANKS = 1 << 8
 

remove blank nodes

SAX1 = 1 << 9
 

use the SAX1 interface internally

XINCLUDE = 1 << 10
 

Implement XInclude substitution

NONET = 1 << 11
 

Forbid network access. Recommended for dealing with untrusted documents.

NODICT = 1 << 12
 

Do not reuse the context dictionary

NSCLEAN = 1 << 13
 

remove redundant namespaces declarations

NOCDATA = 1 << 14
 

merge CDATA as text nodes

NOXINCNODE = 1 << 15
 

do not generate XINCLUDE START/END nodes

COMPACT = 1 << 16
 

compact small text nodes; no modification of the tree allowed afterwards (will possibly crash if you try to modify the tree)

OLD10 = 1 << 17
 

parse using XML-1.0 before update 5

NOBASEFIX = 1 << 18
 

do not fixup XINCLUDE xml:base uris

HUGE = 1 << 19
 

relax any hardcoded limit from the parser

DEFAULT_XML = RECOVER | NONET
 

the default options used for parsing XML documents

DEFAULT_HTML = RECOVER | NOERROR | NOWARNING | NONET
 

the default options used for parsing HTML documents

Attributes
[RW] options
[RW] to_i
Class Public methods
new(options = STRICT)
# File lib/nokogiri/xml/parse_options.rb, line 77
def initialize options = STRICT
  @options = options
end
Instance Public methods
inspect()
# File lib/nokogiri/xml/parse_options.rb, line 111
def inspect
  options = []
  self.class.constants.each do |k|
    options << k.downcase if send(:"#{k.downcase}?")
  end
  super.sub(/>$/, " " + options.join(', ') + ">")
end
strict()
# File lib/nokogiri/xml/parse_options.rb, line 100
def strict
  @options &= ~RECOVER
  self
end
strict?()
# File lib/nokogiri/xml/parse_options.rb, line 105
def strict?
  @options & RECOVER == STRICT
end