Skip to content

Commit

Permalink
Merge pull request #1 from alka-dev/master
Browse files Browse the repository at this point in the history
Add loading attribute to image element
  • Loading branch information
naucon authored Jul 15, 2021
2 parents 5ead8e8 + 0b92beb commit 62b90c8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ Instances of `HtmlElementUniversalAbstract`, such as achor, break, button, div,

`getId()`, `setId($id)`, `getClass()`, `setClass($class)`, `getStyle()`, `setStyle($style)`, `getTitle()`, `setTitle($title)`, `getOnClick()`, `setOnClick($event)`, `getOnDoubleClick()`, `setOnDoubleClick($event)`, `getOnMouseDown()`, `setOnMouseDown($event)`, `getOnMouseUp()`, `setOnMouseUp($event)`, `getOnMouseOver()`, `setOnMouseOver($event)`, `getOnMouseMove()`, `setOnMouseMove($event)`, `getOnMouseOut()`, `setOnMouseOut($event)`, `getOnKeyPress()`, `setOnKeyPress($event)`, `getOnKeyDown()`, `setOnKeyDown($event)`, `getOnKeyUp()`, `setOnKeyUp($event)`

Some html elements also have special getter-/setter-methodes to access and modify attributes. For Example anchor (href, target, name, onblur, onfocus), button (type, name, value, tabindex, disabled, onblur, onfocus), image (src, alt, width, height), meta (name), label (for), form (action, method, target, enctype, autocomplete, onreset, onsubmit), input (type, name, disabled, readonly, onblur, onchange, onfocus).
Some html elements also have special getter-/setter-methodes to access and modify attributes. For Example anchor (href, target, name, onblur, onfocus), button (type, name, value, tabindex, disabled, onblur, onfocus), image (src, alt, width, height, loading), meta (name), label (for), form (action, method, target, enctype, autocomplete, onreset, onsubmit), input (type, name, disabled, readonly, onblur, onchange, onfocus).


### Nested elements
Expand Down
25 changes: 24 additions & 1 deletion src/HtmlImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,27 @@ public function setHeight($height)
}
return $this;
}
}

/**
* @return string
*/
public function getLoading()
{
return $this->getAttribute('loading');
}

/**
* @param string $loading
* @return HtmlImage
*/
public function setLoading($loading)
{
if ($loading === 'eager' || $loading === 'lazy') {
$this->setAttribute('loading', $loading);
} else {
$this->setAttribute('loading', null);
}

return $this;
}
}
22 changes: 22 additions & 0 deletions tests/HtmlImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,26 @@ public function testSetClass()
$htmlElementObject->setClass('testClass');
$this->assertEquals('<img src="' . $src . '" class="testClass" />', $htmlBuilder->render($htmlElementObject));
}

public function testSetLoading()
{
$htmlBuilder = new HtmlBuilder();

$src = 'foo.png';

$htmlElementObject = new HtmlImage($src);
$htmlElementObject->setLoading('eager');
$this->assertEquals('<img src="' . $src . '" loading="eager" />', $htmlBuilder->render($htmlElementObject));
}

public function testSetLoadingWithWrongValue()
{
$htmlBuilder = new HtmlBuilder();

$src = 'foo.png';

$htmlElementObject = new HtmlImage($src);
$htmlElementObject->setLoading('xyz');
$this->assertEquals('<img src="' . $src . ' />', $htmlBuilder->render($htmlElementObject));
}
}

0 comments on commit 62b90c8

Please sign in to comment.