Skip to content

Commit

Permalink
fix(mock-doc): no indentation w/in whitespace sensitive elements
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Sep 24, 2020
1 parent aea79a4 commit 46ff715
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/mock-doc/serialize-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ function serializeToHtml(node: Node, opts: SerializeNodeToHtmlOptions, output: S
) {
// skip over empty text nodes
} else {
if (opts.indentSpaces > 0 && ignoreTag === false) {
const isWithinWhitespaceSensitiveNode =
opts.newLines || opts.indentSpaces > 0 ? isWithinWhitespaceSensitive(node) : false;

if (!isWithinWhitespaceSensitiveNode && opts.indentSpaces > 0 && ignoreTag === false) {
output.indent = output.indent + opts.indentSpaces;
}

Expand All @@ -239,8 +242,6 @@ function serializeToHtml(node: Node, opts: SerializeNodeToHtmlOptions, output: S
}

if (ignoreTag === false) {
const isWithinWhitespaceSensitiveNode =
opts.newLines || opts.indentSpaces > 0 ? isWithinWhitespaceSensitive(node) : false;
if (opts.newLines && !isWithinWhitespaceSensitiveNode) {
output.text.push('\n');
output.currentLineWidth = 0;
Expand Down
9 changes: 9 additions & 0 deletions src/mock-doc/test/serialize-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ describe('serializeNodeToHtml', () => {
expect(html).toBe(`<div><span>var </span><b> value </b><span> =</span><code> 88 </code>;</div>`);
});

it('do not add extra indentation when pretty print <pre><code>', () => {
const elm = doc.createElement('div');

elm.innerHTML = `<section><article><pre><code><span>88</span></code></pre></article></section>`;

const html = serializeNodeToHtml(elm, { prettyHtml: true });
expect(html).toBe(`<section>\n <article><pre><code><span>88</span></code></pre>\n </article>\n</section>`);
});

it('do not pretty print <pre><code>', () => {
const elm = doc.createElement('div');

Expand Down

0 comments on commit 46ff715

Please sign in to comment.