public function testOuterHtmlEmpty() { $a = new Tag('a'); $a->setAttributes(['href' => ['value' => 'http://google.com', 'doubleQuote' => false]]); $node = new HtmlNode($a); $this->assertEquals("<a href='http://google.com'></a>", $node->OuterHtml()); }
/** * Function to try a few tricks to determine the displayed size of an img on the page. * NOTE: This will ONLY work on an IMG tag. Returns FALSE on all other tag types. * * Future enhancement: * Look in the tag to see if there is a class or id specified that has a height or width attribute to it. * * Far future enhancement * Look at all the parent tags of this image to see if they specify a class or id that has an img selector that specifies a height or width * Note that in this case, the class or id will have the img sub-selector for it to apply to the image. * * ridiculously far future development * If the class or id is specified in a SEPARATE css file that's not on the page, go get it and do what we were just doing for the ones on the page. * * @author John Schlick * @return array an array containing the 'height' and 'width' of the image on the page or -1 if we can't figure it out. */ public function get_display_size() { $width = -1; $height = -1; if ($this->tag->name() != 'img') { return false; } // See if there is a height or width attribute in the tag itself. if (!is_null($this->tag->getAttribute('width'))) { $width = $this->tag->getAttribute('width'); } if (!is_null($this->tag->getAttribute('height'))) { $height = $this->tag->getAttribute('height'); } // Now look for an inline style. if (!is_null($this->tag->getAttribute('style'))) { // Thanks to user 'gnarf' from stackoverflow for this regular expression. $attributes = []; preg_match_all("/([\\w-]+)\\s*:\\s*([^;]+)\\s*;?/", $this->tag->getAttribute('style'), $matches, PREG_SET_ORDER); foreach ($matches as $match) { $attributes[$match[1]] = $match[2]; } // If there is a width in the style attributes: if (isset($attributes['width']) and $width == -1) { // check that the last two characters are px (pixels) if (strtolower(substr($attributes['width'], -2)) == 'px') { $proposed_width = substr($attributes['width'], 0, -2); // Now make sure that it's an integer and not something stupid. if (filter_var($proposed_width, FILTER_VALIDATE_INT)) { $width = $proposed_width; } } } // If there is a width in the style attributes: if (isset($attributes['height']) and $height == -1) { // check that the last two characters are px (pixels) if (strtolower(substr($attributes['height'], -2)) == 'px') { $proposed_height = substr($attributes['height'], 0, -2); // Now make sure that it's an integer and not something stupid. if (filter_var($proposed_height, FILTER_VALIDATE_INT)) { $height = $proposed_height; } } } } $result = ['height' => $height, 'width' => $width]; return $result; }
/** * A wrapper method that simply calls the removeAllAttributes * method on the tag of this node. * * @return void */ public function removeAllAttributes() { $this->tag->removeAllAttributes(); }
public function testMakeClosingTagSelfClosing() { $tag = new Tag('div'); $tag->selfClosing(); $this->assertEmpty($tag->makeClosingTag()); }
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); }