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

Preserve multiple definitions of the same property in the same rule? #16

Open
papandreou opened this issue Feb 28, 2011 · 21 comments
Open

Comments

@papandreou
Copy link
Contributor

It's not uncommon for CSS authors to take advantage of the fact that a browser must ignore a CSS property in its entirety if a parse error or unsupported token is encountered in the value. For example:

body {
    background-image: -moz-radial-gradient(center, #a8e936, #76a326);
    background-image: -webkit-gradient(radial, 50% 50%, 0, 50% 50%, 800, from(#a8e936), to(#76a326));
    background-image: -webkit-radial-gradient(center, #a8e936, #76a326);
}

If parsed and reserialized using CSSOM only the last of the three background-image properties will survive. In the project I'm working on it would be very desirable to be able to reconstruct the whole thing. I'm not sure if this falls within the scope of CSSOM -- but would it be possible to achieve that behavior somehow?

Best regards,
Papandreou

@NV
Copy link
Owner

NV commented Mar 1, 2011

I fully understand your problem. I'm rewriting the parser in SAC-style. It will allow me to parse CSS into different object models, not just CSSOM. Like so:

body {
    background-image: -moz-radial-gradient(center, #a8e936, #76a326);
    background-image: -webkit-gradient(radial, 50% 50%, 0, 50% 50%, 800, from(#a8e936), to(#76a326));
    background-image: -webkit-radial-gradient(center, #a8e936, #76a326);
}

into

{
  "cssRules": [
    {
      "selectorText": "body",
      "style": {
        "0": {
          name: "background-image",
          value: "-moz-radial-gradient(center, #a8e936, #76a326)"
        },
        "1": {
          name: "background-image",
          value: "-webkit-gradient(radial, 50% 50%, 0, 50% 50%, 800, from(#a8e936), to(#76a326))"
        },
        "2": {
          name: "background-image",
          value: "-webkit-radial-gradient(center, #a8e936, #76a326);"
        },
        "length": 3,
        "background-image": [0, 1, 2]
      }
    }
  ]
}

Stay tuned!

@papandreou
Copy link
Contributor Author

Awesome :). That'd be exactly what I need!

@stef
Copy link

stef commented Mar 20, 2011

yes, i have the same problem. would be awesome to have this.

@NV
Copy link
Owner

NV commented Mar 20, 2011

I don't think I'll fix it soon. If you want it ASAP you could override CSSStyleDeclaration.prototype.setProperty.

@papandreou
Copy link
Contributor Author

I ended up hacking the CSS parser to continue with a new rule (with an identical selector) if it sees a property redefinition. Seems like that preserves the semantics: assetgraph/assetgraph@9609d66...ee00c99#diff-0 (only the changes to lib/3rdparty/CSSOM/lib/parse.js are relevant, don't mind the other diffs).

Haven't looked into doing the opposite during serialization yet.

@NV
Copy link
Owner

NV commented Sep 12, 2011

@papandreou

img {
  border: none;
  border: 1px solid red;
}

serializes to

img {
  border: none;
}
img {
  border: 1px solid red;
}

The downside is it changes amount of selectors.

@papandreou
Copy link
Contributor Author

@NV: Since I wrote the previous message I've patched the serialization code to do the inverse: https://github.com/One-com/assetgraph/blob/master/lib/3rdparty/CSSOM/lib/CSSStyleSheet.js#L77-L91

So your example now serializes to one rule with a single selector :)

Yes, it's still a hack, and in some corner cases it will collapse neighboring rules with the same selector that were separate originally, but the semantics are preserved and it seems to be adequate for my use case.

@sveisvei
Copy link

sveisvei commented Dec 8, 2011

+1 for fixing this, using papandreou fork for now.

@quaelin
Copy link

quaelin commented Mar 17, 2012

+1 vote

@papandreou
Copy link
Contributor Author

@quaelin: In the mean time you can npm install cssom-papandreou to use my hack.

@quaelin
Copy link

quaelin commented Mar 17, 2012

Thanks @papandreou, your fork is doing the trick. :)

@sveisvei
Copy link

Ive been using papandreou fork for some time as well, works great - love OS this way :)

@tj
Copy link

tj commented Jul 18, 2012

i definitely need this too. do you have the SAC thing open-sourced anywhere?

@NV
Copy link
Owner

NV commented Jul 20, 2012

No, I still haven’t released it.

@tj
Copy link

tj commented Jul 20, 2012

i ended up just whipping up https://github.com/visionmedia/node-css since I don't (currently at least) need completely parsed selectors / values

@nc
Copy link

nc commented Oct 23, 2012

+1 for merging in the pull request

@ralyodio
Copy link

This does not seem to have been merged yet. I tried using cssom-papandreou but that doesn't preserve multiple rules for same attribute.

@andineck
Copy link

I also need this. https://github.com/papandreou/CSSOM/tree/cssom-papandreou has fixed it, but it does not have the latest version of cssom. So I created another fork: https://github.com/intesso/CSSOM and created a pull request: #53.

@ryno1234
Copy link

Is there any fix or suggested work around to this yet? Thx!

papandreou added a commit to papandreou/CSSOM that referenced this issue Sep 25, 2015
…property gets redefined within the same rule. Also, do the inverse when serializing a CSSStyleSheet. This is a temporary workaround until NV#16 gets resolved.

Cherry-picked from 65e0b93.

Conflicts:
	package.json
@superlc
Copy link

superlc commented Jun 29, 2016

vote +1 , this is very important when i use flex. such as

display:-webkit-box;
display:-webkit-flex;
display:flex;

@papandreou
Copy link
Contributor Author

@superlc I recommend using postcss instead, unless it's important to work with the CSSOM api. Postcss doesn't have this limitation.

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

No branches or pull requests