Class: Nokogiri::HTML::Document::EncodingReader
- Inherits:
-
Object
- Object
- Nokogiri::HTML::Document::EncodingReader
show all
- Defined in:
- lib/nokogiri/html/document.rb
Overview
Defined Under Namespace
Classes: JumpSAXHandler, SAXHandler
Instance Attribute Summary collapse
-
#encoding_found ⇒ Object
readonly
This method is used by the C extension so that Nokogiri::HTML::Document#read_io() does not leak memory when EncodingFound is raised.
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
214
215
216
217
218
|
# File 'lib/nokogiri/html/document.rb', line 214
def initialize(io)
@io = io
@firstchunk = nil
@encoding_found = nil
end
|
Instance Attribute Details
#encoding_found ⇒ Object
This method is used by the C extension so that Nokogiri::HTML::Document#read_io() does not leak memory when EncodingFound is raised.
223
224
225
|
# File 'lib/nokogiri/html/document.rb', line 223
def encoding_found
@encoding_found
end
|
Class Method Details
.detect_encoding(chunk) ⇒ Object
.detect_encoding_for_jruby_without_fix(chunk) ⇒ Object
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/nokogiri/html/document.rb', line 198
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
nil
end
|
.is_jruby_without_fix? ⇒ Boolean
194
195
196
|
# File 'lib/nokogiri/html/document.rb', line 194
def self.is_jruby_without_fix?
JRUBY_VERSION.split('.').join.to_i < 165
end
|
Instance Method Details
#read(len) ⇒ Object
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
|
# File 'lib/nokogiri/html/document.rb', line 225
def read(len)
if !@firstchunk
@firstchunk = @io.read(len) or return nil
if encoding = EncodingReader.detect_encoding(@firstchunk)
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
|