public static function namespaceSelector( Array $params = array(), Array $selectAttribs = array() ) {
global $wgContLang;
- // Default 'id' & 'name' <select> attributes
- $selectAttribs = $selectAttribs + array(
- 'id' => 'namespace',
- 'name' => 'namespace',
- );
ksort( $selectAttribs );
// Is a namespace selected?
// main we don't use "" but the user message descripting it (e.g. "(Main)" or "(Article)")
$nsName = wfMsg( 'blanknamespace' );
}
- $optionsHtml[] = Xml::option( $nsName, $nsId, $nsId === $params['selected'], array(
- 'disabled' => in_array( $nsId, $params['disable'] ),
- ) );
+ $optionsHtml[] = Html::element(
+ 'option', array(
+ 'disabled' => in_array( $nsId, $params['disable'] ),
+ 'value' => $nsId,
+ 'selected' => $nsId === $params['selected'],
+ ), $nsName
+ );
}
$ret = '';
if ( isset( $params['label'] ) ) {
- $ret .= Xml::label( $params['label'], $selectAttribs['id'] ) . ' ';
+ $ret .= Html::element(
+ 'label', array(
+ 'for' => isset( $selectAttribs['id'] ) ? $selectAttribs['id'] : null,
+ ), $params['label']
+ ) . ' ';
}
// Wrap options in a <select>
function testNamespaceSelector() {
$this->assertEquals(
- '<select id="namespace" name="namespace">' . "\n" .
+ '<select>' . "\n" .
'<option value="0">(Main)</option>' . "\n" .
'<option value="1">Talk</option>' . "\n" .
'<option value="2">User</option>' . "\n" .
Html::namespaceSelector(),
'Basic namespace selector without custom options'
);
+
$this->assertEquals(
'<label for="mw-test-namespace">Select a namespace:</label> ' .
'<select id="mw-test-namespace" name="wpNamespace">' . "\n" .
),
'Basic namespace selector with custom values'
);
- }
- function testCanFilterOutNamespaces() {
- $this->assertEquals(
-'<select id="namespace" name="namespace">' . "\n" .
+ $this->assertEquals(
+ '<label>Select a namespace:</label> ' .
+'<select>' . "\n" .
+'<option value="0">(Main)</option>' . "\n" .
+'<option value="1">Talk</option>' . "\n" .
+'<option value="2">User</option>' . "\n" .
+'<option value="3">User talk</option>' . "\n" .
+'<option value="4">MyWiki</option>' . "\n" .
+'<option value="5">MyWiki Talk</option>' . "\n" .
+'<option value="6">File</option>' . "\n" .
+'<option value="7">File talk</option>' . "\n" .
+'<option value="8">MediaWiki</option>' . "\n" .
+'<option value="9">MediaWiki talk</option>' . "\n" .
+'<option value="10">Template</option>' . "\n" .
+'<option value="11">Template talk</option>' . "\n" .
+'<option value="14">Category</option>' . "\n" .
+'<option value="15">Category talk</option>' . "\n" .
+'<option value="100">Custom</option>' . "\n" .
+'<option value="101">Custom talk</option>' . "\n" .
+'</select>',
+ Html::namespaceSelector(
+ array( 'label' => 'Select a namespace:' )
+ ),
+ 'Basic namespace selector with a custom label but no id attribtue for the <select>'
+ );
+ }
+
+ function testCanFilterOutNamespaces() {
+ $this->assertEquals(
+'<select>' . "\n" .
'<option value="2">User</option>' . "\n" .
'<option value="4">MyWiki</option>' . "\n" .
'<option value="5">MyWiki Talk</option>' . "\n" .
),
'Namespace selector namespace filtering.'
);
- }
+ }
- function testCanDisableANamespaces() {
- $this->assertEquals(
-'<select id="namespace" name="namespace">' . "\n" .
+ function testCanDisableANamespaces() {
+ $this->assertEquals(
+'<select>' . "\n" .
'<option disabled="" value="0">(Main)</option>' . "\n" .
'<option disabled="" value="1">Talk</option>' . "\n" .
'<option disabled="" value="2">User</option>' . "\n" .
);
}
- function testNamespaceSelectorIdAndNameDefaultsAttributes() {
-
- $this->assertNsSelectorIdAndName(
- 'namespace', 'namespace',
- Html::namespaceSelector( array(), array(
- # neither 'id' nor 'name' key given
- )),
- "Neither 'id' nor 'name' key given"
- );
-
- $this->assertNsSelectorIdAndName(
- 'namespace', 'select_name',
- Html::namespaceSelector( array(), array(
- 'name' => 'select_name',
- # no 'id' key given
- )),
- "No 'id' key given, 'name' given"
- );
-
- $this->assertNsSelectorIdAndName(
- 'select_id', 'namespace',
- Html::namespaceSelector( array(), array(
- 'id' => 'select_id',
- # no 'name' key given
- )),
- "'id' given, no 'name' key given"
- );
-
- $this->assertNsSelectorIdAndName(
- 'select_id', 'select_name',
- Html::namespaceSelector( array(), array(
- 'id' => 'select_id',
- 'name' => 'select_name',
- )),
- "Both 'id' and 'name' given"
- );
- }
-
- /**
- * Helper to verify <select> attributes generated by Html::namespaceSelector()
- * This helper expect the Html method to use 'namespace' as a default value for
- * both 'id' and 'name' attributes.
- *
- * @param String $expectedId <select> id attribute value
- * @param String $expectedName <select> name attribute value
- * @param String $html Output of a call to Html::namespaceSelector()
- * @param String $msg Optional message (default: '')
- */
- function assertNsSelectorIdAndName( $expectedId, $expectedName, $html, $msg = '' ) {
- $actualId = 'namespace';
- if( 1 === preg_match( '/id="(.+?)"/', $html, $m ) ) {
- $actualId = $m[1];
- }
-
- $actualName = 'namespace';
- if( 1 === preg_match( '/name="(.+?)"/', $html, $m ) ) {
- $actualName = $m[1];
- }
- $this->assertEquals(
- array( #expected
- 'id' => $expectedId,
- 'name' => $expectedName,
- ),
- array( #actual
- 'id' => $actualId,
- 'name' => $actualName,
- ),
- 'Html::namespaceSelector() got wrong id and/or name attribute(s). ' . $msg
- );
- }
-
}