Parse, log and continue

Issue #19 new
karl dubost
created an issue

I'm using cssutils in a script for making a survey on the way CSS is used in some circumstances. A nice feature of cssutils is that it will validate the CSS rules.

I came across this nasty rule on Yahoo! Canada Web site



.y-lang-tgl { background-color:#fof6fb;} }}}

Currently my code is along this for this part



def getStyleElementRules(self, htmltext):
    """Given an htmltext,
    return the CSS rules contained in the content"""
    compiledstyle = ""
    tree = etree.HTML(htmltext)
    styleelements = tree.xpath('//style')
    for styleelt in styleelements:
        if styleelt.text != None:
            compiledstyle = compiledstyle + styleelt.text
            logging.debug("STYLE ELEMENT NONE")

    if compiledstyle != None:
        cssutils.ser.prefs.indentClosingBrace = False
        cssutils.ser.prefs.keepComments = False
        cssutils.ser.prefs.lineSeparator = u''
        cssutils.ser.prefs.omitLastSemicolon = False
        stylesheet = cssutils.parseString(compiledstyle)
        raise ValueError("STYLE ELEMENT: no CSS Rules")
    return stylesheet


In this case I get "ValueError: invalid literal for int() with base 16: 'fo'" for the invalid rule.

I would love in these circumstances to be able to catch all style errors, log the event, but continue the processing.

Basically CSSStyleSheet could return CSSStyleSheet.cssRules and CSSStyleSheet.bogusRules

where bogusRules would be css rules which are really not understandable. OR maybe there is another way

Comments (5)

  1. C. Dot repo owner

    did you look into the feature? Not sure if that would help. I think there was a patch for the ValueError problem but sadly I am currently a bit busy with other projects :( But maybe you could try the current bitbucket version?

    Let me know if this helps, maybe i could find the time to at least make a release with the ValueError fix. To be frank though, I won't have time for a bogusRules feature, simply because I would have no time for it.

  2. karl dubost reporter

    I could install the new version from the bitbucket. No worries. I'm currently using.

    >>> import cssutils
    >>> cssutils.__version__
    '0.9.10b1 $Id$'

    Thanks. If I have time after this program I'm working on, I might send you pull requests.

  3. C. Dot repo owner

    btw, the new release 0.9.10 should at least parse the original css you tried (wrong hex color). Does not change your real question though, sorry.

  4. Log in to comment