From d97fe0a1e0a2a3be17214f4d7dec39fb365b8125 Mon Sep 17 00:00:00 2001 From: Ika Date: Tue, 11 Dec 2018 12:43:53 +0800 Subject: [PATCH] feat(html): preserve surrounding linebreaks (#5596) --- src/language-html/utils.js | 23 +- .../__snapshots__/jsfmt.spec.js.snap | 201 +++++++++++++----- .../__snapshots__/jsfmt.spec.js.snap | 3 +- .../html_svg/__snapshots__/jsfmt.spec.js.snap | 8 +- .../__snapshots__/jsfmt.spec.js.snap | 150 ++++++++++--- tests/html_tags/tags.html | 5 + .../html_vue/__snapshots__/jsfmt.spec.js.snap | 15 +- .../__snapshots__/jsfmt.spec.js.snap | 44 ++++ .../surrounding-linebreak.html | 17 ++ 9 files changed, 366 insertions(+), 100 deletions(-) create mode 100644 tests/html_whitespace/surrounding-linebreak.html diff --git a/src/language-html/utils.js b/src/language-html/utils.js index fe1b6d31..5b263ece 100644 --- a/src/language-html/utils.js +++ b/src/language-html/utils.js @@ -273,7 +273,12 @@ function forceBreakContent(node) { (node.type === "element" && node.children.length !== 0 && (["body", "template", "script", "style"].indexOf(node.name) !== -1 || - node.children.some(child => hasNonTextChild(child)))) + node.children.some(child => hasNonTextChild(child)))) || + (node.firstChild && + node.firstChild === node.lastChild && + (hasLeadingLineBreak(node.firstChild) && + (!node.lastChild.isTrailingSpaceSensitive || + hasTrailingLineBreak(node.lastChild)))) ); } @@ -291,7 +296,7 @@ function preferHardlineAsLeadingSpaces(node) { return ( preferHardlineAsSurroundingSpaces(node) || (node.prev && preferHardlineAsTrailingSpaces(node.prev)) || - isCustomElementWithSurroundingLineBreak(node) + hasSurroundingLineBreak(node) ); } @@ -299,19 +304,7 @@ function preferHardlineAsTrailingSpaces(node) { return ( preferHardlineAsSurroundingSpaces(node) || (node.type === "element" && node.fullName === "br") || - isCustomElementWithSurroundingLineBreak(node) - ); -} - -function isCustomElementWithSurroundingLineBreak(node) { - return isCustomElement(node) && hasSurroundingLineBreak(node); -} - -function isCustomElement(node) { - return ( - node.type === "element" && - !node.namespace && - (node.name.includes("-") || /[A-Z]/.test(node.name[0])) + hasSurroundingLineBreak(node) ); } diff --git a/tests/html_angular/__snapshots__/jsfmt.spec.js.snap b/tests/html_angular/__snapshots__/jsfmt.spec.js.snap index de9d3daa..2bfac28a 100644 --- a/tests/html_angular/__snapshots__/jsfmt.spec.js.snap +++ b/tests/html_angular/__snapshots__/jsfmt.spec.js.snap @@ -3458,7 +3458,10 @@ can be found in the LICENSE file at http://angular.io/license

My current hero is {{ currentHero.name }}

-

{{ title }}

+

+ {{ title }} + +

The sum of 1 + 1 is {{ 1 + 1 }}

@@ -3476,7 +3479,8 @@ can be found in the LICENSE file at http://angular.io/license [hidden]="isUnchanged")

- {{ title }} changed + {{ title }} + changed

Template input variable expression context (let hero)

@@ -3487,7 +3491,8 @@ can be found in the LICENSE file at http://angular.io/license

Template reference variable expression context (#heroInput)

- Type something: {{ heroInput.value }} + Type something: + {{ heroInput.value }}
top @@ -3496,10 +3501,14 @@ can be found in the LICENSE file at http://angular.io/license

Statement context

Component statement context ( (click)="onSave() )

-
+
+ +

Template $event statement context

-
+
+ +

Template input variable statement context (let hero)

@@ -3523,7 +3532,9 @@ can be found in the LICENSE file at http://angular.io/license
Mental Model
-

+ + +

@@ -3549,17 +3560,25 @@ can be found in the LICENSE file at http://angular.io/license
click me
-{{ clicked }}

- -
Hero Name: {{ name }}
+{{ clicked }}

-

+
+ Hero Name: + + {{ name }} +
+

+ + +

Special


- + top @@ -3571,7 +3590,8 @@ can be found in the LICENSE file at http://angular.io/license

- + + top @@ -3580,8 +3600,10 @@ can be found in the LICENSE file at http://angular.io/license

Buttons

- -

+ + + +



@@ -3604,7 +3626,9 @@ can be found in the LICENSE file at http://angular.io/license -
+
+ +

is the interpolated image.

@@ -3769,14 +3793,20 @@ can be found in the LICENSE file at http://angular.io/license [value]="currentHero.name" (input)="currentHero.name = $event.target.value" /> -without NgModel
- [(ngModel)]
- bindon-ngModel
+without NgModel +
+ +[(ngModel)] +
+ +bindon-ngModel +
-(ngModelChange)="...name=$event"
+(ngModelChange)="...name=$event" +
[value]="!isUnchanged" (change)="isUnchanged = !isUnchanged" /> -| +| +

@@ -3840,7 +3871,8 @@ without NgModel
| | -

+ +

This div should be {{ canSave ? "italic" : "plain" }}, {{ isUnchanged ? "normal weight" : "bold" }} and, @@ -4033,7 +4065,8 @@ without NgModel

Inputs and Outputs

- + + @@ -4050,7 +4083,10 @@ without NgModel
Title through uppercase pipe: {{ title | uppercase }}
-
Title through a pipe chain: {{ title | uppercase | lowercase }}
+
+ Title through a pipe chain: + {{ title | uppercase | lowercase }} +
Birthdate: {{ currentHero?.birthdate | date: "longDate" }}
@@ -4160,7 +4196,9 @@ can be found in the LICENSE file at http://angular.io/license
-
{{ submitMessage }}
+
+ {{ submitMessage }} +

The sum of 1 + 1 is {{ 1 + 1 }}

@@ -4967,7 +5008,8 @@ can be found in the LICENSE file at http://angular.io/license [hidden]="isUnchanged")

- {{ title }} changed + {{ title }} + changed

Template input variable expression context (let hero)

@@ -4978,7 +5020,8 @@ can be found in the LICENSE file at http://angular.io/license

Template reference variable expression context (#heroInput)

- Type something: {{ heroInput.value }} + Type something: + {{ heroInput.value }}
top @@ -4987,10 +5030,14 @@ can be found in the LICENSE file at http://angular.io/license

Statement context

Component statement context ( (click)="onSave() )

-
+
+ +

Template $event statement context

-
+
+ +

Template input variable statement context (let hero)

@@ -5014,7 +5061,9 @@ can be found in the LICENSE file at http://angular.io/license
Mental Model
-

+ + +

@@ -5040,17 +5089,25 @@ can be found in the LICENSE file at http://angular.io/license
click me
-{{ clicked }}

- -
Hero Name: {{ name }}
+{{ clicked }}

-

+
+ Hero Name: + + {{ name }} +
+

+ + +

Special


- + top @@ -5062,7 +5119,8 @@ can be found in the LICENSE file at http://angular.io/license

- + + top @@ -5071,8 +5129,10 @@ can be found in the LICENSE file at http://angular.io/license

Buttons

- -

+ + + +



@@ -5095,7 +5155,9 @@ can be found in the LICENSE file at http://angular.io/license -
+
+ +

is the interpolated image.

@@ -5260,14 +5322,20 @@ can be found in the LICENSE file at http://angular.io/license [value]="currentHero.name" (input)="currentHero.name = $event.target.value" /> -without NgModel
- [(ngModel)]
- bindon-ngModel
+without NgModel +
+ +[(ngModel)] +
+ +bindon-ngModel +
-(ngModelChange)="...name=$event"
+(ngModelChange)="...name=$event" +
[value]="!isUnchanged" (change)="isUnchanged = !isUnchanged" /> -| +| +

@@ -5331,7 +5400,8 @@ without NgModel
| | -

+ +

This div should be {{ canSave ? "italic" : "plain" }}, {{ isUnchanged ? "normal weight" : "bold" }} and, @@ -5524,7 +5594,8 @@ without NgModel

Inputs and Outputs

- + + @@ -5541,7 +5612,10 @@ without NgModel
Title through uppercase pipe: {{ title | uppercase }}
-
Title through a pipe chain: {{ title | uppercase | lowercase }}
+
+ Title through a pipe chain: + {{ title | uppercase | lowercase }} +
Birthdate: {{ currentHero?.birthdate | date: "longDate" }}
@@ -5651,7 +5725,9 @@ can be found in the LICENSE file at http://angular.io/license
-
{{ submitMessage }}
+
+ {{ submitMessage }} +
@@ -9996,7 +10076,9 @@ can be found in the LICENSE file at http://angular.io/license

- + top @@ -10047,7 +10129,9 @@ can be found in the LICENSE file at http://angular.io/license -
+
+ +

@@ -10370,7 +10454,9 @@ bindon-ngModel

Hero Detail removed from DOM (via template) because isActive is false
- + + +
Show with class
@@ -10583,7 +10669,10 @@ bindon-ngModel
Title through uppercase pipe: {{ title | uppercase }}
-
Title through a pipe chain: {{ title | uppercase | lowercase }}
+
+ Title through a pipe chain: + {{ title | uppercase | lowercase }} +
Birthdate: {{ currentHero?.birthdate | date: "longDate" }}
@@ -10706,7 +10795,9 @@ can be found in the LICENSE file at http://angular.io/license
-
{{ submitMessage }}
+
+ {{ submitMessage }} +
-

123

- 123 +

+ 123 +

+ + 123 +
diff --git a/tests/html_tags/__snapshots__/jsfmt.spec.js.snap b/tests/html_tags/__snapshots__/jsfmt.spec.js.snap index 92468054..41f028cc 100644 --- a/tests/html_tags/__snapshots__/jsfmt.spec.js.snap +++ b/tests/html_tags/__snapshots__/jsfmt.spec.js.snap @@ -181,8 +181,15 @@ printWidth: Infinity =====================================output===================================== -
aaaaaaaaaa bbbbbbbbbb cccccccccc
-
aaaaaaaaaa bbbbbbbbbbcccccccccc
+
+ aaaaaaaaaa + bbbbbbbbbb + cccccccccc +
+
+ aaaaaaaaaa + bbbbbbbbbbcccccccccc +
================================================================================ `; @@ -1240,7 +1247,9 @@ ___________________________ || || ___________________________ -
A cow saying, "I'm an expert in my field." The cow is illustrated using preformatted text characters.
+
+ A cow saying, "I'm an expert in my field." The cow is illustrated using preformatted text characters. +
      Foo     Bar
@@ -1743,6 +1752,11 @@ printWidth: 80
   Lorem ipsum dolor sit amet, consectetur adipiscing elit,
   seddoeiusmod.
 
+
+  
+  
+  
+
 
 =====================================output=====================================
 
@@ -1848,8 +1862,12 @@ printWidth: 80 -
string
-
string
+
+
string
+
+
+
string
+
string
@@ -1875,7 +1893,9 @@ printWidth: 80 -|
+| + +


@@ -1916,6 +1936,11 @@ printWidth: 80 Lorem ipsum dolor sit amet, consectetur adipiscing elit, seddoeiusmod.
+ + + + + ================================================================================ `; @@ -2036,6 +2061,11 @@ printWidth: 1 Lorem ipsum dolor sit amet, consectetur adipiscing elit, seddoeiusmod. + + + + + =====================================output=====================================
@@ -2410,6 +2440,17 @@ printWidth: 1 >seddoeiusmod. + + + + + ================================================================================ `; @@ -2529,6 +2570,11 @@ printWidth: Infinity Lorem ipsum dolor sit amet, consectetur adipiscing elit, seddoeiusmod. + + + + + =====================================output=====================================
@@ -2584,8 +2630,12 @@ printWidth: Infinity -
string
-
string
+
+
string
+
+
+
string
+
string
@@ -2598,20 +2648,25 @@ printWidth: Infinity
  • Second
  • 789 -*200 123 +*200 +123
    123456

    x

    x

    x

    - |
    + | + +


    -

    + +



    -

    + +

    "" is the property bound title.

  • 12345678901234567890123456789012345678901234567890123456789012345678901234567890
  • @@ -2628,8 +2683,18 @@ printWidth: Infinity
    foreign tag name should not be lower cased -
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, "seddoeiusmod".
    -
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, seddoeiusmod.
    +
    + Lorem ipsum dolor sit amet, consectetur adipiscing elit, "seddoeiusmod". +
    +
    + Lorem ipsum dolor sit amet, consectetur adipiscing elit, + seddoeiusmod. +
    + + + + + ================================================================================ `; @@ -2751,6 +2816,11 @@ printWidth: 80 Lorem ipsum dolor sit amet, consectetur adipiscing elit, seddoeiusmod.
    + + + + + =====================================output=====================================
    @@ -2819,10 +2889,11 @@ printWidth: 80 > -
    string
    string
    string
    +
    string
    +
    string
    string
    string
    string
    string
    string
    -
    +
    +
    -
    string
    string
    +
    +
    string
    +
    +
    +
    string
    +
    string
    @@ -2856,13 +2933,19 @@ printWidth: 80 *200 123
    123456

    x

    -

    x

    x

    +/>123 +
    123456
    +

    x

    +

    x

    +

    x

    + -|
    +| + +


    @@ -2903,6 +2986,11 @@ printWidth: 80 Lorem ipsum dolor sit amet, consectetur adipiscing elit, seddoeiusmod.
    + + + + + ================================================================================ `; @@ -3024,6 +3112,11 @@ printWidth: 80 Lorem ipsum dolor sit amet, consectetur adipiscing elit, seddoeiusmod.
    + + + + + =====================================output=====================================
    @@ -3129,8 +3222,12 @@ printWidth: 80 -
    string
    -
    string
    +
    +
    string
    +
    +
    +
    string
    +
    string
    @@ -3228,6 +3325,11 @@ printWidth: 80 seddoeiusmod .
    + + + + + ================================================================================ `; diff --git a/tests/html_tags/tags.html b/tests/html_tags/tags.html index 8d978c4f..96b7a27f 100644 --- a/tests/html_tags/tags.html +++ b/tests/html_tags/tags.html @@ -108,3 +108,8 @@ Lorem ipsum dolor sit amet, consectetur adipiscing elit, seddoeiusmod. + + + + + diff --git a/tests/html_vue/__snapshots__/jsfmt.spec.js.snap b/tests/html_vue/__snapshots__/jsfmt.spec.js.snap index 7e59fbb1..0362594d 100644 --- a/tests/html_vue/__snapshots__/jsfmt.spec.js.snap +++ b/tests/html_vue/__snapshots__/jsfmt.spec.js.snap @@ -2437,7 +2437,10 @@ printWidth: 80 ================================================================================ @@ -2466,7 +2469,10 @@ trailingComma: "es5" ================================================================================ @@ -2495,7 +2501,10 @@ semi: false ================================================================================ diff --git a/tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap b/tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap index 489d5563..ac9e673c 100644 --- a/tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap +++ b/tests/html_whitespace/__snapshots__/jsfmt.spec.js.snap @@ -218,6 +218,50 @@ printWidth: 80 ================================================================================ `; +exports[`surrounding-linebreak.html 1`] = ` +====================================options===================================== +parsers: ["html"] +printWidth: 80 + | printWidth +=====================================input====================================== +123 + +123 +123 + + +123 + + +
    123
    +
    +123
    +
    123 +
    +
    +123 +
    + +=====================================output===================================== +123 + 123 +123 + + 123 + + +
    123
    +
    + 123 +
    +
    123
    +
    + 123 +
    + +================================================================================ +`; + exports[`table.html 1`] = ` ====================================options===================================== parsers: ["html"] diff --git a/tests/html_whitespace/surrounding-linebreak.html b/tests/html_whitespace/surrounding-linebreak.html new file mode 100644 index 00000000..06862eba --- /dev/null +++ b/tests/html_whitespace/surrounding-linebreak.html @@ -0,0 +1,17 @@ +123 + +123 +123 + + +123 + + +
    123
    +
    +123
    +
    123 +
    +
    +123 +