# frozen_string_literal: true module Nokogiri module HTML4 # 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. # :nodoc: all class EncodingReader class EncodingFound < StandardError attr_reader :found_encoding def initialize(encoding) @found_encoding = encoding super(format("encoding found: %s", encoding)) end end class SAXHandler < Nokogiri::XML::SAX::Document attr_reader :encoding def initialize @encoding = nil super() end def start_element(name, attrs = []) return unless name == "meta" attr = Hash[attrs] (charset = attr["charset"]) && (@encoding = charset) (http_equiv = attr["http-equiv"]) && http_equiv.match(/\AContent-Type\z/i) && (content = attr["content"]) && (m = content.match(/;\s*charset\s*=\s*([\w-]+)/)) && (@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 /\A(?:div|h1|img|p|br)\z/.match?(name) end end def self.detect_encoding(chunk) (m = chunk.match(/\A(<\?xml[ \t\r\n][^>]*>)/)) && (return Nokogiri.XML(m[1]).encoding) if Nokogiri.jruby? (m = chunk.match(/( 0 (rest = @io.read(len)) && ret << (rest) end if ret.empty? nil else ret end end end end end