Skip to content
Open
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
30 changes: 24 additions & 6 deletions js/formidable.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@
response = defaultResponse;
} else {
// Response is a string. Convert it to an object.
response = response.replace( /^\s+|\s+$/g, '' );

Check failure on line 896 in js/formidable.js

View workflow job for this annotation

GitHub Actions / Run ESLint

Simplify this regular expression to reduce its runtime, as it has super-linear performance due to backtracking

Check failure on line 896 in js/formidable.js

View workflow job for this annotation

GitHub Actions / Run ESLint

Simplify this regular expression to reduce its runtime, as it has super-linear performance due to backtracking
if ( response.indexOf( '{' ) === 0 ) {
response = JSON.parse( response );
} else {
Expand Down Expand Up @@ -1154,6 +1154,24 @@
return kvp.join( '&' );
}

/**
* Inserts error HTML into a field's container, tagging every inserted top-level
* element with a data-frm-error attribute. removeFieldError()/removeAllErrors() rely
* on that attribute (rather than the frm_error class) to find and remove it again,
* since a site's own custom field HTML template can render the [error] placeholder
* without a frm_error class or id, and errors that can't be found never get removed.
*
* @param {HTMLElement} container
* @param {string} errorHtml
* @return {void}
*/
function insertErrorHtml( container, errorHtml ) {
const template = document.createElement( 'template' );
template.innerHTML = errorHtml;
Array.from( template.content.children ).forEach( el => el.setAttribute( 'data-frm-error', '' ) );
container.append( template.content );
}

function addFieldError( $fieldCont, key, jsErrors ) {
const container = $fieldCont instanceof jQuery ? $fieldCont.get( 0 ) : $fieldCont;

Expand All @@ -1177,7 +1195,7 @@
const roleString = frm_js.include_alert_role ? 'role="alert"' : '';
errorHtml = `<div class="frm_error" ${ roleString } id="${ id }">${ jsErrors[ key ] }</div>`;
}
container.insertAdjacentHTML( 'beforeend', errorHtml );
insertErrorHtml( container, errorHtml );
inputs.forEach( input => {
describedBy = input.getAttribute( 'aria-describedby' );
if ( ! describedBy ) {
Expand Down Expand Up @@ -1236,7 +1254,7 @@
return;
}

const errorMessage = container.querySelector( '.frm_error' );
const errorMessages = container.querySelectorAll( '.frm_error, [data-frm-error]' );
const input = container.querySelector( 'input, select, textarea' );

container.classList.remove( 'frm_blank_field', 'has-error' );
Expand All @@ -1252,10 +1270,10 @@
}
}

if ( errorMessage ) {
errorMessages.forEach( errorMessage => {
removeElementFromInputDescribedBy( errorMessage );
errorMessage.remove();
}
} );
}

/**
Expand Down Expand Up @@ -1286,7 +1304,7 @@
document.querySelectorAll( '.form-field' ).forEach( field => {
field.classList.remove( 'frm_blank_field', 'has-error' );
} );
document.querySelectorAll( '.form-field .frm_error' ).forEach( el => {
document.querySelectorAll( '.form-field .frm_error, .form-field [data-frm-error]' ).forEach( el => {
removeElementFromInputDescribedBy( el );
el.remove();
} );
Expand Down Expand Up @@ -1438,7 +1456,7 @@
return;
}

const errors = document.querySelectorAll( '.frm_form_field .frm_error' );
const errors = document.querySelectorAll( '.frm_form_field .frm_error, .frm_form_field [data-frm-error]' );
if ( ! errors.length ) {
return;
}
Expand Down Expand Up @@ -2284,7 +2302,7 @@
: price.split( options.decimal_separator );

if ( options.thousand_separator ) {
split[ 0 ] = split[ 0 ].replace( /\B(?=(\d{3})+(?!\d))/g, options.thousand_separator );

Check failure on line 2305 in js/formidable.js

View workflow job for this annotation

GitHub Actions / Run ESLint

Simplify this regular expression to reduce its runtime, as it has super-linear performance due to backtracking

Check failure on line 2305 in js/formidable.js

View workflow job for this annotation

GitHub Actions / Run ESLint

Simplify this regular expression to reduce its runtime, as it has super-linear performance due to backtracking
}

return split.join( options.decimal_separator );
Expand Down
Loading