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

Add option to preserve whitespace for certain elements #154

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const noop = () => {};
* @param {Boolean} [options.shallow=false] If `true`, renders nested Components as HTML elements (`<Foo a="b" />`).
* @param {Boolean} [options.xml=false] If `true`, uses self-closing tags for elements without children.
* @param {Boolean} [options.pretty=false] If `true`, adds whitespace for readability
* @param {Boolean} [options.preserveWhitespace=['pre']] Array of HTML tags for which to preserve indentation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: type should be string array here 👍

*/
renderToString.render = renderToString;

Expand Down Expand Up @@ -51,7 +52,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
context = context || {};
opts = opts || {};

let pretty = opts.pretty,
let pretty = (opts.preserveWhitespace || ['pre']).indexOf(nodeName) === -1 && opts.pretty,
indentChar = pretty && typeof pretty==='string' ? pretty : '\t';

// #text nodes
Expand All @@ -73,7 +74,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
for (let i = 0; i < children.length; i++) {
rendered += (i > 0 && pretty ? '\n' : '') + renderToString(children[i], context, opts, opts.shallowHighOrder!==false, isSvgMode, selectValue);
}
return rendered;
return pretty ? indent(rendered, indentChar) : rendered;
Copy link
Member Author

@sventschui sventschui Jun 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to move the call to indent here to not indent HTML content but do indent Fragment children. I'm not 100% sure this doesn't break other things. I'll try to add some more tests.

EDIT yes indeed it breaks text indentation :(

}
else {
let rendered;
Expand Down Expand Up @@ -269,7 +270,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
}
if (pretty && hasLarge) {
for (let i=pieces.length; i--; ) {
pieces[i] = '\n' + indentChar + indent(pieces[i], indentChar);
pieces[i] = '\n' + indentChar + pieces[i];
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,34 @@ describe('pretty', () => {
expect(rendered).to.equal(`<section><a href="/foo">foo</a>bar<p>hello</p></section>`);
});

it('should preserve indentation of pre content', () => {
let rendered = prettyRender(
<div>
<pre dangerouslySetInnerHTML={{ __html: 'foo\nbar' }} />
</div>
);

expect(rendered).to.equal(`<div>
<pre>foo
bar</pre>
</div>`);
});

it('should indent text content', () => {
let rendered = prettyRender(
<div>
<div dangerouslySetInnerHTML={{ __html: 'foo\nbar' }} />
</div>
);

expect(rendered).to.equal(`<div>
<div>
foo
bar
</div>
</div>`);
});

it('should render whitespace when pretty=true', () => {
let rendered = prettyRender(
<section>
Expand Down
11 changes: 11 additions & 0 deletions test/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,17 @@ describe('render', () => {
expect(html).to.equal('<div>\n\t<div>foo</div>\n\t<div>bar</div>\n\t<div>\n\t\t<div>baz</div>\n\t\t<div>quux</div>\n\t</div>\n</div>');
});

it('should preserve indentation of pre content', () => {
let rendered = render(
<div>
<pre dangerouslySetInnerHTML={{ __html: 'foo\nbar' }} />
</div>
);

expect(rendered).to.equal(`<div><pre>foo
bar</pre></div>`);
});

it('should skip Fragment even if it has props', () => {
let html = render(
<div>
Expand Down