parse();
$this->errors = $treeBuilder->getErrors();
return $treeBuilder->document();
}
/**
* Utility function for parsing a fragment of HTML5.
*/
protected function parseFragment($string)
{
$treeBuilder = new DOMTreeBuilder(true);
$input = new StringInputStream($string);
$scanner = new Scanner($input);
$parser = new Tokenizer($scanner, $treeBuilder);
$parser->parse();
$this->errors = $treeBuilder->getErrors();
return $treeBuilder->fragment();
}
public function testDocument()
{
$html = "";
$doc = $this->parse($html);
$this->assertInstanceOf('\DOMDocument', $doc);
$this->assertEquals('html', $doc->documentElement->tagName);
}
public function testStrangeCapitalization()
{
$html = "
Hello, world!
TheBody
";
$doc = $this->parse($html);
$this->assertInstanceOf('\DOMDocument', $doc);
$this->assertEquals('html', $doc->documentElement->tagName);
$xpath = new \DOMXPath( $doc );
$xpath->registerNamespace( "x", "http://www.w3.org/1999/xhtml" );
$this->assertEquals("Hello, world!", $xpath->query( "//x:title" )->item( 0 )->nodeValue);
$this->assertEquals("foo", $xpath->query( "//x:script" )->item( 0 )->nodeValue);
}
public function testDocumentFakeAttrAbsence()
{
$html = "foo";
$doc = $this->parse($html, array('xmlNamespaces'=>true));
$xp = new \DOMXPath($doc);
$this->assertEquals(0, $xp->query("//@html5-php-fake-id-attribute")->length);
}
public function testFragment()
{
$html = "test
test2";
$doc = $this->parseFragment($html);
$this->assertInstanceOf('\DOMDocumentFragment', $doc);
$this->assertTrue($doc->hasChildNodes());
$this->assertEquals('div', $doc->childNodes->item(0)->tagName);
$this->assertEquals('test', $doc->childNodes->item(0)->textContent);
$this->assertEquals('span', $doc->childNodes->item(1)->tagName);
$this->assertEquals('test2', $doc->childNodes->item(1)->textContent);
}
public function testElements()
{
$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 testImplicitNamespaces()
{
$dom = $this->parse('foo');
$a = $dom->getElementsByTagName('a')->item(0);
$attr = $a->getAttributeNode('xlink:href');
$this->assertEquals('http://www.w3.org/1999/xlink', $attr->namespaceURI);
$dom = $this->parse('foo');
$a = $dom->getElementsByTagName('a')->item(0);
$attr = $a->getAttributeNode('xml:base');
$this->assertEquals('http://www.w3.org/XML/1998/namespace', $attr->namespaceURI);
}
public function testCustomImplicitNamespaces()
{
$dom = $this->parse('foo', array(
'implicitNamespaces' => array(
't' => 'http://www.example.com'
)
));
$a = $dom->getElementsByTagName('a')->item(0);
$attr = $a->getAttributeNode('t:href');
$this->assertEquals('http://www.example.com', $attr->namespaceURI);
$dom = $this->parse('foo', array(
'implicitNamespaces' => array(
't' => 'http://www.example.com'
)
));
$list = $dom->getElementsByTagNameNS('http://www.example.com', 'a');
$this->assertEquals(1, $list->length);
}
public function testXmlNamespaces()
{
$dom = $this->parse(
'
foo
foo