Skip to content

Commit 71c7783

Browse files
committed
Handle HTML that lacks a root element (eg, has 2 top-level elements)
1 parent 425c82a commit 71c7783

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/Quip.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ static public function load($source, $options = 0, $data_is_url = FALSE, $ns = '
8585
));
8686
}
8787
$quip = new QuipXmlElement($source, $options, $data_is_url, $ns, $is_prefix);
88-
} catch (\Exception $e) {
88+
}
89+
catch (\Exception $e) {
8990
try {
9091
$data = $data_is_url ? file_get_contents($source) : $source;
9192
$data = strtr($data, array(
@@ -102,16 +103,23 @@ static public function load($source, $options = 0, $data_is_url = FALSE, $ns = '
102103
$cursor = &$dom->getElementsByTagName('body')->item(0);
103104
}
104105
else {
106+
// If there is no single root element, wrap the HTML and try again.
107+
$children = &$dom->getElementsByTagName('body')->item(0)->childNodes;
108+
if ($children->count() > 1) {
109+
return static::load("<div>$data</div>", $options, FALSE, $ns, $is_prefix, $quip_options);
110+
}
111+
105112
// hhvm returns the root by default rather than the original imported item.
106-
$cursor = &$dom->getElementsByTagName('body')->item(0)->childNodes->item(0);
113+
$cursor = $children->item(0);
107114
}
108115
}
109116
else {
110117
$dom->loadHTML($data);
111118
$cursor = &$dom;
112119
}
113120
$quip = simplexml_import_dom($cursor, '\\QuipXml\\Xml\\QuipXmlElement');
114-
} catch (\Exception $e) {
121+
}
122+
catch (\Exception $e) {
115123
$return();
116124
throw $e;
117125
}
@@ -121,4 +129,4 @@ static public function load($source, $options = 0, $data_is_url = FALSE, $ns = '
121129
return $quip;
122130
}
123131

124-
}
132+
}

0 commit comments

Comments
 (0)