diff options
-rw-r--r-- | src/HTMLParser.php | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/src/HTMLParser.php b/src/HTMLParser.php index 8931678..cc5c1be 100644 --- a/src/HTMLParser.php +++ b/src/HTMLParser.php @@ -1203,23 +1203,28 @@ class HTMLParser **/ public function _cleanStyles($node) { - $cur = $node->firstChild; - - // Remove any root styles, if we're able. - //TODO Check if we actually need to check for the method to exist - if (method_exists($cur, 'removeAttribute')) { - $cur->removeAttribute('style'); + if (property_exists($node, 'tagName') && $node->tagName === 'svg') { + return; } - // Go until there are no more child nodes - while ($cur !== null) { - if ($cur->nodeType === XML_ELEMENT_NODE) { - // Remove style attribute(s) : - $cur->removeAttribute('style'); + // Do not bother if there's no method to remove an attribute + if (method_exists($node, 'removeAttribute')) { + $presentational_attributes = ['align', 'background', 'bgcolor', 'border', 'cellpadding', 'cellspacing', 'frame', 'hspace', 'rules', 'style', 'valign', 'vspace']; + // Remove `style` and deprecated presentational attributes + foreach ($presentational_attributes as $presentational_attribute) { + $node->removeAttribute($presentational_attribute); + } - $this->_cleanStyles($cur); + $deprecated_size_attribute_elems = ['table', 'th', 'td', 'hr', 'pre']; + if (property_exists($node, 'tagName') && in_array($node->tagName, $deprecated_size_attribute_elems)) { + $node->removeAttribute('width'); + $node->removeAttribute('height'); } + } + $cur = $node->firstChild; + while ($cur !== null) { + $this->_cleanStyles($cur); $cur = $cur->nextSibling; } } |