Commit 48605a2b authored by Guido van Rossum's avatar Guido van Rossum

Quite a bit of refactoring, and macro slots now work.

- The DOMVisitor class is simplified a bit; it doesn't visit
  attributes for you and it doesn't define endVisitElement().

- The CopyingDOMVisitor class is fixed to cope with this; instead of
  endVisitElement() it defines and uses backUp(), with the same
  purpose.  It defines and uses copyAllAttributes() and
  copyAttribute() to deal with attributes.

- The TALVisitor class is refactored so that the processing of METAL
  and TAL attributes is done more orderly (I discovered
  getAttributeNS() :-).

- Used a different way to split parts into semicolon-separated parts
  with doubling used to quote semicolons.

- Sped up the macro indexer by using a recursive function with an
  explicit argument rather than a visitor class.

- Added a slot indexer that works the same way.  (The macro and slot
  indexers have a lot in common, but they're still so small that I
  didn't bother factoring it out.  Later.)

- The runtest.sh script now takes optional command line arguments
  (full pathnames of testfiles) to specify a set of tests to run.

- The timer.py script was adapted to use the new macroIndexer().
parent 23700b46
......@@ -103,9 +103,11 @@ class CopyingDOMVisitor(DOMVisitor):
def visitElement(self, node):
self.copyElement(node)
self.copyAllAttributes(node)
DOMVisitor.visitElement(self, node)
self.backUp()
def endVisitElement(self, node):
def backUp(self):
self.curNode = self.curNode.parentNode
def copyElement(self, node):
......@@ -120,17 +122,24 @@ class CopyingDOMVisitor(DOMVisitor):
self.curNode.appendChild(newNode)
self.curNode = newNode
def copyAllAttributes(self, node):
for attr in node.attributes.values():
self.copyAttribute(attr)
def copyAttribute(self, attr):
if attr.namespaceURI:
self.curNode.setAttributeNS(
attr.namespaceURI, attr.nodeName, attr.nodeValue)
else:
self.curNode.setAttribute(attr.nodeName, attr.nodeValue)
def visitText(self, node):
newNode = self.newDocument.createTextNode(node.nodeValue)
self.curNode.appendChild(newNode)
def visitComment(self, node):
newNode = self.newDocument.createComment(node.nodeValue)
self.curNode.appendChild(newNode)
def visitAttribute(self, node):
if node.namespaceURI:
self.curNode.setAttributeNS(
node.namespaceURI, node.nodeName, node.nodeValue)
else:
self.curNode.setAttribute(node.nodeName, node.nodeValue)
if self.newDocument:
newNode = self.newDocument.createComment(node.nodeValue)
self.curNode.appendChild(newNode)
# XXX Else, this is a comment before the documentElement; lose it.
......@@ -114,11 +114,10 @@ class DOMVisitor:
implementation).
You can influence which nodes are visited in which order by
overriding visitAllChildren() and visitAllAttributes(), and/or by
overriding visitDocument() and visitElement(). If you override
the latter two, be careful to either call the base class method or
call visitAllAttributes(), visitAllChildren() and
endVisitElement() as shown in their base class implementations,
overriding visitAllChildren(), and/or by overriding
visitDocument() and visitElement(). If you override the latter
two, be careful to either call the base class method or call
visitAllChildren() as shown in their base class implementations,
otherwise the tree traversal will break.
"""
......@@ -154,7 +153,7 @@ class DOMVisitor:
# Dictionary mapping node types to method names, used in visitNode
visitSwitch = {
Node.ELEMENT_NODE: "visitElement",
Node.ATTRIBUTE_NODE: "visitAttr",
Node.ATTRIBUTE_NODE: "visitAttribute",
Node.TEXT_NODE: "visitText",
Node.CDATA_SECTION_NODE: "visitCDATASection",
Node.ENTITY_REFERENCE_NODE: "visitEntityReference",
......@@ -185,22 +184,10 @@ class DOMVisitor:
"""
Visit an element node.
This calls visitAllAttributes() to visit the attributes, and
visitAllChildren() to recurse into the document tree. It also
calls endVisitElement() in order to signal the end of the visit.
This calls visitAllChildren() to recurse into the document
tree.
"""
self.visitAllAttributes(node)
self.visitAllChildren(node)
self.endVisitElement(node)
def endVisitElement(self, node):
"""
This method is called after visitElement has finished visiting
all its children. It is useful for processing to be done
after the element's subtree has been visited, e.g. spitting
out an end tag or closing a scope.
"""
pass
def visitAllChildren(self, node):
"""
......@@ -209,13 +196,6 @@ class DOMVisitor:
for child in node.childNodes:
self.visitNode(child)
def visitAllAttributes(self, node):
"""
Call visitAttribute() for each attribute of the given node.
"""
for attr in node.attributes.values():
self.visitAttribute(attr)
def visitAttribute(self, node):
"""
Override this empty method to visit an attribute node.
......
This diff is collapsed.
......@@ -4,17 +4,17 @@
: ${TMPDIR=/tmp}
for test in test/test*.xml
for test in ${*-test/test*.xml}
do
out=` echo $test | sed 's,test/test,test/out,' `
out=` echo $test | sed 's,/[^/0-9]*\([0-9]*\).xml,/out\1.xml,' `
tmp=$TMPDIR/taltest$$`basename $test`
./driver.py $test >$tmp
if cmp -s $tmp $out
then
echo $test OK
else
echo $test failed -- diff follows
diff $out $tmp
echo "$test failed -- diff (expected vs. actual) follows"
diff $out $tmp
fi
rm $tmp
done
<?xml version="1.0" ?>
<table xmlns:m="http://xml.zope.org/namespaces/metal" m:define-macro="myTable">
<!-- macro definition with slots -->
<tr>
<td>Top Left</td>
<td>Top Right</td>
</tr>
<tr>
<td>Bottom left</td>
<td><span m:define-slot="bottomRight">Bottom Right</span></td>
</tr>
</table>
<?xml version="1.0" ?>
<table xmlns:m="http://xml.zope.org/namespaces/metal" m:use-macro="myTable">
<!-- macro definition with slots -->
<tr>
<td>Top Left</td>
<td>Top Right</td>
</tr>
<tr>
<td>Bottom left</td>
<td><span m:use-slot="bottomRight">
<h1>Some headline</h1>
<p>This is the real contents of the bottom right slot.</p>
<p>It is supposed to contain a lot of text. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb.</p>
<p>It is supposed to contain a lot of text. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb.</p>
<p>It is supposed to contain a lot of text. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb.</p>
</span></td>
</tr>
</table>
<?xml version="1.0" ?>
<table xmlns:m="http://xml.zope.org/namespaces/metal" m:define-macro="myTable">
<!-- macro definition with slots -->
<tr>
<td>Top Left</td>
<td>Top Right</td>
</tr>
<tr>
<td>Bottom left</td>
<td><span m:define-slot="bottomRight">Bottom Right</span></td>
</tr>
</table>
<?xml version="1.0" ?>
<table xmlns:m="http://xml.zope.org/namespaces/metal" m:use-macro="test/test7.xml/myTable">
<!-- macro use with slots -->
<tr>
<td>
<span m:use-slot="bottomRight">
<h1>Some headline</h1>
<p>This is the real contents of the bottom right slot.</p>
<p>It is supposed to contain a lot of text. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb.</p>
<p>It is supposed to contain a lot of text. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb.</p>
<p>It is supposed to contain a lot of text. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb.</p>
</span>
</td>
</tr>
</table>
<?xml version="1.0" ?>
<table xmlns:m="http://xml.zope.org/namespaces/metal" m:define-macro="myTable">
<!-- macro definition with slots -->
<tr>
<td>Top Left</td>
<td>Top Right</td>
</tr>
<tr>
<td>Bottom left</td>
<td><span m:define-slot="bottomRight">Bottom Right</span></td>
</tr>
</table>
<?xml version="1.0" ?>
<table xmlns:m="http://xml.zope.org/namespaces/metal" m:use-macro="myTable">
<!-- macro definition with slots -->
<tr>
<td>Top Left</td>
<td>Top Right</td>
</tr>
<tr>
<td>Bottom left</td>
<td><span m:use-slot="bottomRight">
<h1>Some headline</h1>
<p>This is the real contents of the bottom right slot.</p>
<p>It is supposed to contain a lot of text. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb.</p>
<p>It is supposed to contain a lot of text. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb.</p>
<p>It is supposed to contain a lot of text. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb.</p>
</span></td>
</tr>
</table>
<?xml version="1.0" ?>
<table xmlns:m="http://xml.zope.org/namespaces/metal" m:define-macro="myTable">
<!-- macro definition with slots -->
<tr>
<td>Top Left</td>
<td>Top Right</td>
</tr>
<tr>
<td>Bottom left</td>
<td><span m:define-slot="bottomRight">Bottom Right</span></td>
</tr>
</table>
<?xml version="1.0" ?>
<table xmlns:m="http://xml.zope.org/namespaces/metal" m:use-macro="test/test7.xml/myTable">
<!-- macro use with slots -->
<tr>
<td>
<span m:use-slot="bottomRight">
<h1>Some headline</h1>
<p>This is the real contents of the bottom right slot.</p>
<p>It is supposed to contain a lot of text. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb.</p>
<p>It is supposed to contain a lot of text. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb.</p>
<p>It is supposed to contain a lot of text. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb. Blah, blah, blab.
Blabber, blabber, blah. Baah, baah, barb.</p>
</span>
</td>
</tr>
</table>
......@@ -125,8 +125,8 @@ def timefunc(count, func, *args):
return result
def findmacros(doc):
from TALVisitor import MacroIndexer
return MacroIndexer(doc)()
from TALVisitor import macroIndexer
return macroIndexer(doc)
if __name__ == "__main__":
main()
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