diff options
author | Andres Rey <[email protected]> | 2017-11-12 19:52:12 +0000 |
---|---|---|
committer | Andres Rey <[email protected]> | 2017-11-12 19:52:12 +0000 |
commit | 0dac0772bbb1e723557d9eca5c793d502a2b2e6e (patch) | |
tree | 6c456bb555ac805d55dee4a9fe28315e512369f5 /src | |
parent | 422ae0004eba3431fb9d7653ff913443ab048d16 (diff) |
Clean specific attributes during _cleanStyles
Diffstat (limited to 'src')
-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; } } |