r96170 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96169‎ | r96170 | r96171 >
Date:03:55, 3 September 2011
Author:krinkle
Status:ok (Comments)
Tags:
Comment:
Html.php: The "future"[1] is here. Add features for space-separated value attributes of html elements.
* Has been suggested since August 2009 in r54767 (doc-comment from rawElement/element function)
* Implements normalization for these attributes (removal of duplicates and redundant space)
* Adds support for arrays (instead of just string) for these attributes.
* String are still supported, and are converted to arrays to get the same normalization.
* Wrote unit tests (which pass locally: $ php phpunit.php includes/HtmlTest.php)
* Not trigger for the media-attribute. Reason: Although some people think it's space-separated, it's actually comma-separated. Treating them as space separated might even destroy the value. [2] [3]. Neither the html4 or html5 spec documents media-attribute as space-separated, and as of HTML5/CSS3 the media attribute may contain "media queries".


[1] "In the future, other HTML-specific features might be added, like allowing arrays for the values of attributes like class= and media=" in r54767 by Simetrical.
[2] http://www.w3.org/TR/1999/REC-html401-19991224/types.html#h-6.13
[3] http://dev.w3.org/csswg/css3-mediaqueries/#background

Implementation note: I choose to have a single list of attributes that trigger this feature. Some of these attributes only support multiple values and/or are documented as space-separated as of html5 (such as accesskey), but since those attributes in general have existed in html4 as well (just different w3c spec), they are not stripped if wgHtml5 is not true. So if this feature would (eg. for accesskey) would only be done if wgHtml5=true, then people could get output like <a accesskey=Array /> depending on a configuration variable, which will get messy and make developers' life hard.
Modified paths:
  • /trunk/phase3/includes/Html.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/HtmlTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/tests/phpunit/includes/HtmlTest.php
@@ -91,4 +91,56 @@
9292 'Value is a numeric zero'
9393 );
9494 }
 95+
 96+ /**
 97+ * Html::expandAttributes has special features for HTML
 98+ * attributes that use space separated lists and also
 99+ * allows arrays to be used as values.
 100+ */
 101+ public function testExpandAttributesListValueAttributes() {
 102+ ### STRING VALUES
 103+ $this->AssertEquals(
 104+ ' class="redundant spaces here"',
 105+ Html::expandAttributes( array( 'class' => ' redundant spaces here ' ) ),
 106+ 'Normalization should strip redundant spaces'
 107+ );
 108+ $this->AssertEquals(
 109+ ' class="foo bar"',
 110+ Html::expandAttributes( array( 'class' => 'foo bar foo bar bar' ) ),
 111+ 'Normalization should remove duplicates in string-lists'
 112+ );
 113+ ### "EMPTY" ARRAY VALUES
 114+ $this->AssertEquals(
 115+ ' class=""',
 116+ Html::expandAttributes( array( 'class' => array() ) ),
 117+ 'Value with an empty array'
 118+ );
 119+ $this->AssertEquals(
 120+ ' class=""',
 121+ Html::expandAttributes( array( 'class' => array( null, '', ' ', ' ' ) ) ),
 122+ 'Array with null, empty string and spaces'
 123+ );
 124+ ### NON-EMPTY ARRAY VALUES
 125+ $this->AssertEquals(
 126+ ' class="foo bar"',
 127+ Html::expandAttributes( array( 'class' => array(
 128+ 'foo',
 129+ 'bar',
 130+ 'foo',
 131+ 'bar',
 132+ 'bar',
 133+ ) ) ),
 134+ 'Normalization should remove duplicates in the array'
 135+ );
 136+ $this->AssertEquals(
 137+ ' class="foo bar"',
 138+ Html::expandAttributes( array( 'class' => array(
 139+ 'foo bar',
 140+ 'bar foo',
 141+ 'foo',
 142+ 'bar bar',
 143+ ) ) ),
 144+ 'Normalization should remove duplicates in string-lists in the array'
 145+ );
 146+ }
95147 }
Index: trunk/phase3/includes/Html.php
@@ -111,9 +111,7 @@
112112 * HTML-specific logic. For instance, there is no $allowShortTag
113113 * parameter: the closing tag is magically omitted if $element has an empty
114114 * content model. If $wgWellFormedXml is false, then a few bytes will be
115 - * shaved off the HTML output as well. In the future, other HTML-specific
116 - * features might be added, like allowing arrays for the values of
117 - * attributes like class= and media=.
 115+ * shaved off the HTML output as well.
118116 *
119117 * @param $element string The element's name, e.g., 'a'
120118 * @param $attribs array Associative array of attributes, e.g., array(
@@ -415,6 +413,37 @@
416414 continue;
417415 }
418416
 417+ // http://www.w3.org/TR/html401/index/attributes.html ("space-separated")
 418+ // http://www.w3.org/TR/html5/index.html#attributes-1 ("space-separated")
 419+ $spaceSeparatedListAttributes = array(
 420+ 'class', // html4, html5
 421+ 'accesskey', // as of html5, multiple space-separated values allowed
 422+ // html4-spec doesn't document rel= as space-separated
 423+ // but has been used like that and is now documented as such
 424+ // in the html5-spec.
 425+ 'rel',
 426+ );
 427+
 428+ # Specific features for attributes that allow a list of space-separated values
 429+ if ( in_array( $key, $spaceSeparatedListAttributes ) ) {
 430+ // Apply some normalization and remove duplicates
 431+
 432+ // Convert into correct array. Array can contain space-seperated
 433+ // values. Implode/explode to get those into the main array as well.
 434+ if ( is_array( $value ) ) {
 435+ // If input wasn't an array, we can skip this step
 436+ $value = implode( ' ', $value );
 437+ }
 438+ $value = explode( ' ', $value );
 439+
 440+ // Normalize spacing by fixing up cases where people used
 441+ // more than 1 space and/or a trailing/leading space
 442+ $value = array_diff( $value, array( '', ' ') );
 443+
 444+ // Remove duplicates and create the string
 445+ $value = implode( ' ', array_unique( $value ) );
 446+ }
 447+
419448 # See the "Attributes" section in the HTML syntax part of HTML5,
420449 # 9.1.2.3 as of 2009-08-10. Most attributes can have quotation
421450 # marks omitted, but not all. (Although a literal " is not

Sign-offs

UserFlagDate
Simetricalinspected16:38, 5 September 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r96171RELEASE-NOTES for r96170krinkle04:02, 3 September 2011
r96188Expand r96170's support for space separated attributes with support for boole...dantman14:36, 3 September 2011
r96256Add documentation for r96170 and r96188.krinkle21:18, 4 September 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r54767For HTML 5, drop type="" attributes for CSS/JS...simetrical00:09, 11 August 2009

Comments

#Comment by Simetrical (talk | contribs)   13:42, 4 September 2011

You removed the "In the future" comment from the method, but didn't replace it with any documentation of the new API. The method documentation should fully describe the format of the parameters.

#Comment by Simetrical (talk | contribs)   13:44, 4 September 2011

(Other than that, I'll sign off as inspected.)

#Comment by Krinkle (talk | contribs)   21:18, 4 September 2011

Done in r96256.

#Comment by Hashar (talk | contribs)   15:14, 24 October 2011

Looks fine to me. Marking ok.

Status & tagging log