Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bf0b060

Browse files
committedJun 2, 2020
Add option to preserve whitespace for certain elements
Add an option to preserve whitespace/indentation for certain elements. Defaults to `['pre']` Signed-off-by: Sven Tschui <[email protected]>
1 parent 09d7675 commit bf0b060

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed
 

‎src/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const noop = () => {};
2020
* @param {Boolean} [options.shallow=false] If `true`, renders nested Components as HTML elements (`<Foo a="b" />`).
2121
* @param {Boolean} [options.xml=false] If `true`, uses self-closing tags for elements without children.
2222
* @param {Boolean} [options.pretty=false] If `true`, adds whitespace for readability
23+
* @param {Boolean} [options.preserveWhitespace=['pre']] Array of HTML tags for which to preserve indentation.
2324
*/
2425
renderToString.render = renderToString;
2526

@@ -51,7 +52,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
5152
context = context || {};
5253
opts = opts || {};
5354

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

5758
// #text nodes
@@ -73,7 +74,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
7374
for (let i = 0; i < children.length; i++) {
7475
rendered += (i > 0 && pretty ? '\n' : '') + renderToString(children[i], context, opts, opts.shallowHighOrder!==false, isSvgMode, selectValue);
7576
}
76-
return rendered;
77+
return indent(rendered, indentChar);
7778
}
7879
else {
7980
let rendered;
@@ -269,7 +270,7 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
269270
}
270271
if (pretty && hasLarge) {
271272
for (let i=pieces.length; i--; ) {
272-
pieces[i] = '\n' + indentChar + indent(pieces[i], indentChar);
273+
pieces[i] = '\n' + indentChar + pieces[i];
273274
}
274275
}
275276
}

‎test/pretty.js

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ describe('pretty', () => {
2121
expect(rendered).to.equal(`<section><a href="/foo">foo</a>bar<p>hello</p></section>`);
2222
});
2323

24+
it('should preserve indentation of pre content', () => {
25+
let rendered = prettyRender(
26+
<div>
27+
<pre dangerouslySetInnerHTML={{ __html: 'foo\nbar' }} />
28+
</div>
29+
);
30+
31+
expect(rendered).to.equal(`<div>
32+
<pre>foo
33+
bar</pre>
34+
</div>`);
35+
});
36+
2437
it('should render whitespace when pretty=true', () => {
2538
let rendered = prettyRender(
2639
<section>

‎test/render.js

+11
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,17 @@ describe('render', () => {
712712
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>');
713713
});
714714

715+
it('should preserve indentation of pre content', () => {
716+
let rendered = render(
717+
<div>
718+
<pre dangerouslySetInnerHTML={{ __html: 'foo\nbar' }} />
719+
</div>
720+
);
721+
722+
expect(rendered).to.equal(`<div><pre>foo
723+
bar</pre></div>`);
724+
});
725+
715726
it('should skip Fragment even if it has props', () => {
716727
let html = render(
717728
<div>

0 commit comments

Comments
 (0)
Please sign in to comment.