pyRdfa.transform.lite

 1# -*- coding: utf-8 -*-
 2"""
 3
 4@author: U{Ivan Herman<a href="http://www.w3.org/People/Ivan/">}
 5@license: This software is available for use under the
 6U{W3C® SOFTWARE NOTICE AND LICENSE<href="http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231">}
 7@contact: Ivan Herman, ivan@w3.org
 8@version: $Id: lite.py,v 1.11 2013-09-26 16:37:54 ivan Exp $
 9$Date: 2013-09-26 16:37:54 $
10"""
11
12from ..host        import HostLanguage
13
14non_lite_attributes =      ["about","inlist","datatype","rev","rel","content"]
15non_lite_attributes_html = ["about","inlist","datatype","rev"]
16
17def lite_prune(top, options, state):
18    """
19    This is a misnomer. The current version does not remove anything from the tree, just generates warnings as for the
20    usage of non-lite attributes. A more aggressive version would mean to remove those attributes, but that would,
21    in fact, define an RDFa Lite conformance level in the parser, which is against the WG decisions. So this should
22    not be done; the corresponding commands are commented in the code below...
23    
24    @param top: a DOM node for the top level element
25    @param options: invocation options
26    @type options: L{Options<pyRdfa.options>}
27    @param state: top level execution state
28    @type state: L{State<pyRdfa.state>}
29    """
30    def generate_warning(node, attr):
31        if attr == "rel":
32            msg = "Attribute @rel should not be used in RDFa Lite (consider using @property)"
33        elif attr == "about":
34            msg = "Attribute @about should not be used in RDFa Lite (consider using a <link> element with @href or @resource)"
35        else:
36            msg = "Attribute @%s should not be used in RDFa Lite" % attr
37        options.add_warning(msg, node=node)
38
39    def remove_attrs(node):
40        from ..termorcurie import termname
41        # first the @content; this has a special treatment
42        # there are some extras to check for HTML dialects
43        if options.host_language in [ HostLanguage.html5, HostLanguage.xhtml5, HostLanguage.xhtml ]:
44            if node.tagName != "meta" and node.hasAttribute("content"):
45                generate_warning(node, "content")
46                # node.removeAttribute("content")
47            if node.tagName != "link" and node.hasAttribute("rel"):
48                # Unfortunately, this has to be checked separately and run-time for <link> where @rel is allowed for non-RDFa purposes...
49                # Additional complication: @rel is allowed in an <a> element, for example, if used as a pure term and not as a URI or CURIE
50                if node.tagName == "a":
51                    vals = node.getAttribute("rel").strip().split()
52                    if len(vals) != 0:
53                        final_vals = [ v for v in vals if not termname.match(v) ]
54                        if len(final_vals) != 0:
55                            generate_warning(node, "rel")
56                else:
57                    generate_warning(node, "rel")
58            for attr in non_lite_attributes_html:
59                if node.hasAttribute(attr):
60                    generate_warning(node, attr)
61                    # node.removeAttribute(attr)
62        else:
63            for attr in non_lite_attributes:
64                if node.hasAttribute(attr):
65                    generate_warning(node, attr)
66                    # node.removeAttribute(attr)
67
68    remove_attrs(top)
69    for n in top.childNodes:
70        if n.nodeType == top.ELEMENT_NODE:
71            lite_prune(n, options, state)
non_lite_attributes = ['about', 'inlist', 'datatype', 'rev', 'rel', 'content']
non_lite_attributes_html = ['about', 'inlist', 'datatype', 'rev']
def lite_prune(top, options, state):
18def lite_prune(top, options, state):
19    """
20    This is a misnomer. The current version does not remove anything from the tree, just generates warnings as for the
21    usage of non-lite attributes. A more aggressive version would mean to remove those attributes, but that would,
22    in fact, define an RDFa Lite conformance level in the parser, which is against the WG decisions. So this should
23    not be done; the corresponding commands are commented in the code below...
24    
25    @param top: a DOM node for the top level element
26    @param options: invocation options
27    @type options: L{Options<pyRdfa.options>}
28    @param state: top level execution state
29    @type state: L{State<pyRdfa.state>}
30    """
31    def generate_warning(node, attr):
32        if attr == "rel":
33            msg = "Attribute @rel should not be used in RDFa Lite (consider using @property)"
34        elif attr == "about":
35            msg = "Attribute @about should not be used in RDFa Lite (consider using a <link> element with @href or @resource)"
36        else:
37            msg = "Attribute @%s should not be used in RDFa Lite" % attr
38        options.add_warning(msg, node=node)
39
40    def remove_attrs(node):
41        from ..termorcurie import termname
42        # first the @content; this has a special treatment
43        # there are some extras to check for HTML dialects
44        if options.host_language in [ HostLanguage.html5, HostLanguage.xhtml5, HostLanguage.xhtml ]:
45            if node.tagName != "meta" and node.hasAttribute("content"):
46                generate_warning(node, "content")
47                # node.removeAttribute("content")
48            if node.tagName != "link" and node.hasAttribute("rel"):
49                # Unfortunately, this has to be checked separately and run-time for <link> where @rel is allowed for non-RDFa purposes...
50                # Additional complication: @rel is allowed in an <a> element, for example, if used as a pure term and not as a URI or CURIE
51                if node.tagName == "a":
52                    vals = node.getAttribute("rel").strip().split()
53                    if len(vals) != 0:
54                        final_vals = [ v for v in vals if not termname.match(v) ]
55                        if len(final_vals) != 0:
56                            generate_warning(node, "rel")
57                else:
58                    generate_warning(node, "rel")
59            for attr in non_lite_attributes_html:
60                if node.hasAttribute(attr):
61                    generate_warning(node, attr)
62                    # node.removeAttribute(attr)
63        else:
64            for attr in non_lite_attributes:
65                if node.hasAttribute(attr):
66                    generate_warning(node, attr)
67                    # node.removeAttribute(attr)
68
69    remove_attrs(top)
70    for n in top.childNodes:
71        if n.nodeType == top.ELEMENT_NODE:
72            lite_prune(n, options, state)

This is a misnomer. The current version does not remove anything from the tree, just generates warnings as for the usage of non-lite attributes. A more aggressive version would mean to remove those attributes, but that would, in fact, define an RDFa Lite conformance level in the parser, which is against the WG decisions. So this should not be done; the corresponding commands are commented in the code below...

@param top: a DOM node for the top level element @param options: invocation options @type options: L{Options<pyRdfa.options>} @param state: top level execution state @type state: L{State<pyRdfa.state>}