Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure whitespace display css for custom components #5415

Open
henry-alakazhang opened this issue Nov 9, 2018 · 7 comments
Open

Configure whitespace display css for custom components #5415

henry-alakazhang opened this issue Nov 9, 2018 · 7 comments
Labels
lang:angular Issues affecting Angular template (not general JS/TS issues used for Angular) lang:html Issues affecting HTML (and SVG but not JSX) lang:vue Issues affecting Vue status:needs discussion Issues needing discussion and a decision to be made before action can be taken type:option request Issues requesting a new option. We generally don’t accept these unless there is technical necessity.

Comments

@henry-alakazhang
Copy link

henry-alakazhang commented Nov 9, 2018

--html-whitespace-sensitivity css is nice when working with native HTML as it sets the display status properly (most of the time)

However, for custom components, it seems to always err on the side of caution and defaults to inline.

It would be nice if it were possible to set default display for custom JSX/NG/Vue? components. This would be nice for codebases where many components may be used in place of HTML elements.

Prettier 1.15.1
Playground link

--parser angular

Input:

<div>
  Something
</div>

<span>
  Something
</span>

<custom-inline>
  Something
</custom-inline>

<custom-block>
  Something Else
</custom-block>

<!-- display: block -->
<custom-block>
  Something Else
</custom-block>

<!-- it would be nice to set custom-block as "block" always -->

Output:

<div>Something</div>

<span> Something </span>

<custom-inline> Something </custom-inline>

<custom-block> Something Else </custom-block>

<!-- display: block -->
<custom-block>Something Else</custom-block>

<!-- it would be nice to set custom-block as "block" always -->
@j-f1 j-f1 added lang:html Issues affecting HTML (and SVG but not JSX) status:needs discussion Issues needing discussion and a decision to be made before action can be taken type:option request Issues requesting a new option. We generally don’t accept these unless there is technical necessity. labels Nov 9, 2018
@henry-alakazhang henry-alakazhang changed the title Configure default display css for custom components Configure whitespace display css for custom components Nov 12, 2018
@christianmemije
Copy link

Somewhat related, it would also be nice to declare certain custom components (e.g. <pre-like-component/>) that are meant to act like a <pre/> element to remain pre-formatted and not inlined. This would also be helpful for custom JSX/NG/Vue components.

Playground link

@mashpie
Copy link

mashpie commented Dec 20, 2018

There might be tons of examples on custom elements that now get treated as standard html, but should be not - not an easy task (examples on vue-i18n):

Playground vue-i18n as inline-yaml

Playground vue-i18n as inline-json

in that case it could help to specify desired lang as

<i18n lang="yaml">
en:
  welcome: Welcome!
de:
  welcome: Willkommen!
</i18n>

or

<i18n lang="json">
{
    "en": {
        "welcome": "Welcome!"
    },
    "de": {
        "welcome": "Willkommen!"
    }
}
</i18n>

still there are a few more special cases imaginable (esp. as some packages/libs might not stick to official html/css specs), which makes me think a simple --html-ignore: i18n,custom-block option is the only common workaround and maybe even fastes to implement?

// 2-cents-end

@diminutivesloop
Copy link

diminutivesloop commented May 13, 2020

It would be helpful if this supported non custom elements as well. In our codebase the styling we use to add custom markers to list items causes them to be whitespace sensitive and currently the only solution to avoid misaligned list items is to set htmlWhitespaceSensitivity to strict.

@alexander-akait
Copy link
Member

@thorn0 What about do not apply html-whitespace-sensitivity option to custom components by default (or use strict)? I think it is real unsafe and can lead to broken code. On second step we can implement options/ability to describe custom components and their default display?

@thorn0
Copy link
Member

thorn0 commented Jul 21, 2020

@evilebottnawi It's already strict (inline) for custom (unknown) elements. So this issue is about your second step.

@diminutivesloop
Copy link

@evilebottnawi It's already strict (inline) for custom (unknown) elements. So this issue is about your second step.

This doesn't appear to be documented anywhere.
https://prettier.io/docs/en/options.html#html-whitespace-sensitivity
https://prettier.io/blog/2018/11/07/1.15.0.html#whitespace-sensitive-formatting

@thorn0 thorn0 added lang:angular Issues affecting Angular template (not general JS/TS issues used for Angular) lang:vue Issues affecting Vue labels Mar 4, 2021
@Yogu
Copy link

Yogu commented Aug 12, 2022

This doesn't appear to be documented anywhere.

https://prettier.io/docs/en/options.html#html-whitespace-sensitivity

It is, just in the page you linked. The initial value for display is inline, it's just specified to be block for the standard block-evel elements. So custom elements have a default display value of inline, and strict is used. However, strict is not safe for white-space: pre, and so it is not for e.g. the `i18n element mentioned above.

However, applying a pre-safe formatting rule to all custom elements would basically stop prettier from working in a file that makes heavy use of custom elements, e.g. an angular template, because prettier could no longer really format them.

That being said, an option for overrides would be really good, but it should probably be easy to use a pre-compiled list (or a combination of them) from dependencies like @angular/material or your own UI frameworks.

Prettier currently prints a lot of code like this in our codebase:

        <nx-card-content>{{
            'view.recursionLimitReached.description' | translate
        }}</nx-card-content>

I think this is pretty hard to read. I would reformat it manually to

        <nx-card-content>
            {{ 'view.recursionLimitReached.description' | translate }}
        </nx-card-content>

But if the line gets shorter and you later decide to put it into one line by removing the line break, prettier keeps the spaces which makes it ugly again

        <nx-card-content> {{ 'view' | translate }} </nx-card-content>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang:angular Issues affecting Angular template (not general JS/TS issues used for Angular) lang:html Issues affecting HTML (and SVG but not JSX) lang:vue Issues affecting Vue status:needs discussion Issues needing discussion and a decision to be made before action can be taken type:option request Issues requesting a new option. We generally don’t accept these unless there is technical necessity.
Projects
None yet
Development

No branches or pull requests

8 participants