parse();
$this->errors = $treeBuilder->getErrors();
return $treeBuilder->document();
}
/**
* Utility function for parsing a fragment of HTML5.
*/
protected function parseFragment($string)
{
$treeBuilder = new DOMTreeBuilder(true);
$scanner = new Scanner($string);
$parser = new Tokenizer($scanner, $treeBuilder);
$parser->parse();
$this->errors = $treeBuilder->getErrors();
return $treeBuilder->fragment();
}
public function testDocument()
{
$html = '';
$doc = $this->parse($html);
$this->assertEquals('UTF-8', $doc->encoding);
$this->assertInstanceOf('\DOMDocument', $doc);
$this->assertEquals('html', $doc->documentElement->tagName);
$this->assertEquals('http://www.w3.org/1999/xhtml', $doc->documentElement->namespaceURI);
}
public function testBareAmpersand()
{
$html = "
";
$doc = $this->parse($html);
$this->assertEmpty($this->errors);
$this->assertXmlStringEqualsXmlString('
', $doc->saveXML());
}
public function testBareAmpersandNotAllowedInAttributes()
{
$html = "
";
$doc = $this->parse($html);
$this->assertCount(2, $this->errors);
$this->assertXmlStringEqualsXmlString('
', $doc->saveXML());
}
public function testBareAmpersandNotAllowedInBody()
{
$html = '
a&b
a&=
a&=c
a&=9
a&+
a& -- valid
';
$doc = $this->parse($html);
$this->assertCount(5, $this->errors);
$this->assertXmlStringEqualsXmlString('
a&b
a&=
a&=c
a&=9
a&+
a& -- valid
', $doc->saveXML());
}
public function testEntityAtEndOfFile()
{
$fragment = $this->parseFragment('');
$this->assertInstanceOf('DOMDocumentFragment', $fragment);
$this->assertSame('', $fragment->textContent);
$this->assertEquals('Line 1, Col 2: Expected DEC; HEX;, got EOF', $this->errors[0]);
}
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 testDocumentWithDisabledNamespaces()
{
$html = '';
$doc = $this->parse($html, array('disable_html_ns' => true));
$this->assertInstanceOf('\DOMDocument', $doc);
$this->assertEquals('html', $doc->documentElement->tagName);
$this->assertNull($doc->documentElement->namespaceURI);
}
public function testDocumentWithATargetDocument()
{
$targetDom = new \DOMDocument();
$html = '';
$doc = $this->parse($html, array('target_document' => $targetDom));
$this->assertInstanceOf('\DOMDocument', $doc);
$this->assertSame($doc, $targetDom);
$this->assertEquals('html', $doc->documentElement->tagName);
}
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