Closed nonara closed 3 years ago
@taoqf Ready to go. Let me know if you have any questions.
So many thanks, I don't have much time on this rep recently, so I am going to merge your pr and publish another version now.
No problem! I know how it is. Thanks for the quick merge!
Abstract
The following PR takes a holistic approach to mitigating issue #115 by implementing HTML-specific trim functionality, where necessary. Its aim is to provide a more accurate correction for the
trim()
issue by replicating trim, while explicitly preserving only single leading or trailing non-breaking whitespace, where present.Background
In addition to the already corrected area (
structuredText
) a second affected area was found.The purpose of
HTMLElement#removeWhitespace
is to remove unnecessary whitespace nodes and otherwise cleanup the text. To do this, it callstrim()
onTextNode
rawText. Unfortuntately, this is too greedy.Trim rightly removes repeating spaces, tabs, and line-breaks, which are often simply junk from multi-line HTML formatting. Unfortunately, it also removes legitimate, single, non-breaking space at the beginning or end of the text in a
TextNode
.Put more simply, it's overcorrecting in some scenarios. Here are some examples:
Solution
I've replaced the two calls to
trim()
with a cached accessor on eachTextNode
. The accessor performs the same action as trim, with the exception that it will allow a single non-breaking space before and after the text, if one is present.Associated Issues
taoqf/node-html-parser#115 crosstype/node-html-markdown#9