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

Ensure correct ordering of styles #159

Closed
wants to merge 7 commits into from
Closed

Ensure correct ordering of styles #159

wants to merge 7 commits into from

Conversation

Stadly
Copy link
Contributor

@Stadly Stadly commented Sep 15, 2016

When a property can be set using different names, such as padding and padding-left, the inlined styles were not always correctly ordered.

Examples:

<style type="text/css">
span.test {
   background-color: blue;
}
span {
   background: red;
}
</style>
<span class="test">This should be blue</span>

Will result in <span style="background-color: blue; background: red;">This should be blue</span> even though background-color: blue; has higher specificity than background: red;. This issue is fixed by sorting all rules by specificity instead of order of appearance: bdf1c2e

<style type="text/css">
span {
   background: red;
}
</style>
<span style="background-color: blue;">This should be blue</span>

Will result in <span style="background-color: blue; background: red;">This should be blue</span> since the styles being inlined are positioned after the styles that were already inline. This issue is fixed by positioning the styles that were already inline last: 716da4a, e7d4528

Some properties can be set using shorthand names. Therefore it is important to sort the rules overall, and not only when looking at the rules for a specific property name.

Example:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
       <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
          <meta name="viewport" content="width=device-width"/>
          <title>Test css inliner</title>
          <style type="text/css">
    tr th {
       background-color: blue;
    }
    th {
       background: red;
    }
          </style>
       </head>
       <body>
          <table>
             <tr>
                <th>This should be blue</th>
             </tr>
          </table>
       </body>
    </html>

Will result in `<th style="background-color: blue; background: red;">This should be blue</th>` even though `background-color: blue;` has higher specificity than `background: red;`: when the properties have different names, they are inlined in the order they appear in the stylesheet.

This issue is fixed by sorting all rules by specificity instead of order of appearance.
Place styles that were inline in the source styles after the styles that are being inlined. This is because some properties may be set using different names. Example:

Example:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
       <head>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
          <meta name="viewport" content="width=device-width"/>
          <title>Test css inliner</title>
          <style type="text/css">
    th {
       background: red;
    }
          </style>
       </head>
       <body>
          <table>
             <tr>
                <th style="background-color: blue;">This should be blue</th>
             </tr>
          </table>
       </body>
    </html>

Will result in `<th style="background-color: blue; background: red;">This should be blue</th>` when the inlined styles are positioned after the styles that were already inline.

This issue is fixed by inlining styles before the styles that were already inline.
@stof
Copy link
Collaborator

stof commented Sep 16, 2016

isn't this a duplicate of #155 ?

@Stadly
Copy link
Contributor Author

Stadly commented Sep 16, 2016

Yes. How did I overlook that? 👎 It seems that #155 fixes the first issue, while the second issue where inline styles are not put last is not fixed by that PR.

@Hikariii
Copy link
Contributor

Improved this and made #160.

@tijsverkoyen
Copy link
Owner

Fixed in #160

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants