Inheritance: extends InnerNode
示例#1
0
 public function href($href = null)
 {
     if ($href !== null) {
         $this->node->setAttribute('href', $href);
     }
     return $this->node->getAttribute('href');
 }
 /**
  * Attempts to detect the charset that the html was sent in.
  *
  * @return bool
  */
 protected function detectCharset()
 {
     // set the default
     $encode = new Encode();
     $encode->from($this->defaultCharset);
     $encode->to($this->defaultCharset);
     if (!is_null($this->options->enforceEncoding)) {
         //  they want to enforce the given encoding
         $encode->from($this->options->enforceEncoding);
         $encode->to($this->options->enforceEncoding);
         return false;
     }
     $meta = $this->root->find('meta[http-equiv=Content-Type]', 0);
     if (is_null($meta)) {
         // could not find meta tag
         $this->root->propagateEncoding($encode);
         return false;
     }
     $content = $meta->content;
     if (empty($content)) {
         // could not find content
         $this->root->propagateEncoding($encode);
         return false;
     }
     $matches = [];
     if (preg_match('/charset=(.+)/', $content, $matches)) {
         $encode->from(trim($matches[1]));
         $this->root->propagateEncoding($encode);
         return true;
     }
     // no charset found
     $this->root->propagateEncoding($encode);
     return false;
 }
 public function testToArray()
 {
     $root = new HtmlNode(new Tag('root'));
     $parent = new HtmlNode(new Tag('div'));
     $child1 = new HtmlNode(new Tag('a'));
     $child2 = new HtmlNode(new Tag('p'));
     $child3 = new HtmlNode(new Tag('a'));
     $root->addChild($parent);
     $parent->addChild($child1);
     $parent->addChild($child2);
     $child2->addChild($child3);
     $selector = new Selector('a');
     $collection = $selector->find($root);
     $array = $collection->toArray();
     $lastA = end($array);
     $this->assertEquals($child3->id(), $lastA->id());
 }
示例#4
0
 public function testFindChildUsingChildSelector()
 {
     $root = new HtmlNode(new Tag('root'));
     $parent = new HtmlNode(new Tag('div'));
     $child1 = new HtmlNode(new Tag('ul'));
     $child2 = new HtmlNode(new Tag('span'));
     $child3 = new HtmlNode(new Tag('ul'));
     $root->addChild($parent);
     $parent->addChild($child1);
     $child2->addChild($child3);
     $parent->addChild($child2);
     $selector = new Selector('div > ul');
     $this->assertEquals(1, count($selector->find($root)));
 }
 public function testGetAttributes()
 {
     $node = new HtmlNode('a');
     $node->getTag()->setAttributes(['href' => ['value' => 'http://google.com', 'doubleQuote' => false], 'class' => ['value' => 'outerlink rounded', 'doubleQuote' => true]]);
     $this->assertEquals('outerlink rounded', $node->getAttributes()['class']);
 }
示例#6
0
 public function testSetAttribute()
 {
     $node = new HtmlNode('a');
     $node->setAttribute('class', 'foo');
     $this->assertEquals('foo', $node->getAttribute('class'));
 }
 public function testFindXpathKeySelector()
 {
     $parent = new HtmlNode(new Tag('div'));
     $child1 = new HtmlNode(new Tag('a'));
     $child2 = new HtmlNode(new Tag('p'));
     $child3 = new HtmlNode(new Tag('a'));
     $child3->getTag()->setAttributes(['class' => ['value' => 'link outer', 'doubleQuote' => false]]);
     $parent->addChild($child1);
     $parent->addChild($child2);
     $parent->addChild($child3);
     $selector = new Selector('div[1]');
     $this->assertEquals($parent->id(), $selector->find($parent)[0]->id());
 }
示例#8
0
 /**
  * @expectedException PHPHtmlParser\Exceptions\ParentNotFoundException
  */
 public function testAncestorByTagFailure()
 {
     $a = new Tag('a');
     $node = new HtmlNode($a);
     $node->ancestorByTag('div');
 }
示例#9
0
 public function testIterator()
 {
     $div = new Tag('div');
     $div->setAttributes(['class' => ['value' => 'all', 'doubleQuote' => true]]);
     $a = new Tag('a');
     $a->setAttributes(['href' => ['value' => 'http://google.com', 'doubleQuote' => false]]);
     $br = new Tag('br');
     $br->selfClosing();
     $parent = new HtmlNode($div);
     $childa = new HtmlNode($a);
     $childbr = new HtmlNode($br);
     $parent->addChild($childa);
     $parent->addChild($childbr);
     $childa->addChild(new TextNode('link'));
     $children = 0;
     foreach ($parent as $child) {
         ++$children;
     }
     $this->assertEquals(2, $children);
 }