Commit 091b862d authored by Guido van Rossum's avatar Guido van Rossum

Raise an exception when an unknown TAL or METAL attribute is found.

- Should I check for spurious TAL attributes even when doing just
  METAL (macro) expansion?

- Should I turn all the other errors into exceptions too?  (Currently
  they are all print statements.)
parent 8b765f8c
......@@ -100,6 +100,28 @@ from TALVisitor import macroIndexer, slotIndexer
from TALVisitor import splitParts, parseAttributeReplacements
from TALVisitor import parseSubstitution
class TALError(Exception):
pass
class METALError(TALError):
pass
KNOWN_METAL_ATTRIBUTES = [
"define-macro",
"use-macro",
"define-slot",
"fill-slot",
]
KNOWN_TAL_ATTRIBUTES = [
"define",
"condition",
"insert",
"replace",
"repeat",
"attributes",
]
class METALCompiler(DOMVisitor):
def __init__(self, document):
......@@ -183,9 +205,20 @@ class METALCompiler(DOMVisitor):
if not node.hasAttributes():
self.emitElement(node)
else:
self.checkSpuriousAttributes(node)
self.expandElement(node)
self.popNS()
def checkSpuriousAttributes(self, node,
ns=ZOPE_METAL_NS,
known=KNOWN_METAL_ATTRIBUTES):
for attr in node.attributes.values():
if attr.namespaceURI == ns:
if attr.localName not in known:
raise METALError(
"bad METAL attribute: %s;\nallowed are: %s" %
(repr(attr.name), string.join(known)))
def expandElement(self, node):
macroName = node.getAttributeNS(ZOPE_METAL_NS, "use-macro")
if macroName:
......@@ -253,7 +286,7 @@ class METALCompiler(DOMVisitor):
class TALCompiler(METALCompiler):
# Overriding METAL method to add attribute replacements
# Extending METAL method to add attribute replacements
def getAttributeList(self, node):
attrList = METALCompiler.getAttributeList(self, node)
attrDict = getAttributeReplacements(node)
......@@ -281,6 +314,12 @@ class TALCompiler(METALCompiler):
else:
self.conditionalElement(node)
# Extending METAL method to check for TAL statements
def checkSpuriousAttributes(self, node):
METALCompiler.checkSpuriousAttributes(self, node)
METALCompiler.checkSpuriousAttributes(
self, node, ZOPE_TAL_NS, KNOWN_TAL_ATTRIBUTES)
def emitDefines(self, defines):
for part in splitParts(defines):
m = re.match(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment