Core: Extract the file name without including "C:\fakepath\"

For historical reasons, the value IDL attribute prefixes the file name
with the string "C:\fakepath\". As a result of this,
this fix will extract the file name from the value IDL attribute in a
backwards-compatible way.

For more details, see:
 - http://www.w3.org/TR/html5/forms.html#dom-input-value-filename
 - http://www.w3.org/TR/html5/forms.html#fakepath-srsly

Fixes #1615
This commit is contained in:
Brahim Arkni
2015-11-16 19:06:39 +00:00
parent 1289397f94
commit a336e14755
2 changed files with 77 additions and 3 deletions

View File

@@ -624,9 +624,9 @@ $.extend( $.validator, {
},
elementValue: function( element ) {
var val,
$element = $( element ),
type = element.type;
var $element = $( element ),
type = element.type,
val, idx;
if ( type === "radio" || type === "checkbox" ) {
return this.findByName( element.name ).filter( ":checked" ).val();
@@ -639,6 +639,31 @@ $.extend( $.validator, {
} else {
val = $element.val();
}
if ( type === "file" ) {
// Modern browser (chrome & safari)
if ( val.substr( 0, 12 ) === "C:\\fakepath\\" ) {
return val.substr( 12 );
}
// Legacy browsers
// Unix-based path
idx = val.lastIndexOf( "/" );
if ( idx >= 0 ) {
return val.substr( idx + 1 );
}
// Windows-based path
idx = val.lastIndexOf( "\\" );
if ( idx >= 0 ) {
return val.substr( idx + 1 );
}
// Just the file name
return val;
}
if ( typeof val === "string" ) {
return val.replace( /\r/g, "" );
}