Add parser tests for bug 52468 and bug 52363.
[mediawiki.git] / resources / mediawiki.special / mediawiki.special.createAccount.js
blob5cbb1ee00185c90aaa8d1cd9b4b9c32b4758aa5d
1 /**
2  * JavaScript for Create account form (Special:UserLogin?type=signup).
3  */
4 ( function ( mw, $ ) {
6         // When sending password by email, hide the password input fields.
7         // This function doesn't need to be loaded early by ResourceLoader, but is tiny.
8         function hidePasswordOnEmail() {
9                 // Always required if checked, otherwise it depends, so we use the original
10                 var $emailLabel = $( 'label[for="wpEmail"]' ),
11                         originalText = $emailLabel.text(),
12                         requiredText = mw.message( 'createacct-emailrequired' ).text(),
13                         $createByMailCheckbox = $( '#wpCreateaccountMail' ),
14                         $beforePwds = $( '.mw-row-password:first' ).prev(),
15                         $pwds;
17                 function updateForCheckbox() {
18                         var checked = $createByMailCheckbox.prop( 'checked' );
19                         if ( checked ) {
20                                 $pwds = $( '.mw-row-password' ).detach();
21                                 $emailLabel.text( requiredText );
22                         } else {
23                                 if ( $pwds ) {
24                                         $beforePwds.after( $pwds );
25                                         $pwds = null;
26                                 }
27                                 $emailLabel.text( originalText );
28                         }
29                 }
31                 $createByMailCheckbox.on( 'change', updateForCheckbox );
32                 updateForCheckbox();
33         }
35         // Move the FancyCaptcha image into a more attractive container.
36         // This function does need to be run early by ResourceLoader.
37         function adjustFancyCaptcha() {
38                 var $content = $( '#mw-content-text' ),
39                         $submit = $content.find( '#wpCreateaccount' ),
40                         tabIndex,
41                         $captchaStuff,
42                         $captchaImageContainer,
43                         // JavaScript can't yet parse the message createacct-imgcaptcha-help when it
44                         // contains a MediaWiki transclusion, so PHP parses it and sends the HTML.
45                         helpMsg = mw.config.get( 'wgCreateacctImgcaptchaHelp' ),
46                         helpHtml = '';
48                 /*
49                  * CAPTCHA
50                  * The CAPTCHA is in a div style="captcha" at the top of the form.
51                  * If it's a FancyCaptcha, then we remove it and insert it lower down,
52                  * in a customized div with just what we need (e.g. no
53                  * fancycaptcha-createaccount message).
54                  */
55                 if ( !$submit.length) {
56                         return;
57                 }
58                 tabIndex = $submit.prop( 'tabindex' ) - 1;
59                 $captchaStuff = $content.find ( '.captcha' );
61                 if ( $captchaStuff.length ) {
63                         // The FancyCaptcha has this class in the ConfirmEdit extension
64                         // after 2013-04-18.
65                         $captchaImageContainer = $captchaStuff.find( '.fancycaptcha-image-container' );
66                         if ( $captchaImageContainer.length !== 1 ) {
67                                 return;
68                         }
70                         $captchaStuff.remove();
72                         if ( helpMsg) {
73                                 helpHtml = '<small class="mw-createacct-captcha-assisted">' + helpMsg + '</small>';
74                         }
76                         // Insert another div before the submit button that will include the
77                         // repositioned FancyCaptcha div, an input field, and possible help.
78                         $submit.closest( 'div' )
79                                 .before( [
80                         '<div>',
81                                 '<label for="wpCaptchaWord">' + mw.message( 'createacct-captcha' ).escaped() + '</label>',
82                                 '<div class="mw-createacct-captcha-container">',
83                                         '<div class="mw-createacct-captcha-and-reload" />',
84                                         '<input id="wpCaptchaWord" name="wpCaptchaWord" type="text" placeholder="' +
85                                                 mw.message( 'createacct-imgcaptcha-ph' ).escaped() +
86                                                 '" tabindex="' + tabIndex + '" autocapitalize="off" autocorrect="off">',
87                                                 helpHtml,
88                                 '</div>',
89                         '</div>'
90                                         ].join( '' )
91                                 );
93                         // Stick the FancyCaptcha container inside our bordered and framed parents.
94                         $captchaImageContainer
95                                 .prependTo( $content.find( '.mw-createacct-captcha-and-reload' ) );
97                         // Find the input field, add the text (if any) of the existing CAPTCHA
98                         // field (although usually it's blanked out on every redisplay),
99                         // and after it move over the hidden field that tells the CAPTCHA
100                         // what to do.
101                         $content.find( '#wpCaptchaWord' )
102                                 .val( $captchaStuff.find( '#wpCaptchaWord' ).val() )
103                                 .after( $captchaStuff.find( '#wpCaptchaId' ) );
104                 }
105         }
107         $( function () {
108                 adjustFancyCaptcha();
109                 hidePasswordOnEmail();
110         } );
112 }( mediaWiki, jQuery ) );