Permalink
Browse files

Add option to retain specified HTML attributes.

  • Loading branch information...
1 parent c9ea9eb commit 6a85902df69df8eea8cd3097d1de3fc35bb06bec @jimsafley jimsafley committed May 2, 2012
Showing with 7 additions and 2 deletions.
  1. +7 −2 lib/Scripto.php
View
@@ -744,9 +744,10 @@ static public function isValidApiUrl($apiUrl)
*
* @see http://www.php.net/manual/en/domdocument.loadhtml.php#95251
* @param string $html
+ * @param array $exceptions Do not remove these attributes.
* @return string
*/
- static public function removeHtmlAttributes($html)
+ static public function removeHtmlAttributes($html, array $exceptions = array('href'))
{
// Check for an empty string.
$html = trim($html);
@@ -760,8 +761,12 @@ static public function removeHtmlAttributes($html)
$doc->loadHTML('<?xml encoding="UTF-8">' . $html);
$xpath = new DOMXPath($doc);
- // Iterate over and remove all attributes.
+ // Iterate over and remove attributes.
foreach ($xpath->evaluate('//@*') as $attribute) {
+ // Do not remove specified attributes.
+ if (in_array($attribute->name, $exceptions)) {
+ continue;
+ }
$attribute->ownerElement->removeAttributeNode($attribute);
}

0 comments on commit 6a85902

Please sign in to comment.