Skip to content

Commit 26fc815

Browse files
authored
Only visible attributes should be rendered
1 parent ad0994c commit 26fc815

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/Html5Gen/Html5Gen.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ public static function __callStatic($name, $arguments)
196196
$attrName = $attrValue;
197197
}
198198

199-
$elem->setAttribute($attrName, $attrValue);
199+
if (self::isVisible($elem->tagName, $attrName, $attrValue) ||
200+
$attrValue != false && $attrValue != "false")
201+
{
202+
$elem->setAttribute($attrName, $attrValue);
203+
}
200204
}
201205
}
202206

@@ -208,7 +212,7 @@ public static function __callStatic($name, $arguments)
208212
if (isset($arguments[1]) && is_callable($arguments[1]))
209213
{
210214
self::$parentNodes[] = $elem;
211-
$retVal = $arguments[1]();
215+
$retVal = $arguments[1]($elem);
212216

213217
if ($retVal instanceof \Traversable)
214218
{
@@ -231,6 +235,21 @@ public static function __callStatic($name, $arguments)
231235
return $elem->ownerDocument->saveHTML($elem);
232236
}
233237

238+
private static function isVisible($elem, $attrName, $attrValue)
239+
{
240+
$html = new \DOMDocument();
241+
242+
$elem = $html->createElement($elem);
243+
$elem->setAttribute($attrName, $attrValue);
244+
$html->appendChild($elem);
245+
246+
if ($attrValue && strpos($html->saveHTML(), $attrValue) !== false) {
247+
return true;
248+
}
249+
250+
return false;
251+
}
252+
234253
/**
235254
* @param string $tag
236255
* @param array &$attrs
@@ -721,4 +740,4 @@ public function __toString()
721740
{
722741
return self::$document->saveHTML();
723742
}
724-
}
743+
}

0 commit comments

Comments
 (0)