Skip to content

Commit e104abf

Browse files
committed
Fix issue where multiline attribute values could trigger indentation of children in non-pretty mode. /cc @rauchg
1 parent 796da37 commit e104abf

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

.eslintrc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"indent": [2, "tab", {"SwitchCase": 1}],
2626
"no-mixed-spaces-and-tabs": [2, "smart-tabs"],
2727
"no-trailing-spaces": [2, { "skipBlankLines": true }],
28-
"max-nested-callbacks": [2, 3],
2928
"no-eval": 2,
3029
"no-implied-eval": 2,
3130
"no-new-func": 2,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"build": "npm run -s transpile && npm run -s transpile:jsx",
1010
"transpile": "rollup -c rollup.config.js -m ${npm_package_main}.map -f umd -n $npm_package_amdName $npm_package_jsnext_main -o $npm_package_main",
1111
"transpile:jsx": "ENTRY=jsx rollup -c rollup.config.js -m dist/jsx.js.map -f umd -n $npm_package_amdName src/jsx.js -o dist/jsx.js",
12-
"test": "eslint {src,test} && mocha --compilers js:babel-register test/**/*.js",
12+
"test": "eslint src test && mocha --compilers js:babel-register test/**/*.js",
1313
"prepublish": "npm run build",
1414
"release": "npm run build && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
1515
},

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export default function renderToString(vnode, context, opts, inner, isSvgMode) {
156156
// account for >1 multiline attribute
157157
let sub = s.replace(/^\n\s*/, ' ');
158158
if (sub!==s && !~sub.indexOf('\n')) s = sub;
159-
else if (~s.indexOf('\n')) s += '\n';
159+
else if (pretty && ~s.indexOf('\n')) s += '\n';
160160

161161
s = `<${nodeName}${s}>`;
162162

@@ -184,7 +184,7 @@ export default function renderToString(vnode, context, opts, inner, isSvgMode) {
184184
if (ret) pieces.push(ret);
185185
}
186186
}
187-
if (hasLarge) {
187+
if (pretty && hasLarge) {
188188
for (let i=pieces.length; i--; ) {
189189
pieces[i] = '\n' + indentChar + indent(pieces[i], indentChar);
190190
}

test/render.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,39 @@ describe('render', () => {
1414
expect(rendered).to.equal(expected);
1515
});
1616

17+
describe('whitespace', () => {
18+
it('should omit whitespace between elements', () => {
19+
let children = [];
20+
for (let i=0; i<1000; i++) {
21+
children.push(Math.random()>.5 ? String(i) : h('x-'+String(i), null, i));
22+
}
23+
let rendered = render(
24+
<div class="foo">
25+
x
26+
<a>a</a>
27+
<b>b</b>
28+
c
29+
{children}
30+
d
31+
</div>
32+
);
33+
34+
expect(rendered).not.to.contain(/\s/);
35+
});
36+
37+
it('should not indent when attributes contain newlines', () => {
38+
let rendered = render(
39+
<div class={`foo\n\tbar\n\tbaz`}>
40+
<a>a</a>
41+
<b>b</b>
42+
c
43+
</div>
44+
);
45+
46+
expect(rendered).to.equal(`<div class="foo\n\tbar\n\tbaz"><a>a</a><b>b</b>c</div>`);
47+
});
48+
});
49+
1750
it('should omit falsey attributes', () => {
1851
let rendered = render(<div a={null} b={undefined} c={false} />),
1952
expected = `<div></div>`;

0 commit comments

Comments
 (0)