diff --git a/lib/nokogumbo.rb b/lib/nokogumbo.rb index 262d45b9..ce1679c5 100644 --- a/lib/nokogumbo.rb +++ b/lib/nokogumbo.rb @@ -1,17 +1,38 @@ require 'nokogiri' require 'nokogumbo/version' -require 'nokogumbo/html5' -require 'nokogumbo/nokogumbo' +if ((defined?(Nokogiri::HTML5) && Nokogiri::HTML5.respond_to?(:parse)) && + (defined?(Nokogiri::Gumbo) && Nokogiri::Gumbo.respond_to?(:parse)) && + !(ENV.key?("NOKOGUMBO_IGNORE_NOKOGIRI_HTML5") && ENV["NOKOGUMBO_IGNORE_NOKOGIRI_HTML5"] != "false")) -module Nokogumbo - # The default maximum number of attributes per element. - DEFAULT_MAX_ATTRIBUTES = 400 + warn "NOTE: nokogumbo: Using Nokogiri::HTML5 provided by Nokogiri. See https://github.com/sparklemotion/nokogiri/issues/2205 for more information." - # The default maximum number of errors for parsing a document or a fragment. - DEFAULT_MAX_ERRORS = 0 + module Nokogumbo + def self.parse(*args) + Nokogiri::Gumbo.parse(*args) + end - # The default maximum depth of the DOM tree produced by parsing a document - # or fragment. - DEFAULT_MAX_TREE_DEPTH = 400 + def self.fragment(*args) + Nokogiri::Gumbo.fragment(*args) + end + + DEFAULT_MAX_ATTRIBUTES = Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES + DEFAULT_MAX_ERRORS = Nokogiri::Gumbo::DEFAULT_MAX_ERRORS + DEFAULT_MAX_TREE_DEPTH = Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH + end +else + require 'nokogumbo/html5' + require 'nokogumbo/nokogumbo' + + module Nokogumbo + # The default maximum number of attributes per element. + DEFAULT_MAX_ATTRIBUTES = 400 + + # The default maximum number of errors for parsing a document or a fragment. + DEFAULT_MAX_ERRORS = 0 + + # The default maximum depth of the DOM tree produced by parsing a document + # or fragment. + DEFAULT_MAX_TREE_DEPTH = 400 + end end