Followup r79707, nicer whitespace. Beyond this, I'm beyond caring...
[lhc/web/wiklou.git] / resources / mediawiki.special / mediawiki.special.preferences.js
1 /*
2 * JavaScript for Special:Preferences
3 */
4
5 $( '#prefsubmit' ).attr( 'id', 'prefcontrol' );
6 $( '#preferences' )
7 .addClass( 'jsprefs' )
8 .before( $( '<ul id="preftoc"></ul>' ) )
9 .children( 'fieldset' )
10 .hide()
11 .addClass( 'prefsection' )
12 .children( 'legend' )
13 .addClass( 'mainLegend' )
14 .each( function( i ) {
15 $(this).parent().attr( 'id', 'prefsection-' + i );
16 if ( i === 0 ) {
17 $(this).parent().show();
18 }
19 $( '#preftoc' ).append(
20 $( '<li></li>' )
21 .addClass( i === 0 ? 'selected' : null )
22 .append(
23 $( '<a></a>')
24 .text( $(this).text() )
25 .attr( 'href', '#prefsection-' + i )
26 .mousedown( function( e ) {
27 $(this).parent().parent().find( 'li' ).removeClass( 'selected' );
28 $(this).parent().addClass( 'selected' );
29 e.preventDefault();
30 return false;
31 } )
32 .click( function( e ) {
33 $( '#preferences > fieldset' ).hide();
34 $( '#prefsection-' + i ).show();
35 e.preventDefault();
36 return false;
37 } )
38 )
39 );
40 } );
41
42 // Lame tip to let user know if its email is valid. See bug 22449
43 $( '#mw-input-emailaddress' )
44 .keyup( function () {
45 if( $( "#mw-emailaddress-validity" ).length == 0 ) {
46 $(this).after( '<label for="mw-input-emailaddress" id="mw-emailaddress-validity"></label>' );
47 }
48 var isValid = wfValidateEmail( $(this).val() );
49 var class_to_add = isValid ? 'valid' : 'invalid';
50 var class_to_remove = isValid ? 'invalid' : 'valid';
51 $( '#mw-emailaddress-validity' )
52 .text( isValid ? 'Looks valid' : 'Valid address required!' )
53 .addClass( class_to_add )
54 .removeClass( class_to_remove );
55 } );
56
57 /**
58 * Validate a string as representing a valid e-mail address
59 * according to HTML5 specification. Please note the specification
60 * does not validate a domain with one character.
61 *
62 * FIXME: should be moved to a JavaScript validation module.
63 */
64 wfValidateEmail = function( mailtxt ) {
65 if( mailtxt == '' ) { return null; }
66
67 /**
68 * HTML 5 define a string as valid e-mail address if it matches
69 * the ABNF :
70 * 1 * ( atext / "." ) "@" ldh-str 1*( "." ldh-str )
71 * With:
72 * - atext : defined in RFC 5322 section 3.2.3
73 * - ldh-str : defined in RFC 1034 section 3.5
74 *
75 * (see STD 68 / RFC 5234 http://tools.ietf.org/html/std68):
76 */
77
78 /**
79 * First, define the RFC 5322 'atext' which is pretty easy :
80 * atext = ALPHA / DIGIT / ; Printable US-ASCII
81 "!" / "#" / ; characters not including
82 "$" / "%" / ; specials. Used for atoms.
83 "&" / "'" /
84 "*" / "+" /
85 "-" / "/" /
86 "=" / "?" /
87 "^" / "_" /
88 "`" / "{" /
89 "|" / "}" /
90 "~"
91 */
92 var rfc5322_atext = "a-z0-9!#$%&'*+-/=?^_`{|}—~" ;
93
94 /**
95 * Next define the RFC 1034 'ldh-str'
96 * <domain> ::= <subdomain> | " "
97 * <subdomain> ::= <label> | <subdomain> "." <label>
98 * <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]
99 * <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>
100 * <let-dig-hyp> ::= <let-dig> | "-"
101 * <let-dig> ::= <letter> | <digit>
102 */
103 var rfc1034_ldh_str = "a-z0-9-" ;
104
105 var HTML5_email_regexp = new RegExp(
106 // start of string
107 '^'
108 +
109 // User part which is liberal :p
110 '[' + rfc5322_atext + '\\.' + ']' + '+'
111 +
112 // "apostrophe"
113 '@'
114 +
115 // Domain first part
116 '[' + rfc1034_ldh_str + ']+'
117 +
118 // Second part and following are separated by a dot
119 '(\\.[' + rfc1034_ldh_str + ']+)+'
120 +
121 // End of string
122 '$',
123 // RegExp is case insensitive
124 'i'
125 );
126 return mailtxt.match( HTML5_email_regexp );
127 };