Author: chabotc
Date: Sun Jun 14 11:45:18 2009
New Revision: 784546
URL: http://svn.apache.org/viewvc?rev=784546&view=rev
Log:
The libxml2 based DOM parser sometimes mangles (invalid) html, which caused
previously working gadgets (like the todo gadget we often use as an example) to
break, so now the code detects if content rewriting and/or template parsing is
required, and if not, defaults to the old string-concactination behavior
Modified:
incubator/shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php
incubator/shindig/trunk/php/src/gadgets/render/GadgetHtmlRenderer.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=784546&r1=784545&r2=784546&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php
(original)
+++ incubator/shindig/trunk/php/src/gadgets/render/GadgetBaseRenderer.php Sun
Jun 14 11:45:18 2009
@@ -23,6 +23,7 @@
//TODO check if the opensocial-templates feature has disableAutoProcessing =
true as param, if so don't
+
class EmptyClass {
}
@@ -173,7 +174,7 @@
$this->addContextData($this->dataInserts);
}
} else {
- echo "Error parsing os-data:\n".XmlError::getErrors($osDataRequests);
+ echo "Error parsing os-data:\n" . XmlError::getErrors($osDataRequests);
}
}
@@ -194,7 +195,7 @@
$this->doc->recover = false;
$this->doc->resolveExternals = false;
if (! $this->doc->loadXML($template)) {
- return "Error parsing os-template:\n".XmlError::getErrors($template);
+ return "Error parsing os-template:\n" . XmlError::getErrors($template);
}
if ($this->doc->childNodes->length < 1 || $this->doc->childNodes->length
>> 1) {
return 'Invalid script block';
@@ -206,7 +207,7 @@
$requires = explode(',', $require);
foreach ($requires as $val) {
$val = trim($val);
- if (!isset($this->dataContext[$val])) {
+ if (! isset($this->dataContext[$val])) {
return false;
}
}
@@ -221,7 +222,8 @@
$output->appendChild($outNode);
}
// Restore single tags to their html variant, and remove the xml header
- $ret = str_replace(array('<?xml version="" encoding="utf-8"?>',
'<br/>'), array('', '<br>'), $output->saveXML());
+ $ret = str_replace(array(
+ '<?xml version="" encoding="utf-8"?>', '<br/>'), array('', '<br>'),
$output->saveXML());
return $ret;
}
return false;
@@ -245,43 +247,40 @@
}
/**
- * Append the runOnLoadHandlers script to the gadget's document body
+ * Generates the body script content
*
- * @param DOMElement $node
- * @param DOMDocument $doc
+ * @return string script
*/
- public function addBodyTags(DOMElement &$node, DOMDocument &$doc) {
+ public function getBodyScript() {
$script = "gadgets.util.runOnLoadHandlers();";
if ($this instanceof GadgetHrefRenderer) {
$script .= "
window.setTimeout(function(){gadgets.window.adjustHeight()}, 10);";
}
- $scriptNode = $doc->createElement('script');
- $scriptNode->setAttribute('type', 'text/javascript');
- $scriptNode->nodeValue = str_replace('&', '&', $script);
- $node->appendChild($scriptNode);
+ return $script;
}
/**
- * Adds the various bits of javascript to the gadget's document head element
+ * Append the runOnLoadHandlers script to the gadget's document body
*
* @param DOMElement $node
* @param DOMDocument $doc
*/
- public function addHeadTags(DOMElement &$node, DOMDocument &$doc) {
- // Inject our configured gadget document style
- $styleNode = $doc->createElement('style');
- $styleNode->setAttribute('type', 'text/css');
- $styleNode->appendChild($doc->createTextNode(Config::get('gadget_css')));
- $node->appendChild($styleNode);
- // Inject the OpenSocial feature javascripts
+ public function addBodyTags(DOMElement &$node, DOMDocument &$doc) {
+ $script = $this->getBodyScript();
+ $scriptNode = $doc->createElement('script');
+ $scriptNode->setAttribute('type', 'text/javascript');
+ $scriptNode->nodeValue = str_replace('&', '&', $script);
+ $node->appendChild($scriptNode);
+ }
+
+ public function getJavaScripts() {
$forcedJsLibs = $this->getForcedJsLibs();
+ $externalScript = false;
if (! empty($forcedJsLibs)) {
// if some of the feature libraries are externalized (through a browser
cachable <script src="/gadgets/js/opensocial-0.9:settitle.js"> type url)
// we inject the tag and don't inline those libs (and their dependencies)
$forcedJsLibs = explode(':', $forcedJsLibs);
- $scriptNode = $doc->createElement('script');
- $scriptNode->setAttribute('src', Config::get('default_js_prefix') .
$this->getJsUrl($forcedJsLibs, $this->gadget) . "&container=" .
$this->context->getContainer());
- $node->appendChild($scriptNode);
+ $externalScript = Config::get('default_js_prefix') .
$this->getJsUrl($forcedJsLibs, $this->gadget) . "&container=" .
$this->context->getContainer();
$registry = $this->context->getRegistry();
$missing = array();
$registry->resolveFeatures($forcedJsLibs, $forcedJsLibs, $missing);
@@ -304,9 +303,31 @@
$script .= "opensocial.data.DataContext.putDataSet(\"$key\",
$data);\n";
}
}
+ return array('inline' => $script, 'external' => $externalScript);
+ }
+
+ /**
+ * Adds the various bits of javascript to the gadget's document head element
+ *
+ * @param DOMElement $node
+ * @param DOMDocument $doc
+ */
+ public function addHeadTags(DOMElement &$node, DOMDocument &$doc) {
+ // Inject our configured gadget document style
+ $styleNode = $doc->createElement('style');
+ $styleNode->setAttribute('type', 'text/css');
+ $styleNode->appendChild($doc->createTextNode(Config::get('gadget_css')));
+ $node->appendChild($styleNode);
+ // Inject the OpenSocial feature javascripts
+ $scripts = $this->getJavaScripts();
+ if ($scripts['external']) {
+ $scriptNode = $doc->createElement('script');
+ $scriptNode->setAttribute('src', $scripts['external']);
+ $node->appendChild($scriptNode);
+ }
$scriptNode = $doc->createElement('script');
$scriptNode->setAttribute('type', 'text/javascript');
- $scriptNode->appendChild($doc->createTextNode($script));
+ $scriptNode->appendChild($doc->createTextNode($scripts['inline']));
$node->appendChild($scriptNode);
}
Modified: incubator/shindig/trunk/php/src/gadgets/render/GadgetHtmlRenderer.php
URL:
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/render/GadgetHtmlRenderer.php?rev=784546&r1=784545&r2=784546&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/render/GadgetHtmlRenderer.php
(original)
+++ incubator/shindig/trunk/php/src/gadgets/render/GadgetHtmlRenderer.php Sun
Jun 14 11:45:18 2009
@@ -38,13 +38,39 @@
if (! empty($view['quirks']) || ! $view['quirks']) {
$content .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"
\"http://www.w3.org/TR/html4/strict.dtd\">\n";
}
- $content .= "<html><head><meta http-equiv=\"Content-Type\"
content=\"text/html; charset=utf-8\"/></head><body>\n";
- // Append the content for the selected view
- $content .= $gadget->substitutions->substitute($view['content']);
- $content .= "\n</body>\n</html>";
- $content = $this->parseTemplates($content);
- $content = $this->rewriteContent($content);
- $content = $this->addTemplates($content);
+ // Rewriting the gadget's content using the libxml library does impose
some restrictions to the validity of the input html, so
+ // for the time being (until either gadgets are all fixed, or we find a
more tolerant html parsing lib), we try to avoid it when we can
+ $domRewrite = false;
+ if (isset($gadget->gadgetSpec->rewrite) ||
Config::get('rewrite_by_default')) {
+ $domRewrite = true;
+ } elseif (strpos($view['content'], 'text/os-data') !== false ||
strpos($view['content'], 'text/os-template') !== false) {
+ $domRewrite = true;
+ }
+ if (!$domRewrite) {
+ // Manually generate the html document using basic string concatinations
instead of using our DOM based functions
+ $content .= "<html>\n<head>\n<meta http-equiv=\"Content-Type\"
content=\"text/html; charset=utf-8\"/>\n";
+ $content .= '<style>'.Config::get('gadget_css')."</style>\n";
+ $scripts = $this->getJavaScripts();
+ if ($scripts['external']) {
+ $content .= "<script type=\"text/javascript\"
src=\"{$scripts['external']}\"></script>\n";
+ }
+ if (!empty($scripts['inline'])) {
+ $content .= "<script
type=\"text/javascript\">{$scripts['inline']}</script>\n";
+ }
+ $content .= "</head>\n<body>\n";
+ $content .= $gadget->substitutions->substitute($view['content']);
+ $content .= '<script
type="text/javascript">'.$this->getBodyScript()."</script>\n";
+ $content .= "\n</body>\n</html>\n";
+ } else {
+ // Use the (libxml2 based) DOM rewriter
+ $content .= "<html><head><meta http-equiv=\"Content-Type\"
content=\"text/html; charset=utf-8\"/></head><body>\n";
+ // Append the content for the selected view
+ $content .= $gadget->substitutions->substitute($view['content']);
+ $content .= "\n</body>\n</html>";
+ $content = $this->parseTemplates($content);
+ $content = $this->rewriteContent($content);
+ $content = $this->addTemplates($content);
+ }
echo $content;
}
}