From dbf70515587dfcc03a722f460c700c8c7bb10826 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Sun, 21 Mar 2021 16:58:13 +0100 Subject: [PATCH] Core: Add `tabindex` to code blocks to enable keyboard navigation (#2799) --- components/prism-core.js | 6 ++++++ components/prism-core.min.js | 2 +- docs/Prism.hooks.html | 6 +++--- docs/Prism.html | 4 ++-- docs/Token.html | 8 ++++---- docs/global.html | 10 +++++----- docs/prism-core.js.html | 6 ++++++ prism.js | 6 ++++++ 8 files changed, 33 insertions(+), 15 deletions(-) diff --git a/components/prism-core.js b/components/prism-core.js index d7367099da..9a75c71741 100644 --- a/components/prism-core.js +++ b/components/prism-core.js @@ -558,6 +558,12 @@ var _ = { _.hooks.run('before-sanity-check', env); + // plugins may change/add the parent/element + parent = env.element.parentElement; + if (parent && parent.nodeName.toLowerCase() === 'pre' && !parent.hasAttribute('tabindex')) { + parent.setAttribute('tabindex', '0'); + } + if (!env.code) { _.hooks.run('complete', env); callback && callback.call(env.element); diff --git a/components/prism-core.min.js b/components/prism-core.min.js index 1723237c3c..e1a5842c3a 100644 --- a/components/prism-core.min.js +++ b/components/prism-core.min.js @@ -1 +1 @@ -var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(u){var c=/\blang(?:uage)?-([\w-]+)\b/i,n=0,e={},M={manual:u.Prism&&u.Prism.manual,disableWorkerMessageHandler:u.Prism&&u.Prism.disableWorkerMessageHandler,util:{encode:function e(n){return n instanceof W?new W(n.type,e(n.content),n.alias):Array.isArray(n)?n.map(e):n.replace(/&/g,"&").replace(/=l.reach);y+=m.value.length,m=m.next){var k=m.value;if(t.length>n.length)return;if(!(k instanceof W)){var b,x=1;if(h){if(!(b=z(p,y,n,f)))break;var w=b.index,A=b.index+b[0].length,P=y;for(P+=m.value.length;P<=w;)m=m.next,P+=m.value.length;if(P-=m.value.length,y=P,m.value instanceof W)continue;for(var S=m;S!==t.tail&&(Pl.reach&&(l.reach=N);var j=m.prev;O&&(j=I(t,j,O),y+=O.length),q(t,j,x);var C=new W(o,g?M.tokenize(E,g):E,d,E);if(m=I(t,j,C),L&&I(t,m,L),1l.reach&&(l.reach=_.reach)}}}}}}(e,a,n,a.head,0),function(e){var n=[],t=e.head.next;for(;t!==e.tail;)n.push(t.value),t=t.next;return n}(a)},hooks:{all:{},add:function(e,n){var t=M.hooks.all;t[e]=t[e]||[],t[e].push(n)},run:function(e,n){var t=M.hooks.all[e];if(t&&t.length)for(var r,a=0;r=t[a++];)r(n)}},Token:W};function W(e,n,t,r){this.type=e,this.content=n,this.alias=t,this.length=0|(r||"").length}function z(e,n,t,r){e.lastIndex=n;var a=e.exec(t);if(a&&r&&a[1]){var i=a[1].length;a.index+=i,a[0]=a[0].slice(i)}return a}function i(){var e={value:null,prev:null,next:null},n={value:null,prev:e,next:null};e.next=n,this.head=e,this.tail=n,this.length=0}function I(e,n,t){var r=n.next,a={value:t,prev:n,next:r};return n.next=a,r.prev=a,e.length++,a}function q(e,n,t){for(var r=n.next,a=0;a"+a.content+""},!u.document)return u.addEventListener&&(M.disableWorkerMessageHandler||u.addEventListener("message",function(e){var n=JSON.parse(e.data),t=n.language,r=n.code,a=n.immediateClose;u.postMessage(M.highlight(r,M.languages[t],t)),a&&u.close()},!1)),M;var t=M.util.currentScript();function r(){M.manual||M.highlightAll()}if(t&&(M.filename=t.src,t.hasAttribute("data-manual")&&(M.manual=!0)),!M.manual){var a=document.readyState;"loading"===a||"interactive"===a&&t&&t.defer?document.addEventListener("DOMContentLoaded",r):window.requestAnimationFrame?window.requestAnimationFrame(r):window.setTimeout(r,16)}return M}(_self);"undefined"!=typeof module&&module.exports&&(module.exports=Prism),"undefined"!=typeof global&&(global.Prism=Prism); \ No newline at end of file +var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(u){var c=/\blang(?:uage)?-([\w-]+)\b/i,n=0,e={},M={manual:u.Prism&&u.Prism.manual,disableWorkerMessageHandler:u.Prism&&u.Prism.disableWorkerMessageHandler,util:{encode:function e(n){return n instanceof W?new W(n.type,e(n.content),n.alias):Array.isArray(n)?n.map(e):n.replace(/&/g,"&").replace(/=l.reach);y+=m.value.length,m=m.next){var b=m.value;if(t.length>n.length)return;if(!(b instanceof W)){var k,x=1;if(h){if(!(k=z(v,y,n,f)))break;var w=k.index,A=k.index+k[0].length,P=y;for(P+=m.value.length;P<=w;)m=m.next,P+=m.value.length;if(P-=m.value.length,y=P,m.value instanceof W)continue;for(var E=m;E!==t.tail&&(Pl.reach&&(l.reach=N);var j=m.prev;O&&(j=I(t,j,O),y+=O.length),q(t,j,x);var C=new W(o,g?M.tokenize(S,g):S,d,S);if(m=I(t,j,C),L&&I(t,m,L),1l.reach&&(l.reach=_.reach)}}}}}}(e,a,n,a.head,0),function(e){var n=[],t=e.head.next;for(;t!==e.tail;)n.push(t.value),t=t.next;return n}(a)},hooks:{all:{},add:function(e,n){var t=M.hooks.all;t[e]=t[e]||[],t[e].push(n)},run:function(e,n){var t=M.hooks.all[e];if(t&&t.length)for(var r,a=0;r=t[a++];)r(n)}},Token:W};function W(e,n,t,r){this.type=e,this.content=n,this.alias=t,this.length=0|(r||"").length}function z(e,n,t,r){e.lastIndex=n;var a=e.exec(t);if(a&&r&&a[1]){var i=a[1].length;a.index+=i,a[0]=a[0].slice(i)}return a}function i(){var e={value:null,prev:null,next:null},n={value:null,prev:e,next:null};e.next=n,this.head=e,this.tail=n,this.length=0}function I(e,n,t){var r=n.next,a={value:t,prev:n,next:r};return n.next=a,r.prev=a,e.length++,a}function q(e,n,t){for(var r=n.next,a=0;a"+a.content+""},!u.document)return u.addEventListener&&(M.disableWorkerMessageHandler||u.addEventListener("message",function(e){var n=JSON.parse(e.data),t=n.language,r=n.code,a=n.immediateClose;u.postMessage(M.highlight(r,M.languages[t],t)),a&&u.close()},!1)),M;var t=M.util.currentScript();function r(){M.manual||M.highlightAll()}if(t&&(M.filename=t.src,t.hasAttribute("data-manual")&&(M.manual=!0)),!M.manual){var a=document.readyState;"loading"===a||"interactive"===a&&t&&t.defer?document.addEventListener("DOMContentLoaded",r):window.requestAnimationFrame?window.requestAnimationFrame(r):window.setTimeout(r,16)}return M}(_self);"undefined"!=typeof module&&module.exports&&(module.exports=Prism),"undefined"!=typeof global&&(global.Prism=Prism); \ No newline at end of file diff --git a/docs/Prism.hooks.html b/docs/Prism.hooks.html index 31e525f469..851bd8df2c 100644 --- a/docs/Prism.hooks.html +++ b/docs/Prism.hooks.html @@ -73,7 +73,7 @@

Source:
@@ -152,7 +152,7 @@

(static) addSource:
@@ -314,7 +314,7 @@

(static) runSource:
diff --git a/docs/Prism.html b/docs/Prism.html index b61e07f6a1..89b73d0c77 100644 --- a/docs/Prism.html +++ b/docs/Prism.html @@ -263,7 +263,7 @@

(static) hi
Source:
@@ -1157,7 +1157,7 @@

(static) tok
Source:
diff --git a/docs/Token.html b/docs/Token.html index 8bd43e28d3..d32782852f 100644 --- a/docs/Token.html +++ b/docs/Token.html @@ -80,7 +80,7 @@

new TokenSource:
@@ -364,7 +364,7 @@

aliasSource:
@@ -447,7 +447,7 @@

contentSource:
@@ -524,7 +524,7 @@

typeSource:
diff --git a/docs/global.html b/docs/global.html index 689a70efe4..afcfd9f0e6 100644 --- a/docs/global.html +++ b/docs/global.html @@ -143,7 +143,7 @@

Grammar

Source:
@@ -274,7 +274,7 @@

GrammarToken

Source:
@@ -559,7 +559,7 @@

High
Source:
@@ -713,7 +713,7 @@

HookCallb
Source:
@@ -859,7 +859,7 @@

TokenStream

Source:
diff --git a/docs/prism-core.js.html b/docs/prism-core.js.html index 9dbb4c0548..53f95c1263 100644 --- a/docs/prism-core.js.html +++ b/docs/prism-core.js.html @@ -611,6 +611,12 @@

prism-core.js

_.hooks.run('before-sanity-check', env); + // plugins may change/add the parent/element + parent = env.element.parentElement; + if (parent && parent.nodeName.toLowerCase() === 'pre' && !parent.hasAttribute('tabindex')) { + parent.setAttribute('tabindex', '0'); + } + if (!env.code) { _.hooks.run('complete', env); callback && callback.call(env.element); diff --git a/prism.js b/prism.js index 19e34a1463..1ba0d239c1 100644 --- a/prism.js +++ b/prism.js @@ -563,6 +563,12 @@ var _ = { _.hooks.run('before-sanity-check', env); + // plugins may change/add the parent/element + parent = env.element.parentElement; + if (parent && parent.nodeName.toLowerCase() === 'pre' && !parent.hasAttribute('tabindex')) { + parent.setAttribute('tabindex', '0'); + } + if (!env.code) { _.hooks.run('complete', env); callback && callback.call(env.element);