Commit 45e210ce authored by Eduardo Sanz García's avatar Eduardo Sanz García

Apply applyFocusVisiblePolyfill to shadow DOM elements

Follow instructions from:
https://github.com/WICG/focus-visible#shadow-dom
parent bd5b2de0
......@@ -45,5 +45,12 @@ export function createShadowRoot(container) {
const shadowRoot = container.attachShadow({ mode: 'open' });
loadStyles(shadowRoot);
// @ts-ignore The window doesn't know about the polyfill
const applyFocusVisible = window.applyFocusVisiblePolyfill;
if (applyFocusVisible) {
applyFocusVisible(shadowRoot);
}
return shadowRoot;
}
import { createShadowRoot } from '../shadow-root';
describe('annotator/util/shadow-root', () => {
let applyFocusVisiblePolyfill;
let container;
beforeEach(() => {
container = document.createElement('div');
applyFocusVisiblePolyfill = window.applyFocusVisiblePolyfill;
window.applyFocusVisiblePolyfill = sinon.stub();
});
afterEach(() => {
container.remove();
window.applyFocusVisiblePolyfill = applyFocusVisiblePolyfill;
});
describe('createShadowRoot', () => {
it('attaches a shadow root to the container', () => {
const container = document.createElement('div');
const shadowRoot = createShadowRoot(container);
assert.ok(shadowRoot);
......@@ -12,17 +23,13 @@ describe('annotator/util/shadow-root', () => {
});
it('does not attach a shadow root if Shadow DOM is unavailable', () => {
const container = document.createElement('div');
container.attachShadow = null;
const shadowRoot = createShadowRoot(container);
assert.equal(shadowRoot, container);
});
it('injects stylesheets into the shadow root', () => {
const container = document.createElement('div');
createShadowRoot(container);
const styleEl = container.shadowRoot.querySelector('style');
......@@ -30,9 +37,13 @@ describe('annotator/util/shadow-root', () => {
assert.match(styleEl.textContent, /@import ".*annotator\.css.*"/);
});
it('does not inject stylesheets into the shadow root if style is not found', () => {
const container = document.createElement('div');
it('applies the applyFocusVisiblePolyfill if exists', () => {
const shadowRoot = createShadowRoot(container);
assert.calledWith(window.applyFocusVisiblePolyfill, shadowRoot);
});
it('does not inject stylesheets into the shadow root if style is not found', () => {
const link = document.querySelector(
'link[rel="stylesheet"][href*="/build/styles/annotator.css"]'
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment