Module: Capybara::Node::WhitespaceNormalizer

Included in:
RackTest::Node, Selenium::Node
Defined in:
lib/capybara/node/whitespace_normalizer.rb

Overview

WhitespaceNormalizer provides methods that help to normalize the spacing of text content inside of Elements by removing various unicode spacing and directional markings.

Constant Summary collapse

NON_BREAKING_SPACE =

Unicode for NBSP, or  

"\u00a0"
LINE_SEPERATOR =
"\u2028"
PARAGRAPH_SEPERATOR =
"\u2029"
BREAKING_SPACES =

All spaces except for NBSP

"[[:space:]&&[^#{NON_BREAKING_SPACE}]]".freeze
SQUEEZED_SPACES =

Whitespace we want to substitute with plain spaces

" \n\f\t\v#{LINE_SEPERATOR}#{PARAGRAPH_SEPERATOR}".freeze
LEADING_SPACES =

Any whitespace at the front of text

/\A#{BREAKING_SPACES}+/
TRAILING_SPACES =

Any whitespace at the end of text

/#{BREAKING_SPACES}+\z/
ZERO_WIDTH_SPACE =

"Invisible" space character

"\u200b"
LEFT_TO_RIGHT_MARK =

Signifies text is read left to right

"\u200e"
RIGHT_TO_LEFT_MARK =

Signifies text is read right to left

"\u200f"
REMOVED_CHARACTERS =

Characters we want to truncate from text

[ZERO_WIDTH_SPACE, LEFT_TO_RIGHT_MARK, RIGHT_TO_LEFT_MARK].join
EMPTY_LINES =

Matches multiple empty lines

/[\ \n]*\n[\ \n]*/

Instance Method Summary collapse

Instance Method Details

#normalize_spacing(text) ⇒ String

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

Parameters:

  • text (String)

Returns:

  • (String)


53
54
55
56
57
58
59
60
61
# 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) ⇒ String

Variant on Normalizer#normalize_spacing that targets the whitespace of visible elements only.

Parameters:

  • text (String)

Returns:

  • (String)


71
72
73
74
75
76
77
78
# 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