private function getDOMNodeOuterHTML(DOMNode $elem) { $tag = $elem->nodeName; $id = $elem->getAttribute('id'); $class = $elem->getAttribute('class'); $elemInnerHtml = $this->getInnerHtml($elem); $parentInnerHtml = $this->getInnerHTML($elem->parentNode); $parentXNode = new XNode($parentInnerHtml); $select = ''; if ($tag) { $select .= $tag; } if ($id) { $select .= '#' . $id; } if ($class) { $select .= '.' . preg_replace('/\\s+/', '.', $class); } $elemsCount = $parentXNode->getCount($select); if ($elemsCount == 1) { return $parentXNode($select, 0)->outer(); } if ($elemsCount > 1) { $possibles = []; foreach ($parentXNode($select) as $_elem) { if ($_elem->inner() == $elemInnerHtml) { $possibles[] = $_elem; } } if (count($possibles) != 1) { trigger_error('DOMNode parse error: ambiguous parent-child elements. (selected ' . count($possibles) . ' elements by "' . $select . '" query) ', E_USER_WARNING); } return $possibles[0]->outer(); } if ($elemsCount == 0) { throw new XParserException('DOMNode parse error.'); } }
public function mainTest() { $tpl = new XNode('<html> <head> <title>Test page</title> </head> <body> <h1>Lorem ipsum</h1> <div /> <div>asd</div> <div id= "hello01" asdasdw /> <!-- <div id= "hello02" asdasdw class="message" asdasd /> --> <hr> <div id="hello1" class="message"> Hello World! </div> <hr> <div id="hello2" class="message selected"> before <span>Hello World!</span> after </div> <hr> <div id="hello3" class="message"> before <div>Hello <span>here</span> World!</div> after </div> <hr> <input type="text" id="myinput1" value="my value here.."> </body> </html>'); $before = $tpl->find('div#hello2.selected.message, div#hello1')->inner(); $tpl->find('div#hello2.selected.message, div#hello1')->inner('yupeeee!'); $after = $tpl->find('div#hello2.selected.message, div#hello1')->inner(); $this->equ($before, ' Hello World! '); $this->equ($after, 'yupeeee!'); $before = $tpl->find('html body input')->attr('value'); $tpl->find('input')->attr('value', 'elembe!'); $after = $tpl->find('html body input')->attr('value'); $this->equ($before, 'my value here..'); $this->equ($after, 'elembe!'); $before = $tpl->outer(); $this->equ(count($tpl('#hello02')->getElements()), 0); $after = $tpl->outer(); $this->equ($before, $after); $this->equ(count($tpl('#hello02')->getElements()), $tpl->getCount('#hello02')); }