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 Element
s by removing various unicode spacing and directional markings.
Constant Summary
-
BREAKING_SPACES =
All spaces except for NBSP
"[[:space:]&&[^#{NON_BREAKING_SPACE}]]".freeze
-
EMPTY_LINES =
Matches multiple empty lines
/[\ \n]*\n[\ \n]*/
-
LEADING_SPACES =
Any whitespace at the front of text
/\A#{BREAKING_SPACES}+/
-
LEFT_TO_RIGHT_MARK =
Signifies text is read left to right
"\u200e"
-
LINE_SEPERATOR =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 15"\u2028"
-
NON_BREAKING_SPACE =
Unicode for NBSP, or
"\u00a0"
-
PARAGRAPH_SEPERATOR =
# File 'lib/capybara/node/whitespace_normalizer.rb', line 16"\u2029"
-
REMOVED_CHARACTERS =
Characters we want to truncate from text
[ZERO_WIDTH_SPACE, LEFT_TO_RIGHT_MARK, RIGHT_TO_LEFT_MARK].join
-
RIGHT_TO_LEFT_MARK =
Signifies text is read right to left
"\u200f"
-
SQUEEZED_SPACES =
Whitespace we want to substitute with plain spaces
" \n\f\t\v#{LINE_SEPERATOR}#{PARAGRAPH_SEPERATOR}".freeze
-
TRAILING_SPACES =
Any whitespace at the end of text
/#{BREAKING_SPACES}+\z/
-
ZERO_WIDTH_SPACE =
“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_spacing
that 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