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

Clean up extraneous attributes to pass HTML validation. #529

Merged
merged 3 commits into from
Jun 12, 2023
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
2 changes: 1 addition & 1 deletion dist/cookieconsent.esm.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cookieconsent.umd.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions src/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,9 @@ export const hide = () => {
toggleDisableInteraction();

/**
* Fix focus restoration
* Fix focus restoration to body with Chrome
*/
focus(_dom._focusSpan);
focus(_dom._focusSpan, false, true);

removeClass(_dom._htmlDom, TOGGLE_CONSENT_MODAL_CLASS);
setAttribute(_dom._cm, ARIA_HIDDEN, 'true');
Expand Down Expand Up @@ -327,9 +327,9 @@ export const hidePreferences = () => {
discardUnsavedPreferences();

/**
* Fix focus restoration
* Fix focus restoration to body with Chrome
*/
focus(globalObj._dom._pmFocusSpan);
focus(globalObj._dom._pmFocusSpan, false, true);

removeClass(globalObj._dom._htmlDom, TOGGLE_PREFERENCES_MODAL_CLASS);
setAttribute(globalObj._dom._pm, ARIA_HIDDEN, 'true');
Expand Down
1 change: 0 additions & 1 deletion src/core/modals/consentModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import { createPreferencesModal } from './preferencesModal';
*/
const createFocusSpan = () => {
const span = createNode('span');
span.tabIndex = -1;
orestbida marked this conversation as resolved.
Show resolved Hide resolved

if(!globalObj._dom._focusSpan)
globalObj._dom._focusSpan = span;
Expand Down
3 changes: 0 additions & 3 deletions src/core/modals/preferencesModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export const createPreferencesModal = (api, createMainContainer) => {
addEvent(dom._pmCloseBtn, CLICK_EVENT, hidePreferences);

dom._pmFocusSpan = createNode('span');
dom._pmFocusSpan.tabIndex = -1;
dom._pmFocusSpan.innerHTML = getSvgIcon();
appendChild(dom._pmCloseBtn, dom._pmFocusSpan);

Expand Down Expand Up @@ -360,7 +359,6 @@ export const createPreferencesModal = (api, createMainContainer) => {
*/
const trHeadFragment = dom._document.createDocumentFragment();
const trHead = createNode('tr');
setAttribute(trHead, 'role', 'row');

for(const headerKey of tableHeadersKeys){
const headerValue = headerData[headerKey];
Expand All @@ -385,7 +383,6 @@ export const createPreferencesModal = (api, createMainContainer) => {
for(const bodyItem of sCookieTableBody){

const tr = createNode('tr');
setAttribute(tr, 'role', 'row');
addClassPm(tr, 'table-tr');

for(const tdKey of tableHeadersKeys){
Expand Down
20 changes: 18 additions & 2 deletions src/utils/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,9 +725,19 @@ export const addDataButtonListeners = (elem, api, createPreferencesModal, create
/**
* @param {HTMLElement} el
* @param {1 | 2} [modalId]
* @param {boolean} [toggleTabIndex]
*/
export const focus = (el, modalId) => {
el && el.focus();
export const focus = (el, modalId, toggleTabIndex) => {

if(el){
/**
* Momentarily add the `tabindex` attribute to fix
* a bug with focus restoration in chrome
*/
toggleTabIndex && (el.tabIndex = -1);

el.focus();
}

if(modalId) {
globalObj._state._currentFocusedModal = modalId === 1
Expand All @@ -738,6 +748,12 @@ export const focus = (el, modalId) => {
? globalObj._state._cmFocusableElements
: globalObj._state._pmFocusableElements;
}

/**
* Remove the `tabindex` attribute so
* that the html markup is valid again
*/
toggleTabIndex && (el && el.removeAttribute('tabindex'));
};

/**
Expand Down