Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/rude-apes-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'preact-render-to-string': patch
---

Preserve whitespace in `<pre>`/`<textarea>` when using `pretty: true`
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 15 additions & 6 deletions src/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const UNNAMED = [];

const EMPTY_ARR = [];
const EMPTY_STR = '';
const PRESERVE_WHITESPACE_TAGS = new Set(['pre', 'textarea']);

/**
* Render Preact JSX + Components to a pretty-printed HTML-like string.
Expand Down Expand Up @@ -216,6 +217,11 @@ function _renderToStringPretty(
propChildren,
html;

const shouldPreserveWhitespace =
pretty &&
typeof nodeName === 'string' &&
PRESERVE_WHITESPACE_TAGS.has(nodeName);

if (props) {
let attrs = Object.keys(props);

Expand Down Expand Up @@ -349,15 +355,17 @@ function _renderToStringPretty(
let children;
if (html) {
// if multiline, indent.
if (pretty && isLargeString(html)) {
if (pretty && !shouldPreserveWhitespace && isLargeString(html)) {
html = '\n' + indentChar + indent(html, indentChar);
}
s = s + html;
} else if (
propChildren != null &&
getChildren((children = []), propChildren).length
) {
let hasLarge = pretty && ~s.indexOf('\n');
const shouldPrettyFormatChildren =
pretty && !shouldPreserveWhitespace && typeof nodeName === 'string';
let hasLarge = shouldPrettyFormatChildren && ~s.indexOf('\n');
let lastWasText = false;

for (let i = 0; i < children.length; i++) {
Expand All @@ -379,11 +387,12 @@ function _renderToStringPretty(
selectValue
);

if (pretty && !hasLarge && isLargeString(ret)) hasLarge = true;
if (shouldPrettyFormatChildren && !hasLarge && isLargeString(ret))
hasLarge = true;

// Skip if we received an empty string
if (ret) {
if (pretty) {
if (shouldPrettyFormatChildren) {
let isText = ret.length > 0 && ret[0] != '<';

// We merge adjacent text nodes, otherwise each piece would be printed
Expand All @@ -401,7 +410,7 @@ function _renderToStringPretty(
}
}
}
if (pretty && hasLarge) {
if (shouldPrettyFormatChildren && hasLarge) {
for (let i = pieces.length; i--; ) {
pieces[i] = '\n' + indentChar + indent(pieces[i], indentChar);
}
Expand All @@ -419,7 +428,7 @@ function _renderToStringPretty(
if (isVoid && !children && !html) {
s = s.replace(/>$/, ' />');
} else {
if (pretty && ~s.indexOf('\n')) s = s + '\n';
if (pretty && !shouldPreserveWhitespace && ~s.indexOf('\n')) s = s + '\n';
s = s + `</${nodeName}>`;
}

Expand Down
19 changes: 19 additions & 0 deletions test/pretty.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ describe('pretty', () => {
);
});

it('should not add whitespace to pre tag children', () => {
let rendered = prettyRender(
<pre>
<code>hello</code>
</pre>,
{ jsx: false }
);

expect(rendered).to.equal(`<pre><code>hello</code></pre>`);
});

it('should maintain whitespace in textarea tag', () => {
let rendered = prettyRender(<textarea>{' hello\nworld '}</textarea>, {
jsx: false
});

expect(rendered).to.equal(`<textarea> hello\nworld </textarea>`);
});

describe('Attribute casing', () => {
it('should have correct SVG casing', () => {
for (let name in svgAttributes) {
Expand Down
7 changes: 7 additions & 0 deletions test/render.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,13 @@ describe('render', () => {
expect(rendered).to.equal(expected);
});

it('should serialize textarea value with leading/trailing whitespace', () => {
let rendered = render(<textarea value={` a\nb `} />),
expected = `<textarea> a\nb </textarea>`;

expect(rendered).to.equal(expected);
});

it('should escape textarea value', () => {
let rendered = render(<textarea value={`a&b"c`} />),
expected = `<textarea>a&amp;b&quot;c</textarea>`;
Expand Down