Skip to content

Commit b0d58b8

Browse files
committed
fix preserve whitespace, fix unstable formatting result when node was broken after first text element
1 parent a1e7a3c commit b0d58b8

6 files changed

Lines changed: 37 additions & 1458 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Adding this flag before a tag will preserve from whitespace and/or attribute wra
5050
<div></div>
5151
```
5252

53-
2. Preserve only from whitespace processing
53+
2. Preserve only from whitespace processing. This excludes no indentation.
5454

5555
```html
5656
<!--prettyhtml-preserve-whitespace-->

packages/prettyhtml-formatter/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function format(options) {
111111
level--
112112
}
113113

114-
if (node.data && node.data.ignore) {
114+
if (node.data && (node.data.ignore || node.data.preserveWhitespace)) {
115115
return visit.SKIP
116116
}
117117

@@ -249,8 +249,15 @@ function format(options) {
249249
* 2. We put template expressions on new lines
250250
*/
251251

252-
// remove trailing whitespaces and tabs because a newline is inserted before
253252
if (is('text', prevChild)) {
253+
// In order to produce a stable result we need to indent the text on a newline
254+
// when the subsequent node is from type "element"
255+
// this is only needed because we want to indent leading text
256+
if (is(child) && index === 1) {
257+
prevChild.value =
258+
single + repeat(indent, indentLevel) + prevChild.value
259+
}
260+
// remove trailing whitespaces and tabs because a newline is inserted before
254261
prevChild.value = prevChild.value.replace(/[ \t]+$/, '')
255262
}
256263
// remove leading whitespaces and tabs because a newline is inserted before
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<section>
3+
<p>Here is a link <a href='https://randomuser.me/' target='_blank'>Random User Generator</a>. Here is another link <a href='https://randomuser.me/' target='_blank'>Random User Generator</a> but this time is followed by more text</p>
4+
</section>
5+
</template>

packages/prettyhtml-formatter/test/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function each(fixture) {
2626
input: vfile.readSync(path.join(base, 'input.html'))
2727
}
2828

29-
t.plan(6)
29+
t.plan(5)
3030

3131
check(t, fixture, opts)
3232
})
@@ -53,7 +53,6 @@ function check(t, fixture, options) {
5353
proc.process(options.input, function(err, vFile) {
5454
t.falsy(err, 'shouldn’t throw')
5555
t.is(options.input.messages.length, 0, 'shouldn’t warn')
56-
t.snapshot(vFile.contents)
5756
// run again with the result to ensure that our formatter is stable
5857
proc.process(vFile.contents, function(err, vFile) {
5958
t.falsy(err, 'shouldn’t throw')

0 commit comments

Comments
 (0)