Don't format whitespace around phrasing content elements

#2 · open · 4 comments

View on GitHub ↗

johno

Comments

TroyAlford

Can you add some description around what this issue is? Or - better yet - provide a couple test cases which should pass?

johno

Sure thing. I'm not 100% on how we want to approach this or tackle formatting, but the thought was handling [phrasing content](https://www.w3.org/TR/html5/dom.html#phrasing-content) tags a bit differently (similarly to how `a` tags are handled). I tossed in a quick [failing test](https://github.com/johnotander/prettify-html/blob/4c3e45415c7fb7d2b25dda6554f58a16e321a0a9/test.js#L53), but not even sure if the output there is exactly what we want. I'd be interested in your thoughts on how we could best format these types of cases.

TroyAlford

I would think that in the failing test, we'd actually expect https://github.com/johnotander/prettify-html/blob/4c3e45415c7fb7d2b25dda6554f58a16e321a0a9/test.js#L62-L67 to be: `<span> qux </span>` instead of split onto multiple lines? I've seen other parsers which break on content like: `<p>Sentence which contains a <a href="link">link</a> in the middle</p>` In specific - the problem seems to be consistently killing the space before / after the `<a>` tag. One way of approaching this might be to inspect siblings as they're parsed, and watch for scenarios in which you are parsing `text` immediately before/after a `tag`. If you are - and the `text` begins/ends with whitespace, it should maintain one space at lead/tail.

TroyAlford

To expand on the above - the most common problems I've seen are the following (imho _wrong_) transformations: `<p>Sentence which contains a <a href="link">link</a> in the middle</p>` gets parsed in one of the following wrong ways: ``` <p>Sentence which contains a<a href="link">link</a>in the middle</p>` ^ ^ ``` or ``` <p>Sentence which contains a&nbsp;<a href="link">link</a>&nbsp;in the middle</p>` ^^^^^^ ^^^^^^ ``` The first case is wrong because it removes meaningful whitespace. The second case is wrong because it _injects_ meaningful whitespace (and may cause word-breaks not to occur in the correct location). In both cases, the intention is that the tag is preceeded / proceeded by a single, normal space. From my perspective, the "right" action, here, is to detect whether or not there is whitespace between text and a tag, and if so - inject a single space character, or a `\n`. Either of these, to a browser, is rendered identically - as a single space. Either of the example transformations, above, changes the meaning.