class Nokogiri::HTML::Document

Public Class Methods

parse(string_or_io, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML) { |options| ... } click to toggle source

Parse HTML. string_or_io may be a String, or any object that responds to read and close such as an IO, or StringIO. url is resource where this document is located. encoding is the encoding that should be used when processing the document. options is a number that sets options in the parser, such as Nokogiri::XML::ParseOptions::RECOVER. See the constants in Nokogiri::XML::ParseOptions.

# File lib/nokogiri/html/document.rb, line 83
def parse string_or_io, url = nil, encoding = nil, options = XML::ParseOptions::DEFAULT_HTML

  options = Nokogiri::XML::ParseOptions.new(options) if Fixnum === options
  # Give the options to the user
  yield options if block_given?

  if string_or_io.respond_to?(:encoding)
    unless string_or_io.encoding.name == "ASCII-8BIT"
      encoding ||= string_or_io.encoding.name
    end
  end

  if string_or_io.respond_to?(:read)
    url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
    if !encoding
      # Libxml2's parser has poor support for encoding
      # detection.  First, it does not recognize the HTML5
      # style meta charset declaration.  Secondly, even if it
      # successfully detects an encoding hint, it does not
      # re-decode or re-parse the preceding part which may be
      # garbled.
      #
      # EncodingReader aims to perform advanced encoding
      # detection beyond what Libxml2 does, and to emulate
      # rewinding of a stream and make Libxml2 redo parsing
      # from the start when an encoding hint is found.
      string_or_io = EncodingReader.new(string_or_io)
      begin
        return read_io(string_or_io, url, encoding, options.to_i)
      rescue EncodingFound => e
        encoding = e.found_encoding
      end
    end
    return read_io(string_or_io, url, encoding, options.to_i)
  end

  # read_memory pukes on empty docs
  return new if string_or_io.nil? or string_or_io.empty?

  encoding ||= EncodingReader.detect_encoding(string_or_io)

  read_memory(string_or_io, url, encoding, options.to_i)
end

Public Instance Methods

fragment(tags = nil) click to toggle source

Create a Nokogiri::XML::DocumentFragment from tags

# File lib/nokogiri/html/document.rb, line 70
def fragment tags = nil
  DocumentFragment.new(self, tags, self.root)
end
meta_encoding() click to toggle source

Get the meta tag encoding for this document. If there is no meta tag, then nil is returned.

# File lib/nokogiri/html/document.rb, line 7
def meta_encoding
  meta = meta_content_type and
    match = /charset\s*=\s*([\w-]+)/.match(meta['content']) and
    match[1]
end
meta_encoding=(encoding) click to toggle source

Set the meta tag encoding for this document. If there is no meta content tag, the encoding is not set.

# File lib/nokogiri/html/document.rb, line 16
def meta_encoding= encoding
  meta = meta_content_type and
    meta['content'] = "text/html; charset=%s" % encoding
end
serialize(options = {}) click to toggle source

Serialize Node using options. Save options can also be set using a block. See SaveOptions.

These two statements are equivalent:

node.serialize(:encoding => 'UTF-8', :save_with => FORMAT | AS_XML)

or

node.serialize(:encoding => 'UTF-8') do |config|
  config.format.as_xml
end
Calls superclass method Nokogiri::XML::Node#serialize
# File lib/nokogiri/html/document.rb, line 63
def serialize options = {}
  options[:save_with] ||= XML::Node::SaveOptions::DEFAULT_HTML
  super
end
title() click to toggle source

Get the title string of this document. Return nil if there is no title tag.

# File lib/nokogiri/html/document.rb, line 33
def title
  title = at('title') and title.inner_text
end
title=(text) click to toggle source

Set the title string of this document. If there is no head element, the title is not set.

# File lib/nokogiri/html/document.rb, line 40
def title=(text)
  unless title = at('title')
    head = at('head') or return nil
    title = Nokogiri::XML::Node.new('title', self)
    head << title
  end
  title.children = XML::Text.new(text, self)
end