phax / ph-css

Advanced Java CSS 3 parser and builder

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@media parse fails

biancashouse opened this issue · comments

Error in @media rule. (Invalid token "and". Was expecting one of: , , .)

Happens when something like this present:

@media screen and (min-width: 768px) {.section {.
    padding: 40px
}

}

I'm getting the following error:

Failed to parse CSS: [1:49] Encountered text '.' corresponding to token ".". Was expecting one of: <S>, "}", ";", "@charset", "@import", "@namespace", "@page", "@top-left-corner", "@top-left", "@top-center", "@top-right", "@top-right-corner", "@bottom-left-corner", "@bottom-left", "@bottom-center", "@bottom-right", "@bottom-right-corner", "@left-top", "@left-middle", "@left-bottom", "@right-top", "@right-middle", "@right-bottom", "@media", <FONTFACE_SYM>, <KEYFRAMES_SYM>, <VIEWPORT_SYM>, "@supports", <AT_UNKNOWN>, <IDENT>

It is the single dot after double opening {.

To work around this problem in 5.x switch to browser compliant mode and set and "ignore all" custom error handler:

   final String css = "@media screen and (min-width: 768px) {.section {.\r\n" +
                       "    padding: 40px\r\n" +
                       "}\r\n" +
                       "\r\n" +
                       "}";
    final CSSReaderSettings aSettings = new CSSReaderSettings ().setCSSVersion (ECSSVersion.LATEST)
                                                                .setBrowserCompliantMode (true)
                                                                .setCustomErrorHandler (new DoNothingCSSParseErrorHandler ());
    final CascadingStyleSheet cascadingStyleSheet = CSSReader.readFromStringStream (css, aSettings);
    final CSSWriter writer = new CSSWriter (new CSSWriterSettings (ECSSVersion.LATEST, true));
    assertEquals ("@media screen and (min-width:768px){.section{}}", writer.getCSSAsString (cascadingStyleSheet));

I see the error now you pointed it out, but when I look at the css file I just sent you re keyframes parsing, I don't see any illegal "." in the media queries.

Anyway, what you show above makes perfect sense.

If I use the DoNothingCSSParseErrorHandler it will ignore errors you say. So I won't be able to list warnings and errors etc like I was using CollectingCSSParseErrorHandler then ?

In a nutshell, what I mean to say is: I want to get a list of parse issues, but I also want to always produce a stylesheet, because I still need to extract and replace embedded urls.

The basic thing that you were (potentially) missing was the enabling of the "browser compliant mode" (see above example).
Stick with your CollectingCSSParseErrorHandler and it should be fine.
Btw. are you using v4 or v5 of ph-css?

BTW - can you tell me what "browser compliant parsing" refers to ?
i.e. if it is not the default then is the default better in some way ?

On Mon, Oct 24, 2016 at 6:41 PM, Philip Helger notifications@github.com
wrote:

I'm getting the following error:

Failed to parse CSS: [1:49] Encountered text '.' corresponding to token ".". Was expecting one of: , "}", ";", "@charset", "@import", "@namespace", "@page", "@top-left-corner", "@top-left", "@top-center", "@top-right", "@top-right-corner", "@bottom-left-corner", "@bottom-left", "@bottom-center", "@bottom-right", "@bottom-right-corner", "@left-top", "@left-middle", "@left-bottom", "@right-top", "@right-middle", "@right-bottom", "@media", <FONTFACE_SYM>, <KEYFRAMES_SYM>, <VIEWPORT_SYM>, "@supports", <AT_UNKNOWN>,

It is the single dot after double opening {.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#36 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AArVxL3KrLAxT56Yz7j2W65Ieyb9Aj4Gks5q3GErgaJpZM4KeRqk
.

Ian White
h: +61 2 9948 9739
m: +61 404 950 122
hangout: biancashouse@gmail.com

Doh,

your later releases have a java 8 depencency !
I'll have to unwind, otherwise can't upload my app to Appengine.

On Mon, Oct 24, 2016 at 11:28 PM, Ian White ian@biancashouse.com wrote:

aha 5.0.2 thanks.

On Mon, Oct 24, 2016 at 11:27 PM, Ian White ian@biancashouse.com wrote:

com.helger

ph-css

4.1.5

didn't realise there was a version 5 - will take a look at your github
pom...

On Mon, Oct 24, 2016 at 11:23 PM, Philip Helger <notifications@github.com

wrote:

The basic thing that you were (potentially) missing was the enabling of
the "browser compliant mode" (see above example).
Stick with your CollectingCSSParseErrorHandler and it should be fine.
Btw. are you using v4 or v5 of ph-css?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#36 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AArVxAHAUoHjr9OAdE_LNzvlTzEW86Ljks5q3KNNgaJpZM4KeRqk
.

aha 5.0.2 thanks.

On Mon, Oct 24, 2016 at 11:27 PM, Ian White ian@biancashouse.com wrote:

com.helger

ph-css

4.1.5

didn't realise there was a version 5 - will take a look at your github
pom...

On Mon, Oct 24, 2016 at 11:23 PM, Philip Helger notifications@github.com
wrote:

The basic thing that you were (potentially) missing was the enabling of
the "browser compliant mode" (see above example).
Stick with your CollectingCSSParseErrorHandler and it should be fine.
Btw. are you using v4 or v5 of ph-css?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#36 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AArVxAHAUoHjr9OAdE_LNzvlTzEW86Ljks5q3KNNgaJpZM4KeRqk
.

I personally prefer totally valid CSS over the browser quirks. But since the library is sometimes used to parse "in the wild" CSS I added this "browser compliant mode" to try to interpret the CSS as browsers do.
So if you parse CSS from the wild, I strongly recommend to turn this mode on.

Btw. for JDK 6 there is version 4.1.6 which has nearly the same features...

com.helger

ph-css

4.1.5

didn't realise there was a version 5 - will take a look at your github
pom...

On Mon, Oct 24, 2016 at 11:23 PM, Philip Helger notifications@github.com
wrote:

The basic thing that you were (potentially) missing was the enabling of
the "browser compliant mode" (see above example).
Stick with your CollectingCSSParseErrorHandler and it should be fine.
Btw. are you using v4 or v5 of ph-css?


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#36 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AArVxAHAUoHjr9OAdE_LNzvlTzEW86Ljks5q3KNNgaJpZM4KeRqk
.

Ah okay.
Latest 4.x release is 4.1.6 (JDK 6)
Latest 5.x release is 5.0.2 (JDK 8)

Any news on this one?