Add parser tests for bug 52468 and bug 52363.
[mediawiki.git] / resources / jquery / jquery.placeholder.js
blob8044d8803860df574d512f40a49843056c1963e8
1 /**
2  * HTML5 placeholder emulation for jQuery plugin
3  *
4  * This will automatically use the HTML5 placeholder attribute if supported, or emulate this behavior if not.
5  *
6  * @author Trevor Parscal <tparscal@wikimedia.org>, 2012
7  * @author Krinkle <krinklemail@gmail.com>, 2012
8  * @version 0.2.0
9  * @license GPL v2
10  */
11 ( function ( $ ) {
13         $.fn.placeholder = function ( text ) {
14                 var hasArg = arguments.length;
16                 return this.each( function () {
17                         var placeholder, $input;
19                         if ( hasArg ) {
20                                 this.setAttribute( 'placeholder', text );
21                         }
23                         // If the HTML5 placeholder attribute is supported, use it
24                         if ( this.placeholder && 'placeholder' in document.createElement( this.tagName ) ) {
25                                 return;
26                         }
28                         placeholder = hasArg ? text : this.getAttribute( 'placeholder' );
29                         $input = $(this);
31                         // Show initially, if empty
32                         if ( this.value === '' || this.value === placeholder ) {
33                                 $input.addClass( 'placeholder' ).val( placeholder );
34                         }
36                         $input
37                                 // Show on blur if empty
38                                 .blur( function () {
39                                         if ( this.value === '' ) {
40                                                 this.value = placeholder;
41                                                 $input.addClass( 'placeholder' );
42                                         }
43                                 } )
45                                 // Hide on focus
46                                 // Also listen for other events in case $input was
47                                 // already focused when the events were bound
48                                 .on( 'focus drop keydown paste', function ( e ) {
49                                         if ( $input.hasClass( 'placeholder' ) ) {
50                                                 if ( e.type === 'drop' && e.originalEvent.dataTransfer ) {
51                                                         // Support for drag&drop. Instead of inserting the dropped
52                                                         // text somewhere in the middle of the placeholder string,
53                                                         // we want to set the contents of the search box to the
54                                                         // dropped text.
56                                                         // IE wants getData( 'text' ) but Firefox wants getData( 'text/plain' )
57                                                         // Firefox fails gracefully with an empty string, IE barfs with an error
58                                                         try {
59                                                                 // Try the Firefox way
60                                                                 this.value = e.originalEvent.dataTransfer.getData( 'text/plain' );
61                                                         } catch ( exception ) {
62                                                                 // Got an exception, so use the IE way
63                                                                 this.value = e.originalEvent.dataTransfer.getData( 'text' );
64                                                         }
66                                                         // On Firefox, drop fires after the dropped text has been inserted,
67                                                         // but on IE it fires before. If we don't prevent the default action,
68                                                         // IE will insert the dropped text twice.
69                                                         e.preventDefault();
70                                                 } else {
71                                                         this.value = '';
72                                                 }
73                                                 $input.removeClass( 'placeholder' );
74                                         }
75                                 } );
77                         // Blank on submit -- prevents submitting with unintended value
78                         if ( this.form ) {
79                                 $( this.form ).submit( function () {
80                                         // $input.trigger( 'focus' ); would be problematic
81                                         // because it actually focuses $input, leading
82                                         // to nasty behavior in mobile browsers
83                                         if ( $input.hasClass( 'placeholder' ) ) {
84                                                 $input
85                                                         .val( '' )
86                                                         .removeClass( 'placeholder' );
87                                         }
88                                 });
89                         }
91                 });
92         };
94 }( jQuery ) );