MDL-45256 portfolio: Do not require strict XML when parsing content

This commit is contained in:
Frederic Massart
2014-04-29 17:44:08 +08:00
parent 8f2747e5ca
commit d72a0202b7
+20 -4
View File
@@ -1224,13 +1224,25 @@ function portfolio_format_text_options() {
* @return object|array|string
*/
function portfolio_rewrite_pluginfile_url_callback($contextid, $component, $filearea, $itemid, $format, $options, $matches) {
$matches = $matches[0]; // no internal matching
$matches = $matches[0]; // No internal matching.
// Loads the HTML.
$dom = new DomDocument();
if (!$dom->loadXML($matches)) {
if (!$dom->loadHTML($matches)) {
return $matches;
}
// Navigates to the node.
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('/html/body/child::*');
if (empty($nodes) || count($nodes) > 1) {
// Unexpected sequence, none or too many nodes.
return $matches;
}
$dom = $nodes->item(0);
$attributes = array();
foreach ($dom->documentElement->attributes as $attr => $node) {
foreach ($dom->attributes as $attr => $node) {
$attributes[$attr] = $node->value;
}
// now figure out the file
@@ -1360,7 +1372,11 @@ function portfolio_include_callback_file($component, $class = null) {
* @return mixed
*/
function portfolio_rewrite_pluginfile_urls($text, $contextid, $component, $filearea, $itemid, $format, $options=null) {
$pattern = '/(<[^<]*?="@@PLUGINFILE@@\/[^>]*?(?:\/>|>.*?<\/[^>]*?>))/';
$patterns = array(
'(<(a|A)[^<]*?href="@@PLUGINFILE@@/[^>]*?>.*?</(a|A)>)',
'(<(img|IMG)\s[^<]*?src="@@PLUGINFILE@@/[^>]*?/?>)',
);
$pattern = '~' . implode('|', $patterns) . '~';
$callback = partial('portfolio_rewrite_pluginfile_url_callback', $contextid, $component, $filearea, $itemid, $format, $options);
return preg_replace_callback($pattern, $callback, $text);
}