Skip to content

feat(html): whitespace-sensitive formatting#5168

Merged
ikatyang merged 77 commits intoprettier:masterfrom
ikatyang:feat/html/whitespace-sensitive-formatting
Oct 13, 2018
Merged

feat(html): whitespace-sensitive formatting#5168
ikatyang merged 77 commits intoprettier:masterfrom
ikatyang:feat/html/whitespace-sensitive-formatting

Conversation

@ikatyang
Copy link
Copy Markdown
Member

@ikatyang ikatyang commented Sep 30, 2018

  • whitespace-sensitive formatting (comment)
    • respect css display: block/white-space: pre (data from html-styles)
    • support magic comment (<!-- display: block -->)
    • add an option to specify whitespace sensitivity (--html-whitespace-sensitivity <css|strict|ignore>)
      • magic comments take precedence
      • (default) css: respect default css style (safe in the most cases)
      • strict: every node is considered whitespace sensitive (the safest)
      • ignore: every node is considered whitespace insensitive (dangerous, the current behavior)
    • inline inline-tags (comment 1, comment 2)
  • support ie conditional comment (<!--[if IE]><![endif]-->)
  • indent the script/style content
  • no inconsistent output for 2+ attributes (repro, comment 1, comment 2)
  • force break tag if there're multiline attributes (repro, comment)

Prettier pr-5168 Playground link


  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory)
  • I’ve read the contributing guidelines.

This was referenced Sep 30, 2018
@ikatyang
Copy link
Copy Markdown
Member Author

@claytonrcarter

This is the result that we force-break multiline element:

catalogs annually. We work out of three warehouses on the
<a
  href="javascript:;"
  onClick="MM_openBrWindow('/ogs/map_popup.php','FedcoMap','width=380,height=850')"
  >Bellsqueeze and Hinckley Roads</a
>
in Clinton, Maine.

I originally thought that we should move the text to the prev line:

catalogs annually. We work out of three warehouses on the
<a
  href="javascript:;"
  onClick="MM_openBrWindow('/ogs/map_popup.php','FedcoMap','width=380,height=850')"
  >Bellsqueeze and Hinckley Roads</a
> in Clinton, Maine.

to align with the non-breakable one (in this case, the > before the text is actually considered a part of the text, which is why it did not break):

catalogs annually. We work out of three warehouses on the
<a
  href="javascript:;"
  onClick="MM_openBrWindow('/ogs/map_popup.php','FedcoMap','width=380,height=850')"
  >Bellsqueeze and Hinckley Roads</a
>in Clinton, Maine.

but then I found that if we do so it'll cause some inconsistent behavior for non-text elements that I don't think it looks better:

catalogs annually. We work out of three warehouses on the
<input
  some-long-long-long-long-long-long-long-long-long-long-long-long-long-attr
/> <input attr />

so I think it's not a good idea to inline it (and actually the original output looks more readable to me).

@michaeljota
Copy link
Copy Markdown

I understand this output is about the correctness of the code, but I don't think it's a good output to have. It's not as easy to read and to understand the intention as you may desire.

@ikatyang
Copy link
Copy Markdown
Member Author

@michaeljota That's why there's a --html-whitespace-sensitivity ignore option, it basically just output whatever looks better regardless of the correctness.

@michaeljota
Copy link
Copy Markdown

@ikatyang Do you think we could have an option to prefer void tags over self-closing tags? 😃

@ikatyang
Copy link
Copy Markdown
Member Author

Based on our Option Philosophy, we avoid adding options if possible, the --html-whitespace-sensitivity option here is added for the correctness issue.

@michaeljota
Copy link
Copy Markdown

I understand. Still, I'm going to say that there is enough people that prefer the usage of void elements over xml compatible self closing tags to at least have this as an option.

@ikatyang
Copy link
Copy Markdown
Member Author

@michaeljota Could you open a new issue for it?

@michaeljota
Copy link
Copy Markdown

michaeljota commented Oct 11, 2018

Sure. thanks!

#5246. Thank you so much.

@ikatyang
Copy link
Copy Markdown
Member Author

I'll merge this PR tomorrow if there's no further comment.

@thorn0
Copy link
Copy Markdown
Member

thorn0 commented Oct 12, 2018

I still think that breaking the closing tag is a last resort and should be avoided as much as possible.

IMHO the input looks better than the output in this example:

Playground link

Input:

<p>
  <b>Fuerit quovis est vacabo ac contineat aestimare ac contineat aestimare
    pro</b>,
  requiratur ii ut cognoscere concipitur.
</p>

Output:

<p>
  <b
    >Fuerit quovis est vacabo ac contineat aestimare ac contineat aestimare
    pro</b
  >, requiratur ii ut cognoscere concipitur.
</p>

Also breaking the opening b tag here looks unneeded or even like a bug.

@thorn0
Copy link
Copy Markdown
Member

thorn0 commented Oct 12, 2018

Commas and other punctuation probably should be a special case as this looks okay to me:

<p>
  <a
    href="https://en.wikipedia.org/wiki/Balkans"
    target="_blank"
    class="link-to-wiki"
    >balkan</a
  >ization
</p>

Better than:

<p>
  <a
    href="https://en.wikipedia.org/wiki/Balkans"
    target="_blank"
    class="link-to-wiki"
    >balkan</a>ization
</p>

@michaeljota
Copy link
Copy Markdown

I play with that a little bit, and notice that if you set an space, then the tags won't weirdly break. I know that in most case, a leading and trailing space is not a big issue, but I wonder if in some cases this is could be undesirable.

@ikatyang
Copy link
Copy Markdown
Member Author

@thorn0 It seems it only happens with the specific print width, it looks okay if there're more or less text. Since it's a rare case, I think it should be fine to use the current behavior.

Playground link

<p>
  <b>ac contineat aestimare pro</b>, requiratur ii ut cognoscere concipitur.
</p>

<p>
  <b
    >Fuerit quovis est vacabo ac contineat aestimare ac contineat aestimare
    pro</b
  >, requiratur ii ut cognoscere concipitur.
</p>

<p>
  <b
    >Fuerit quovis est vacabo ac contineat aestimare ac contineat Fuerit quovis
    est vacabo ac contineat aestimare ac contineat Fuerit quovis est vacabo ac
    contineat aestimare ac contineat aestimare pro</b
  >, requiratur ii ut cognoscere concipitur.
</p>

@ikatyang
Copy link
Copy Markdown
Member Author

@michaeljota If you don't care about those leading/trailing spaces, you can always enable the --html-whitespace-sensitivity ignore option, which won't break any tag.

@thorn0
Copy link
Copy Markdown
Member

thorn0 commented Oct 12, 2018

No matter what print width is the opening b tag is always broken: link

@ikatyang
Copy link
Copy Markdown
Member Author

Yeah, but it didn't look that weird comparing to the original one. And if we do so, the ignore output will become weird:

<p>
  <b>
    Fuerit quovis est vacabo ac contineat aestimare ac contineat aestimare
    pro
  </b>,
  requiratur ii ut cognoscere concipitur.
</p>

which is also not consistent with JSX:

<p>
  <b>
    Fuerit quovis est vacabo ac contineat aestimare ac contineat aestimare
    pro
  </b>
  , requiratur ii ut cognoscere concipitur.
</p>;

@pauldraper

This comment has been minimized.

@ikatyang
Copy link
Copy Markdown
Member Author

As this looks acceptable in most cases so I'm going to merge this PR. Please open new issues for those special cases.

@ikatyang ikatyang merged commit dd4687e into prettier:master Oct 13, 2018
@ikatyang ikatyang deleted the feat/html/whitespace-sensitive-formatting branch October 13, 2018 05:55
@ikatyang ikatyang added this to the 1.15 milestone Oct 25, 2018
@lock lock bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 23, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants