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

Consistently handle inline elements with spaces #281

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ function flankingWhitespace (node) {
var trailing = ''

if (!node.isBlock) {
var hasLeading = /^[ \r\n\t]/.test(node.textContent)
var hasTrailing = /[ \r\n\t]$/.test(node.textContent)
var hasLeading = /^\s/.test(node.textContent)
var hasTrailing = /\s$/.test(node.textContent)
var blankWithSpaces = node.isBlank && hasLeading && hasTrailing

if (hasLeading && !isFlankedByWhitespace('left', node)) {
leading = ' '
}
if (hasTrailing && !isFlankedByWhitespace('right', node)) {

if (!blankWithSpaces && hasTrailing && !isFlankedByWhitespace('right', node)) {
trailing = ' '
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,20 @@ <h2>This is a header.</h2>
<pre class="expected">![](http://example.com/logo.png)</pre>
</div>

<div class="case" data-name="text separated by a space in an element">
<div class="input">
<p>Foo<span> </span>Bar</p>
</div>
<pre class="expected">Foo Bar</pre>
</div>

<div class="case" data-name="text separated by a non-breaking space in an element">
<div class="input">
<p>Foo<span>&nbsp;</span>Bar</p>
</div>
<pre class="expected">Foo Bar</pre>
</div>

<!-- /TEST CASES -->

<script src="turndown-test.browser.js"></script>
Expand Down