Class: Leptris::XML::ParseOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/leptris/xml/parse_options.rb

Overview

Parser flags, mirroring LeptrisParseFlags in libleptris's public headers. Pass an instance as Leptris::XML.parse(xml, options:).

Constant Summary collapse

NOBLANKS =

Discard whitespace-only text nodes (runs between tags that contain nothing but spaces/tabs/newlines). Matches libxml2's XML_PARSE_NOBLANKS and Nokogiri's noblanks. Documents parsed this way do not round-trip pretty-printed formatting byte-for-byte.

Leptris::XML::FFI::LEPTRIS_PARSE_DROP_WS_TEXT
DTDATTR =

Apply DTD ATTLIST default (and #FIXED) attribute values at parse time (libleptris >= 1.9.8, leptris/leptris#606). Matches libxml2's XML_PARSE_DTDATTR and Nokogiri's dtdload+dtdattr semantics. Off by default: the ecosystem compares against libxml2's no-DTDATTR default, and W3C C14N 1.1 example 3.3's canonical form excludes defaulted attributes.

Leptris::XML::FFI::LEPTRIS_PARSE_DTDATTR

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flags = Leptris::XML::FFI::LEPTRIS_PARSE_DEFAULT, recover: false) ⇒ ParseOptions

Returns a new instance of ParseOptions.



29
30
31
32
33
# File 'lib/leptris/xml/parse_options.rb', line 29

def initialize(flags = Leptris::XML::FFI::LEPTRIS_PARSE_DEFAULT,
               recover: false)
  @flags = flags.to_i
  @recover = recover ? true : false
end

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags.



20
21
22
# File 'lib/leptris/xml/parse_options.rb', line 20

def flags
  @flags
end

#recoverObject (readonly)

Recover (libleptris 1.9.0, #547): a parse failure returns an empty document with the failure recorded in Document#last_error instead of raising — the libxml2 XML_PARSE_RECOVER semantics adapters emulate. Struct-only (not a parse flag): carrying it routes Document.parse through leptris_parse_string_ex.



27
28
29
# File 'lib/leptris/xml/parse_options.rb', line 27

def recover
  @recover
end

Class Method Details

.dtdattrObject



47
48
49
# File 'lib/leptris/xml/parse_options.rb', line 47

def self.dtdattr
  new(DTDATTR)
end

.noblanksObject



35
36
37
# File 'lib/leptris/xml/parse_options.rb', line 35

def self.noblanks
  new(NOBLANKS)
end

.recoveringObject



39
40
41
# File 'lib/leptris/xml/parse_options.rb', line 39

def self.recovering
  new(recover: true)
end

Instance Method Details

#dtdattr=(value) ⇒ Object



55
56
57
# File 'lib/leptris/xml/parse_options.rb', line 55

def dtdattr=(value)
  if value then @flags |= DTDATTR else @flags &= ~DTDATTR end
end

#dtdattr?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/leptris/xml/parse_options.rb', line 51

def dtdattr?
  @flags & DTDATTR != 0
end

#noblanks?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/leptris/xml/parse_options.rb', line 43

def noblanks?
  @flags & NOBLANKS != 0
end

#recover?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/leptris/xml/parse_options.rb', line 59

def recover?
  @recover == true
end

#struct_required?Boolean

True when the options cannot ride the flags-only parse path and need the full LeptrisParseOptions struct (leptris_parse_string_ex).

Returns:

  • (Boolean)


69
70
71
# File 'lib/leptris/xml/parse_options.rb', line 69

def struct_required?
  recover?
end

#to_c_structObject

Builds the C LeptrisParseOptions struct mirroring this instance.



74
75
76
77
78
79
80
81
# File 'lib/leptris/xml/parse_options.rb', line 74

def to_c_struct
  struct = Leptris::XML::FFI::ParseOptionsStruct.new
  struct[:flags] = @flags
  struct[:strict_mode] = -1
  struct[:max_depth] = 0
  struct[:recover] = @recover ? 1 : 0
  struct
end

#|(other) ⇒ Object



63
64
65
# File 'lib/leptris/xml/parse_options.rb', line 63

def |(other)
  self.class.new(@flags | other.flags, recover: @recover || other.recover?)
end