/usr/lib/ruby/vendor_ruby/nokogiri/html/document.rb is in ruby-nokogiri 1.6.7.2-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | module Nokogiri
module HTML
class Document < Nokogiri::XML::Document
###
# Get the meta tag encoding for this document. If there is no meta tag,
# then nil is returned.
def meta_encoding
case
when meta = at('//meta[@charset]')
meta[:charset]
when meta = meta_content_type
meta['content'][/charset\s*=\s*([\w-]+)/i, 1]
end
end
###
# Set the meta tag encoding for this document.
#
# If an meta encoding tag is already present, its content is
# replaced with the given text.
#
# Otherwise, this method tries to create one at an appropriate
# place supplying head and/or html elements as necessary, which
# is inside a head element if any, and before any text node or
# content element (typically <body>) if any.
#
# The result when trying to set an encoding that is different
# from the document encoding is undefined.
#
# Beware in CRuby, that libxml2 automatically inserts a meta tag
# into a head element.
def meta_encoding= encoding
case
when meta = meta_content_type
meta['content'] = 'text/html; charset=%s' % encoding
encoding
when meta = at('//meta[@charset]')
meta['charset'] = encoding
else
meta = XML::Node.new('meta', self)
if dtd = internal_subset and dtd.html5_dtd?
meta['charset'] = encoding
else
meta['http-equiv'] = 'Content-Type'
meta['content'] = 'text/html; charset=%s' % encoding
end
case
when head = at('//head')
head.prepend_child(meta)
else
set_metadata_element(meta)
end
encoding
end
end
def meta_content_type
xpath('//meta[@http-equiv and boolean(@content)]').find { |node|
node['http-equiv'] =~ /\AContent-Type\z/i
}
end
private :meta_content_type
###
# Get the title string of this document. Return nil if there is
# no title tag.
def title
title = at('//title') and title.inner_text
end
###
# Set the title string of this document.
#
# If a title element is already present, its content is replaced
# with the given text.
#
# Otherwise, this method tries to create one at an appropriate
# place supplying head and/or html elements as necessary, which
# is inside a head element if any, right after a meta
# encoding/charset tag if any, and before any text node or
# content element (typically <body>) if any.
def title=(text)
tnode = XML::Text.new(text, self)
if title = at('//title')
title.children = tnode
return text
end
title = XML::Node.new('title', self) << tnode
case
when head = at('//head')
head << title
when meta = at('//meta[@charset]') || meta_content_type
# better put after charset declaration
meta.add_next_sibling(title)
else
set_metadata_element(title)
end
text
end
def set_metadata_element(element)
case
when head = at('//head')
head << element
when html = at('//html')
head = html.prepend_child(XML::Node.new('head', self))
head.prepend_child(element)
when first = children.find { |node|
case node
when XML::Element, XML::Text
true
end
}
# We reach here only if the underlying document model
# allows <html>/<head> elements to be omitted and does not
# automatically supply them.
first.add_previous_sibling(element)
else
html = add_child(XML::Node.new('html', self))
head = html.add_child(XML::Node.new('head', self))
head.prepend_child(element)
end
end
private :set_metadata_element
####
# 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
#
def serialize options = {}
options[:save_with] ||= XML::Node::SaveOptions::DEFAULT_HTML
super
end
####
# Create a Nokogiri::XML::DocumentFragment from +tags+
def fragment tags = nil
DocumentFragment.new(self, tags, self.root)
end
class << self
###
# 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.
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
end
class EncodingFound < StandardError # :nodoc:
attr_reader :found_encoding
def initialize(encoding)
@found_encoding = encoding
super("encoding found: %s" % encoding)
end
end
class EncodingReader # :nodoc:
class SAXHandler < Nokogiri::XML::SAX::Document # :nodoc:
attr_reader :encoding
def initialize
@encoding = nil
super()
end
def start_element(name, attrs = [])
return unless name == 'meta'
attr = Hash[attrs]
charset = attr['charset'] and
@encoding = charset
http_equiv = attr['http-equiv'] and
http_equiv.match(/\AContent-Type\z/i) and
content = attr['content'] and
m = content.match(/;\s*charset\s*=\s*([\w-]+)/) and
@encoding = m[1]
end
end
class JumpSAXHandler < SAXHandler
def initialize(jumptag)
@jumptag = jumptag
super()
end
def start_element(name, attrs = [])
super
throw @jumptag, @encoding if @encoding
throw @jumptag, nil if name =~ /\A(?:div|h1|img|p|br)\z/
end
end
def self.detect_encoding(chunk)
if Nokogiri.jruby? && EncodingReader.is_jruby_without_fix?
return EncodingReader.detect_encoding_for_jruby_without_fix(chunk)
end
m = chunk.match(/\A(<\?xml[ \t\r\n]+[^>]*>)/) and
return Nokogiri.XML(m[1]).encoding
if Nokogiri.jruby?
m = chunk.match(/(<meta\s)(.*)(charset\s*=\s*([\w-]+))(.*)/i) and
return m[4]
catch(:encoding_found) {
Nokogiri::HTML::SAX::Parser.new(JumpSAXHandler.new(:encoding_found)).parse(chunk)
nil
}
else
handler = SAXHandler.new
parser = Nokogiri::HTML::SAX::PushParser.new(handler)
parser << chunk rescue Nokogiri::SyntaxError
handler.encoding
end
end
def self.is_jruby_without_fix?
JRUBY_VERSION.split('.').join.to_i < 165
end
def self.detect_encoding_for_jruby_without_fix(chunk)
m = chunk.match(/\A(<\?xml[ \t\r\n]+[^>]*>)/) and
return Nokogiri.XML(m[1]).encoding
m = chunk.match(/(<meta\s)(.*)(charset\s*=\s*([\w-]+))(.*)/i) and
return m[4]
catch(:encoding_found) {
Nokogiri::HTML::SAX::Parser.new(JumpSAXHandler.new(:encoding_found.to_s)).parse(chunk)
nil
}
rescue Nokogiri::SyntaxError, RuntimeError
# Ignore parser errors that nokogiri may raise
nil
end
def initialize(io)
@io = io
@firstchunk = nil
@encoding_found = nil
end
# This method is used by the C extension so that
# Nokogiri::HTML::Document#read_io() does not leak memory when
# EncodingFound is raised.
attr_reader :encoding_found
def read(len)
# no support for a call without len
if !@firstchunk
@firstchunk = @io.read(len) or return nil
# This implementation expects that the first call from
# htmlReadIO() is made with a length long enough (~1KB) to
# achieve advanced encoding detection.
if encoding = EncodingReader.detect_encoding(@firstchunk)
# The first chunk is stored for the next read in retry.
raise @encoding_found = EncodingFound.new(encoding)
end
end
@encoding_found = nil
ret = @firstchunk.slice!(0, len)
if (len -= ret.length) > 0
rest = @io.read(len) and ret << rest
end
if ret.empty?
nil
else
ret
end
end
end
end
end
end
|