Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace whitespace characters in text also #7

Merged
merged 1 commit into from Dec 22, 2016

Conversation

Projects
None yet
2 participants
@leereamsnyder
Copy link
Contributor

commented Dec 19, 2016

per https://adactio.com/journal/11632

Any selected text that includes the last two words of a paragraph fails to match. I’ve tried tweaking the script, but I’m stumped. If you fancy having a go, please have at it.

I'll take a stab.

What I'm seeing is the text that gets passed in the #hash has a non-breaking space character in between the last two words, which matches the markup.

However, in getElementsByText the passed text is being compared to the .innerText or .textContent of the node like so:

(element.innerText || element.textContent || '').replace(/\s+/g, ' ').indexOf(text)

.replace(/\s+/g, ' ') will find any whitespace character, crucially including non-breaking space characters, and replace them all with a regular space.

However text in .indexOf(text) will still have the non-breaking space. So the strings will be technically different and you won't find a match.

Run the same .replace(/\s+/g, ' ') on both the element's innerText and the passed text and it appears to work.

(You might also be able to lose the replace in both places, but I imagine it's there for a reason.)

@adactio adactio merged commit ffa5811 into adactio:master Dec 22, 2016

@adactio

This comment has been minimized.

Copy link
Owner

commented Dec 22, 2016

Brilliant! Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.