2009-08-11 00:09:24 +00:00
|
|
|
<?php
|
2010-10-23 14:16:26 +00:00
|
|
|
/**
|
|
|
|
|
* Collection of methods to generate HTML content
|
|
|
|
|
*
|
|
|
|
|
* Copyright © 2009 Aryeh Gregor
|
|
|
|
|
* http://www.mediawiki.org/
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2009-08-11 00:09:24 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class is a collection of static functions that serve two purposes:
|
|
|
|
|
*
|
2009-12-30 07:08:52 +00:00
|
|
|
* 1) Implement any algorithms specified by HTML5, or other HTML
|
2009-08-11 00:09:24 +00:00
|
|
|
* specifications, in a convenient and self-contained way.
|
|
|
|
|
*
|
|
|
|
|
* 2) Allow HTML elements to be conveniently and safely generated, like the
|
|
|
|
|
* current Xml class but a) less confused (Xml supports HTML-specific things,
|
|
|
|
|
* but only sometimes!) and b) not necessarily confined to XML-compatible
|
|
|
|
|
* output.
|
|
|
|
|
*
|
|
|
|
|
* There are two important configuration options this class uses:
|
|
|
|
|
*
|
2014-07-24 14:04:48 +00:00
|
|
|
* $wgMimeType: If this is set to an xml MIME type then output should be
|
2013-05-10 04:04:33 +00:00
|
|
|
* valid XHTML5.
|
2009-08-11 00:09:24 +00:00
|
|
|
* $wgWellFormedXml: If this is set to true, then all output should be
|
|
|
|
|
* well-formed XML (quotes on attributes, self-closing tags, etc.).
|
|
|
|
|
*
|
|
|
|
|
* This class is meant to be confined to utility functions that are called from
|
|
|
|
|
* trusted code paths. It does not do enforcement of policy like not allowing
|
|
|
|
|
* <a> elements.
|
2010-02-24 16:06:55 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.16
|
2009-08-11 00:09:24 +00:00
|
|
|
*/
|
|
|
|
|
class Html {
|
2012-11-04 18:17:04 +00:00
|
|
|
// List of void elements from HTML5, section 8.1.2 as of 2011-08-12
|
2009-08-11 00:09:24 +00:00
|
|
|
private static $voidElements = array(
|
2009-08-19 01:39:05 +00:00
|
|
|
'area',
|
|
|
|
|
'base',
|
|
|
|
|
'br',
|
|
|
|
|
'col',
|
|
|
|
|
'command',
|
|
|
|
|
'embed',
|
|
|
|
|
'hr',
|
|
|
|
|
'img',
|
|
|
|
|
'input',
|
|
|
|
|
'keygen',
|
|
|
|
|
'link',
|
|
|
|
|
'meta',
|
|
|
|
|
'param',
|
|
|
|
|
'source',
|
2011-08-12 21:04:25 +00:00
|
|
|
'track',
|
|
|
|
|
'wbr',
|
2009-08-11 00:09:24 +00:00
|
|
|
);
|
|
|
|
|
|
2012-11-04 18:17:04 +00:00
|
|
|
// Boolean attributes, which may have the value omitted entirely. Manually
|
|
|
|
|
// collected from the HTML5 spec as of 2011-08-12.
|
2009-08-19 01:39:05 +00:00
|
|
|
private static $boolAttribs = array(
|
|
|
|
|
'async',
|
|
|
|
|
'autofocus',
|
|
|
|
|
'autoplay',
|
|
|
|
|
'checked',
|
|
|
|
|
'controls',
|
2011-08-12 21:04:25 +00:00
|
|
|
'default',
|
2009-08-19 01:39:05 +00:00
|
|
|
'defer',
|
|
|
|
|
'disabled',
|
|
|
|
|
'formnovalidate',
|
|
|
|
|
'hidden',
|
|
|
|
|
'ismap',
|
2010-06-07 21:07:13 +00:00
|
|
|
'itemscope',
|
2009-08-19 01:39:05 +00:00
|
|
|
'loop',
|
|
|
|
|
'multiple',
|
2011-08-12 21:04:25 +00:00
|
|
|
'muted',
|
2009-08-19 01:39:05 +00:00
|
|
|
'novalidate',
|
|
|
|
|
'open',
|
2010-06-07 21:07:13 +00:00
|
|
|
'pubdate',
|
2009-08-19 01:39:05 +00:00
|
|
|
'readonly',
|
|
|
|
|
'required',
|
|
|
|
|
'reversed',
|
|
|
|
|
'scoped',
|
|
|
|
|
'seamless',
|
2010-05-09 09:32:43 +00:00
|
|
|
'selected',
|
2011-08-12 21:04:25 +00:00
|
|
|
'truespeed',
|
|
|
|
|
'typemustmatch',
|
2012-11-04 18:17:04 +00:00
|
|
|
// HTML5 Microdata
|
2011-08-28 23:03:19 +00:00
|
|
|
'itemscope',
|
2009-08-11 01:00:44 +00:00
|
|
|
);
|
|
|
|
|
|
2014-09-29 23:45:45 +00:00
|
|
|
/**
|
|
|
|
|
* Modifies a set of attributes meant for button elements
|
|
|
|
|
* and apply a set of default attributes when $wgUseMediaWikiUIEverywhere enabled.
|
2015-03-26 09:38:35 +00:00
|
|
|
* @param array $attrs
|
|
|
|
|
* @param string[] $modifiers to add to the button
|
2014-10-03 22:07:06 +00:00
|
|
|
* @see https://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
|
2014-09-29 23:45:45 +00:00
|
|
|
* @return array $attrs A modified attribute array
|
|
|
|
|
*/
|
2015-03-26 09:47:30 +00:00
|
|
|
public static function buttonAttributes( array $attrs, array $modifiers = array() ) {
|
2014-09-29 23:45:45 +00:00
|
|
|
global $wgUseMediaWikiUIEverywhere;
|
|
|
|
|
if ( $wgUseMediaWikiUIEverywhere ) {
|
|
|
|
|
if ( isset( $attrs['class'] ) ) {
|
|
|
|
|
if ( is_array( $attrs['class'] ) ) {
|
|
|
|
|
$attrs['class'][] = 'mw-ui-button';
|
|
|
|
|
$attrs = array_merge( $attrs, $modifiers );
|
|
|
|
|
// ensure compatibility with Xml
|
|
|
|
|
$attrs['class'] = implode( ' ', $attrs['class'] );
|
|
|
|
|
} else {
|
|
|
|
|
$attrs['class'] .= ' mw-ui-button ' . implode( ' ', $modifiers );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$attrs['class'] = array( 'mw-ui-button' );
|
|
|
|
|
// ensure compatibility with Xml
|
|
|
|
|
$attrs['class'] = implode( ' ', array_merge( $attrs['class'], $modifiers ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $attrs;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-30 17:56:25 +00:00
|
|
|
/**
|
|
|
|
|
* Modifies a set of attributes meant for text input elements
|
|
|
|
|
* and apply a set of default attributes.
|
|
|
|
|
* Removes size attribute when $wgUseMediaWikiUIEverywhere enabled.
|
|
|
|
|
* @param array $attrs An attribute array.
|
|
|
|
|
* @return array $attrs A modified attribute array
|
|
|
|
|
*/
|
2015-03-26 09:47:30 +00:00
|
|
|
public static function getTextInputAttributes( array $attrs ) {
|
2014-07-30 17:56:25 +00:00
|
|
|
global $wgUseMediaWikiUIEverywhere;
|
2015-03-13 18:49:15 +00:00
|
|
|
if ( $wgUseMediaWikiUIEverywhere ) {
|
|
|
|
|
if ( isset( $attrs['class'] ) ) {
|
|
|
|
|
if ( is_array( $attrs['class'] ) ) {
|
|
|
|
|
$attrs['class'][] = 'mw-ui-input';
|
|
|
|
|
} else {
|
|
|
|
|
$attrs['class'] .= ' mw-ui-input';
|
|
|
|
|
}
|
2014-07-30 17:56:25 +00:00
|
|
|
} else {
|
2015-03-13 18:49:15 +00:00
|
|
|
$attrs['class'] = 'mw-ui-input';
|
2014-07-30 17:56:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $attrs;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-29 23:45:45 +00:00
|
|
|
/**
|
2015-03-03 18:27:51 +00:00
|
|
|
* Returns an HTML link element in a string styled as a button
|
|
|
|
|
* (when $wgUseMediaWikiUIEverywhere is enabled).
|
2014-09-29 23:45:45 +00:00
|
|
|
*
|
|
|
|
|
* @param string $contents The raw HTML contents of the element: *not*
|
|
|
|
|
* escaped!
|
|
|
|
|
* @param array $attrs Associative array of attributes, e.g., array(
|
|
|
|
|
* 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for
|
|
|
|
|
* further documentation.
|
2015-03-26 09:38:35 +00:00
|
|
|
* @param string[] $modifiers to add to the button
|
2014-09-29 23:45:45 +00:00
|
|
|
* @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
|
|
|
|
|
* @return string Raw HTML
|
|
|
|
|
*/
|
2015-03-26 09:47:30 +00:00
|
|
|
public static function linkButton( $contents, array $attrs, array $modifiers = array() ) {
|
2015-03-26 09:17:07 +00:00
|
|
|
return self::element( 'a',
|
2014-09-29 23:45:45 +00:00
|
|
|
self::buttonAttributes( $attrs, $modifiers ),
|
|
|
|
|
$contents
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-03-03 18:27:51 +00:00
|
|
|
* Returns an HTML link element in a string styled as a button
|
|
|
|
|
* (when $wgUseMediaWikiUIEverywhere is enabled).
|
2014-09-29 23:45:45 +00:00
|
|
|
*
|
|
|
|
|
* @param string $contents The raw HTML contents of the element: *not*
|
|
|
|
|
* escaped!
|
|
|
|
|
* @param array $attrs Associative array of attributes, e.g., array(
|
|
|
|
|
* 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for
|
|
|
|
|
* further documentation.
|
2015-03-26 09:38:35 +00:00
|
|
|
* @param string[] $modifiers to add to the button
|
2014-09-29 23:45:45 +00:00
|
|
|
* @see http://tools.wmflabs.org/styleguide/desktop/index.html for guidance on available modifiers
|
|
|
|
|
* @return string Raw HTML
|
|
|
|
|
*/
|
2015-03-26 09:47:30 +00:00
|
|
|
public static function submitButton( $contents, array $attrs, array $modifiers = array() ) {
|
2014-09-29 23:45:45 +00:00
|
|
|
$attrs['type'] = 'submit';
|
|
|
|
|
$attrs['value'] = $contents;
|
2015-03-26 09:17:07 +00:00
|
|
|
return self::element( 'input', self::buttonAttributes( $attrs, $modifiers ) );
|
2014-09-29 23:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-11 00:09:24 +00:00
|
|
|
/**
|
|
|
|
|
* Returns an HTML element in a string. The major advantage here over
|
|
|
|
|
* manually typing out the HTML is that it will escape all attribute
|
2015-01-09 07:02:42 +00:00
|
|
|
* values.
|
2009-08-11 00:09:24 +00:00
|
|
|
*
|
2009-08-21 20:39:16 +00:00
|
|
|
* This is quite similar to Xml::tags(), but it implements some useful
|
2009-08-11 00:09:24 +00:00
|
|
|
* HTML-specific logic. For instance, there is no $allowShortTag
|
|
|
|
|
* parameter: the closing tag is magically omitted if $element has an empty
|
|
|
|
|
* content model. If $wgWellFormedXml is false, then a few bytes will be
|
2011-09-03 03:55:23 +00:00
|
|
|
* shaved off the HTML output as well.
|
2009-08-11 00:09:24 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $element The element's name, e.g., 'a'
|
2014-07-24 17:42:24 +00:00
|
|
|
* @param array $attribs Associative array of attributes, e.g., array(
|
2011-09-04 21:18:27 +00:00
|
|
|
* 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for
|
2009-09-23 15:16:05 +00:00
|
|
|
* further documentation.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $contents The raw HTML contents of the element: *not*
|
2009-08-11 00:09:24 +00:00
|
|
|
* escaped!
|
|
|
|
|
* @return string Raw HTML
|
|
|
|
|
*/
|
2009-08-18 01:01:47 +00:00
|
|
|
public static function rawElement( $element, $attribs = array(), $contents = '' ) {
|
2010-01-15 01:16:52 +00:00
|
|
|
global $wgWellFormedXml;
|
|
|
|
|
$start = self::openElement( $element, $attribs );
|
|
|
|
|
if ( in_array( $element, self::$voidElements ) ) {
|
|
|
|
|
if ( $wgWellFormedXml ) {
|
2012-11-04 18:17:04 +00:00
|
|
|
// Silly XML.
|
2010-01-15 01:16:52 +00:00
|
|
|
return substr( $start, 0, -1 ) . ' />';
|
|
|
|
|
}
|
|
|
|
|
return $start;
|
|
|
|
|
} else {
|
2010-03-21 05:12:02 +00:00
|
|
|
return "$start$contents" . self::closeElement( $element );
|
2010-01-15 01:16:52 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Identical to rawElement(), but HTML-escapes $contents (like
|
|
|
|
|
* Xml::element()).
|
2011-05-04 21:23:25 +00:00
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $element
|
|
|
|
|
* @param array $attribs
|
|
|
|
|
* @param string $contents
|
2011-05-04 21:23:25 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2010-01-15 01:16:52 +00:00
|
|
|
*/
|
|
|
|
|
public static function element( $element, $attribs = array(), $contents = '' ) {
|
|
|
|
|
return self::rawElement( $element, $attribs, strtr( $contents, array(
|
2012-11-04 18:17:04 +00:00
|
|
|
// There's no point in escaping quotes, >, etc. in the contents of
|
|
|
|
|
// elements.
|
2010-01-15 01:16:52 +00:00
|
|
|
'&' => '&',
|
|
|
|
|
'<' => '<'
|
|
|
|
|
) ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Identical to rawElement(), but has no third parameter and omits the end
|
2010-03-21 05:12:02 +00:00
|
|
|
* tag (and the self-closing '/' in XML mode for empty elements).
|
2011-05-04 21:23:25 +00:00
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $element
|
|
|
|
|
* @param array $attribs
|
2011-05-04 21:23:25 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2010-01-15 01:16:52 +00:00
|
|
|
*/
|
|
|
|
|
public static function openElement( $element, $attribs = array() ) {
|
2009-09-22 17:41:34 +00:00
|
|
|
$attribs = (array)$attribs;
|
2012-11-04 18:17:04 +00:00
|
|
|
// This is not required in HTML5, but let's do it anyway, for
|
|
|
|
|
// consistency and better compression.
|
2009-08-11 00:09:24 +00:00
|
|
|
$element = strtolower( $element );
|
2009-08-21 20:50:35 +00:00
|
|
|
|
2012-11-04 18:17:04 +00:00
|
|
|
// Remove invalid input types
|
2010-06-30 22:14:36 +00:00
|
|
|
if ( $element == 'input' ) {
|
|
|
|
|
$validTypes = array(
|
|
|
|
|
'hidden',
|
|
|
|
|
'text',
|
|
|
|
|
'password',
|
|
|
|
|
'checkbox',
|
|
|
|
|
'radio',
|
|
|
|
|
'file',
|
|
|
|
|
'submit',
|
|
|
|
|
'image',
|
|
|
|
|
'reset',
|
|
|
|
|
'button',
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2013-05-10 04:04:33 +00:00
|
|
|
// HTML input types
|
|
|
|
|
'datetime',
|
|
|
|
|
'datetime-local',
|
|
|
|
|
'date',
|
|
|
|
|
'month',
|
|
|
|
|
'time',
|
|
|
|
|
'week',
|
|
|
|
|
'number',
|
|
|
|
|
'range',
|
|
|
|
|
'email',
|
|
|
|
|
'url',
|
|
|
|
|
'search',
|
|
|
|
|
'tel',
|
|
|
|
|
'color',
|
|
|
|
|
);
|
2014-11-16 19:18:00 +00:00
|
|
|
if ( isset( $attribs['type'] ) && !in_array( $attribs['type'], $validTypes ) ) {
|
2010-06-30 22:14:36 +00:00
|
|
|
unset( $attribs['type'] );
|
Fix bugs in r59360, r59361, r59363
* spellcheck is not a boolean attribute; it is an enumerated attribute
whose possible values are "true" and "false". If it were boolean, the
permitted constructs would be <input spellcheck>, <input
spellcheck="spellcheck">, and <input spellcheck="">, which would all
set it true, and it would only be set to false if omitted entirely.
(It would be boolean if HTML5 had invented it, but can't be for
historical reasons.)
* spellcheck is valid on any HTML element, not just input, and so should
be stripped on any element.
For reference, a table of all HTML5 attributes can be found at:
<http://www.whatwg.org/specs/web-apps/current-work/multipage/section-index.html#attributes-0>
2009-12-11 19:01:16 +00:00
|
|
|
}
|
2009-08-21 20:50:35 +00:00
|
|
|
}
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2012-10-30 19:06:16 +00:00
|
|
|
// According to standard the default type for <button> elements is "submit".
|
|
|
|
|
// Depending on compatibility mode IE might use "button", instead.
|
|
|
|
|
// We enforce the standard "submit".
|
|
|
|
|
if ( $element == 'button' && !isset( $attribs['type'] ) ) {
|
|
|
|
|
$attribs['type'] = 'submit';
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-15 01:16:52 +00:00
|
|
|
return "<$element" . self::expandAttributes(
|
|
|
|
|
self::dropDefaults( $element, $attribs ) ) . '>';
|
2009-08-18 01:01:47 +00:00
|
|
|
}
|
|
|
|
|
|
2010-03-21 05:12:02 +00:00
|
|
|
/**
|
2014-08-10 02:23:27 +00:00
|
|
|
* Returns "</$element>"
|
2011-03-08 18:12:17 +00:00
|
|
|
*
|
2011-01-20 19:26:30 +00:00
|
|
|
* @since 1.17
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $element Name of the element, e.g., 'a'
|
2014-08-10 02:23:27 +00:00
|
|
|
* @return string A closing tag
|
2010-03-21 05:12:02 +00:00
|
|
|
*/
|
|
|
|
|
public static function closeElement( $element ) {
|
|
|
|
|
$element = strtolower( $element );
|
|
|
|
|
|
|
|
|
|
return "</$element>";
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-07 00:21:55 +00:00
|
|
|
/**
|
|
|
|
|
* Given an element name and an associative array of element attributes,
|
|
|
|
|
* return an array that is functionally identical to the input array, but
|
|
|
|
|
* possibly smaller. In particular, attributes might be stripped if they
|
|
|
|
|
* are given their default values.
|
|
|
|
|
*
|
|
|
|
|
* This method is not guaranteed to remove all redundant attributes, only
|
|
|
|
|
* some common ones and some others selected arbitrarily at random. It
|
|
|
|
|
* only guarantees that the output array should be functionally identical
|
|
|
|
|
* to the input array (currently per the HTML 5 draft as of 2009-09-06).
|
|
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $element Name of the element, e.g., 'a'
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param array $attribs Associative array of attributes, e.g., array(
|
2009-09-23 15:16:05 +00:00
|
|
|
* 'href' => 'http://www.mediawiki.org/' ). See expandAttributes() for
|
|
|
|
|
* further documentation.
|
2009-09-07 00:21:55 +00:00
|
|
|
* @return array An array of attributes functionally identical to $attribs
|
|
|
|
|
*/
|
2015-03-26 09:47:30 +00:00
|
|
|
private static function dropDefaults( $element, array $attribs ) {
|
2012-11-04 18:17:04 +00:00
|
|
|
// Whenever altering this array, please provide a covering test case
|
|
|
|
|
// in HtmlTest::provideElementsWithAttributesHavingDefaultValues
|
2009-09-07 00:21:55 +00:00
|
|
|
static $attribDefaults = array(
|
|
|
|
|
'area' => array( 'shape' => 'rect' ),
|
|
|
|
|
'button' => array(
|
|
|
|
|
'formaction' => 'GET',
|
|
|
|
|
'formenctype' => 'application/x-www-form-urlencoded',
|
|
|
|
|
),
|
|
|
|
|
'canvas' => array(
|
|
|
|
|
'height' => '150',
|
|
|
|
|
'width' => '300',
|
|
|
|
|
),
|
|
|
|
|
'command' => array( 'type' => 'command' ),
|
|
|
|
|
'form' => array(
|
|
|
|
|
'action' => 'GET',
|
|
|
|
|
'autocomplete' => 'on',
|
|
|
|
|
'enctype' => 'application/x-www-form-urlencoded',
|
|
|
|
|
),
|
|
|
|
|
'input' => array(
|
|
|
|
|
'formaction' => 'GET',
|
|
|
|
|
'type' => 'text',
|
|
|
|
|
),
|
|
|
|
|
'keygen' => array( 'keytype' => 'rsa' ),
|
|
|
|
|
'link' => array( 'media' => 'all' ),
|
|
|
|
|
'menu' => array( 'type' => 'list' ),
|
2012-11-04 18:17:04 +00:00
|
|
|
// Note: the use of text/javascript here instead of other JavaScript
|
|
|
|
|
// MIME types follows the HTML5 spec.
|
2009-09-07 00:21:55 +00:00
|
|
|
'script' => array( 'type' => 'text/javascript' ),
|
|
|
|
|
'style' => array(
|
|
|
|
|
'media' => 'all',
|
|
|
|
|
'type' => 'text/css',
|
|
|
|
|
),
|
|
|
|
|
'textarea' => array( 'wrap' => 'soft' ),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$element = strtolower( $element );
|
|
|
|
|
|
|
|
|
|
foreach ( $attribs as $attrib => $value ) {
|
|
|
|
|
$lcattrib = strtolower( $attrib );
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( is_array( $value ) ) {
|
2012-09-01 01:25:27 +00:00
|
|
|
$value = implode( ' ', $value );
|
|
|
|
|
} else {
|
|
|
|
|
$value = strval( $value );
|
|
|
|
|
}
|
2009-09-07 00:21:55 +00:00
|
|
|
|
2012-11-04 18:17:04 +00:00
|
|
|
// Simple checks using $attribDefaults
|
2014-11-16 19:18:00 +00:00
|
|
|
if ( isset( $attribDefaults[$element][$lcattrib] )
|
|
|
|
|
&& $attribDefaults[$element][$lcattrib] == $value
|
|
|
|
|
) {
|
2009-09-07 15:25:22 +00:00
|
|
|
unset( $attribs[$attrib] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $lcattrib == 'class' && $value == '' ) {
|
2009-09-07 00:21:55 +00:00
|
|
|
unset( $attribs[$attrib] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-04 18:17:04 +00:00
|
|
|
// More subtle checks
|
2014-11-16 19:18:00 +00:00
|
|
|
if ( $element === 'link'
|
|
|
|
|
&& isset( $attribs['type'] ) && strval( $attribs['type'] ) == 'text/css'
|
|
|
|
|
) {
|
2009-09-07 00:21:55 +00:00
|
|
|
unset( $attribs['type'] );
|
|
|
|
|
}
|
2012-08-16 07:37:56 +00:00
|
|
|
if ( $element === 'input' ) {
|
|
|
|
|
$type = isset( $attribs['type'] ) ? $attribs['type'] : null;
|
|
|
|
|
$value = isset( $attribs['value'] ) ? $attribs['value'] : null;
|
|
|
|
|
if ( $type === 'checkbox' || $type === 'radio' ) {
|
|
|
|
|
// The default value for checkboxes and radio buttons is 'on'
|
|
|
|
|
// not ''. By stripping value="" we break radio boxes that
|
|
|
|
|
// actually wants empty values.
|
|
|
|
|
if ( $value === 'on' ) {
|
|
|
|
|
unset( $attribs['value'] );
|
|
|
|
|
}
|
|
|
|
|
} elseif ( $type === 'submit' ) {
|
|
|
|
|
// The default value for submit appears to be "Submit" but
|
|
|
|
|
// let's not bother stripping out localized text that matches
|
|
|
|
|
// that.
|
|
|
|
|
} else {
|
|
|
|
|
// The default value for nearly every other field type is ''
|
|
|
|
|
// The 'range' and 'color' types use different defaults but
|
|
|
|
|
// stripping a value="" does not hurt them.
|
|
|
|
|
if ( $value === '' ) {
|
|
|
|
|
unset( $attribs['value'] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-09-07 00:21:55 +00:00
|
|
|
if ( $element === 'select' && isset( $attribs['size'] ) ) {
|
|
|
|
|
if ( in_array( 'multiple', $attribs )
|
|
|
|
|
|| ( isset( $attribs['multiple'] ) && $attribs['multiple'] !== false )
|
|
|
|
|
) {
|
2012-11-04 18:17:04 +00:00
|
|
|
// A multi-select
|
2009-09-07 15:25:22 +00:00
|
|
|
if ( strval( $attribs['size'] ) == '4' ) {
|
2009-09-07 00:21:55 +00:00
|
|
|
unset( $attribs['size'] );
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2012-11-04 18:17:04 +00:00
|
|
|
// Single select
|
2009-09-07 15:25:22 +00:00
|
|
|
if ( strval( $attribs['size'] ) == '1' ) {
|
2009-09-07 00:21:55 +00:00
|
|
|
unset( $attribs['size'] );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $attribs;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-11 00:09:24 +00:00
|
|
|
/**
|
|
|
|
|
* Given an associative array of element attributes, generate a string
|
|
|
|
|
* to stick after the element name in HTML output. Like array( 'href' =>
|
|
|
|
|
* 'http://www.mediawiki.org/' ) becomes something like
|
|
|
|
|
* ' href="http://www.mediawiki.org"'. Again, this is like
|
|
|
|
|
* Xml::expandAttributes(), but it implements some HTML-specific logic.
|
2009-08-11 01:00:44 +00:00
|
|
|
* For instance, it will omit quotation marks if $wgWellFormedXml is false,
|
|
|
|
|
* and will treat boolean attributes specially.
|
2009-08-11 00:09:24 +00:00
|
|
|
*
|
2014-05-10 08:58:19 +00:00
|
|
|
* Attributes that can contain space-separated lists ('class', 'accesskey' and 'rel') array
|
2011-09-04 21:18:27 +00:00
|
|
|
* values are allowed as well, which will automagically be normalized
|
|
|
|
|
* and converted to a space-separated string. In addition to a numerical
|
|
|
|
|
* array, the attribute value may also be an associative array. See the
|
|
|
|
|
* example below for how that works.
|
2012-02-01 20:53:38 +00:00
|
|
|
*
|
|
|
|
|
* @par Numerical array
|
|
|
|
|
* @code
|
2011-09-04 21:18:27 +00:00
|
|
|
* Html::element( 'em', array(
|
|
|
|
|
* 'class' => array( 'foo', 'bar' )
|
|
|
|
|
* ) );
|
|
|
|
|
* // gives '<em class="foo bar"></em>'
|
2012-02-01 20:53:38 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
|
|
|
|
* @par Associative array
|
|
|
|
|
* @code
|
2011-09-04 21:18:27 +00:00
|
|
|
* Html::element( 'em', array(
|
|
|
|
|
* 'class' => array( 'foo', 'bar', 'foo' => false, 'quux' => true )
|
|
|
|
|
* ) );
|
|
|
|
|
* // gives '<em class="bar quux"></em>'
|
2012-02-01 20:53:38 +00:00
|
|
|
* @endcode
|
2011-09-04 21:18:27 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $attribs Associative array of attributes, e.g., array(
|
2009-08-11 00:09:24 +00:00
|
|
|
* 'href' => 'http://www.mediawiki.org/' ). Values will be HTML-escaped.
|
2009-09-23 15:16:05 +00:00
|
|
|
* A value of false means to omit the attribute. For boolean attributes,
|
|
|
|
|
* you can omit the key, e.g., array( 'checked' ) instead of
|
|
|
|
|
* array( 'checked' => 'checked' ) or such.
|
2014-05-10 08:58:19 +00:00
|
|
|
*
|
2014-07-24 17:42:24 +00:00
|
|
|
* @throws MWException If an attribute that doesn't allow lists is set to an array
|
2009-08-11 00:09:24 +00:00
|
|
|
* @return string HTML fragment that goes between element name and '>'
|
|
|
|
|
* (starting with a space if at least one attribute is output)
|
|
|
|
|
*/
|
2015-03-26 09:47:30 +00:00
|
|
|
public static function expandAttributes( array $attribs ) {
|
2013-05-10 04:04:33 +00:00
|
|
|
global $wgWellFormedXml;
|
2009-08-11 00:09:24 +00:00
|
|
|
|
|
|
|
|
$ret = '';
|
|
|
|
|
foreach ( $attribs as $key => $value ) {
|
2013-10-16 15:42:07 +00:00
|
|
|
// Support intuitive array( 'checked' => true/false ) form
|
2011-02-05 20:28:04 +00:00
|
|
|
if ( $value === false || is_null( $value ) ) {
|
2009-08-26 14:59:59 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-04 18:17:04 +00:00
|
|
|
// For boolean attributes, support array( 'foo' ) instead of
|
|
|
|
|
// requiring array( 'foo' => 'meaningless' ).
|
2014-11-16 19:18:00 +00:00
|
|
|
if ( is_int( $key ) && in_array( strtolower( $value ), self::$boolAttribs ) ) {
|
2009-08-21 21:06:06 +00:00
|
|
|
$key = $value;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-10 04:04:33 +00:00
|
|
|
// Not technically required in HTML5 but we'd like consistency
|
|
|
|
|
// and better compression anyway.
|
2009-08-21 21:06:06 +00:00
|
|
|
$key = strtolower( $key );
|
|
|
|
|
|
2012-11-04 18:17:04 +00:00
|
|
|
// Bug 23769: Blacklist all form validation attributes for now. Current
|
|
|
|
|
// (June 2010) WebKit has no UI, so the form just refuses to submit
|
|
|
|
|
// without telling the user why, which is much worse than failing
|
|
|
|
|
// server-side validation. Opera is the only other implementation at
|
|
|
|
|
// this time, and has ugly UI, so just kill the feature entirely until
|
|
|
|
|
// we have at least one good implementation.
|
2012-11-29 12:12:35 +00:00
|
|
|
|
|
|
|
|
// As the default value of "1" for "step" rejects decimal
|
|
|
|
|
// numbers to be entered in 'type="number"' fields, allow
|
|
|
|
|
// the special case 'step="any"'.
|
|
|
|
|
|
2013-12-01 19:58:51 +00:00
|
|
|
if ( in_array( $key, array( 'max', 'min', 'pattern', 'required' ) )
|
|
|
|
|
|| $key === 'step' && $value !== 'any' ) {
|
2011-09-03 00:35:08 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-03 03:55:23 +00:00
|
|
|
// http://www.w3.org/TR/html401/index/attributes.html ("space-separated")
|
|
|
|
|
// http://www.w3.org/TR/html5/index.html#attributes-1 ("space-separated")
|
|
|
|
|
$spaceSeparatedListAttributes = array(
|
|
|
|
|
'class', // html4, html5
|
|
|
|
|
'accesskey', // as of html5, multiple space-separated values allowed
|
|
|
|
|
// html4-spec doesn't document rel= as space-separated
|
2012-10-19 20:03:05 +00:00
|
|
|
// but has been used like that and is now documented as such
|
2011-09-03 03:55:23 +00:00
|
|
|
// in the html5-spec.
|
|
|
|
|
'rel',
|
|
|
|
|
);
|
|
|
|
|
|
2012-11-04 18:17:04 +00:00
|
|
|
// Specific features for attributes that allow a list of space-separated values
|
2011-09-03 03:55:23 +00:00
|
|
|
if ( in_array( $key, $spaceSeparatedListAttributes ) ) {
|
|
|
|
|
// Apply some normalization and remove duplicates
|
|
|
|
|
|
2013-03-13 07:42:41 +00:00
|
|
|
// Convert into correct array. Array can contain space-separated
|
2011-09-03 03:55:23 +00:00
|
|
|
// values. Implode/explode to get those into the main array as well.
|
|
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
|
// If input wasn't an array, we can skip this step
|
2011-09-03 14:36:58 +00:00
|
|
|
$newValue = array();
|
|
|
|
|
foreach ( $value as $k => $v ) {
|
|
|
|
|
if ( is_string( $v ) ) {
|
|
|
|
|
// String values should be normal `array( 'foo' )`
|
|
|
|
|
// Just append them
|
|
|
|
|
if ( !isset( $value[$v] ) ) {
|
|
|
|
|
// As a special case don't set 'foo' if a
|
|
|
|
|
// separate 'foo' => true/false exists in the array
|
2013-03-13 07:42:41 +00:00
|
|
|
// keys should be authoritative
|
2011-09-03 14:36:58 +00:00
|
|
|
$newValue[] = $v;
|
|
|
|
|
}
|
|
|
|
|
} elseif ( $v ) {
|
|
|
|
|
// If the value is truthy but not a string this is likely
|
|
|
|
|
// an array( 'foo' => true ), falsy values don't add strings
|
|
|
|
|
$newValue[] = $k;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$value = implode( ' ', $newValue );
|
2011-09-03 03:55:23 +00:00
|
|
|
}
|
|
|
|
|
$value = explode( ' ', $value );
|
|
|
|
|
|
|
|
|
|
// Normalize spacing by fixing up cases where people used
|
|
|
|
|
// more than 1 space and/or a trailing/leading space
|
2011-09-03 14:36:58 +00:00
|
|
|
$value = array_diff( $value, array( '', ' ' ) );
|
2011-09-03 03:55:23 +00:00
|
|
|
|
|
|
|
|
// Remove duplicates and create the string
|
|
|
|
|
$value = implode( ' ', array_unique( $value ) );
|
2014-07-19 21:12:10 +00:00
|
|
|
} elseif ( is_array( $value ) ) {
|
2014-05-10 08:58:19 +00:00
|
|
|
throw new MWException( "HTML attribute $key can not contain a list of values" );
|
2011-09-03 03:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-04 18:17:04 +00:00
|
|
|
// See the "Attributes" section in the HTML syntax part of HTML5,
|
|
|
|
|
// 9.1.2.3 as of 2009-08-10. Most attributes can have quotation
|
|
|
|
|
// marks omitted, but not all. (Although a literal " is not
|
|
|
|
|
// permitted, we don't check for that, since it will be escaped
|
|
|
|
|
// anyway.)
|
2014-11-16 19:18:00 +00:00
|
|
|
|
2012-11-04 18:17:04 +00:00
|
|
|
// See also research done on further characters that need to be
|
|
|
|
|
// escaped: http://code.google.com/p/html5lib/issues/detail?id=93
|
2009-09-18 15:28:46 +00:00
|
|
|
$badChars = "\\x00- '=<>`/\x{00a0}\x{1680}\x{180e}\x{180F}\x{2000}\x{2001}"
|
|
|
|
|
. "\x{2002}\x{2003}\x{2004}\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}"
|
|
|
|
|
. "\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}";
|
2014-11-16 19:18:00 +00:00
|
|
|
if ( $wgWellFormedXml || $value === '' || preg_match( "![$badChars]!u", $value ) ) {
|
2009-08-11 00:09:24 +00:00
|
|
|
$quote = '"';
|
|
|
|
|
} else {
|
|
|
|
|
$quote = '';
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-11 01:00:44 +00:00
|
|
|
if ( in_array( $key, self::$boolAttribs ) ) {
|
2013-05-10 04:04:33 +00:00
|
|
|
// In HTML5, we can leave the value empty. If we don't need
|
|
|
|
|
// well-formed XML, we can omit the = entirely.
|
2009-08-11 01:00:44 +00:00
|
|
|
if ( !$wgWellFormedXml ) {
|
|
|
|
|
$ret .= " $key";
|
|
|
|
|
} else {
|
2013-05-10 04:04:33 +00:00
|
|
|
$ret .= " $key=\"\"";
|
2009-08-11 01:00:44 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2012-11-04 18:17:04 +00:00
|
|
|
// Apparently we need to entity-encode \n, \r, \t, although the
|
|
|
|
|
// spec doesn't mention that. Since we're doing strtr() anyway,
|
2015-02-19 23:05:40 +00:00
|
|
|
// we may as well not call htmlspecialchars().
|
2012-11-04 18:17:04 +00:00
|
|
|
// @todo FIXME: Verify that we actually need to
|
|
|
|
|
// escape \n\r\t here, and explain why, exactly.
|
2009-10-01 01:30:58 +00:00
|
|
|
#
|
2012-11-04 18:17:04 +00:00
|
|
|
// We could call Sanitizer::encodeAttribute() for this, but we
|
|
|
|
|
// don't because we're stubborn and like our marginal savings on
|
|
|
|
|
// byte size from not having to encode unnecessary quotes.
|
2015-02-19 23:05:40 +00:00
|
|
|
// The only difference between this transform and the one by
|
|
|
|
|
// Sanitizer::encodeAttribute() is '<' is only encoded here if
|
|
|
|
|
// $wgWellFormedXml is set, and ' is not encoded.
|
2009-10-01 01:30:58 +00:00
|
|
|
$map = array(
|
|
|
|
|
'&' => '&',
|
|
|
|
|
'"' => '"',
|
2015-02-19 23:05:40 +00:00
|
|
|
'>' => '>',
|
2009-10-01 01:30:58 +00:00
|
|
|
"\n" => ' ',
|
|
|
|
|
"\r" => ' ',
|
|
|
|
|
"\t" => '	'
|
|
|
|
|
);
|
|
|
|
|
if ( $wgWellFormedXml ) {
|
2012-11-04 18:17:04 +00:00
|
|
|
// This is allowed per spec: <http://www.w3.org/TR/xml/#NT-AttValue>
|
|
|
|
|
// But reportedly it breaks some XML tools?
|
|
|
|
|
// @todo FIXME: Is this really true?
|
2009-10-01 01:30:58 +00:00
|
|
|
$map['<'] = '<';
|
2009-09-16 05:29:44 +00:00
|
|
|
}
|
2009-10-01 01:30:58 +00:00
|
|
|
$ret .= " $key=$quote" . strtr( $value, $map ) . $quote;
|
2009-08-11 01:00:44 +00:00
|
|
|
}
|
2009-08-11 00:09:24 +00:00
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-07-10 12:48:06 +00:00
|
|
|
* Output a "<script>" tag with the given contents.
|
|
|
|
|
*
|
|
|
|
|
* @todo do some useful escaping as well, like if $contents contains
|
|
|
|
|
* literal "</script>" or (for XML) literal "]]>".
|
2009-08-11 00:09:24 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $contents JavaScript
|
2009-08-11 00:09:24 +00:00
|
|
|
* @return string Raw HTML
|
|
|
|
|
*/
|
|
|
|
|
public static function inlineScript( $contents ) {
|
2013-05-10 04:04:33 +00:00
|
|
|
global $wgWellFormedXml;
|
2009-08-11 00:09:24 +00:00
|
|
|
|
|
|
|
|
$attrs = array();
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2009-08-23 21:06:54 +00:00
|
|
|
if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) {
|
2009-08-11 00:09:24 +00:00
|
|
|
$contents = "/*<![CDATA[*/$contents/*]]>*/";
|
|
|
|
|
}
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2009-08-18 01:01:47 +00:00
|
|
|
return self::rawElement( 'script', $attrs, $contents );
|
2009-08-11 00:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-07-10 12:48:06 +00:00
|
|
|
* Output a "<script>" tag linking to the given URL, e.g.,
|
|
|
|
|
* "<script src=foo.js></script>".
|
2009-08-11 00:09:24 +00:00
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $url
|
2009-08-11 00:09:24 +00:00
|
|
|
* @return string Raw HTML
|
|
|
|
|
*/
|
|
|
|
|
public static function linkedScript( $url ) {
|
|
|
|
|
$attrs = array( 'src' => $url );
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2009-08-11 00:09:24 +00:00
|
|
|
return self::element( 'script', $attrs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-07-10 12:48:06 +00:00
|
|
|
* Output a "<style>" tag with the given contents for the given media type
|
2009-08-11 00:09:24 +00:00
|
|
|
* (if any). TODO: do some useful escaping as well, like if $contents
|
2012-07-10 12:48:06 +00:00
|
|
|
* contains literal "</style>" (admittedly unlikely).
|
2009-08-11 00:09:24 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $contents CSS
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $media A media type string, like 'screen'
|
2009-08-11 00:09:24 +00:00
|
|
|
* @return string Raw HTML
|
|
|
|
|
*/
|
2009-09-07 00:21:55 +00:00
|
|
|
public static function inlineStyle( $contents, $media = 'all' ) {
|
|
|
|
|
global $wgWellFormedXml;
|
2009-08-11 00:09:24 +00:00
|
|
|
|
2009-08-23 21:06:54 +00:00
|
|
|
if ( $wgWellFormedXml && preg_match( '/[<&]/', $contents ) ) {
|
|
|
|
|
$contents = "/*<![CDATA[*/$contents/*]]>*/";
|
|
|
|
|
}
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2009-09-07 00:21:55 +00:00
|
|
|
return self::rawElement( 'style', array(
|
|
|
|
|
'type' => 'text/css',
|
|
|
|
|
'media' => $media,
|
|
|
|
|
), $contents );
|
2009-08-11 00:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-07-10 12:48:06 +00:00
|
|
|
* Output a "<link rel=stylesheet>" linking to the given URL for the given
|
2009-08-11 00:09:24 +00:00
|
|
|
* media type (if any).
|
|
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $url
|
|
|
|
|
* @param string $media A media type string, like 'screen'
|
2009-08-11 00:09:24 +00:00
|
|
|
* @return string Raw HTML
|
|
|
|
|
*/
|
2009-09-07 00:21:55 +00:00
|
|
|
public static function linkedStyle( $url, $media = 'all' ) {
|
|
|
|
|
return self::element( 'link', array(
|
|
|
|
|
'rel' => 'stylesheet',
|
|
|
|
|
'href' => $url,
|
|
|
|
|
'type' => 'text/css',
|
|
|
|
|
'media' => $media,
|
|
|
|
|
) );
|
2009-08-11 00:09:24 +00:00
|
|
|
}
|
2009-08-11 01:00:44 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-07-10 12:48:06 +00:00
|
|
|
* Convenience function to produce an "<input>" element. This supports the
|
2013-05-10 04:04:33 +00:00
|
|
|
* new HTML5 input types and attributes.
|
2009-08-11 01:00:44 +00:00
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $name Name attribute
|
2015-03-26 09:38:35 +00:00
|
|
|
* @param string $value Value attribute
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $type Type attribute
|
|
|
|
|
* @param array $attribs Associative array of miscellaneous extra
|
2009-08-21 22:30:51 +00:00
|
|
|
* attributes, passed to Html::element()
|
2009-08-11 01:00:44 +00:00
|
|
|
* @return string Raw HTML
|
|
|
|
|
*/
|
2015-03-26 09:47:30 +00:00
|
|
|
public static function input( $name, $value = '', $type = 'text', array $attribs = array() ) {
|
2009-09-06 15:07:52 +00:00
|
|
|
$attribs['type'] = $type;
|
2009-09-07 00:21:55 +00:00
|
|
|
$attribs['value'] = $value;
|
2009-08-11 01:00:44 +00:00
|
|
|
$attribs['name'] = $name;
|
2014-07-30 17:56:25 +00:00
|
|
|
if ( in_array( $type, array( 'text', 'search', 'email', 'password', 'number' ) ) ) {
|
2015-03-26 09:17:07 +00:00
|
|
|
$attribs = self::getTextInputAttributes( $attribs );
|
2014-07-30 17:56:25 +00:00
|
|
|
}
|
2009-08-11 01:00:44 +00:00
|
|
|
return self::element( 'input', $attribs );
|
|
|
|
|
}
|
2009-08-21 21:57:26 +00:00
|
|
|
|
2013-03-07 19:03:44 +00:00
|
|
|
/**
|
|
|
|
|
* Convenience function to produce a checkbox (input element with type=checkbox)
|
|
|
|
|
*
|
|
|
|
|
* @param string $name Name attribute
|
|
|
|
|
* @param bool $checked Whether the checkbox is checked or not
|
|
|
|
|
* @param array $attribs Array of additional attributes
|
2015-03-26 09:38:35 +00:00
|
|
|
* @return string Raw HTML
|
2013-03-07 19:03:44 +00:00
|
|
|
*/
|
|
|
|
|
public static function check( $name, $checked = false, array $attribs = array() ) {
|
|
|
|
|
if ( isset( $attribs['value'] ) ) {
|
|
|
|
|
$value = $attribs['value'];
|
|
|
|
|
unset( $attribs['value'] );
|
|
|
|
|
} else {
|
|
|
|
|
$value = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $checked ) {
|
|
|
|
|
$attribs[] = 'checked';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::input( $name, $value, 'checkbox', $attribs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convenience function to produce a checkbox (input element with type=checkbox)
|
|
|
|
|
*
|
|
|
|
|
* @param string $name Name attribute
|
|
|
|
|
* @param bool $checked Whether the checkbox is checked or not
|
|
|
|
|
* @param array $attribs Array of additional attributes
|
2015-03-26 09:38:35 +00:00
|
|
|
* @return string Raw HTML
|
2013-03-07 19:03:44 +00:00
|
|
|
*/
|
|
|
|
|
public static function radio( $name, $checked = false, array $attribs = array() ) {
|
|
|
|
|
if ( isset( $attribs['value'] ) ) {
|
|
|
|
|
$value = $attribs['value'];
|
|
|
|
|
unset( $attribs['value'] );
|
|
|
|
|
} else {
|
|
|
|
|
$value = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $checked ) {
|
|
|
|
|
$attribs[] = 'checked';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::input( $name, $value, 'radio', $attribs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convenience function for generating a label for inputs.
|
|
|
|
|
*
|
|
|
|
|
* @param string $label Contents of the label
|
|
|
|
|
* @param string $id ID of the element being labeled
|
|
|
|
|
* @param array $attribs Additional attributes
|
2015-03-26 09:38:35 +00:00
|
|
|
* @return string Raw HTML
|
2013-03-07 19:03:44 +00:00
|
|
|
*/
|
|
|
|
|
public static function label( $label, $id, array $attribs = array() ) {
|
|
|
|
|
$attribs += array(
|
|
|
|
|
'for' => $id
|
|
|
|
|
);
|
|
|
|
|
return self::element( 'label', $attribs, $label );
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-21 21:57:26 +00:00
|
|
|
/**
|
2010-11-06 15:10:18 +00:00
|
|
|
* Convenience function to produce an input element with type=hidden
|
2009-08-21 21:57:26 +00:00
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $name Name attribute
|
|
|
|
|
* @param string $value Value attribute
|
|
|
|
|
* @param array $attribs Associative array of miscellaneous extra
|
2009-08-21 21:57:26 +00:00
|
|
|
* attributes, passed to Html::element()
|
|
|
|
|
* @return string Raw HTML
|
|
|
|
|
*/
|
2015-03-26 09:47:30 +00:00
|
|
|
public static function hidden( $name, $value, array $attribs = array() ) {
|
2009-08-21 21:57:26 +00:00
|
|
|
return self::input( $name, $value, 'hidden', $attribs );
|
|
|
|
|
}
|
2009-12-02 07:22:29 +00:00
|
|
|
|
|
|
|
|
/**
|
2013-11-23 15:31:07 +00:00
|
|
|
* Convenience function to produce a <textarea> element.
|
2012-07-10 12:48:06 +00:00
|
|
|
*
|
|
|
|
|
* This supports leaving out the cols= and rows= which Xml requires and are
|
2013-05-10 04:04:33 +00:00
|
|
|
* required by HTML4/XHTML but not required by HTML5.
|
2009-12-02 07:22:29 +00:00
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $name Name attribute
|
|
|
|
|
* @param string $value Value attribute
|
|
|
|
|
* @param array $attribs Associative array of miscellaneous extra
|
2009-12-02 07:22:29 +00:00
|
|
|
* attributes, passed to Html::element()
|
|
|
|
|
* @return string Raw HTML
|
|
|
|
|
*/
|
2015-03-26 09:47:30 +00:00
|
|
|
public static function textarea( $name, $value = '', array $attribs = array() ) {
|
2009-12-02 07:22:29 +00:00
|
|
|
$attribs['name'] = $name;
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2013-02-03 20:05:24 +00:00
|
|
|
if ( substr( $value, 0, 1 ) == "\n" ) {
|
2011-09-30 22:50:48 +00:00
|
|
|
// Workaround for bug 12130: browsers eat the initial newline
|
|
|
|
|
// assuming that it's just for show, but they do keep the later
|
|
|
|
|
// newlines, which we may want to preserve during editing.
|
|
|
|
|
// Prepending a single newline
|
|
|
|
|
$spacedValue = "\n" . $value;
|
|
|
|
|
} else {
|
|
|
|
|
$spacedValue = $value;
|
|
|
|
|
}
|
2015-03-26 09:17:07 +00:00
|
|
|
return self::element( 'textarea', self::getTextInputAttributes( $attribs ), $spacedValue );
|
2009-12-02 07:22:29 +00:00
|
|
|
}
|
2014-03-15 19:57:00 +00:00
|
|
|
|
2012-01-25 03:01:20 +00:00
|
|
|
/**
|
|
|
|
|
* Build a drop-down box for selecting a namespace
|
|
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param array $params Params to set.
|
2012-01-25 03:01:20 +00:00
|
|
|
* - selected: [optional] Id of namespace which should be pre-selected
|
2014-05-11 15:32:18 +00:00
|
|
|
* - all: [optional] Value of item for "all namespaces". If null or unset,
|
|
|
|
|
* no "<option>" is generated to select all namespaces.
|
|
|
|
|
* - label: text for label to add before the field.
|
|
|
|
|
* - exclude: [optional] Array of namespace ids to exclude.
|
|
|
|
|
* - disable: [optional] Array of namespace ids for which the option should
|
|
|
|
|
* be disabled in the selector.
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $selectAttribs HTML attributes for the generated select element.
|
2014-05-11 15:32:18 +00:00
|
|
|
* - id: [optional], default: 'namespace'.
|
|
|
|
|
* - name: [optional], default: 'namespace'.
|
2012-01-30 11:02:56 +00:00
|
|
|
* @return string HTML code to select a namespace.
|
2012-01-25 03:01:20 +00:00
|
|
|
*/
|
2014-05-11 15:32:18 +00:00
|
|
|
public static function namespaceSelector( array $params = array(),
|
|
|
|
|
array $selectAttribs = array()
|
|
|
|
|
) {
|
2012-01-25 03:01:20 +00:00
|
|
|
global $wgContLang;
|
|
|
|
|
|
|
|
|
|
ksort( $selectAttribs );
|
|
|
|
|
|
2012-01-30 11:02:56 +00:00
|
|
|
// Is a namespace selected?
|
2012-01-25 03:25:54 +00:00
|
|
|
if ( isset( $params['selected'] ) ) {
|
|
|
|
|
// If string only contains digits, convert to clean int. Selected could also
|
|
|
|
|
// be "all" or "" etc. which needs to be left untouched.
|
|
|
|
|
// PHP is_numeric() has issues with large strings, PHP ctype_digit has other issues
|
|
|
|
|
// and returns false for already clean ints. Use regex instead..
|
|
|
|
|
if ( preg_match( '/^\d+$/', $params['selected'] ) ) {
|
|
|
|
|
$params['selected'] = intval( $params['selected'] );
|
|
|
|
|
}
|
2012-01-30 11:02:56 +00:00
|
|
|
// else: leaves it untouched for later processing
|
2012-01-25 03:25:54 +00:00
|
|
|
} else {
|
|
|
|
|
$params['selected'] = '';
|
2012-01-25 03:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
2012-02-13 15:08:26 +00:00
|
|
|
if ( !isset( $params['exclude'] ) || !is_array( $params['exclude'] ) ) {
|
|
|
|
|
$params['exclude'] = array();
|
|
|
|
|
}
|
|
|
|
|
if ( !isset( $params['disable'] ) || !is_array( $params['disable'] ) ) {
|
|
|
|
|
$params['disable'] = array();
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-12 18:58:28 +00:00
|
|
|
// Associative array between option-values and option-labels
|
2012-01-25 03:01:20 +00:00
|
|
|
$options = array();
|
2012-01-30 11:02:56 +00:00
|
|
|
|
2012-01-25 03:01:20 +00:00
|
|
|
if ( isset( $params['all'] ) ) {
|
2012-02-12 18:58:28 +00:00
|
|
|
// add an option that would let the user select all namespaces.
|
|
|
|
|
// Value is provided by user, the name shown is localized for the user.
|
2012-07-24 01:04:15 +00:00
|
|
|
$options[$params['all']] = wfMessage( 'namespacesall' )->text();
|
2012-01-25 03:01:20 +00:00
|
|
|
}
|
2013-03-13 07:42:41 +00:00
|
|
|
// Add all namespaces as options (in the content language)
|
2012-01-25 03:01:20 +00:00
|
|
|
$options += $wgContLang->getFormattedNamespaces();
|
|
|
|
|
|
2012-02-12 18:58:28 +00:00
|
|
|
// Convert $options to HTML and filter out namespaces below 0
|
2012-01-25 03:01:20 +00:00
|
|
|
$optionsHtml = array();
|
|
|
|
|
foreach ( $options as $nsId => $nsName ) {
|
2012-02-13 15:08:26 +00:00
|
|
|
if ( $nsId < NS_MAIN || in_array( $nsId, $params['exclude'] ) ) {
|
2012-01-25 03:01:20 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2012-05-29 09:56:21 +00:00
|
|
|
if ( $nsId === NS_MAIN ) {
|
2014-12-16 00:41:45 +00:00
|
|
|
// For other namespaces use the namespace prefix as label, but for
|
2013-03-13 07:42:41 +00:00
|
|
|
// main we don't use "" but the user message describing it (e.g. "(Main)" or "(Article)")
|
2012-07-24 01:04:15 +00:00
|
|
|
$nsName = wfMessage( 'blanknamespace' )->text();
|
2012-05-29 09:56:21 +00:00
|
|
|
} elseif ( is_int( $nsId ) ) {
|
|
|
|
|
$nsName = $wgContLang->convertNamespace( $nsId );
|
2012-01-25 03:01:20 +00:00
|
|
|
}
|
2015-03-26 09:17:07 +00:00
|
|
|
$optionsHtml[] = self::element(
|
2012-03-07 19:14:20 +00:00
|
|
|
'option', array(
|
|
|
|
|
'disabled' => in_array( $nsId, $params['disable'] ),
|
|
|
|
|
'value' => $nsId,
|
|
|
|
|
'selected' => $nsId === $params['selected'],
|
|
|
|
|
), $nsName
|
|
|
|
|
);
|
2012-01-25 03:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-05 13:54:58 +00:00
|
|
|
if ( !array_key_exists( 'id', $selectAttribs ) ) {
|
|
|
|
|
$selectAttribs['id'] = 'namespace';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !array_key_exists( 'name', $selectAttribs ) ) {
|
|
|
|
|
$selectAttribs['name'] = 'namespace';
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-30 11:02:56 +00:00
|
|
|
$ret = '';
|
|
|
|
|
if ( isset( $params['label'] ) ) {
|
2015-03-26 09:17:07 +00:00
|
|
|
$ret .= self::element(
|
2012-03-07 19:14:20 +00:00
|
|
|
'label', array(
|
|
|
|
|
'for' => isset( $selectAttribs['id'] ) ? $selectAttribs['id'] : null,
|
|
|
|
|
), $params['label']
|
|
|
|
|
) . ' ';
|
2012-01-30 11:02:56 +00:00
|
|
|
}
|
2012-02-12 18:58:28 +00:00
|
|
|
|
|
|
|
|
// Wrap options in a <select>
|
2015-03-26 09:17:07 +00:00
|
|
|
$ret .= self::openElement( 'select', $selectAttribs )
|
2012-01-25 03:01:20 +00:00
|
|
|
. "\n"
|
|
|
|
|
. implode( "\n", $optionsHtml )
|
|
|
|
|
. "\n"
|
2015-03-26 09:17:07 +00:00
|
|
|
. self::closeElement( 'select' );
|
2012-02-12 18:58:28 +00:00
|
|
|
|
2012-01-25 03:01:20 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
2010-05-08 13:45:14 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs the opening html-tag with necessary doctypes depending on
|
|
|
|
|
* global variables.
|
|
|
|
|
*
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param array $attribs Associative array of miscellaneous extra
|
2010-05-08 13:45:14 +00:00
|
|
|
* attributes, passed to Html::element() of html tag.
|
2014-04-08 15:29:17 +00:00
|
|
|
* @return string Raw HTML
|
2010-05-08 13:45:14 +00:00
|
|
|
*/
|
2015-03-26 09:47:30 +00:00
|
|
|
public static function htmlHeader( array $attribs = array() ) {
|
2010-05-08 13:45:14 +00:00
|
|
|
$ret = '';
|
|
|
|
|
|
2013-05-10 04:04:33 +00:00
|
|
|
global $wgHtml5Version, $wgMimeType, $wgXhtmlNamespaces;
|
2010-05-08 13:45:14 +00:00
|
|
|
|
2013-05-10 04:04:33 +00:00
|
|
|
$isXHTML = self::isXmlMimeType( $wgMimeType );
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2013-05-10 04:04:33 +00:00
|
|
|
if ( $isXHTML ) { // XHTML5
|
2014-07-24 14:04:48 +00:00
|
|
|
// XML MIME-typed markup should have an xml header.
|
2013-05-10 04:04:33 +00:00
|
|
|
// However a DOCTYPE is not needed.
|
|
|
|
|
$ret .= "<?xml version=\"1.0\" encoding=\"UTF-8\" ?" . ">\n";
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2013-05-10 04:04:33 +00:00
|
|
|
// Add the standard xmlns
|
|
|
|
|
$attribs['xmlns'] = 'http://www.w3.org/1999/xhtml';
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2013-05-10 04:04:33 +00:00
|
|
|
// And support custom namespaces
|
2010-05-08 13:45:14 +00:00
|
|
|
foreach ( $wgXhtmlNamespaces as $tag => $ns ) {
|
|
|
|
|
$attribs["xmlns:$tag"] = $ns;
|
|
|
|
|
}
|
2013-05-10 04:04:33 +00:00
|
|
|
} else { // HTML5
|
|
|
|
|
// DOCTYPE
|
|
|
|
|
$ret .= "<!DOCTYPE html>\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $wgHtml5Version ) {
|
|
|
|
|
$attribs['version'] = $wgHtml5Version;
|
2010-05-08 13:45:14 +00:00
|
|
|
}
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2015-03-26 09:17:07 +00:00
|
|
|
$html = self::openElement( 'html', $attribs );
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2010-09-04 03:43:33 +00:00
|
|
|
if ( $html ) {
|
|
|
|
|
$html .= "\n";
|
|
|
|
|
}
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2010-07-08 13:34:03 +00:00
|
|
|
$ret .= $html;
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2010-07-08 13:34:03 +00:00
|
|
|
return $ret;
|
2010-05-08 13:45:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-07-24 14:04:48 +00:00
|
|
|
* Determines if the given MIME type is xml.
|
2010-05-08 13:45:14 +00:00
|
|
|
*
|
2014-07-24 14:04:48 +00:00
|
|
|
* @param string $mimetype MIME type
|
2014-04-08 15:29:17 +00:00
|
|
|
* @return bool
|
2010-05-08 13:45:14 +00:00
|
|
|
*/
|
|
|
|
|
public static function isXmlMimeType( $mimetype ) {
|
2013-06-08 11:48:01 +00:00
|
|
|
# http://www.whatwg.org/html/infrastructure.html#xml-mime-type
|
2013-05-10 04:04:33 +00:00
|
|
|
# * text/xml
|
|
|
|
|
# * application/xml
|
2014-07-24 14:04:48 +00:00
|
|
|
# * Any MIME type with a subtype ending in +xml (this implicitly includes application/xhtml+xml)
|
2013-08-31 16:36:02 +00:00
|
|
|
return (bool)preg_match( '!^(text|application)/xml$|^.+/.+\+xml$!', $mimetype );
|
2010-05-08 13:45:14 +00:00
|
|
|
}
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2011-06-01 13:57:50 +00:00
|
|
|
/**
|
|
|
|
|
* Get HTML for an info box with an icon.
|
|
|
|
|
*
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $text Wikitext, get this with wfMessage()->plain()
|
2014-09-05 18:17:40 +00:00
|
|
|
* @param string $icon Path to icon file (used as 'src' attribute)
|
2014-04-22 11:07:02 +00:00
|
|
|
* @param string $alt Alternate text for the icon
|
|
|
|
|
* @param string $class Additional class name to add to the wrapper div
|
2011-06-01 13:57:50 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2015-03-26 09:43:01 +00:00
|
|
|
static function infoBox( $text, $icon, $alt, $class = '' ) {
|
2015-03-26 09:17:07 +00:00
|
|
|
$s = self::openElement( 'div', array( 'class' => "mw-infobox $class" ) );
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2015-03-26 09:17:07 +00:00
|
|
|
$s .= self::openElement( 'div', array( 'class' => 'mw-infobox-left' ) ) .
|
|
|
|
|
self::element( 'img',
|
2011-06-01 13:57:50 +00:00
|
|
|
array(
|
|
|
|
|
'src' => $icon,
|
|
|
|
|
'alt' => $alt,
|
|
|
|
|
)
|
2013-04-13 11:36:24 +00:00
|
|
|
) .
|
2015-03-26 09:17:07 +00:00
|
|
|
self::closeElement( 'div' );
|
2011-06-01 13:57:50 +00:00
|
|
|
|
2015-03-26 09:17:07 +00:00
|
|
|
$s .= self::openElement( 'div', array( 'class' => 'mw-infobox-right' ) ) .
|
2013-04-13 11:36:24 +00:00
|
|
|
$text .
|
2015-03-26 09:17:07 +00:00
|
|
|
self::closeElement( 'div' );
|
|
|
|
|
$s .= self::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
|
2011-06-01 13:57:50 +00:00
|
|
|
|
2015-03-26 09:17:07 +00:00
|
|
|
$s .= self::closeElement( 'div' );
|
2011-06-01 13:57:50 +00:00
|
|
|
|
2015-03-26 09:17:07 +00:00
|
|
|
$s .= self::element( 'div', array( 'style' => 'clear: left;' ), ' ' );
|
2011-06-01 14:12:53 +00:00
|
|
|
|
2011-06-01 13:57:50 +00:00
|
|
|
return $s;
|
|
|
|
|
}
|
Initial stab at responsive images for screen densities.
* adds $wgResponsiveImages setting, defaulting to true, to enable the feature
* adds 'srcset' attribute with 1.5x and 2x URLs to image links and image thumbs
* adds jquery.hidpi plugin to check pixel density and implement partial 'srcset' polyfill
** $.devicePixelRatio() returns window.devicePixelRatio, with compat fallback for IE 10
** $().hidpi() performs a 'srcset' polyfill for browsers with no native 'srcset' support
* adds mediawiki.hidpi RL script to trigger hidpi loads after main images load
Note that this is a work in progress. There will be places where this doesn't yet work which output their imgs differently. If moving from a low to high-DPI screen on a MacBook Pro Retina display, you won't see images load until you reload.
Confirmed basic images and thumbs in wikitext appear to work in Safari 6, Chrome 21, Firefox 18 nightly on MacBook Pro Retina display, and IE 10 in Windows 8 at 150% zoom, 200% zoom, and 140% and 180%-ratio Metro tablet sizes.
Internally this is still a bit of a hack; Linker::makeImageLink and Linker::makeThumbLink explicitly ask for 1.5x and 2x scaled versions and insert their URLs, if different, into the original thumbnail object which (in default handler) outputs the srcset. This means that a number of places that handle images differently won't see the higher-resolution versions, such as <gallery> and the large thumbnail on the File: description page.
At some point we may wish to redo some of how the MediaHandler stuff works so that requesting a single thumbnail automatically produces the extra sizes in all circumstances. We might also consider outputting a 'srcset' or multiple src sizes in 'imageinfo' API requests, which would make ApiForeignRepo/InstantCommons more efficient. (Currently it has to make three requests for each image to get the three sizes.)
Change-Id: Id80ebd07a1a9f401a2c2bfeb21aae987e5aa863b
2012-09-18 07:18:50 +00:00
|
|
|
|
|
|
|
|
/**
|
2015-04-03 23:17:13 +00:00
|
|
|
* Generate a srcset attribute value.
|
|
|
|
|
*
|
|
|
|
|
* Generates a srcset attribute value from an array mapping pixel densities
|
|
|
|
|
* to URLs. A trailing 'x' in pixel density values is optional.
|
|
|
|
|
*
|
|
|
|
|
* @note srcset width and height values are not supported.
|
|
|
|
|
*
|
|
|
|
|
* @see http://www.whatwg.org/html/embedded-content-1.html#attr-img-srcset
|
|
|
|
|
*
|
|
|
|
|
* @par Example:
|
|
|
|
|
* @code
|
|
|
|
|
* Html::srcSet( array(
|
|
|
|
|
* '1x' => 'standard.jpeg',
|
|
|
|
|
* '1.5x' => 'large.jpeg',
|
|
|
|
|
* '3x' => 'extra-large.jpeg',
|
|
|
|
|
* ) );
|
|
|
|
|
* // gives 'standard.jpeg 1x, large.jpeg 1.5x, extra-large.jpeg 2x'
|
|
|
|
|
* @endcode
|
Initial stab at responsive images for screen densities.
* adds $wgResponsiveImages setting, defaulting to true, to enable the feature
* adds 'srcset' attribute with 1.5x and 2x URLs to image links and image thumbs
* adds jquery.hidpi plugin to check pixel density and implement partial 'srcset' polyfill
** $.devicePixelRatio() returns window.devicePixelRatio, with compat fallback for IE 10
** $().hidpi() performs a 'srcset' polyfill for browsers with no native 'srcset' support
* adds mediawiki.hidpi RL script to trigger hidpi loads after main images load
Note that this is a work in progress. There will be places where this doesn't yet work which output their imgs differently. If moving from a low to high-DPI screen on a MacBook Pro Retina display, you won't see images load until you reload.
Confirmed basic images and thumbs in wikitext appear to work in Safari 6, Chrome 21, Firefox 18 nightly on MacBook Pro Retina display, and IE 10 in Windows 8 at 150% zoom, 200% zoom, and 140% and 180%-ratio Metro tablet sizes.
Internally this is still a bit of a hack; Linker::makeImageLink and Linker::makeThumbLink explicitly ask for 1.5x and 2x scaled versions and insert their URLs, if different, into the original thumbnail object which (in default handler) outputs the srcset. This means that a number of places that handle images differently won't see the higher-resolution versions, such as <gallery> and the large thumbnail on the File: description page.
At some point we may wish to redo some of how the MediaHandler stuff works so that requesting a single thumbnail automatically produces the extra sizes in all circumstances. We might also consider outputting a 'srcset' or multiple src sizes in 'imageinfo' API requests, which would make ApiForeignRepo/InstantCommons more efficient. (Currently it has to make three requests for each image to get the three sizes.)
Change-Id: Id80ebd07a1a9f401a2c2bfeb21aae987e5aa863b
2012-09-18 07:18:50 +00:00
|
|
|
*
|
2015-03-26 09:38:35 +00:00
|
|
|
* @param string[] $urls
|
Initial stab at responsive images for screen densities.
* adds $wgResponsiveImages setting, defaulting to true, to enable the feature
* adds 'srcset' attribute with 1.5x and 2x URLs to image links and image thumbs
* adds jquery.hidpi plugin to check pixel density and implement partial 'srcset' polyfill
** $.devicePixelRatio() returns window.devicePixelRatio, with compat fallback for IE 10
** $().hidpi() performs a 'srcset' polyfill for browsers with no native 'srcset' support
* adds mediawiki.hidpi RL script to trigger hidpi loads after main images load
Note that this is a work in progress. There will be places where this doesn't yet work which output their imgs differently. If moving from a low to high-DPI screen on a MacBook Pro Retina display, you won't see images load until you reload.
Confirmed basic images and thumbs in wikitext appear to work in Safari 6, Chrome 21, Firefox 18 nightly on MacBook Pro Retina display, and IE 10 in Windows 8 at 150% zoom, 200% zoom, and 140% and 180%-ratio Metro tablet sizes.
Internally this is still a bit of a hack; Linker::makeImageLink and Linker::makeThumbLink explicitly ask for 1.5x and 2x scaled versions and insert their URLs, if different, into the original thumbnail object which (in default handler) outputs the srcset. This means that a number of places that handle images differently won't see the higher-resolution versions, such as <gallery> and the large thumbnail on the File: description page.
At some point we may wish to redo some of how the MediaHandler stuff works so that requesting a single thumbnail automatically produces the extra sizes in all circumstances. We might also consider outputting a 'srcset' or multiple src sizes in 'imageinfo' API requests, which would make ApiForeignRepo/InstantCommons more efficient. (Currently it has to make three requests for each image to get the three sizes.)
Change-Id: Id80ebd07a1a9f401a2c2bfeb21aae987e5aa863b
2012-09-18 07:18:50 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2015-03-26 09:47:30 +00:00
|
|
|
static function srcSet( array $urls ) {
|
Initial stab at responsive images for screen densities.
* adds $wgResponsiveImages setting, defaulting to true, to enable the feature
* adds 'srcset' attribute with 1.5x and 2x URLs to image links and image thumbs
* adds jquery.hidpi plugin to check pixel density and implement partial 'srcset' polyfill
** $.devicePixelRatio() returns window.devicePixelRatio, with compat fallback for IE 10
** $().hidpi() performs a 'srcset' polyfill for browsers with no native 'srcset' support
* adds mediawiki.hidpi RL script to trigger hidpi loads after main images load
Note that this is a work in progress. There will be places where this doesn't yet work which output their imgs differently. If moving from a low to high-DPI screen on a MacBook Pro Retina display, you won't see images load until you reload.
Confirmed basic images and thumbs in wikitext appear to work in Safari 6, Chrome 21, Firefox 18 nightly on MacBook Pro Retina display, and IE 10 in Windows 8 at 150% zoom, 200% zoom, and 140% and 180%-ratio Metro tablet sizes.
Internally this is still a bit of a hack; Linker::makeImageLink and Linker::makeThumbLink explicitly ask for 1.5x and 2x scaled versions and insert their URLs, if different, into the original thumbnail object which (in default handler) outputs the srcset. This means that a number of places that handle images differently won't see the higher-resolution versions, such as <gallery> and the large thumbnail on the File: description page.
At some point we may wish to redo some of how the MediaHandler stuff works so that requesting a single thumbnail automatically produces the extra sizes in all circumstances. We might also consider outputting a 'srcset' or multiple src sizes in 'imageinfo' API requests, which would make ApiForeignRepo/InstantCommons more efficient. (Currently it has to make three requests for each image to get the three sizes.)
Change-Id: Id80ebd07a1a9f401a2c2bfeb21aae987e5aa863b
2012-09-18 07:18:50 +00:00
|
|
|
$candidates = array();
|
2013-04-20 22:49:30 +00:00
|
|
|
foreach ( $urls as $density => $url ) {
|
2015-04-03 23:17:13 +00:00
|
|
|
// Cast density to float to strip 'x'.
|
|
|
|
|
$candidates[] = $url . ' ' . (float)$density . 'x';
|
Initial stab at responsive images for screen densities.
* adds $wgResponsiveImages setting, defaulting to true, to enable the feature
* adds 'srcset' attribute with 1.5x and 2x URLs to image links and image thumbs
* adds jquery.hidpi plugin to check pixel density and implement partial 'srcset' polyfill
** $.devicePixelRatio() returns window.devicePixelRatio, with compat fallback for IE 10
** $().hidpi() performs a 'srcset' polyfill for browsers with no native 'srcset' support
* adds mediawiki.hidpi RL script to trigger hidpi loads after main images load
Note that this is a work in progress. There will be places where this doesn't yet work which output their imgs differently. If moving from a low to high-DPI screen on a MacBook Pro Retina display, you won't see images load until you reload.
Confirmed basic images and thumbs in wikitext appear to work in Safari 6, Chrome 21, Firefox 18 nightly on MacBook Pro Retina display, and IE 10 in Windows 8 at 150% zoom, 200% zoom, and 140% and 180%-ratio Metro tablet sizes.
Internally this is still a bit of a hack; Linker::makeImageLink and Linker::makeThumbLink explicitly ask for 1.5x and 2x scaled versions and insert their URLs, if different, into the original thumbnail object which (in default handler) outputs the srcset. This means that a number of places that handle images differently won't see the higher-resolution versions, such as <gallery> and the large thumbnail on the File: description page.
At some point we may wish to redo some of how the MediaHandler stuff works so that requesting a single thumbnail automatically produces the extra sizes in all circumstances. We might also consider outputting a 'srcset' or multiple src sizes in 'imageinfo' API requests, which would make ApiForeignRepo/InstantCommons more efficient. (Currently it has to make three requests for each image to get the three sizes.)
Change-Id: Id80ebd07a1a9f401a2c2bfeb21aae987e5aa863b
2012-09-18 07:18:50 +00:00
|
|
|
}
|
|
|
|
|
return implode( ", ", $candidates );
|
|
|
|
|
}
|
2009-08-11 00:09:24 +00:00
|
|
|
}
|