-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathImageElement.php
54 lines (46 loc) · 1.25 KB
/
ImageElement.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace Bkwld\Decoy\Markup;
use HtmlObject\Element;
/**
* An image Element should render nothing if there is no image src data
*/
class ImageElement extends Element
{
/**
* Expose a method that lets the $isSelfClosing state be set exeternally
*
* @param boolean $bool
* @return $this
*/
public function isSelfClosing($bool = true)
{
$this->isSelfClosing = $bool;
return $this;
}
/**
* Check that an image URL exists before allowing the tag to render
*
* @return string
*/
public function render()
{
// Different conditions for different types of tags
switch ($this->element) {
// Img tags use the src
case 'img':
if (empty($this->getAttribute('src'))) {
return '';
}
break;
// Divs have the image as a background-image
// https://regex101.com/r/eF0oD0/1
default:
if (!preg_match('#background-image:\s*url\([\'"]?[\w\/]#',
$this->getAttribute('style'))) {
return '';
}
}
// Carry on with normal rendering
return parent::render();
}
}