summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/HTML5.php2
-rw-r--r--src/HTML5/Parser/DOMTreeBuilder.php23
-rw-r--r--test/HTML5/Serializer/TraverserTest.php24
3 files changed, 35 insertions, 14 deletions
diff --git a/src/HTML5.php b/src/HTML5.php
index a36de0f..d43539f 100644
--- a/src/HTML5.php
+++ b/src/HTML5.php
@@ -152,7 +152,7 @@ class HTML5 {
}
public static function parseFragment(\HTML5\Parser\InputStream $input) {
- $events = new DOMTreeBuilder(DOMTreeBuilder::IM_IN_BODY);
+ $events = new DOMTreeBuilder(TRUE);
$scanner = new Scanner($input);
$parser = new Tokenizer($scanner, $events);
diff --git a/src/HTML5/Parser/DOMTreeBuilder.php b/src/HTML5/Parser/DOMTreeBuilder.php
index 416d29f..ebb0acc 100644
--- a/src/HTML5/Parser/DOMTreeBuilder.php
+++ b/src/HTML5/Parser/DOMTreeBuilder.php
@@ -56,8 +56,9 @@ class DOMTreeBuilder implements EventHandler {
*/
protected $quirks = TRUE;
- public function __construct($insertMode = self::IM_INITIAL) {
- $this->insertMode = $insertMode;
+ public $isFragment = FALSE;
+
+ public function __construct($isFragment = FALSE) {
$impl = new \DOMImplementation();
// XXX:
// Create the doctype. For now, we are always creating HTML5
@@ -72,6 +73,14 @@ class DOMTreeBuilder implements EventHandler {
// Create a rules engine for tags.
$this->rules = new TreeBuildingRules($this->doc);
+
+ if ($isFragment) {
+ $this->isFragment = TRUE;
+ $this->insertMode = self::IM_IN_BODY;
+ $ele = $this->doc->createElement('html');
+ $this->doc->appendChild($ele);
+ $this->current = $ele;
+ }
}
/**
@@ -93,9 +102,18 @@ class DOMTreeBuilder implements EventHandler {
$append = $this->doc->documentElement->childNodes;
$frag = $this->doc->createDocumentFragment();
+ // appendChild() modifies the DOMNodeList, so we
+ // have to buffer up the items first, then use the
+ // array buffer and loop twice.
+ $buffer = array();
foreach ($append as $node) {
+ $buffer[] = $node;
+ }
+
+ foreach ($buffer as $node) {
$frag->appendChild($node);
}
+
$frag->errors = $this->doc->errors;
return $frag;
}
@@ -132,6 +150,7 @@ class DOMTreeBuilder implements EventHandler {
* - Omission rules: 8.1.2.4 Optional tags
*/
public function startTag($name, $attributes = array(), $selfClosing = FALSE) {
+ // fprintf(STDOUT, $name);
$lname = $this->normalizeTagName($name);
// Make sure we have an html element.
diff --git a/test/HTML5/Serializer/TraverserTest.php b/test/HTML5/Serializer/TraverserTest.php
index faeb0e3..872922d 100644
--- a/test/HTML5/Serializer/TraverserTest.php
+++ b/test/HTML5/Serializer/TraverserTest.php
@@ -57,17 +57,19 @@ class TraverserTest extends \HTML5\Tests\TestCase {
$this->assertInstanceOf('\HTML5\Serializer\Traverser', $t);
}
- // function testFragment() {
- // $html = '<span class="bar">foo</span>';
- // $input = new \HTML5\Parser\StringInputStream($html);
- // $dom = \HTML5::parseFragment($input);
+ function testFragment() {
+ $html = '<span class="bar">foo</span><span></span><div>bar</div>';
+ $input = new \HTML5\Parser\StringInputStream($html);
+ $dom = \HTML5::parseFragment($input);
- // $this->assertInstanceOf('\DOMDocumentFragment', $dom);
+ //fprintf(STDOUT, print_r($dom, TRUE));
- // $stream = fopen('php://temp', 'w');
- // $t = new Traverser($dom, $stream, \HTML5::options());
+ $this->assertInstanceOf('\DOMDocumentFragment', $dom);
- // $out = $t->walk();
- // $this->assertEquals($html, stream_get_contents($stream, -1, 0));
- // }
-} \ No newline at end of file
+ $stream = fopen('php://temp', 'w');
+ $t = new Traverser($dom, $stream, \HTML5::options());
+
+ $out = $t->walk();
+ $this->assertEquals($html, stream_get_contents($stream, -1, 0));
+ }
+}