-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
150 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
;;;; sanitize.asd | ||
|
||
(defsystem sanitize | ||
:depends-on (#:cl-libxml2) | ||
:components | ||
((:module "src" | ||
:components | ||
((:file "packages") | ||
(:file "mode" :depends-on ("packages")))))) | ||
(:file "mode" :depends-on ("packages")) | ||
(:file "clean" :depends-on ("mode")))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
;;;; clean | ||
|
||
(in-package #:sanitize) | ||
|
||
(defgeneric clean (html mode) | ||
(:documentation "Return sanitize copy of HTML")) | ||
|
||
(defmethod clean ((html string) mode) | ||
(html:with-parse-html-fragment (fragment html) | ||
(clean fragment mode) | ||
(html:serialize-html fragment :to-string))) | ||
|
||
(defmethod clean ((node xtree:node) mode) | ||
(case (xtree:node-type node) | ||
(:xml-document-fragment-node | ||
(dolist (item (xtree:all-childs node)) | ||
(clean item mode))) | ||
(:xml-text-node node) | ||
(:xml-comment-node | ||
(cond | ||
((mode-allow-comments mode) node) | ||
(t (xtree:remove-child node)))) | ||
(:xml-element-node | ||
(clean-element node mode)) | ||
(otherwise | ||
(xtree:remove-child node)))) | ||
|
||
(defun clean-element (element mode | ||
&aux (tagname (xtree:local-name element))) | ||
(dolist (node (xtree:all-childs element)) | ||
(clean node mode)) | ||
|
||
(unless (element-allowed-p mode tagname) | ||
(let ((fragment (xtree:make-document-fragment))) | ||
(dolist (node (xtree:all-childs element)) | ||
(xtree:append-child fragment | ||
(xtree:detach node))) | ||
|
||
(when (whitespace-element-p mode tagname) | ||
(xtree:prepend-child fragment | ||
(xtree:make-text " ")) | ||
(xtree:append-child fragment | ||
(xtree:make-text " "))) | ||
|
||
(xtree:replace-child element fragment) | ||
(return-from clean-element fragment))) | ||
|
||
(dolist (attr (xtree:all-attribute-nodes element)) | ||
(unless (attribute-allowed-p mode tagname (xtree:local-name attr)) | ||
(xtree:remove-child attr))) | ||
|
||
(dolist (attr/value (element-additional-attributes mode tagname)) | ||
(setf (xtree:attribute-value element (car attr/value)) | ||
(cdr attr/value)))) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,7 @@ | |
#:+default+ | ||
#:+basic+ | ||
#:+relaxed+ | ||
#:+restricted+)) | ||
#:+restricted+ | ||
|
||
#:clean)) | ||
|