Skip to content
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

fix google earth issue with invalid radio buttons #21

Merged
merged 7 commits into from Feb 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion Gruntfile.js
Expand Up @@ -45,7 +45,10 @@ module.exports = function( grunt ) {
},
// code quality tasks
qunit: {
unit: [ 'test/**/*.html' ],
unit: [
'test/*.html',
'!test/google-earth.html'
],
// test other jquery versions
jquery: {
options: {
Expand Down
2 changes: 1 addition & 1 deletion bower.json
@@ -1,7 +1,7 @@
{
"name": "html5.constraintValidationAPI",
"title": "HTML5 constraintValidationAPI",
"version": "1.0.3",
"version": "1.0.4",
"homepage": "https://github.com/bboyle/html5-constraint-validation-API",
"authors": [
"Ben Boyle <benjamins.boyle@gmail.com>"
Expand Down
40 changes: 28 additions & 12 deletions dist/html5.constraintValidationAPI.js
@@ -1,6 +1,6 @@
/*! HTML5 constraintValidationAPI - v1.0.3 - 2014-04-03
* https://github.com/bboyle/html5-constraint-validation-API
* Copyright (c) 2014 Ben Boyle; Licensed MIT */
/*! HTML5 constraintValidationAPI - v1.0.4 - 2015-02-02
* https://github.com/bboyle/html5-constraint-validation-API
* Copyright (c) 2015 Ben Boyle; Licensed MIT */
/*exported initConstraintValidationAPI*/
if ( jQuery !== 'undefined' ) {
(function( $ ) {
Expand All @@ -15,10 +15,16 @@ if ( jQuery !== 'undefined' ) {
candidateForValidation = 'input, select, textarea',

// for feature detection
input = $( '<input>' ),
input = $( '<input>' ).get( 0 ),

// polyfill test
polyfill = typeof input[ 0 ].validity !== 'object',
polyfill = typeof input.validity !== 'object',


// invalid fields filter
isInvalid = function() {
return ! ( this.disabled || this.validity.valid );
},


// manage validity state object
Expand Down Expand Up @@ -146,14 +152,24 @@ if ( jQuery !== 'undefined' ) {
}

// NOTE the code below runs in all browsers to polyfill implementation bugs
// e.g. Opera 11 on OSX fires submit event even when fields are invalid

// google earth treats all required radio buttons as invalid
// if the only thing stopping submission is a required radio button group...
invalid = form.find( candidateForValidation ).filter( isInvalid );
if ( invalid.length === invalid.filter( ':radio' ).length && invalid.length === invalid.filter(function() {
// radio button has been checked, but is flagged as value missing
return this.validity.valueMissing && $( this.form.elements[ this.name ]).filter( ':checked' ).length > 0;
}).length ) {
// let submission continue
invalid.removeAttr( 'required' );
}

// Opera 11 on OSX fires submit event even when fields are invalid
// correct implementations will not invoke this submit handler until all fields are valid

// unless @novalidate
// if there are invalid fields
if ( ! novalidate && form.find( candidateForValidation ).filter(function() {
return ! ( this.disabled || this.validity.valid );
}).length > 0 ) {
if ( ! novalidate && form.find( candidateForValidation ).filter( isInvalid ).length > 0 ) {
// abort submit
event.stopImmediatePropagation();
event.preventDefault();
Expand Down Expand Up @@ -185,7 +201,7 @@ if ( jQuery !== 'undefined' ) {
}

// INPUT validitationMessage
if ( typeof input[ 0 ].validationMessage !== 'string' ) {
if ( typeof input.validationMessage !== 'string' ) {
// set us up the API
candidates.filter(function() {
return typeof this.validationMessage !== 'string';
Expand All @@ -195,7 +211,7 @@ if ( jQuery !== 'undefined' ) {
}

// INPUT checkValidity
if ( typeof input[ 0 ].checkValidity !== 'function' ) {
if ( typeof input.checkValidity !== 'function' ) {
// set us up the API
candidates.filter(function() {
return typeof this.checkValidity !== 'function';
Expand All @@ -217,7 +233,7 @@ if ( jQuery !== 'undefined' ) {
}

// INPUT setCustomValidity
if ( typeof input[ 0 ].setCustomValidity !== 'function' ) {
if ( typeof input.setCustomValidity !== 'function' ) {
// set us up the API
candidates.filter(function() {
return typeof this.setCustomValidity !== 'function';
Expand Down
6 changes: 3 additions & 3 deletions dist/html5.constraintValidationAPI.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion libs/jquery-loader.js
@@ -1,6 +1,6 @@
(function() {
// Default to the local version.
var path = '../bower_components/jquery/dist/jquery.js';
var path = '../node_modules/jquery/dist/jquery.js';
// Get any jquery=___ param from the query string.
var jqversion = location.search.match(/[?&]jquery=(.*?)(?=&|$)/);
// If a version was specified, use that version from code.jquery.com.
Expand Down
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -2,7 +2,7 @@
"name": "html5.constraintValidationAPI",
"title": "HTML5 constraintValidationAPI",
"description": "A polyfill for the HTML5 constraintValidationAPI",
"version": "1.0.3",
"version": "1.0.4",
"homepage": "https://github.com/bboyle/html5-constraint-validation-API",
"author": {
"name": "Ben Boyle",
Expand All @@ -29,6 +29,9 @@
"directories": {
"test": "test"
},
"dependencies": {
"jquery": "^2.1.3"
},
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-clean": "~0.6.0",
Expand Down
34 changes: 25 additions & 9 deletions src/html5.constraintValidationAPI.js
Expand Up @@ -19,10 +19,16 @@ if ( jQuery !== 'undefined' ) {
candidateForValidation = 'input, select, textarea',

// for feature detection
input = $( '<input>' ),
input = $( '<input>' ).get( 0 ),

// polyfill test
polyfill = typeof input[ 0 ].validity !== 'object',
polyfill = typeof input.validity !== 'object',


// invalid fields filter
isInvalid = function() {
return ! ( this.disabled || this.validity.valid );
},


// manage validity state object
Expand Down Expand Up @@ -150,14 +156,24 @@ if ( jQuery !== 'undefined' ) {
}

// NOTE the code below runs in all browsers to polyfill implementation bugs
// e.g. Opera 11 on OSX fires submit event even when fields are invalid

// google earth treats all required radio buttons as invalid
// if the only thing stopping submission is a required radio button group...
invalid = form.find( candidateForValidation ).filter( isInvalid );
if ( invalid.length === invalid.filter( ':radio' ).length && invalid.length === invalid.filter(function() {
// radio button has been checked, but is flagged as value missing
return this.validity.valueMissing && $( this.form.elements[ this.name ]).filter( ':checked' ).length > 0;
}).length ) {
// let submission continue
invalid.removeAttr( 'required' );
}

// Opera 11 on OSX fires submit event even when fields are invalid
// correct implementations will not invoke this submit handler until all fields are valid

// unless @novalidate
// if there are invalid fields
if ( ! novalidate && form.find( candidateForValidation ).filter(function() {
return ! ( this.disabled || this.validity.valid );
}).length > 0 ) {
if ( ! novalidate && form.find( candidateForValidation ).filter( isInvalid ).length > 0 ) {
// abort submit
event.stopImmediatePropagation();
event.preventDefault();
Expand Down Expand Up @@ -189,7 +205,7 @@ if ( jQuery !== 'undefined' ) {
}

// INPUT validitationMessage
if ( typeof input[ 0 ].validationMessage !== 'string' ) {
if ( typeof input.validationMessage !== 'string' ) {
// set us up the API
candidates.filter(function() {
return typeof this.validationMessage !== 'string';
Expand All @@ -199,7 +215,7 @@ if ( jQuery !== 'undefined' ) {
}

// INPUT checkValidity
if ( typeof input[ 0 ].checkValidity !== 'function' ) {
if ( typeof input.checkValidity !== 'function' ) {
// set us up the API
candidates.filter(function() {
return typeof this.checkValidity !== 'function';
Expand All @@ -221,7 +237,7 @@ if ( jQuery !== 'undefined' ) {
}

// INPUT setCustomValidity
if ( typeof input[ 0 ].setCustomValidity !== 'function' ) {
if ( typeof input.setCustomValidity !== 'function' ) {
// set us up the API
candidates.filter(function() {
return typeof this.setCustomValidity !== 'function';
Expand Down
77 changes: 77 additions & 0 deletions test/google-earth.html
@@ -0,0 +1,77 @@
<!DOCTYPE html>
<html lang="en-AU">
<head>
<meta charset="UTF-8">
<title>google earth tests · Constraint Validation API tests</title>

<!-- Load local jQuery. This can be overridden with a ?jquery=___ param. -->
<script src="../libs/jquery-loader.js"></script>
<!-- Load local lib and tests. -->
<script src="../src/html5.constraintValidationAPI.js"></script>

<style>
:invalid:before {
display: block;
content: "invalid";
color: red;
}
</style>
</head>

<body>


<p>This form is for testing validation in the google earth internal browser.
You should be able to submit this form (and be taken to this repo home page).</p>

<form action="https://github.com/bboyle/html5-constraint-validation-API" action>

<ol class="questions">

<li>
<fieldset>
<legend>
<span class="label">Radio foo</span>
<abbr title="(required)">*</abbr>
</legend>
<ul class="choices">
<li>
<input type="radio" name="radioFoo" value="foo" id="radio-foo-foo" required>
<label for="radio-foo-foo">Foo</label>
</li>
<li>
<input type="radio" name="radioFoo" value="bar" id="radio-foo-bar">
<label for="radio-foo-bar">Bar</label>
</li>
<li>
<input type="radio" name="radioFoo" value="" id="radio-foo-BLANK">
<label for="radio-foo-BLANK">BLANK</label>
</li>
<li>
<input type="radio" name="radioFoo" value="undefined" id="radio-foo-undefined">
<label for="radio-foo-undefined">undefined</label>
</li>
<li>
<input type="radio" name="radioFoo" value="null" id="radio-foo-null">
<label for="radio-foo-null">null</label>
</li>
</ul>
</fieldset>
</li>

<li class="footer">
<ol class="submit">
<li>
<strong>
<input type="submit" value="Submit">
</strong>
</li>
</ol>
</li>

</ol>
</form>


</body>
</html>
13 changes: 6 additions & 7 deletions test/required.html
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en-AU">
<head>
<meta charset="UTF-8" />
<meta charset="UTF-8">
<title>required tests · Constraint Validation API tests</title>

<!-- Load local jQuery. This can be overridden with a ?jquery=___ param. -->
Expand Down Expand Up @@ -73,19 +73,19 @@
<label for="radio-foo-foo">Foo</label>
</li>
<li>
<input type="radio" name="radioFoo" value="bar" id="radio-foo-bar" />
<input type="radio" name="radioFoo" value="bar" id="radio-foo-bar">
<label for="radio-foo-bar">Bar</label>
</li>
<li>
<input type="radio" name="radioFoo" value="" id="radio-foo-BLANK" />
<input type="radio" name="radioFoo" value="" id="radio-foo-BLANK">
<label for="radio-foo-BLANK">BLANK</label>
</li>
<li>
<input type="radio" name="radioFoo" value="undefined" id="radio-foo-undefined" />
<input type="radio" name="radioFoo" value="undefined" id="radio-foo-undefined">
<label for="radio-foo-undefined">undefined</label>
</li>
<li>
<input type="radio" name="radioFoo" value="null" id="radio-foo-null" />
<input type="radio" name="radioFoo" value="null" id="radio-foo-null">
<label for="radio-foo-null">null</label>
</li>
</ul>
Expand All @@ -109,7 +109,7 @@
<ol class="submit">
<li>
<strong>
<input type="submit" value="Submit" />
<input type="submit" value="Submit">
</strong>
</li>
</ol>
Expand All @@ -119,6 +119,5 @@
</form>
</div>


</body>
</html>