question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Collapse whitespace issue with  

See original GitHub issue

Had an issue that stemmed from Cypress testing library but debugged into Dom testing library. The issue is this function.

When searching for text with a non breaking space such as “٤ UK£”, the regex replaces the nbsp into a space. It’s fairly obvious why the regex was used, but it’s effectively converting all types of spaces into a “regular space”. This then causes the text match to fail.

In my opinion, that’s an unintentional side effect of collapseWhitespace.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
astorijecommented, May 24, 2021

I’m getting a similar issue with non-breaking spaces and I can’t seem to find a way to target them easily. This is what I have tried:

test('non-breaking space', () => {
  render(<div>&nbsp;</div>);

  // None of the following pass:
  expect(screen.getByText(' ')).toBeInTheDocument(); // Regular space
  expect(screen.getByText(' ')).toBeInTheDocument(); // Non-breaking space
  expect(screen.getByText(/ /)).toBeInTheDocument(); // Regular space
  expect(screen.getByText(/ /)).toBeInTheDocument(); // Non-breaking space
  expect(screen.getByText(/\u00a0/)).toBeInTheDocument();
  expect(screen.getByText('&nbsp;')).toBeInTheDocument();
  expect(screen.getByText(/&nbsp;/)).toBeInTheDocument();
});

I have not tried using a custom text matcher function or a custom normalizer, but I feel like there is probably an easy built-in way to do this without going too crazy on customizations. Any ideas?

4reactions
herecydevcommented, Jan 31, 2021

I absoutely can try, there doesn’t seem to be a way to configure this globally though (I have to do this in many places). Is that something that you could see being added?

I’m not sure I agree with the docs here:

By default, normalization consists of trimming whitespace from the start and end of text, and collapsing multiple adjacent whitespace characters into a single space.

Consider the following " a b ". I would expect the first and last spaces to be removed but there are no “multiple adjacent whitespace characters”. So the inner space (non breaking) should have been preserved.

I’m using the Intl.NumberFormat in my application which is outputting that nbsp, and then in Cypress I’m querying for that text which is highlighting that mismatch. Would the default normalizer benefit from collapsing multiple spaces but preserving the “type of space”?

Alternatively, should the same normalization be applied to the textContent?

Read more comments on GitHub >

github_iconTop Results From Across the Web

&nbsp and HTML Space Challenges and Tricks | Mailtrap Blog
Nbsp stands for non-breaking space, meaning that strings separated with this entity will not be separated and put into separate lines. But ...
Read more >
Non-collapsing but still (line-)breakable space in HTML?
Regular whitespace gets collapsed (a run of spaces looks the same as a single space), and at the non-breaking space (&nbsp) the line...
Read more >
Non-breaking space - Wikipedia
In word processing and digital typesetting, a non-breaking space, , also called NBSP, ... consecutive whitespace characters from collapsing into a single space....
Read more >
Non-Breaking Space in HTML
create white space between words or web page elements ... To insert a non-breaking space you would use:   ... Problems with Non-Breaking...
Read more >
359303 - Non-breaking spaces (nbsp) not copied as such
Reproducible: Always Steps to Reproduce: 1. Copy text containing a non-breaking space character (  or UTF-8 0xA0) to the clipboard. 2. Paste into...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found