<commit_before>try:
    from tn.plonehtmlpage import html_page
    HAS_HTML_PAGE = True
except ImportError:
    HAS_HTML_PAGE = False

if HAS_HTML_PAGE:
    from five import grok
    from tn.plonebehavior.template import interfaces
    from tn.plonebehavior.template.html import ContextlessHTML

    class HTMLPageHTML(grok.Adapter):
        grok.context(html_page.IHTMLPageSchema)
        grok.implements(interfaces.IHTML)

        contextless_factory = ContextlessHTML

        def __unicode__(self):
            base_url = self.context.absolute_url()
            return unicode(self.contextless_factory(base_url,
                                                    self.context.html))<commit_msg>Add a validation to ensure that CSS selector actually works<commit_after>try:
    from tn.plonehtmlpage import html_page
    HAS_HTML_PAGE = True
except ImportError:
    HAS_HTML_PAGE = False

if HAS_HTML_PAGE:
    from five import grok
    from tn.plonebehavior.template import _
    from tn.plonebehavior.template import ITemplateConfiguration
    from tn.plonebehavior.template import interfaces
    from tn.plonebehavior.template.html import ContextlessHTML
    from z3c.form import validator

    import collections
    import lxml.cssselect
    import lxml.html
    import zope.interface

    isiterable = lambda o: isinstance(o, collections.Iterable)

    class HTMLPageHTML(grok.Adapter):
        grok.context(html_page.IHTMLPageSchema)
        grok.implements(interfaces.IHTML)

        contextless_factory = ContextlessHTML

        def __unicode__(self):
            base_url = self.context.absolute_url()
            return unicode(self.contextless_factory(base_url,
                                                    self.context.html))

    class CSSSelectorValidator(validator.SimpleFieldValidator):
        def validate(self, value):
            super(CSSSelectorValidator, self).validate(value)

            tree = lxml.html.document_fromstring(self.context.html)
            xpath = lxml.cssselect.CSSSelector(value).path
            selection = tree.xpath(xpath)
            if not isiterable(selection) or len(selection) != 1:
                raise zope.interface.Invalid(_(
                    "Expression doesn't select a single element "
                    "in the HTML page."
                ))

    validator.WidgetValidatorDiscriminators(
        CSSSelectorValidator,
        context=html_page.IHTMLPageSchema,
        field=ITemplateConfiguration['css']
    )

    grok.global_adapter(CSSSelectorValidator)
{'fraction_non_alphanumeric': 0.0650887573964497, 'fraction_numerical': 0.0007889546351084812, 'mean_word_length': 2.541899441340782, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '970977', 'n_tokens_mistral': 702, 'n_tokens_neox': 667, 'n_words': 144}