Skip to content

Squashed/rebased #1373 #1531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/additional/accept.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
$.validator.addMethod( "accept", function( value, element, param ) {

// Split mime on commas in case we have multiple types we can accept
var typeParam = typeof param === "string" ? param.replace( /\s/g, "" ).replace( /,/g, "|" ) : "image/*",
var typeParam = typeof param === "string" ? param.replace( /\s/g, "" ) : "image/*",
optionalValue = this.optional( element ),
i, file;
i, file, regex;

// Element is optional
if ( optionalValue ) {
Expand All @@ -13,16 +13,19 @@ $.validator.addMethod( "accept", function( value, element, param ) {

if ( $( element ).attr( "type" ) === "file" ) {

// If we are using a wildcard, make it regex friendly
typeParam = typeParam.replace( /\*/g, ".*" );
// Escape string to be used in the regex
// see: http://stackoverflow.com/questions/3446170/escape-string-for-use-in-javascript-regex
// Escape also "/*" as "/.*" as a wildcard
typeParam = typeParam.replace( /[\-\[\]\/\{\}\(\)\+\?\.\\\^\$\|]/g, "\\$&" ).replace( /,/g, "|" ).replace( "\/*", "/.*" );

// Check if the element has a FileList before checking each file
if ( element.files && element.files.length ) {
regex = new RegExp( ".?(" + typeParam + ")$", "i" );
for ( i = 0; i < element.files.length; i++ ) {
file = element.files[ i ];

// Grab the mimetype from the loaded file, verify it matches
if ( !file.type.match( new RegExp( "\\.?(" + typeParam + ")$", "i" ) ) ) {
if ( !file.type.match( regex ) ) {
return false;
}
}
Expand Down
54 changes: 54 additions & 0 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@ function methodTest( methodName ) {
};
}

/**
* Creates a dummy DOM file input with FileList
* @param filename
* @param mimeType
* @returns {{}}
*/
function acceptFileDummyInput( filename, mimeType ) {

function dummy() {
return file;
}

// https://developer.mozilla.org/en-US/docs/Web/API/FileList
var file = {
name: filename,
size: 500001,
type: mimeType
},
fileList = {
0: file,
length: 1,
item: dummy
};

return {
type: "file",
files: fileList,
nodeName: "INPUT",
value: "/tmp/fake_value",
hasAttribute: function() { return false; }
};
}

module( "methods" );

test( "default messages", function() {
Expand Down Expand Up @@ -1309,3 +1342,24 @@ test( "cpfBR", function() {
ok( !method( "11144477715" ), "Invalid CPF Number: 1st check number failed" );
ok( !method( "11144477737" ), "Invalid CPF Number: 2nd check number failed" );
} );

test( "file accept - image wildcard", function() {
var input = acceptFileDummyInput( "test.png", "image/png" ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.accept, new $.validator( {}, $form[ 0 ] ), null, input, "image/*" );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this $.proxy come from?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was in the initial PR (#1373) but it's defined via jQuery and essential is ES5's Function.bind:

https://api.jquery.com/jQuery.proxy/

ok( proxy(), "the selected file for upload has specified mime type" );
} );

test( "file accept - specified mime type", function() {
var input = acceptFileDummyInput( "test.kml", "application/vnd.google-earth.kml+xml" ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.accept, new $.validator( {}, $form[ 0 ] ), null, input, "application/vnd.google-earth.kml+xml" );
ok( proxy(), "the selected file for upload has specified mime type" );
} );

test( "file accept - invalid mime type", function() {
var input = acceptFileDummyInput( "test.kml", "foobar/vnd.google-earth.kml+xml" ),
$form = $( "<form />" ),
proxy = $.proxy( $.validator.methods.accept, new $.validator( {}, $form[ 0 ] ), null, input, "application/vnd.google-earth.kml+xml" );
equal( proxy(), false, "the selected file for upload has invalid mime type" );
} );