Skip to content

Commit

Permalink
Join adjacent text nodes (#109)
Browse files Browse the repository at this point in the history
* Join adjacent text nodes in pretty mode

* Refactor pretty printing of adjacent text nodes
  • Loading branch information
marvinhagemeister authored and developit committed Jul 15, 2019
1 parent e87b1ba commit b832c20
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
26 changes: 25 additions & 1 deletion server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,37 @@ function renderToString(vnode, context, opts, inner, isSvgMode, selectValue) {
}
else if (props && getChildren(children = [], props.children).length) {
let hasLarge = pretty && ~s.indexOf('\n');
let lastWasText = false;

for (let i=0; i<children.length; i++) {
let child = children[i];

if (child!=null && child!==false) {
let childSvgMode = nodeName==='svg' ? true : nodeName==='foreignObject' ? false : isSvgMode,
ret = renderToString(child, context, opts, true, childSvgMode, selectValue);

if (pretty && !hasLarge && isLargeString(ret)) hasLarge = true;
if (ret) pieces.push(ret);

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

// We merge adjacent text nodes, otherwise each piece would be printed
// on a new line.
if (lastWasText && isText) {
pieces[pieces.length -1] += ret;
}
else {
pieces.push(ret);
}

lastWasText = isText;
}
else {
pieces.push(ret);
}
}
}
}
if (pretty && hasLarge) {
Expand Down
2 changes: 1 addition & 1 deletion server/test/jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import sinonChai from 'sinon-chai';
chai.use(sinonChai);

// tag to remove leading whitespace from tagged template literal
function dedent([str]) {
export function dedent([str]) {
return str.split( '\n'+str.match(/^\n*(\s+)/)[1] ).join('\n').replace(/(^\n+|\n+\s*$)/g, '');
}

Expand Down
88 changes: 88 additions & 0 deletions server/test/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { render } from '../src/jsx';
import { h, Fragment } from 'preact';
import chai, { expect } from 'chai';
import sinonChai from 'sinon-chai';
import { dedent } from './jsx';
chai.use(sinonChai);

describe('pretty', () => {
Expand Down Expand Up @@ -96,4 +97,91 @@ describe('pretty', () => {
</div>
)).to.equal(`<div>\n\t<div>A</div>\n\t<div>B</div>\n</div>`);
});

it('should join adjacent text nodes', () => {
expect(prettyRender(
<div>hello{' '} <b /></div>
)).to.equal(dedent`
<div>
hello
<b></b>
</div>
`);

expect(prettyRender(
<div>hello{' '} <b />{'a'}{'b'}</div>
)).to.equal(dedent`
<div>
hello
<b></b>
ab
</div>
`);
});

it('should join adjacent text nodeswith Fragments', () => {
expect(prettyRender(
<div><Fragment>foo</Fragment>bar{' '} <b /></div>
)).to.equal(dedent`
<div>
foobar
<b></b>
</div>
`);
});

it('should collapse whitespace', () => {
expect(prettyRender(
<p>a<a>b</a></p>
)).to.equal(dedent`
<p>
a
<a>b</a>
</p>
`);

expect(prettyRender(
<p>
a{' '}
<a>b</a>
</p>
)).to.equal(dedent`
<p>
a
<a>b</a>
</p>
`);

expect(prettyRender(
<p>
a{''}
<a>b</a>
</p>
)).to.equal(dedent`
<p>
a
<a>b</a>
</p>
`);

expect(prettyRender(
<p>a <a>b</a></p>
)).to.equal(dedent`
<p>
a\
<a>b</a>
</p>
`);

expect(prettyRender(<a> b </a>)).to.equal(dedent`
<a> b </a>
`);

expect(prettyRender(<p><b /> a </p>)).to.equal(dedent`
<p>
<b></b>
\ a\
</p>
`);
});
});

0 comments on commit b832c20

Please sign in to comment.