| 1 |
|
| 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 |
| 6 |
U{W3C® SOFTWARE NOTICE AND LICENSE<href="http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231">} |
| 7 |
@contact: Ivan Herman, [email protected] |
| 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 |
|
| 12 |
from ..host import HostLanguage |
| 13 |
|
| 14 |
non_lite_attributes = ["about","inlist","datatype","rev","rel","content"] |
| 15 |
non_lite_attributes_html = ["about","inlist","datatype","rev"] |
| 16 |
|
| 17 |
def 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 |
|
| 42 |
|
| 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 |
|
| 47 |
if node.tagName != "link" and node.hasAttribute("rel"): |
| 48 |
|
| 49 |
|
| 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 |
|
| 62 |
else: |
| 63 |
for attr in non_lite_attributes: |
| 64 |
if node.hasAttribute(attr): |
| 65 |
generate_warning(node, attr) |
| 66 |
|
| 67 |
|
| 68 |
remove_attrs(top) |
| 69 |
for n in top.childNodes: |
| 70 |
if n.nodeType == top.ELEMENT_NODE: |
| 71 |
lite_prune(n, options, state) |
| 72 |
|