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

[BUG] Multiple font-face at-rule CSS rules are not processed #1032

Closed
tommedema opened this issue Apr 11, 2018 · 6 comments · Fixed by #1036
Closed

[BUG] Multiple font-face at-rule CSS rules are not processed #1032

tommedema opened this issue Apr 11, 2018 · 6 comments · Fixed by #1036
Labels

Comments

@tommedema
Copy link
Contributor

Latest version of grapes #cfde1c1

If you supply a single @font-face, this is parsed just fine:

JSFiddle example: https://jsfiddle.net/szLp8h4n/140/

Screenshot:

screen shot 2018-04-11 at 3 19 56 pm

However, if you supply a second @font-face, only the second one is parsed, and the first is lost:

JSFiddle example: https://jsfiddle.net/szLp8h4n/141/

Screenshot:

screen shot 2018-04-11 at 3 21 45 pm

I am trying to debug this but would appreciate your help. Could it be related to line 122 in ParserCss.js?

@tommedema
Copy link
Contributor Author

tommedema commented Apr 12, 2018

@artf FYI I can now reproduce this in a failing test case:

it('Init editor from element with multiple font-face at-rules', () => {
      config.fromElement = 1;
      config.storageManager = { type: 0 };
      fixture.innerHTML = `
      <style>
        @font-face {
          font-family: 'Glyphicons Halflings';
          src: url(https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/fonts/glyphicons-halflings-regular.woff2) format('woff2');
        }
        @font-face {
          font-family: 'Droid Sans';
          src: url(https://fonts.gstatic.com/s/droidsans/v8/SlGVmQWMvZQIdix7AFxXkHNSbRYXags.woff2) format('woff2');
        }
      </style>` + htmlString;
      const editor = obj.init(config);
      const css = editor.getCss();
      const styles = editor.getStyle();
      expect(styles.length).toEqual(2);
    });

After further debugging this seems to be caused by cssComposer thinking that the second rule is the same rule as the previous one and then returning the first rule:

https://github.com/artf/grapesjs/blob/dev/src/css_composer/index.js#L185

@cjpollard
Copy link

I managed to fix this in my fork here. Not sure if this is the best way of doing it, but I can submit a pull request if desired.

@tommedema
Copy link
Contributor Author

tommedema commented Apr 12, 2018

@cjpollard thanks, but it seems like you are taking apart something that was stitched together for the wrong reason.

I believe a better approach is to not stitch together these at-rules in the first place. @artf I have submitted a PR for this:

#1036

To be honest, in my opinion the parsers should not try to be smart and optimize code. The browser is a parser itself, and if the rules are found in the browser, grapes should honor this. By making the parser complicated we introduce several bugs (I myself have gathered a list already). In fact I'd recommend not having a parser at all and just getting components and their styles etc. in real time from the DOM / Javascript API.

artf added a commit that referenced this issue Apr 12, 2018
Allow multiple at-rules, fixes #1032
@artf
Copy link
Member

artf commented Apr 12, 2018

Totally agree with you Tom, the main goal of cssComposer is just to provide a lean interface for style definitions. What we have inside CssParser right now it's actually a browser's parser and the rest of code is just a traverser. Actually, we have issues like #307 which show the inconsistency generated by browser's parsers (indeed you will get different outputs on each browser).
My idea (added alredy to the Roadmap #74) was to create an extendable interface which could allow adding custom parsers to the editor (eg. postcss parser for CSS) but obvisosly didn't yet find the time to start it.

BTW thank you for such a quick solution

@tommedema
Copy link
Contributor Author

@artf I see, but then you probably shouldn't do things like removing empty tags or concatenating the same rules; that's extending your "traverser" with parser capabilities ;)

@lock
Copy link

lock bot commented Sep 17, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot added the outdated label Sep 17, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Sep 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants