Allow 'data-*' attributes in personal tools links
authorMoriel Schottlender <moriel@gmail.com>
Wed, 20 Jul 2016 21:15:01 +0000 (14:15 -0700)
committerMoriel Schottlender <moriel@gmail.com>
Fri, 22 Jul 2016 02:11:38 +0000 (19:11 -0700)
We are limiting the attributes that are read and rendered for the
personal toolbar, but there are cases -- especially as tools get
more and more complex -- for needing to supply and read more information.

The use case that led to this change is the need to style Notification
badges with values in ::before and ::after pseudo-elements; these
elements need to display some prefixed content, and the best (only)
way to do that in no-js mode, is to set it up in CSS to be read
out of an attribute (using attr( xxx ); ).

This means that we need to read a specific attribute from the element,
but the only attributes allowed were insufficient. Allowing for data-*
attributes seemed not only a good solution for this specific case,
but a good idea in general for future tools that are added and
manipulate the personal toolbar links - allowing for storing information
without polluting the DOM.

Bug: T115845
Change-Id: Ic666540d70de52f337f839da0518cb83a990f5fd

includes/skins/BaseTemplate.php

index 13db176..71ca57b 100644 (file)
@@ -140,7 +140,7 @@ abstract class BaseTemplate extends QuickTemplate {
                        if ( isset( $plink['active'] ) ) {
                                $ptool['active'] = $plink['active'];
                        }
-                       foreach ( [ 'href', 'class', 'text', 'dir' ] as $k ) {
+                       foreach ( [ 'href', 'class', 'text', 'dir', 'data' ] as $k ) {
                                if ( isset( $plink[$k] ) ) {
                                        $ptool['links'][0][$k] = $plink[$k];
                                }
@@ -318,6 +318,15 @@ abstract class BaseTemplate extends QuickTemplate {
         *
         * If you don't want an accesskey, set $item['tooltiponly'] = true;
         *
+        * If a "data" key is present, it must be an array, where the keys represent
+        * the data-xxx properties with their provided values. For example,
+        *  $item['data'] = array(
+        *       'foo' => 1,
+        *       'bar' => 'baz',
+        *  );
+        * will render as element properties:
+        *  data-foo='1' data-bar='baz'
+        *
         * @param array $options Can be used to affect the output of a link.
         * Possible options are:
         *   - 'text-wrapper' key to specify a list of elements to wrap the text of
@@ -363,6 +372,13 @@ abstract class BaseTemplate extends QuickTemplate {
                                unset( $attrs[$k] );
                        }
 
+                       if ( isset( $attrs['data'] ) ) {
+                               foreach ( $attrs['data'] as $key => $value ) {
+                                       $attrs[ 'data-' . $key ] = $value;
+                               }
+                               unset( $attrs[ 'data' ] );
+                       }
+
                        if ( isset( $item['id'] ) && !isset( $item['single-id'] ) ) {
                                $item['single-id'] = $item['id'];
                        }