|
1 | 1 | /*! |
2 | | - * jQuery Validation Plugin v1.19.5 |
| 2 | + * jQuery Validation Plugin v1.20.0 |
3 | 3 | * |
4 | 4 | * https://jqueryvalidation.org/ |
5 | 5 | * |
6 | | - * Copyright (c) 2022 Jörn Zaefferer |
| 6 | + * Copyright (c) 2023 Jörn Zaefferer |
7 | 7 | * Released under the MIT license |
8 | 8 | */ |
9 | 9 | (function( factory ) { |
|
192 | 192 | data = $.validator.normalizeRules( |
193 | 193 | $.extend( |
194 | 194 | {}, |
195 | | - $.validator.metadataRules(element), |
196 | 195 | $.validator.classRules( element ), |
197 | 196 | $.validator.attributeRules( element ), |
198 | 197 | $.validator.dataRules( element ), |
|
775 | 774 | val = this.elementValue( element ), |
776 | 775 | result, method, rule, normalizer; |
777 | 776 |
|
| 777 | + // Abort any pending Ajax request from a previous call to this method. |
| 778 | + this.abortRequest( element ); |
| 779 | + |
778 | 780 | // Prioritize the local normalizer defined for this element over the global one |
779 | 781 | // if the former exists, otherwise user the global one in case it exists. |
780 | 782 | if ( typeof rules.normalizer === "function" ) { |
|
783 | 785 | normalizer = this.settings.normalizer; |
784 | 786 | } |
785 | 787 |
|
786 | | - // If normalizer is defined, then call it to the changed value instead |
| 788 | + // If normalizer is defined, then call it to retreive the changed value instead |
787 | 789 | // of using the real one. |
788 | 790 | // Note that `this` in the normalizer is `element`. |
789 | 791 | if ( normalizer ) { |
|
961 | 963 | error.removeClass( this.settings.validClass ).addClass( this.settings.errorClass ); |
962 | 964 |
|
963 | 965 | // Replace message on existing label |
964 | | - error.html( message ); |
| 966 | + if ( this.settings && this.settings.escapeHtml ) { |
| 967 | + error.text( message || "" ); |
| 968 | + } else { |
| 969 | + error.html( message || "" ); |
| 970 | + } |
965 | 971 | } else { |
966 | 972 |
|
967 | 973 | // Create error element |
968 | 974 | error = $( "<" + this.settings.errorElement + ">" ) |
969 | 975 | .attr( "id", elementID + "-error" ) |
970 | | - .addClass( this.settings.errorClass ) |
971 | | - .html( message || "" ); |
| 976 | + .addClass( this.settings.errorClass ); |
| 977 | + |
| 978 | + if ( this.settings && this.settings.escapeHtml ) { |
| 979 | + error.text( message || "" ); |
| 980 | + } else { |
| 981 | + error.html( message || "" ); |
| 982 | + } |
972 | 983 |
|
973 | 984 | // Maintain reference to the element to be placed into the DOM |
974 | 985 | place = error; |
|
1039 | 1050 | // 'aria-describedby' should directly reference the error element |
1040 | 1051 | if ( describer ) { |
1041 | 1052 | selector = selector + ", #" + this.escapeCssMeta( describer ) |
1042 | | - .replace( /\s+/g, ", #" ) + ":visible"; |
| 1053 | + .replace( /\s+/g, ", #" ); |
1043 | 1054 | } |
1044 | 1055 |
|
1045 | 1056 | return this |
|
1114 | 1125 | return !$.validator.methods.required.call( this, val, element ) && "dependency-mismatch"; |
1115 | 1126 | }, |
1116 | 1127 |
|
| 1128 | + elementAjaxPort: function( element ) { |
| 1129 | + return "validate" + element.name; |
| 1130 | + }, |
| 1131 | + |
1117 | 1132 | startRequest: function( element ) { |
1118 | 1133 | if ( !this.pending[ element.name ] ) { |
1119 | 1134 | this.pendingRequest++; |
|
1149 | 1164 | } |
1150 | 1165 | }, |
1151 | 1166 |
|
| 1167 | + abortRequest: function( element ) { |
| 1168 | + var port; |
| 1169 | + |
| 1170 | + if ( this.pending[ element.name ] ) { |
| 1171 | + port = this.elementAjaxPort( element ); |
| 1172 | + $.ajaxAbort( port ); |
| 1173 | + |
| 1174 | + this.pendingRequest--; |
| 1175 | + |
| 1176 | + // Sometimes synchronization fails, make sure pendingRequest is never < 0 |
| 1177 | + if ( this.pendingRequest < 0 ) { |
| 1178 | + this.pendingRequest = 0; |
| 1179 | + } |
| 1180 | + |
| 1181 | + delete this.pending[ element.name ]; |
| 1182 | + $( element ).removeClass( this.settings.pendingClass ); |
| 1183 | + } |
| 1184 | + }, |
| 1185 | + |
1152 | 1186 | previousValue: function( element, method ) { |
1153 | 1187 | method = typeof method === "string" && method || "remote"; |
1154 | 1188 |
|
|
1276 | 1310 | return rules; |
1277 | 1311 | }, |
1278 | 1312 |
|
1279 | | - metadataRules: function (element) { |
1280 | | - if (!$.metadata) { |
1281 | | - return {}; |
1282 | | - } |
1283 | | - |
1284 | | - var meta = $.data(element.form, 'validator').settings.meta; |
1285 | | - return meta ? |
1286 | | - $(element).metadata()[meta] : |
1287 | | - $(element).metadata(); |
1288 | | - }, |
1289 | | - |
1290 | 1313 | dataRules: function( element ) { |
1291 | 1314 | var rules = {}, |
1292 | 1315 | $element = $( element ), |
|
1600 | 1623 | data[ element.name ] = value; |
1601 | 1624 | $.ajax( $.extend( true, { |
1602 | 1625 | mode: "abort", |
1603 | | - port: "validate" + element.name, |
| 1626 | + port: this.elementAjaxPort( element ), |
1604 | 1627 | dataType: "json", |
1605 | 1628 | data: data, |
1606 | 1629 | context: validator.currentForm, |
|
1611 | 1634 | validator.settings.messages[ element.name ][ method ] = previous.originalMessage; |
1612 | 1635 | if ( valid ) { |
1613 | 1636 | submitted = validator.formSubmitted; |
1614 | | - validator.resetInternals(); |
1615 | 1637 | validator.toHide = validator.errorsFor( element ); |
1616 | 1638 | validator.formSubmitted = submitted; |
1617 | 1639 | validator.successList.push( element ); |
|
1636 | 1658 |
|
1637 | 1659 | // Ajax mode: abort |
1638 | 1660 | // usage: $.ajax({ mode: "abort"[, port: "uniqueport"]}); |
| 1661 | +// $.ajaxAbort( port ); |
1639 | 1662 | // if mode:"abort" is used, the previous request on that port (port can be undefined) is aborted via XMLHttpRequest.abort() |
1640 | 1663 |
|
1641 | 1664 | var pendingRequests = {}, |
|
1646 | 1669 | $.ajaxPrefilter( function( settings, _, xhr ) { |
1647 | 1670 | var port = settings.port; |
1648 | 1671 | if ( settings.mode === "abort" ) { |
1649 | | - if ( pendingRequests[ port ] ) { |
1650 | | - pendingRequests[ port ].abort(); |
1651 | | - } |
| 1672 | + $.ajaxAbort( port ); |
1652 | 1673 | pendingRequests[ port ] = xhr; |
1653 | 1674 | } |
1654 | 1675 | } ); |
|
1660 | 1681 | var mode = ( "mode" in settings ? settings : $.ajaxSettings ).mode, |
1661 | 1682 | port = ( "port" in settings ? settings : $.ajaxSettings ).port; |
1662 | 1683 | if ( mode === "abort" ) { |
1663 | | - if ( pendingRequests[ port ] ) { |
1664 | | - pendingRequests[ port ].abort(); |
1665 | | - } |
| 1684 | + $.ajaxAbort( port ); |
1666 | 1685 | pendingRequests[ port ] = ajax.apply( this, arguments ); |
1667 | 1686 | return pendingRequests[ port ]; |
1668 | 1687 | } |
1669 | 1688 | return ajax.apply( this, arguments ); |
1670 | 1689 | }; |
1671 | 1690 | } |
| 1691 | + |
| 1692 | +// Abort the previous request without sending a new one |
| 1693 | + $.ajaxAbort = function( port ) { |
| 1694 | + if ( pendingRequests[ port ] ) { |
| 1695 | + pendingRequests[ port ].abort(); |
| 1696 | + delete pendingRequests[ port ]; |
| 1697 | + } |
| 1698 | + }; |
1672 | 1699 | return $; |
1673 | 1700 | })); |
0 commit comments