[Checkins] SVN: z3c.pt/trunk/ Raise custom validation error when inserted string does not validate.

Malthe Borch mborch at gmail.com
Mon Sep 8 10:22:45 EDT 2008


Log message for revision 90949:
  Raise custom validation error when inserted string does not validate.

Changed:
  U   z3c.pt/trunk/CHANGES.txt
  U   z3c.pt/trunk/src/z3c/pt/clauses.py
  U   z3c.pt/trunk/src/z3c/pt/config.py
  U   z3c.pt/trunk/src/z3c/pt/etree.py

-=-
Modified: z3c.pt/trunk/CHANGES.txt
===================================================================
--- z3c.pt/trunk/CHANGES.txt	2008-09-08 14:09:23 UTC (rev 90948)
+++ z3c.pt/trunk/CHANGES.txt	2008-09-08 14:22:44 UTC (rev 90949)
@@ -15,6 +15,9 @@
 
   Features
 
+- Custom validation error is now raised if inserted string does not
+  validate (when debug mode is enabled). [malthe]
+
 - Added support for omitting rendering of HTML "toggle" attributes
   (option's ``selected`` and input's ``checked``) within dynamic
   attribute assignment.  If the value of the expression in the

Modified: z3c.pt/trunk/src/z3c/pt/clauses.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/clauses.py	2008-09-08 14:09:23 UTC (rev 90948)
+++ z3c.pt/trunk/src/z3c/pt/clauses.py	2008-09-08 14:22:44 UTC (rev 90949)
@@ -813,8 +813,8 @@
                 raise ImportError(
                     "ElementTree (required when XML validation is enabled).")
 
-            stream.symbol_mapping[stream.symbols.elementtree] = _et
-            write("%(elementtree)s.fromstring('<div>%%s</div>' %% %(tmp)s)")
+            stream.symbol_mapping[stream.symbols.validate] = etree.validate
+            write("%(validate)s(%(tmp)s)")
 
     def end(self, stream):
         if self.assign:

Modified: z3c.pt/trunk/src/z3c/pt/config.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/config.py	2008-09-08 14:09:23 UTC (rev 90948)
+++ z3c.pt/trunk/src/z3c/pt/config.py	2008-09-08 14:22:44 UTC (rev 90949)
@@ -54,7 +54,7 @@
     attributes = '_attributes'
     negotiate = '_negotiate'
     translate = '_translate'
-    elementtree = '_et'
+    validate = '_validate'
     path = '_path'
 
     # advertised symbols

Modified: z3c.pt/trunk/src/z3c/pt/etree.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/etree.py	2008-09-08 14:09:23 UTC (rev 90948)
+++ z3c.pt/trunk/src/z3c/pt/etree.py	2008-09-08 14:22:44 UTC (rev 90949)
@@ -3,8 +3,15 @@
 import utils
 import cgi
 import copy
+import xml.parsers.expat
 from StringIO import StringIO
 
+class ValidationError(ValueError):
+    def __str__(self):
+        value, = self.args
+        return "Insertion of %s is not allowed." % \
+               repr(value)
+        
 def import_elementtree():
     try:
         import xml.etree.ElementTree as ET
@@ -16,6 +23,12 @@
         
     return ET
 
+def validate(string):
+    try:
+        import_elementtree().fromstring("<div>%s</div>" % string)
+    except xml.parsers.expat.ExpatError:
+        raise ValidationError(string)
+        
 class Parser(object):
     element_mapping = utils.emptydict()
 



More information about the Checkins mailing list