09 December 2010

We convert CSS stylesheets to inline styles for some email applications with a system built on top of the ZF1 Css2Xpath class. However, it does not support all CSS selectors. Since we are converting to inline, there is no reason to restrict ourselves to browser-supported selectors, and the "E+F" selector (F is preceded immediately by E) is quite handy. For instance, you can use it to modify the margin-top style of the first <p> tag after an <h2> (or <table> or whatnot). Since ZF1 is frozen and soon to be deprecated, the patch is simply dumped below.

Index: Css2Xpath.php
===================================================================
--- Css2Xpath.php       (revision 21101)
+++ Css2Xpath.php       (working copy)
@@ -72,7 +72,15 @@
                 }
             } else {
                 foreach ($paths as $key => $xpath) {
-                    $paths[$key] .= '//' . $pathSegment;
+                    if (substr($xpath, -2) == '::') {
+                        $paths[$key] .= $pathSegment;
+                    }
+                    elseif (strpos($pathSegment, '::') !== FALSE) {
+                        $paths[$key] .= '/' . $pathSegment;
+                    }
+                    else {
+                      $paths[$key] .= '//' . $pathSegment;
+                    }
                 }
             }
         }
@@ -91,6 +99,10 @@
      */
     protected static function _tokenize($expression)
     {
+        if ($expression == '+') {
+            return 'following-sibling::';
+        }
+
         // Child selectors
         $expression = str_replace('>', '/', $expression);


blog comments powered by Disqus