Class: Nokogiri::HTML5::DocumentFragment

Inherits:
Nokogiri::HTML::DocumentFragment show all
Defined in:
lib/nokogiri/html5/document_fragment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc, tags = nil, ctx = nil, options = {}) ⇒ DocumentFragment

Create a document fragment.


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nokogiri/html5/document_fragment.rb', line 27

def initialize(doc, tags = nil, ctx = nil, options = {})
  self.document = doc
  self.errors = []
  return self unless tags

  max_attributes = options[:max_attributes] || Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES
  max_errors = options[:max_errors] || Nokogiri::Gumbo::DEFAULT_MAX_ERRORS
  max_depth = options[:max_tree_depth] || Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH
  tags = Nokogiri::HTML5.read_and_encode(tags, nil)
  Nokogiri::Gumbo.fragment(self, tags, ctx, max_attributes, max_errors, max_depth)
end

Instance Attribute Details

#documentObject

Returns the value of attribute document


23
24
25
# File 'lib/nokogiri/html5/document_fragment.rb', line 23

def document
  @document
end

#errorsObject

Returns the value of attribute errors


24
25
26
# File 'lib/nokogiri/html5/document_fragment.rb', line 24

def errors
  @errors
end

Class Method Details

.parse(tags, encoding = nil, options = {}) ⇒ Object

Parse a document fragment from tags, returning a Nodeset.


46
47
48
49
50
51
# File 'lib/nokogiri/html5/document_fragment.rb', line 46

def self.parse(tags, encoding = nil, options = {})
  doc = HTML5::Document.new
  tags = HTML5.read_and_encode(tags, encoding)
  doc.encoding = 'UTF-8'
  new(doc, tags, nil, options)
end

Instance Method Details

#extract_params(params) ⇒ Object

:nodoc:


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nokogiri/html5/document_fragment.rb', line 53

def extract_params params # :nodoc:
  handler = params.find do |param|
    ![Hash, String, Symbol].include?(param.class)
  end
  params -= [handler] if handler

  hashes = []
  while Hash === params.last || params.last.nil?
    hashes << params.pop
    break if params.empty?
  end
  ns, binds = hashes.reverse

  ns ||=
    begin
      ns = Hash.new
      children.each { |child| ns.merge!(child.namespaces) }
      ns
    end

  [params, handler, ns, binds]
end

#serialize(options = {}, &block) ⇒ Object


39
40
41
42
43
# File 'lib/nokogiri/html5/document_fragment.rb', line 39

def serialize(options = {}, &block)
  # Bypass XML::Document.serialize which doesn't support options even
  # though XML::Node.serialize does!
  XML::Node.instance_method(:serialize).bind(self).call(options, &block)
end