Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adds a convenience method that removes attributes from HTML.

  • Loading branch information...
commit 7507ecff9dac0095c19be5c2ed6920dc67f9f548 1 parent f1e237f
Jim Safley authored
Showing with 31 additions and 0 deletions.
  1. +31 −0 lib/Scripto.php
View
31 lib/Scripto.php
@@ -618,4 +618,35 @@ static public function isValidApiUrl($apiUrl)
{
return Scripto_Service_MediaWiki::isValidApiUrl($apiUrl);
}
+
+ /**
+ * Remove all HTML attributes from the provided markup.
+ *
+ * This filter is useful after getting HTML from the MediaWiki API, which
+ * often contains MediaWiki-specific attributes that may conflict with local
+ * settings.
+ *
+ * @param string $html
+ * @return string
+ */
+ static public function removeHtmlAttributes($html)
+ {
+ // Check for an empty string.
+ $html = trim($html);
+ if (empty($html)) {
+ return $html;
+ }
+
+ // Load the HTML into DOM.
+ $doc = new DOMDocument();
+ $doc->loadHTML($html);
+ $xpath = new DOMXPath($doc);
+
+ // Iterate over and remove all attributes.
+ foreach ($xpath->evaluate('//@*') as $attribute) {
+ $attribute->ownerElement->removeAttributeNode($attribute);
+ }
+
+ return $doc->saveHTML();
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.