Module: Capybara::Node::WhitespaceNormalizer
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Included In:
| |
| 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
-
BREAKING_SPACES =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 19
All spaces except for NBSP
"[[:space:]&&[^#{NON_BREAKING_SPACE}]]".freeze
-
EMPTY_LINES =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 43
Matches multiple empty lines
/[\ \n]*\n[\ \n]*/ -
LEADING_SPACES =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 25
Any whitespace at the front of text
/\A#{BREAKING_SPACES}+/ -
LEFT_TO_RIGHT_MARK =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 34
Signifies text is read left to right
"\u200e" -
LINE_SEPERATOR =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 15"\u2028" -
NON_BREAKING_SPACE =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 14
Unicode for NBSP, or
"\u00a0" -
PARAGRAPH_SEPERATOR =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 16"\u2029" -
REMOVED_CHARACTERS =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 40
Characters we want to truncate from text
[ZERO_WIDTH_SPACE, LEFT_TO_RIGHT_MARK, RIGHT_TO_LEFT_MARK].join
-
RIGHT_TO_LEFT_MARK =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 37
Signifies text is read right to left
"\u200f" -
SQUEEZED_SPACES =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 22
Whitespace we want to substitute with plain spaces
" \n\f\t\v#{LINE_SEPERATOR}#{PARAGRAPH_SEPERATOR}".freeze
-
TRAILING_SPACES =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 28
Any whitespace at the end of text
/#{BREAKING_SPACES}+\z/ -
ZERO_WIDTH_SPACE =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 31
“Invisible” space character
"\u200b"
Instance Method Summary
-
#normalize_spacing(text) ⇒ String
Normalizes the spacing of a node’s text to be similar to what matchers might expect.
-
#normalize_visible_spacing(text) ⇒ String
Variant on
Capybara::Node::Normalizer#normalize_spacingthat targets the whitespace of visible elements only.
Instance Method Details
#normalize_spacing(text) ⇒ String
Normalizes the spacing of a node’s text to be similar to what matchers might expect.
# 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 Capybara::Node::Normalizer#normalize_spacing that targets the whitespace of visible elements only.
# 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