Question

I am generating some html with web2py gluon html library (the application is standalone - not embedded in the web2py framework - and only using the gluon.html helpers)

I am adding some styles to it. Since I am sending the generated html via email, I am pre-processing the html with premailer. But premailer is not accepting CDATA tags:

cssselect.parser.SelectorSyntaxError: Expected selector, got <DELIM '<' at 0>

I have taken a look at the gluon code, but I do not get it:

class STYLE(DIV):

    tag = 'style'

    def xml(self):
        (fa, co) = self._xml()
        # no escaping of subcomponents
        co = '\n'.join([str(component) for component in
                       self.components])
        if co:
            # <style [attributes]><!--/*--><![CDATA[/*><!--*/
            # style body
            # /*]]>*/--></style>
            return '<%s%s><!--/*--><![CDATA[/*><!--*/\n%s\n/*]]>*/--></%s>' % (self.tag, fa, co, self.tag)
        else:
            return DIV.xml(self)

Currently what I am doing to build my style is:

STYLE('body { font: normal 16px/1.6em "Open Sans",Arial,Helvetica,sans-serif; } ... ')

Which produces:

<style>
<!--/*-->
<![CDATA[/*&gt;&lt;!--*/
body {
     font: normal 16px/1.6em "Open Sans",Arial,Helvetica,sans-serif;
}
...

How can I use the STYLE helper to avoid having CDATA tags? Or do I need to do it with an XML helper, manually adding the <style> delimiter?

As a side question: why is the CDATA tag used at all? As far as I can see, it is not really needed for HTML (script and style tags are already by default CDATA).

Was it helpful?

Solution

First, you need to put the CSS code inside XML(). If you don't want the CDATA tag, you're probably better off just manually writing the style tag (that's all the STYLE helper will do anyway).

XML('<style>body { font: normal 16px/1.6em "Open Sans",Arial,Helvetica,sans-serif; }</style>')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top