Allow HTML5 custom data attributes
authorAryeh Gregor <simetrical@users.mediawiki.org>
Thu, 12 Aug 2010 19:19:13 +0000 (19:19 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Thu, 12 Aug 2010 19:19:13 +0000 (19:19 +0000)
HTML5 allows any attribute beginning with "data-" to be used for
site-specific purposes:

http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#embedding-custom-non-visible-data

I don't see any reason not to allow this.  It means users won't have to
hackily overload title="" or class="" if they want to store per-element
data for scripts.

RELEASE-NOTES
includes/Sanitizer.php
maintenance/parserTests.txt

index dc18d40..8cc23cb 100644 (file)
@@ -146,6 +146,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 11005) Add CSS class to empty pages in Special:Newpages
 * The parser cache is now shared amongst users whose different settings aren't
   used in the page.
+* Any attribute beginning with "data-" can now be used in wikitext, per HTML5.
 
 === Bug fixes in 1.17 ===
 * (bug 17560) Half-broken deletion moved image files to deletion archive
index 8926b28..cfbda57 100644 (file)
@@ -627,7 +627,7 @@ class Sanitizer {
         * @todo Check for unique id attribute :P
         */
        static function validateAttributes( $attribs, $whitelist ) {
-               global $wgAllowRdfaAttributes, $wgAllowMicrodataAttributes;
+               global $wgAllowRdfaAttributes, $wgAllowMicrodataAttributes, $wgHtml5;
 
                $whitelist = array_flip( $whitelist );
                $hrefExp = '/^(' . wfUrlProtocols() . ')[^\s]+$/';
@@ -643,7 +643,8 @@ class Sanitizer {
                                continue;
                        }
 
-                       if( !isset( $whitelist[$attribute] ) ) {
+                       # Allow any attribute beginning with "data-", if in HTML5 mode
+                       if ( !($wgHtml5 && preg_match( '/^data-/i', $attribute )) && !isset( $whitelist[$attribute] ) ) {
                                continue;
                        }
 
index 12a2cd1..b6cfe30 100644 (file)
@@ -8146,6 +8146,18 @@ disabled
 
 !! end
 
+!! test
+HTML5 data attributes
+!! input
+<span data-foo="bar">Baz</span>
+<p data-abc-def_hij="">Quuz</p>
+!! result
+<p><span data-foo="bar">Baz</span>
+</p>
+<p data-abc-def_hij="">Quuz</p>
+
+!! end
+
 
 TODO:
 more images