Method: Capybara.HTML

Defined in:
lib/capybara.rb

.HTML(html) ⇒ Nokogiri::HTML::Document

Parse raw html into a document using Nokogiri, and adjust textarea contents as defined by the spec.

Parameters:

  • html (String)

    The raw html

Returns:

  • (Nokogiri::HTML::Document)

    HTML document

[View source]

390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/capybara.rb', line 390

def HTML(html) # rubocop:disable Naming/MethodName
  # Nokogiri >= 1.12.0 or Nokogumbo installed and allowed for use
  html_parser, using_html5 = if defined?(Nokogiri::HTML5) && Capybara.use_html5_parsing
    [Nokogiri::HTML5, true]
  else
    [defined?(Nokogiri::HTML4) ? Nokogiri::HTML4 : Nokogiri::HTML, false]
  end

  html_parser.parse(html).tap do |document|
    document.xpath('//template').each do |template|
      # template elements content is not part of the document
      template.inner_html = ''
    end
    document.xpath('//textarea').each do |textarea|
      # The Nokogiri HTML5 parser already returns spec compliant contents
      textarea['_capybara_raw_value'] = using_html5 ? textarea.content : textarea.content.delete_prefix("\n")
    end
  end
end