module Capybara::Node::WhitespaceNormalizer

{Capybara::Node::WhitespaceNormalizer} provides methods that help to normalize the spacing of text content inside of {Capybara::Node::Element}s by removing various unicode spacing and directional markings.

Constants

BREAKING_SPACES

All spaces except for NBSP

EMPTY_LINES

Matches multiple empty lines

LEADING_SPACES

Any whitespace at the front of text

LEFT_TO_RIGHT_MARK

Signifies text is read left to right

LINE_SEPERATOR
NON_BREAKING_SPACE

Unicode for NBSP, or  

PARAGRAPH_SEPERATOR
REMOVED_CHARACTERS

Characters we want to truncate from text

RIGHT_TO_LEFT_MARK

Signifies text is read right to left

SQUEEZED_SPACES

Whitespace we want to substitute with plain spaces

TRAILING_SPACES

Any whitespace at the end of text

ZERO_WIDTH_SPACE

“Invisible” space character

Public Instance Methods

normalize_spacing(text) click to toggle source

Normalizes the spacing of a node’s text to be similar to what matchers might expect.

@param text [String] @return [String]

# File lib/capybara/node/whitespace_normalizer.rb, line 53
def normalize_spacing(text)
  text
    .delete(REMOVED_CHARACTERS)
    .tr(SQUEEZED_SPACES, ' ')
    .squeeze(' ')
    .sub(LEADING_SPACES, '')
    .sub(TRAILING_SPACES, '')
    .tr(NON_BREAKING_SPACE, ' ')
end
normalize_visible_spacing(text) click to toggle source

Variant on {Capybara::Node::Normalizer#normalize_spacing} that targets the whitespace of visible elements only.

@param text [String] @return [String]

# File lib/capybara/node/whitespace_normalizer.rb, line 71
def normalize_visible_spacing(text)
  text
    .squeeze(' ')
    .gsub(EMPTY_LINES, "\n")
    .sub(LEADING_SPACES, '')
    .sub(TRAILING_SPACES, '')
    .tr(NON_BREAKING_SPACE, ' ')
end