Class: Capybara::Dommy::TextExtractor

Inherits:
Object
  • Object
show all
Includes:
Node::WhitespaceNormalizer
Defined in:
lib/capybara/dommy/text_extractor.rb

Overview

Extracts text from a Dommy subtree, reusing Capybara’s own whitespace normalization so results match the matchers’ expectations. ‘all_text` is the raw textContent; `visible_text` excludes element subtrees that are hidden under the driver’s visibility mode or are non-rendered (script/style/head), and inserts breaks at block boundaries.

Constant Summary collapse

BLOCK_ELEMENTS =
%w[
  p h1 h2 h3 h4 h5 h6 ol ul pre address blockquote dl div fieldset form hr noscript table
].freeze
NON_DISPLAYED_ELEMENTS =
%w[script style head title].freeze

Instance Method Summary collapse

Constructor Details

#initialize(driver) ⇒ TextExtractor

Returns a new instance of TextExtractor.



18
19
20
# File 'lib/capybara/dommy/text_extractor.rb', line 18

def initialize(driver)
  @driver = driver
end

Instance Method Details

#all_text(element) ⇒ Object



22
23
24
# File 'lib/capybara/dommy/text_extractor.rb', line 22

def all_text(element)
  normalize_spacing(element.text_content)
end

#visible_text(element) ⇒ Object



26
27
28
29
30
# File 'lib/capybara/dommy/text_extractor.rb', line 26

def visible_text(element)
  return "" unless @driver.visible?(element)

  normalize_visible_spacing(displayed_text(element))
end