module Ballonizer::Workaround

@api private Don’t use the methods of this module. They are for internal use only.

Public Class Methods

deep_freeze(e) click to toggle source
# File lib/ballonizer.rb, line 124
def self.deep_freeze(e)
  e.each { | v | deep_freeze(v) } if e.is_a?(Enumerable)
  e.freeze
end
join_uris(base, relative) click to toggle source
# File lib/ballonizer.rb, line 120
def self.join_uris(base, relative)
  Addressable::URI.parse(base).join(relative).to_s
end
parse_html_or_xhtml(doc, mime_type) click to toggle source
# File lib/ballonizer.rb, line 129
def self.parse_html_or_xhtml(doc, mime_type)
  # If you parse XHTML as HTML with Nokogiri, and use to_s after, the markup
  # can be messed up, breaking the structural integrity of the xml
  #
  # Example:     <meta name="description" content="not important" />
  #   becomes    <meta name="description" content="not important" >
  #
  # In the other side if you parse HTML as a XML, and use to_s after, the
  # Nokogiri make empty content tags self-close
  #
  # Example:    <script type="text/javascript" src="/ballonizer.js"></script>
  #   becomes:  <script type="text/javascript" src="/ballonizer.js" />
  #
  # What's even worse than the contrary (xml as html)
  parsed_doc = nil

  case mime_type
  when /text\/html/
    parsed_doc = Nokogiri::HTML(doc)
  when /application\/xhtml\+xml/
    options = Nokogiri::XML::ParseOptions::DEFAULT_XML &
                Nokogiri::XML::ParseOptions::STRICT &
                Nokogiri::XML::ParseOptions::NONET
    begin
      parsed_doc = Nokogiri::XML::Document.parse(doc, nil, nil, options)
    rescue
      return nil
    end
  else
    fail Error, "the only mime-types accepted are text/html and" +
                " application/xhtml+xml, the passed argument was " +
                "'#{mime_type}'"
  end

  parsed_doc
end