git.fiddlerwoaroof.com
Browse code

Check if a node is a child before removing it

fiddlerwoaroof authored on 16/08/2017 07:56:19
Showing 1 changed files
... ...
@@ -57,11 +57,12 @@
57 57
     node)
58 58
 
59 59
   (:method ((node plump:comment))
60
-    (plump:remove-child node))
60
+    (when (plump:parent node)
61
+      (plump:remove-child node)))
61 62
 
62
-  (:method :after ((node plump:element))
63
-           (map nil #'sanitize-node
64
-                (plump:children node)))
63
+  (:method :after ((node plump:nesting-node))
64
+    (map nil #'sanitize-node
65
+        (plump:children node)))
65 66
 
66 67
   (:method ((node plump:element))
67 68
     (if (member (plump:tag-name node)
... ...
@@ -72,14 +73,13 @@
72 73
              unless (member attr +safe-attrs+ :test 'equalp) do
73 74
                (plump:remove-attribute node attr))
74 75
           node)
75
-        (plump:remove-child node))))
76
+        (when (plump:parent node)
77
+          (plump:remove-child node)))))
76 78
 
77 79
 (defun sanitize (html)
78 80
   (let ((result (plump:parse html))
79 81
         (*serialization-mode* :html))
80
-    (map nil
81
-         #'sanitize-node 
82
-         (plump:children result))
82
+    (sanitize-node result)
83 83
     (plump:serialize result nil)))
84 84
 
85 85
 (defpackage :html-sanitizer.test