getAttribute() public method

Returns an attribute by the key
public getAttribute ( string $key ) : mixed
$key string
return mixed
Example #1
0
 public function testSetAttributes()
 {
     $attr = ['href' => ['value' => 'http://google.com', 'doublequote' => false]];
     $tag = new Tag('a');
     $tag->setAttributes($attr);
     $this->assertEquals('http://google.com', $tag->getAttribute('href')['value']);
 }
Example #2
0
 public function testSetAttributeNoArray()
 {
     $tag = new Tag('a');
     $tag->setAttribute('href', 'http://google.com');
     $attribute = $tag->getAttribute('href');
     $this->assertEquals('http://google.com', $attribute['value']);
 }
 /**
  * A wrapper method that simply calls the getAttribute method
  * on the tag of this node.
  *
  * @param string $key
  * @return mixed
  */
 public function getAttribute($key)
 {
     $attribute = $this->tag->getAttribute($key);
     if (!is_null($attribute)) {
         $attribute = $attribute['value'];
     }
     return $attribute;
 }
Example #4
0
 /**
  * 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;
 }