Skip to content

Commit

Permalink
fix: Preserve whitespace in certain HTML elements (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
toufic-m authored and adampash committed Mar 19, 2019
1 parent 2a3ade7 commit a250f40
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/utils/text/fixtures/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,16 @@ const HTML = {
`,
after: 'What do you think?',
},
normalizeSpacesPreserve: {
before: `
<div>
<p>What do you think?</p>
<pre> What happens to spaces? </pre>
</div>
`,
after:
'<div> <p>What do you think?</p> <pre> What happens to spaces? </pre> </div>',
},

// cleanHeaders
cleanFirstHeds: {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/text/normalize-spaces.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const NORMALIZE_RE = /\s{2,}/g;
const NORMALIZE_RE = /\s{2,}(?![^<>]*<\/(pre|code|textarea)>)/g;

export default function normalizeSpaces(text) {
return text.replace(NORMALIZE_RE, ' ').trim();
Expand Down
7 changes: 7 additions & 0 deletions src/utils/text/normalize-spaces.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ describe('normalizeSpaces(text)', () => {
);
assert.equal(result, HTML.normalizeSpaces.after);
});

it('preserves spaces in preformatted text blocks', () => {
const $ = cheerio.load(HTML.normalizeSpacesPreserve.before);

const result = normalizeSpaces($.html());
assert.equal(result, HTML.normalizeSpacesPreserve.after);
});
});

0 comments on commit a250f40

Please sign in to comment.