]> git.dolda2000.com Git - wrw.git/commitdiff
Add support for DOM comments in SP.
authorFredrik Tolf <fredrik@dolda2000.com>
Mon, 21 Apr 2025 17:48:19 +0000 (19:48 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Mon, 21 Apr 2025 17:48:19 +0000 (19:48 +0200)
wrw/sp/cons.py
wrw/sp/util.py

index b9437bf16e23a4f3a06d5def19095683e2fca3d6..af53c7b2aec7c0872e1a180d1e84a976fb5fd102 100644 (file)
@@ -12,6 +12,10 @@ class raw(node, str):
     def __todom__(self, doc):
         raise Exception("Cannot convert raw code to DOM objects")
 
+class comment(node, str):
+    def __todom__(self, doc):
+        return doc.createComment(self)
+
 class element(node):
     def __init__(self, ns, name, ctx):
         self.ns = ns
index 931cbdb6d0269810d887d22d52749799f2ff1ac6..09e18b01b260a43e7113fe497422810d2a26c613 100644 (file)
@@ -40,6 +40,8 @@ def flatiter(root, short=True):
                 yield "", ch
             elif isinstance(ch, cons.raw):
                 yield "!", ch
+            elif isinstance(ch, cons.comment):
+                yield "-", ch
             else:
                 raise Exception("Unknown object in element tree: " + el)
 
@@ -123,6 +125,9 @@ class formatter(object):
     def rawcode(self, el):
         self.write(el)
 
+    def comment(self, el):
+        self.write("<!-- " + str(el) + "-->")
+
     def start(self, el):
         self.write('<?xml version="1.0" encoding="' + self.charset + '" ?>\n')
         if isinstance(el, cons.doctype):
@@ -145,6 +150,8 @@ class formatter(object):
             self.text(el)
         elif ev == "!":
             self.rawcode(el)
+        elif ev == "-":
+            self.comment(el)
         elif ev == "^":
             self.start(el)
         elif ev == "$":