4 * Module of static functions for generating XML
9 * Format an XML element with given attributes and, optionally, text content.
10 * Element and attribute names are assumed to be ready for literal inclusion.
11 * Strings are assumed to not contain XML-illegal characters; special
12 * characters (<, >, &) are escaped but illegals are not touched.
14 * @param $element String:
15 * @param $attribs Array: Name=>value pairs. Values will be escaped.
16 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
19 public static function element( $element, $attribs = null, $contents = '') {
20 $out = '<' . $element;
21 if( !is_null( $attribs ) ) {
22 foreach( $attribs as $name => $val ) {
23 $out .= ' ' . $name . '="' . Sanitizer
::encodeAttribute( $val ) . '"';
26 if( is_null( $contents ) ) {
29 if( $contents === '' ) {
32 $out .= '>' . htmlspecialchars( $contents ) . "</$element>";
39 * Format an XML element as with self::element(), but run text through the
40 * UtfNormal::cleanUp() validator first to ensure that no invalid UTF-8
43 * @param $element String:
44 * @param $attribs Array: Name=>value pairs. Values will be escaped.
45 * @param $contents String: NULL to make an open tag only; '' for a contentless closed tag (default)
48 public static function elementClean( $element, $attribs = array(), $contents = '') {
50 $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
53 wfProfileIn( __METHOD__
. '-norm' );
54 $contents = UtfNormal
::cleanUp( $contents );
55 wfProfileOut( __METHOD__
. '-norm' );
57 return self
::element( $element, $attribs, $contents );
61 public static function openElement( $element, $attribs = null ) { return self
::element( $element, $attribs, null ); }
62 public static function closeElement( $element ) { return "</$element>"; }
65 * Create a namespace selector
67 * @param $selected Mixed: the namespace which should be selected, default ''
68 * @param $allnamespaces String: value of a special item denoting all namespaces. Null to not include (default)
69 * @param $includehidden Bool: include hidden namespaces?
70 * @return String: Html string containing the namespace selector
72 public static function namespaceSelector($selected = '', $allnamespaces = null, $includehidden=false) {
74 if( $selected !== '' ) {
75 if( is_null( $selected ) ) {
76 // No namespace selected; let exact match work without hitting Main
79 // Let input be numeric strings without breaking the empty match.
80 $selected = intval( $selected );
83 $s = "\n<select id='namespace' name='namespace' class='namespaceselector'>\n";
84 $arr = $wgContLang->getFormattedNamespaces();
85 if( !is_null($allnamespaces) ) {
86 $arr = array($allnamespaces => wfMsg('namespacesall')) +
$arr;
88 foreach ($arr as $index => $name) {
89 if ($index < NS_MAIN
) continue;
91 $name = $index !== 0 ?
$name : wfMsg('blanknamespace');
93 if ($index === $selected) {
94 $s .= "\t" . self
::element("option",
95 array("value" => $index, "selected" => "selected"),
98 $s .= "\t" . self
::element("option", array("value" => $index), $name) . "\n";
105 public static function span( $text, $class, $attribs=array() ) {
106 return self
::element( 'span', array( 'class' => $class ) +
$attribs, $text );
110 * Convenience function to build an HTML text input field
111 * @return string HTML
113 public static function input( $name, $size=false, $value=false, $attribs=array() ) {
114 return self
::element( 'input', array(
117 'value' => $value ) +
$attribs );
121 * Internal function for use in checkboxes and radio buttons and such.
124 public static function attrib( $name, $present = true ) {
125 return $present ?
array( $name => $name ) : array();
129 * Convenience function to build an HTML checkbox
130 * @return string HTML
132 public static function check( $name, $checked=false, $attribs=array() ) {
133 return self
::element( 'input', array_merge(
136 'type' => 'checkbox',
138 self
::attrib( 'checked', $checked ),
143 * Convenience function to build an HTML radio button
144 * @return string HTML
146 public static function radio( $name, $value, $checked=false, $attribs=array() ) {
147 return self
::element( 'input', array(
150 'value' => $value ) + self
::attrib( 'checked', $checked ) +
$attribs );
154 * Convenience function to build an HTML form label
155 * @return string HTML
157 public static function label( $label, $id ) {
158 return self
::element( 'label', array( 'for' => $id ), $label );
162 * Convenience function to build an HTML text input field with a label
163 * @return string HTML
165 public static function inputLabel( $label, $name, $id, $size=false, $value=false, $attribs=array() ) {
166 return Xml
::label( $label, $id ) .
168 self
::input( $name, $size, $value, array( 'id' => $id ) +
$attribs );
172 * Convenience function to build an HTML checkbox with a label
173 * @return string HTML
175 public static function checkLabel( $label, $name, $id, $checked=false, $attribs=array() ) {
176 return self
::check( $name, $checked, array( 'id' => $id ) +
$attribs ) .
178 self
::label( $label, $id );
182 * Convenience function to build an HTML radio button with a label
183 * @return string HTML
185 public static function radioLabel( $label, $name, $value, $id, $checked=false, $attribs=array() ) {
186 return self
::radio( $name, $value, $checked, array( 'id' => $id ) +
$attribs ) .
188 self
::label( $label, $id );
192 * Convenience function to build an HTML submit button
193 * @param $value String: label text for the button
194 * @param $attribs Array: optional custom attributes
195 * @return string HTML
197 public static function submitButton( $value, $attribs=array() ) {
198 return self
::element( 'input', array( 'type' => 'submit', 'value' => $value ) +
$attribs );
202 * Convenience function to build an HTML hidden form field.
203 * @todo Document $name parameter.
205 * @param $value String: label text for the button
206 * @param $attribs Array: optional custom attributes
207 * @return string HTML
209 public static function hidden( $name, $value, $attribs=array() ) {
210 return self
::element( 'input', array(
213 'value' => $value ) +
$attribs );
217 * Convenience function to build an HTML drop-down list item.
218 * @param $text String: text for this item
219 * @param $value String: form submission value; if empty, use text
220 * @param $selected boolean: if true, will be the default selected item
221 * @param $attribs array: optional additional HTML attributes
222 * @return string HTML
224 public static function option( $text, $value=null, $selected=false,
226 if( !is_null( $value ) ) {
227 $attribs['value'] = $value;
230 $attribs['selected'] = 'selected';
232 return self
::element( 'option', $attribs, $text );
236 * Returns an escaped string suitable for inclusion in a string literal
237 * for JavaScript source code.
238 * Illegal control characters are assumed not to be present.
240 * @param string $string
243 public static function escapeJsString( $string ) {
244 // See ECMA 262 section 7.8.4 for string literal format
252 # To avoid closing the element or CDATA section
256 # To avoid any complaints about bad entity refs
259 return strtr( $string, $pairs );
263 * Encode a variable of unknown type to JavaScript.
264 * Doesn't support hashtables just yet.
266 public static function encodeJsVar( $value ) {
267 if ( is_bool( $value ) ) {
268 $s = $value ?
'true' : 'false';
269 } elseif ( is_null( $value ) ) {
271 } elseif ( is_int( $value ) ) {
273 } elseif ( is_array( $value ) ) {
275 foreach ( $value as $name => $elt ) {
279 $s .= self
::encodeJsVar( $elt );
283 $s = '"' . self
::escapeJsString( $value ) . '"';
290 * Check if a string is well-formed XML.
291 * Must include the surrounding tag.
293 * @param $text String: string to test.
296 * @todo Error position reporting return
298 public static function isWellFormed( $text ) {
299 $parser = xml_parser_create( "UTF-8" );
301 # case folding violates XML standard, turn it off
302 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING
, false );
304 if( !xml_parse( $parser, $text, true ) ) {
305 //$err = xml_error_string( xml_get_error_code( $parser ) );
306 //$position = xml_get_current_byte_index( $parser );
307 //$fragment = $this->extractFragment( $html, $position );
308 //$this->mXmlError = "$err at byte $position:\n$fragment";
309 xml_parser_free( $parser );
312 xml_parser_free( $parser );
317 * Check if a string is a well-formed XML fragment.
318 * Wraps fragment in an \<html\> bit and doctype, so it can be a fragment
319 * and can use HTML named entities.
321 * @param $text String:
324 public static function isWellFormedXmlFragment( $text ) {
326 Sanitizer
::hackDocType() .
330 return Xml
::isWellFormed( $html );
334 * Replace " > and < with their respective HTML entities ( ",
337 * @param $in String: text that might contain HTML tags.
338 * @return string Escaped string
340 public static function escapeTagsOnly( $in ) {
342 array( '"', '>', '<' ),
343 array( '"', '>', '<' ),