summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/HTML5/Parser/DOMTreeBuilder.php13
-rw-r--r--test/HTML5/Parser/DOMTreeBuilderTest.php67
2 files changed, 79 insertions, 1 deletions
diff --git a/src/HTML5/Parser/DOMTreeBuilder.php b/src/HTML5/Parser/DOMTreeBuilder.php
index 2807790..29e83c6 100644
--- a/src/HTML5/Parser/DOMTreeBuilder.php
+++ b/src/HTML5/Parser/DOMTreeBuilder.php
@@ -1,6 +1,7 @@
<?php
namespace HTML5\Parser;
+use HTML5\Elements;
/**
* Create an HTML5 DOM tree from events.
*
@@ -69,11 +70,23 @@ class DOMTreeBuilder implements EventHandler {
}
$ele = $this->doc->createElement($lname);
+ foreach ($attributes as $aName => $aVal) {
+ $ele->setAttribute($aName, $aVal);
+
+ // This is necessary on a non-DTD schema, like HTML5.
+ if ($aName == 'id') {
+ $ele->setIdAttribute('id', TRUE);
+ }
+ }
$this->current->appendChild($ele);
// XXX: Need to handle self-closing tags and unary tags.
$this->current = $ele;
+
+ // Return the element mask, which the tokenizer can then use to set
+ // various processing rules.
+ return Elements::element($name);
}
public function endTag($name) {
diff --git a/test/HTML5/Parser/DOMTreeBuilderTest.php b/test/HTML5/Parser/DOMTreeBuilderTest.php
index 963000c..a901238 100644
--- a/test/HTML5/Parser/DOMTreeBuilderTest.php
+++ b/test/HTML5/Parser/DOMTreeBuilderTest.php
@@ -1,12 +1,17 @@
<?php
/**
* @file
- * Test the Scanner. This requires the InputStream tests are all good.
+ * Test the Tree Builder.
*/
namespace HTML5\Parser;
+use HTML5\Elements;
+
require_once __DIR__ . '/../TestCase.php';
+/**
+ * These tests are functional, not necessarily unit tests.
+ */
class DOMTreeBuilderTest extends \HTML5\Tests\TestCase {
/**
@@ -30,4 +35,64 @@ class DOMTreeBuilderTest extends \HTML5\Tests\TestCase {
$this->assertInstanceOf('\DOMDocument', $doc);
$this->assertEquals('html', $doc->documentElement->tagName);
}
+
+ public function testElements() {
+ $html = "<!DOCTYPE html><html><head><title></title></head><body></body></html>";
+ $doc = $this->parse($html);
+ $root = $doc->documentElement;
+
+ $this->assertEquals('html', $root->tagName);
+ $this->assertEquals('html', $root->localName);
+ $this->assertEquals('html', $root->nodeName);
+
+ $this->assertEquals(2, $root->childNodes->length);
+ $kids = $root->childNodes;
+
+ $this->assertEquals('head', $kids->item(0)->tagName);
+ $this->assertEquals('body', $kids->item(1)->tagName);
+
+ $head = $kids->item(0);
+ $this->assertEquals(1, $head->childNodes->length);
+ $this->assertEquals('title', $head->childNodes->item(0)->tagName);
+ }
+
+ public function testAttributes() {
+ $html = "<!DOCTYPE html>
+ <html>
+ <head><title></title></head>
+ <body id='a' class='b c'></body>
+ </html>";
+ $doc = $this->parse($html);
+ $root = $doc->documentElement;
+
+ $body = $root->GetElementsByTagName('body')->item(0);
+ $this->assertEquals('body', $body->tagName);
+ $this->assertTrue($body->hasAttributes());
+ $this->assertEquals('a', $body->getAttribute('id'));
+ $this->assertEquals('b c', $body->getAttribute('class'));
+
+ $body2 = $doc->getElementById('a');
+ $this->assertEquals('body', $body2->tagName);
+ $this->assertEquals('a', $body2->getAttribute('id'));
+ }
+
+ public function testComment() {
+ $this->markTestIncomplete("Incomplete.");
+ }
+
+ public function testCDATA() {
+ $this->markTestIncomplete("Incomplete.");
+ }
+
+ public function testText() {
+ $this->markTestIncomplete("Incomplete.");
+ }
+
+ public function testParseErrors() {
+ $this->markTestIncomplete("Incomplete.");
+ }
+
+ public function testProcessingInstruction() {
+ $this->markTestIncomplete("Incomplete.");
+ }
}