Skip to content

Commit

Permalink
Merge pull request #281 from kevindew/vanishing-or-duplicating-spacing
Browse files Browse the repository at this point in the history
Consistently handle inline elements with spaces
  • Loading branch information
domchristie authored Aug 23, 2019
2 parents 80297ce + b73068c commit cae7098
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
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

0 comments on commit cae7098

Please sign in to comment.