Skip to content

Core: Add support for Web Components #2493

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

Merged
merged 1 commit into from
Jun 28, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .jscsrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"preset": "jquery",
"maximumLineLength": null,
"requireCamelCaseOrUpperCaseIdentifiers": null
"requireCamelCaseOrUpperCaseIdentifiers": null,
"excludeFiles": [
"test/custom-elements.js"
]
}
1 change: 1 addition & 0 deletions .jshintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ test/qunit/
dist/
demo/
*.min.js
test/custom-elements.js
12 changes: 11 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,17 @@ grunt.initConfig( {
}
},
qunit: {
files: "test/index.html"
files: "test/index.html",
options: {
puppeteer: {
args: [
"--headless",
"--disable-web-security",
"--allow-file-access-from-files"
]
},
timeout: 10000
}
},
jshint: {
options: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"grunt-contrib-concat": "1.0.1",
"grunt-contrib-copy": "1.0.0",
"grunt-contrib-jshint": "1.0.0",
"grunt-contrib-qunit": "1.2.0",
"grunt-contrib-qunit": "10.0.0",
"grunt-contrib-uglify": "1.0.1",
"grunt-contrib-watch": "1.0.0",
"grunt-jscs": "2.8.0",
Expand Down
20 changes: 11 additions & 9 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ $.extend( $.validator, {
onsubmit: true,
ignore: ":hidden",
ignoreTitle: false,
customElements: [],
onfocusin: function( element ) {
this.lastActive = element;

Expand Down Expand Up @@ -422,17 +423,17 @@ $.extend( $.validator, {
settings[ eventType ].call( validator, this, event );
}
}

var focusListeners = [ ":text", "[type='password']", "[type='file']", "select", "textarea", "[type='number']", "[type='search']",
"[type='tel']", "[type='url']", "[type='email']", "[type='datetime']", "[type='date']", "[type='month']",
"[type='week']", "[type='time']", "[type='datetime-local']", "[type='range']", "[type='color']",
"[type='radio']", "[type='checkbox']", "[contenteditable]", "[type='button']" ];
var clickListeners = [ "select", "option", "[type='radio']", "[type='checkbox']" ];
$( this.currentForm )
.on( "focusin.validate focusout.validate keyup.validate",
":text, [type='password'], [type='file'], select, textarea, [type='number'], [type='search'], " +
"[type='tel'], [type='url'], [type='email'], [type='datetime'], [type='date'], [type='month'], " +
"[type='week'], [type='time'], [type='datetime-local'], [type='range'], [type='color'], " +
"[type='radio'], [type='checkbox'], [contenteditable], [type='button']", delegate )
.on( "focusin.validate focusout.validate keyup.validate", focusListeners.concat( this.settings.customElements ).join( ", " ), delegate )

// Support: Chrome, oldIE
// "select" is provided as event.target when clicking a option
.on( "click.validate", "select, option, [type='radio'], [type='checkbox']", delegate );
.on( "click.validate", clickListeners.concat( this.settings.customElements ).join( ", " ), delegate );

if ( this.settings.invalidHandler ) {
$( this.currentForm ).on( "invalid-form.validate", this.settings.invalidHandler );
Expand Down Expand Up @@ -629,11 +630,12 @@ $.extend( $.validator, {

elements: function() {
var validator = this,
rulesCache = {};
rulesCache = {},
selectors = [ "input", "select", "textarea", "[contenteditable]" ];

// Select all valid inputs inside the form (no submit or reset buttons)
return $( this.currentForm )
.find( "input, select, textarea, [contenteditable]" )
.find( selectors.concat( this.settings.customElements ).join( ", " ) )
.not( ":submit, :reset, :image, :disabled" )
.not( this.settings.ignore )
.filter( function() {
Expand Down
17 changes: 17 additions & 0 deletions test/custom-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CustomTextElement extends HTMLElement {
static formAssociated = true;
static observedAttributes = ["name", "id"];

constructor() {
super();
this.internals_ = this.attachInternals();
}
get form() {
return this.internals_ != null ? this.internals_.form : null;
}
get name() {
return this.getAttribute("name");
}
}

window.customElements.define("custom-text", CustomTextElement);
4 changes: 4 additions & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<script src="../lib/jquery.mockjax.js"></script>
<script src="../dist/jquery.validate.js"></script>
<script src="../dist/additional-methods.js"></script>
<script src="custom-elements.js"></script>
<script src="test.js"></script>
<script src="rules.js"></script>
<script src="messages.js"></script>
Expand Down Expand Up @@ -472,6 +473,9 @@ <h3></h3>
</form>
<form id="escapeHtmlForm2">
<input name="escapeHtmlForm2text" id="escapeHtmlForm2text" data-rule-required="true" />
</form>
<form id="customElementsForm">
<custom-text name="customTextElement" id="customTextElement" />
</form>
</div>
</body>
Expand Down
21 changes: 21 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2786,3 +2786,24 @@ QUnit.test( "stopRequest() should submit the form once pendingRequests === 0", f
// Submit the form
$( button ).click();
} );

QUnit.test( "Assign rules to customElement via .validate() method", function( assert ) {
var form = $( "#customElementsForm" );
var v = form.validate( {
customElements: [ "custom-text" ],
rules: {
customTextElement: {
required: true
}
}
} );
var customTextElementRules = $( "#customTextElement", form ).rules();
var expectedRules = { required: true };

assert.deepEqual(
customTextElementRules, expectedRules, "The rules should be the same"
);

v.form();
assert.equal( v.numberOfInvalids(), 1, "The form has one error" );
} );
Loading