Skip to content

Styled-components integration lowercases SVG tag names in selectors #3304

Closed

Description

@seancurtis

Prettier 1.8.2
Playground link

Input:

const Foo = styled.span`
  linearGradient {
    stop-color: inherit;
  }
`;

Output:

const Foo = styled.span`
  lineargradient {
    stop-color: inherit;
  }
`;

Expected behavior:
Expected the selector of "linearGradient" to not be changed to "lineargradient".

The SVG element is <linearGradient>, but prettier rewrites the selector as "lineargradient" which no longer matches in Firefox or Safari.

Activity

  1. changed the title [-]Styled-components integration breaks selectors[/-] [+]Styled-components integration lowercases SVG tag names in selectors[/+] on Nov 21, 2017
  2. added
    lang:css/scss/less
    priority:high
    on Nov 21, 2017
  3. suchipi commented on Nov 21, 2017

    @suchipi
    Member

    Priority high since the selector doesn't target the correct element anymore once it's been lowercased (note that SVG elements are case-sensitive).

  4. seancurtis commented on Nov 22, 2017

    @seancurtis
    Author

    We can get around this temporarily by either adding these files to our .prettierignore file, or by doing:

    const styld = styled;
    const Span = styld.span`...`;
    

    Not a great workaround, but it'll work until this gets fixed.

    Very much a rare occurrence, as not many people would ever be targeting a <linearGradient> node in a styled-component selector. We've got some SVG icons that use gradients, which is why we ran into this.

    I was very surprised to find that prettier was reformatting content within a string though. Expressions I can see, but string content?

  5. duailibe commented on Nov 22, 2017

    @duailibe
    Collaborator

    We format the contents of the styled components as CSS. And one the formatting we do is normalization: lowercase tags, properties and values; numbers; quotes; etc.

    We didn't take into account case sensitive tags..

  6. lydell commented on Nov 22, 2017

    @lydell
    Member

    Sorry, I didn't read the CSS specification carefully enough when I implemented the lowercasing stuff :(

    https://www.w3.org/TR/css3-selectors/#casesens

    All Selectors syntax is case-insensitive within the ASCII range (i.e. [a-z] and [A-Z] are equivalent), except for parts that are not under the control of Selectors. The case sensitivity of document language element names, attribute names, and attribute values in selectors depends on the document language. For example, in HTML, element names are case-insensitive, but in XML, they are case-sensitive.

    SVG is XML, not HTML, so that explains why it's case sensitive.

    We have to remove all lowercasing of element names and attribute names, because we cannot know if a piece of CSS is styling HTML or XML.

    SVG CSS case sensitivity test case

    If things were case insensitive, you'd see green stuff in this test case. But in the real, case sensitive, world, things are red.

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>SVG case</title>
        <style>
          svg {
            border: solid;
          }
    
          linearGradient stop {
            STOP-COLOR: red;
          }
    
          lineargradient stop {
            STOP-COLOR: green;
          }
    
          svg#two[viewBox] {
            background-color: red;
          }
    
          svg#two[viewbox] {
            background-color: green;
          }
        </style>
      </head>
      <body>
        <svg viewBox="0 0 100 100" height="100">
          <defs>
            <linearGradient
              x1="0"
              x2="0"
              y1="0"
              y2="1"
              id="gradient"
            >
              <stop offset="0%" />
              <stop offset="100%" />
            </linearGradient>
          </defs>
          <rect x="0" y="0" width="50" height="50" fill="url(#gradient)" />
        </svg>
    
        <svg id="two" viewBox="0 0 100 100" height="100">
          <rect x="0" y="0" width="50" height="50" />
        </svg>
      </body>
    </html>
  7. suchipi commented on Nov 24, 2017

    @suchipi
    Member

    I was going to recommend keeping a whitelist of HTML tag names to lowercase and keeping the case of everything else, but unfortunately, you could create an XML document that uses the same tag names as HTML does...

  8. added 4 commits that reference this issue on Nov 25, 2017
    9fb1a3d
    b1d3a01
    ee10622
    941b8f8
  9. added a commit that references this issue on Nov 27, 2017
    56951a7
  10. added this to the 1.9 milestone on Nov 28, 2017
  11. added
    locked-due-to-inactivity
    on Jul 6, 2018
  12. locked as resolved and limited conversation to collaborators on Jul 6, 2018
  13. added theissue type on Mar 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    lang:css/scss/lesslocked-due-to-inactivitypriority:high

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions