Skip to content

Commit

Permalink
Extend HtmlImage by loading parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Käfer committed Jul 13, 2021
1 parent 5ead8e8 commit 0b92beb
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 0b92beb

Please sign in to comment.