[claw-cvs] r156 - trunk/main/claw-html/src

Andrea Chiumenti achiumenti at common-lisp.net
Wed Dec 17 14:48:32 UTC 2008


Author: achiumenti
Date: Wed Dec 17 14:48:31 2008
New Revision: 156

Log:
css injection

Modified:
   trunk/main/claw-html/src/tags.lisp

Modified: trunk/main/claw-html/src/tags.lisp
==============================================================================
--- trunk/main/claw-html/src/tags.lisp	(original)
+++ trunk/main/claw-html/src/tags.lisp	Wed Dec 17 14:48:31 2008
@@ -281,6 +281,10 @@
   "Creates an unflattenable value for tag attributes. This is particularly useful when you need to pass a list as an attribute value"
   (make-list-for-tag-attribute :value value))
 
+(defmacro when-let ((var form) &body body)
+           `(let ((,var ,form))
+              (when ,var , at body)))
+
 (defun flatten (tree &optional result-list)
   "Traverses the tree in order, collecting even non-null leaves into a list."
   (let ((result result-list))
@@ -716,10 +720,8 @@
       (page-format-raw page (page-json-suffix page)))))
 
 (defmethod page-body-init-scripts ((page page))
-  (let ((js-body ""))
-    (dolist (current-js (reverse (page-instance-initscripts page)))
-      (setf js-body (format nil "~a~%~a~%" js-body current-js)))
-    (format nil "~@[~a~]" js-body)))
+  (when-let (init-scripts (reverse (page-instance-initscripts page)))
+    (format nil "~{~a~%~}" init-scripts)))
 
 (defmethod page-print-tabulation ((page page))
   (let ((tabulator (page-tabulator page))
@@ -736,16 +738,8 @@
 
 (defmethod page-init-injections ((page page))
   (let ((tag-list)
-        (class-init-scripts "")
-        (init-styles ""))
-    (dolist (script (reverse (page-class-initscripts page)))
-      (setf class-init-scripts (format nil "~a~%~a"
-                                       class-init-scripts
-                                       script)))
-    (dolist (style (reverse (page-initstyles page)))
-      (setf init-styles (format nil "~a~%~a"
-                                init-styles
-                                style)))
+        (class-init-scripts (format nil "~{~a~%~}" (reverse (page-class-initscripts page))))
+        (init-styles (format nil "~{~a~%~}" (reverse (page-initstyles page)))))    
     (unless (string= "" class-init-scripts)
       (let ((current-js (script> :type "text/javascript")))
         (setf (htcomponent-body current-js) class-init-scripts)
@@ -889,6 +883,7 @@
           (cond
             ((stringp child-tag) (htcomponent-render ($> child-tag) page))
             ((functionp child-tag) (htcomponent-render ($> (funcall child-tag)) page))
+            ((null child-tag) nil)
             (t (htcomponent-render child-tag page)))))
       (when (null previous-print-status)
         (setf (page-can-print page) nil)
@@ -979,6 +974,7 @@
           (cond
             ((stringp child-tag) (htcomponent-render ($> child-tag) page))
             ((functionp child-tag) (htcomponent-render ($> (funcall child-tag)) page))
+            ((null child-tag) nil)
             (t (htcomponent-render child-tag page)))))
       (when (or (page-can-print page) previous-print-status)
         (tag-render-endtag tag page))
@@ -1005,6 +1001,7 @@
               (cond
                 ((stringp child-tag) (htcomponent-render ($> child-tag) page))
                 ((functionp child-tag) (htcomponent-render ($> (funcall child-tag)) page))
+                ((null child-tag) nil)
                 (t (htcomponent-render child-tag page)))))
           (dolist (injection injections)
             (when injection
@@ -1062,7 +1059,7 @@
                    (not (null (htcomponent-body htscript))))
           (if (null xml-p)
               (page-format page "~%//<!--~%")
-              (page-format page "~%//<[CDATA[~%"))
+              (page-format page "~%//<![CDATA[~%"))
           (unless (listp body)
             (setf body (list body)))
           (dolist (element body)
@@ -1070,6 +1067,7 @@
               (cond
                 ((stringp element) (htcomponent-render ($raw> element) page))
                 ((functionp element) (htcomponent-render ($raw> (funcall element)) page))
+                ((null element) nil)
                 (t (htcomponent-render element page)))))
           (if (null xml-p)
               (page-format page "~%//-->")
@@ -1116,32 +1114,33 @@
           (cond
             ((stringp child-tag) (htcomponent-render ($> child-tag) page))
             ((functionp child-tag) (htcomponent-render ($> (funcall child-tag)) page))
+            ((null child-tag) nil)
             (t (htcomponent-render child-tag page)))))
       (when (page-can-print page)
-        (htcomponent-render (htbody-init-scripts-tag page t) page)
+        (when-let (init-script-tag (htbody-init-scripts-tag page t))
+          (htcomponent-render init-script-tag page))
         (tag-render-endtag htbody page))
       (when (or (page-can-print page) previous-print-status)
         (setf (page-can-print page) nil)
         (htcomponent-json-print-end-component htbody)))))
 
 (defmethod htbody-init-scripts-tag ((page page) &optional on-load)
-  (let ((js (script> :type "text/javascript"))
-        (js-control-string-directive (if on-load 
-                                         "
-var bodyInitFunction = function\(e){~{~a~%~}};~%
+  (when-let (scripts (page-body-init-scripts page))
+    (let ((js (script> :type "text/javascript"))
+          (js-control-string (if on-load 
+                                 "
+var bodyInitFunction = function\(e){~a~%};~%
 if (/MSIE (\\d+\\.\\d+);/.test(navigator.userAgent)) {~%
   window.attachEvent\('onload', bodyInitFunction);~%
 } else {~%
   document.addEventListener\('DOMContentLoaded', bodyInitFunction, false);~%
 }"
-                                         "~{~a~%~}~%"))
-        (page-body-init-scripts (page-body-init-scripts page)))
-    (setf (htcomponent-page js) page
-          (htcomponent-body js) (when page-body-init-scripts
-                                  (format nil js-control-string-directive (if (listp page-body-init-scripts)
-                                                                              page-body-init-scripts
-                                                                              (list page-body-init-scripts)))))
-    js))
+                                           "~a~%")))
+      (setf (htcomponent-page js) page
+            (htcomponent-body js) (format nil
+                                          js-control-string
+                                          scripts))
+      js)))
 
 ;;;========= WCOMPONENT ===================================
 (defclass wcomponent (htcomponent)
@@ -1299,6 +1298,7 @@
           (cond
             ((stringp child-tag) (htcomponent-render ($> child-tag) page))
             ((functionp child-tag) (htcomponent-render ($> (funcall child-tag)) page))
+            ((null child-tag) nil)
             (t (htcomponent-render child-tag page)))))
       (wcomponent-after-render wcomponent page)
       (when (null previous-print-status)




More information about the Claw-cvs mailing list