2 # Copyright (C) 2009 Aryeh Gregor
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
21 * This class is a collection of static functions that serve two purposes:
23 * 1) Implement any algorithms specified by HTML 5, or other HTML
24 * specifications, in a convenient and self-contained way.
26 * 2) Allow HTML elements to be conveniently and safely generated, like the
27 * current Xml class but a) less confused (Xml supports HTML-specific things,
28 * but only sometimes!) and b) not necessarily confined to XML-compatible
31 * There are two important configuration options this class uses:
33 * $wgHtml5: If this is set to false, then all output should be valid XHTML 1.0
35 * $wgWellFormedXml: If this is set to true, then all output should be
36 * well-formed XML (quotes on attributes, self-closing tags, etc.).
38 * This class is meant to be confined to utility functions that are called from
39 * trusted code paths. It does not do enforcement of policy like not allowing
43 # List of void elements from HTML 5, section 9.1.2 as of 2009-08-10
44 private static $voidElements = array(
61 # Boolean attributes, which may have the value omitted entirely. Manually
62 # collected from the HTML 5 spec as of 2009-08-10.
63 private static $boolAttribs = array(
87 * Returns an HTML element in a string. The major advantage here over
88 * manually typing out the HTML is that it will escape all attribute
89 * values. If you're hardcoding all the attributes, or there are none, you
90 * should probably type out the string yourself.
92 * This is quite similar to Xml::tags(), but it implements some useful
93 * HTML-specific logic. For instance, there is no $allowShortTag
94 * parameter: the closing tag is magically omitted if $element has an empty
95 * content model. If $wgWellFormedXml is false, then a few bytes will be
96 * shaved off the HTML output as well. In the future, other HTML-specific
97 * features might be added, like allowing arrays for the values of
98 * attributes like class= and media=.
100 * @param $element string The element's name, e.g., 'a'
101 * @param $attribs array Associative array of attributes, e.g., array(
102 * 'href' => 'http://www.mediawiki.org/' ). Values will be HTML-escaped.
103 * @param $contents string The raw HTML contents of the element: *not*
105 * @return string Raw HTML
107 public static function rawElement( $element, $attribs = array(), $contents = '' ) {
108 global $wgWellFormedXml;
109 $element = strtolower( $element );
110 $start = "<$element" . self
::expandAttributes( $attribs );
111 if ( in_array( $element, self
::$voidElements ) ) {
112 if ( $wgWellFormedXml ) {
117 return "$start>$contents</$element>";
122 * Identical to rawElement(), but HTML-escapes $contents (like
125 public static function element( $element, $attribs = array(), $contents = '' ) {
126 return self
::rawElement( $element, $attribs, strtr( $contents, array(
127 # There's no point in escaping quotes, >, etc. in the contents of
135 * Given an associative array of element attributes, generate a string
136 * to stick after the element name in HTML output. Like array( 'href' =>
137 * 'http://www.mediawiki.org/' ) becomes something like
138 * ' href="http://www.mediawiki.org"'. Again, this is like
139 * Xml::expandAttributes(), but it implements some HTML-specific logic.
140 * For instance, it will omit quotation marks if $wgWellFormedXml is false,
141 * and will treat boolean attributes specially.
143 * @param $attribs array Associative array of attributes, e.g., array(
144 * 'href' => 'http://www.mediawiki.org/' ). Values will be HTML-escaped.
145 * @return string HTML fragment that goes between element name and '>'
146 * (starting with a space if at least one attribute is output)
148 public static function expandAttributes( $attribs ) {
149 global $wgHtml5, $wgWellFormedXml;
152 foreach ( $attribs as $key => $value ) {
153 # See the "Attributes" section in the HTML syntax part of HTML 5,
154 # 9.1.2.3 as of 2009-08-10. Most attributes can have quotation
155 # marks omitted, but not all. (Although a literal " is not
156 # permitted, we don't check for that, since it will be escaped
158 if ( $wgWellFormedXml ||
$value == ''
159 ||
preg_match( "/[ '=<>]/", $value ) ) {
165 if ( in_array( $key, self
::$boolAttribs ) ) {
166 # In XHTML 1.0 Transitional, the value needs to be equal to the
167 # key. In HTML 5, we can leave the value empty instead. If we
168 # don't need well-formed XML, we can omit the = entirely.
169 if ( !$wgWellFormedXml ) {
171 } elseif ( $wgHtml5 ) {
172 $ret .= " $key=\"\"";
174 $ret .= " $key=\"$key\"";
177 # Apparently we need to entity-encode \n, \r, \t, although the
178 # spec doesn't mention that. Since we're doing strtr() anyway,
179 # and we don't need <> escaped here, we may as well not call
180 # htmlspecialchars(). FIXME: verify that we actually need to
181 # escape \n\r\t here, and explain why, exactly.
182 $ret .= " $key=$quote" . strtr( $value, array(
195 * Output a <script> tag with the given contents. TODO: do some useful
196 * escaping as well, like if $contents contains literal '</script>' or (for
197 * XML) literal "]]>".
199 * @param $contents string JavaScript
200 * @return string Raw HTML
202 public static function inlineScript( $contents ) {
203 global $wgHtml5, $wgJsMimeType;
207 $attrs['type'] = $wgJsMimeType;
208 $contents = "/*<![CDATA[*/$contents/*]]>*/";
210 return self
::rawElement( 'script', $attrs, $contents );
214 * Output a <script> tag linking to the given URL, e.g.,
215 * <script src=foo.js></script>.
218 * @return string Raw HTML
220 public static function linkedScript( $url ) {
221 global $wgHtml5, $wgJsMimeType;
223 $attrs = array( 'src' => $url );
225 $attrs['type'] = $wgJsMimeType;
227 return self
::element( 'script', $attrs );
231 * Output a <style> tag with the given contents for the given media type
232 * (if any). TODO: do some useful escaping as well, like if $contents
233 * contains literal '</style>' (admittedly unlikely).
235 * @param $contents string CSS
236 * @param $media mixed A media type string, like 'screen', or null for all
238 * @return string Raw HTML
240 public static function inlineStyle( $contents, $media = null ) {
245 # Technically we should probably add CDATA stuff here like with
246 # scripts, but in practice, stylesheets tend not to have
247 # problematic characters anyway.
248 $attrs['type'] = 'text/css';
250 if ( $media !== null ) {
251 $attrs['media'] = $media;
253 return self
::rawElement( 'style', $attrs, $contents );
257 * Output a <link rel=stylesheet> linking to the given URL for the given
258 * media type (if any).
261 * @param $media mixed A media type string, like 'screen', or null for all
263 * @return string Raw HTML
265 public static function linkedStyle( $url, $media = null ) {
268 $attrs = array( 'rel' => 'stylesheet', 'href' => $url );
270 $attrs['type'] = 'text/css';
272 if ( $media !== null ) {
273 $attrs['media'] = $media;
275 return self
::element( 'link', $attrs );
279 * Convenience function to produce an <input> element. This supports the
280 * new HTML 5 input types and attributes, and will silently strip them if
283 * @param $name string name attribute
284 * @param $value mixed value attribute (null = omit)
285 * @param $type string type attribute
286 * @param $attribs array Assocative array of miscellaneous extra attributes,
287 * passed to Html::element()
288 * @return string Raw HTML
290 public static function input( $name, $value = null, $type = 'text', $attribs = array() ) {
294 // With $wgHtml5 off we want to validate as XHTML 1, so we
295 // strip out any fancy HTML 5-only input types for now.
297 // Whitelist of valid types:
310 if ( !in_array( $type, $validTypes ) ) {
313 // Here we're blacklisting some HTML5-only attributes...
314 $html5attribs = array(
325 foreach ( $html5attribs as $badAttr ) {
326 unset( $attribs[$badAttr] );
329 if ( $type != 'text' ) {
330 $attribs['type'] = $type;
332 if ( $value !== null ) {
333 $attribs['value'] = $value;
335 $attribs['name'] = $name;
337 return self
::element( 'input', $attribs );