Author: chabotc
Date: Fri Aug 14 12:17:59 2009
New Revision: 804181

URL: http://svn.apache.org/viewvc?rev=804181&view=rev
Log:
When a proxied content document didn't have a head tag, DOM Rewriting based 
script and css injection failed since it never triggered the head listener, to 
avoid that a head tag is automatically added if it's missing

Modified:
    incubator/shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php
    incubator/shindig/trunk/php/src/gadgets/rewrite/GadgetRewriter.php

Modified: incubator/shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php?rev=804181&r1=804180&r2=804181&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php 
(original)
+++ incubator/shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php Fri 
Aug 14 12:17:59 2009
@@ -266,7 +266,7 @@
     $rewriter = new GadgetRewriter($this->context);
     $rewriter->addObserver('head', $this, 'addHeadTags');
     $rewriter->addObserver('body', $this, 'addBodyTags');
-    return $rewriter->rewrite($content, $this->gadget);
+    return $rewriter->rewrite($content, $this->gadget, true);
   }
 
   /**

Modified: incubator/shindig/trunk/php/src/gadgets/rewrite/GadgetRewriter.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/rewrite/GadgetRewriter.php?rev=804181&r1=804180&r2=804181&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/rewrite/GadgetRewriter.php 
(original)
+++ incubator/shindig/trunk/php/src/gadgets/rewrite/GadgetRewriter.php Fri Aug 
14 12:17:59 2009
@@ -38,7 +38,7 @@
    * @param string $content
    * @param Gadget $gadget
    */
-  public function rewrite($content, Gadget &$gadget) {
+  public function rewrite($content, Gadget &$gadget, $checkDocument = false) {
     // Check to see if the gadget requested rewriting, or if rewriting is 
forced in the configuration
     if (is_array($gadget->gadgetSpec->rewrite) || 
Config::get('rewrite_by_default')) {
       require_once "src/gadgets/rewrite/ContentRewriter.php";
@@ -68,6 +68,11 @@
         // parsing failed, return the unmodified content
         return $content;
       }
+
+      if ($checkDocument) {
+       $this->checkDocument();
+      }
+
       // find and parse all nodes in the dom document
       $rootNodes = $this->doc->getElementsByTagName('*');
       $this->parseNodes($rootNodes);
@@ -82,6 +87,32 @@
   }
 
   /**
+   * Proxied content documents do not always have a head tag which would cause 
the css & script injection
+   * to fail (since that node listeners would never be called). Do note that 
the html and body tags will
+   * already be automatically added by the DOMDocument->loadHtml function
+   */
+  private function checkDocument() {
+    foreach ($this->doc->childNodes as $node) {
+       $htmlNode = false;
+       if (isset($node->tagName) && strtolower($node->tagName) == 'html') {
+               $htmlNode = $node;
+               $hasHeadTag = false;
+               foreach ($node->childNodes as $htmlChild) {
+                       if (isset($htmlChild->tagName) && $htmlChild->tagName 
== 'head') {
+                               $hasHeadTag = true;
+                               break;
+                       }
+               }
+               // If no <head> tag was found but we do have a <html> node, 
then add the <head> tag to it
+               if (!$hasHeadTag && $htmlNode && $htmlNode->childNodes->length 
> 0) {
+                       $firstChild = $htmlNode->childNodes->item(0);
+          $htmlNode->insertBefore($this->doc->createElement('head'), 
$firstChild);
+               }
+       }
+    }
+  }
+
+  /**
    * This function should be called from the DomRewriter implmentation class 
in the form of:
    * addObserver('img', $this, 'rewriteImage')
    *


Reply via email to