public function testValueGetter() { $document = new HTMLDocument(test\Helper::HTML_VALUE); $select = $document->getElementById('select'); $this->assertEquals('1', $select->value); $select->value = '2'; $this->assertEquals('2', $select->value); $select = $document->getElementById('select_optgroup'); $this->assertEquals('3', $select->value); $select->value = '4'; $this->assertEquals('4', $select->value); $select = $document->getElementById('select_selected'); $this->assertEquals('2', $select->value); $select = $document->getElementById('select_empty'); $this->assertEquals('', $select->value); $select->value = 'dummy'; $this->assertEquals('', $select->value); }
public function testNamedItem() { $document = new HTMLDocument(test\Helper::HTML_MORE); $whoNamed = $document->body->children->namedItem("who"); $whoH2 = $document->getElementById("who"); $this->assertSame($whoNamed, $whoH2, "Named item should match by id first"); $formsNamed = $document->body->children->namedItem("forms"); $formsAnchor = $document->querySelector("a[name='forms']"); $this->assertSame($formsNamed, $formsAnchor, "Named item should match name when no id match"); }
public function testReplaceWithInSameDocument() { $document = new HTMLDocument(test\Helper::HTML_MORE); $h2 = $document->getElementById("who"); $beforeH2 = $h2->previousSibling; $h1 = $document->firstChild; $h2->replaceWith($h1); $this->assertSame($h1, $beforeH2->nextSibling); $this->assertNotSame($h1, $document->firstChild); }
public function testToggle() { $document = new HTMLDocument(test\Helper::HTML_MORE); $h2 = $document->getElementById("who"); $classString = "h-who m-before-p m-test"; $this->assertEquals($classString, $h2->getAttribute("class")); $classStringNoBefore = str_replace("m-before-p ", "", $classString); $h2->classList->toggle("m-before-p"); $this->assertEquals($classStringNoBefore, $h2->getAttribute("class")); $h2->classList->toggle("m-before-p"); $this->assertEquals("{$classStringNoBefore} m-before-p", $h2->getAttribute("class")); }
public function testElementSiblings() { $document = new HTMLDocument(test\Helper::HTML_MORE); $whoHeading = $document->getElementById("who"); $plugParagraph = $document->querySelector("p.plug"); $formsAnchor = $document->querySelector("a[name='forms']"); $this->assertEquals("p", $whoHeading->nextElementSibling->tagName); $this->assertSame($formsAnchor, $whoHeading->nextElementSibling->nextElementSibling); $this->assertSame($plugParagraph, $whoHeading->previousElementSibling); $this->assertInstanceOf("\\Gt\\Dom\\Element", $formsAnchor); $firstImg = $document->querySelector("img"); $this->assertEquals("h1", $firstImg->previousElementSibling->tagName); $this->assertNull($firstImg->previousElementSibling->previousElementSibling); $this->assertNull($document->body->lastElementChild->nextElementSibling); }
\t<div id="div3" class="hello hello2"> \t\ttext4 \t\t<div id="div4" class="hello2"> \t\t\ttext5 \t\t</div> \t</div> \ttext6 \t<div id="div5" class="hello2"> \t</div> \ttext7 </body> </html> HTML; $doc = new HTMLDocument($html); echo 'by id:'; var_dump($doc->getElementById('div3')->getOuterHTML()); echo 'by tag:'; foreach ($doc->getElementsByTag('div') as $elem) { var_dump($elem->__toString()); } echo 'by class:'; foreach ($doc->getElementsByClass('hello') as $elem) { var_dump($elem->__toString()); } echo 'by attribute:'; foreach ($doc->getElementsByAttribute('class', 'hello2') as $elem) { var_dump($elem->__toString()); } echo 'find----' . PHP_EOL; echo 'by tag'; foreach ($doc->find('div') as $elem) {