86 lines
6.3 KiB
JavaScript
86 lines
6.3 KiB
JavaScript
/* Reveal is a disposable projection. No slide DOM is ever saved as source. */
|
|
(() => {
|
|
'use strict';
|
|
const nonce=document.currentScript?.nonce||'', views=new WeakMap();
|
|
const allowed=new Set('p br hr div span h1 h2 h3 h4 h5 h6 blockquote pre code b strong i em u s del ins sub sup small mark kbd ul ol li dl dt dd table thead tbody tfoot tr th td caption figure figcaption section article a img'.split(' '));
|
|
const blocked=new Set('script style iframe object embed template form input button textarea select link meta base svg math audio video'.split(' '));
|
|
function slides(html,path){
|
|
if(new TextEncoder().encode(html).length>256*1024)throw Error('size');
|
|
const input=document.createElement('template');input.innerHTML=html;
|
|
const clean=document.createDocumentFragment();let count=0;
|
|
const base=new URL(path||'/',location.origin);
|
|
function copy(node,parent,depth=0){
|
|
if(++count>10000||depth>64)throw Error('size');
|
|
if(node.nodeType===Node.TEXT_NODE){parent.append(document.createTextNode(node.textContent));return;}
|
|
if(node.nodeType!==Node.ELEMENT_NODE)return;
|
|
const tag=node.localName;if(blocked.has(tag))return;
|
|
let target=parent;
|
|
if(allowed.has(tag)){
|
|
target=document.createElement(tag==='section'&&depth>0?'div':tag);parent.append(target);
|
|
// Never forward Reveal data-*, IDs, styles or event handlers from content.
|
|
for(const attr of ['title','alt','colspan','rowspan']){
|
|
const value=node.getAttribute(attr);
|
|
if(value!==null&&(!['colspan','rowspan'].includes(attr)||/^[1-9][0-9]{0,2}$/.test(value)))target.setAttribute(attr,value);
|
|
}
|
|
const attr=tag==='a'?'href':tag==='img'?'src':null;
|
|
if(attr&&node.hasAttribute(attr)){
|
|
try{const url=new URL(node.getAttribute(attr),base);
|
|
if(['http:','https:'].includes(url.protocol)&&!url.username&&!url.password&&(tag!=='img'||url.origin===location.origin)){
|
|
target.setAttribute(attr,url.href);
|
|
if(tag==='a'){target.target='_blank';target.rel='noopener noreferrer';}
|
|
else{target.loading='lazy';target.referrerPolicy='no-referrer';}
|
|
}
|
|
}catch{}
|
|
}
|
|
}
|
|
for(const child of node.childNodes)copy(child,target,depth+1);
|
|
}
|
|
for(const node of input.content.childNodes)copy(node,clean);
|
|
const result=[];let section=document.createElement('section');
|
|
const flush=()=>{if(section.textContent.trim()||section.querySelector('img,table,hr')){result.push(section);section=document.createElement('section');}};
|
|
for(const node of [...clean.childNodes]){
|
|
if(node.nodeName==='HR'){flush();}
|
|
else if(node.nodeName==='SECTION'){flush();result.push(node);}
|
|
else section.append(node);
|
|
}
|
|
flush();if(!result.length)result.push(section);if(result.length>100)throw Error('size');
|
|
return result;
|
|
}
|
|
function clear(host){const view=views.get(host);if(view){clearTimeout(view.timer);window.removeEventListener('message',view.listener);views.delete(host);}host.classList.remove('slides-preview');}
|
|
function render(host,html,path){
|
|
clear(host);
|
|
const sections=slides(html,path),doc=document.implementation.createHTMLDocument('Slides');
|
|
const token=crypto.randomUUID(),origin=location.origin;
|
|
const meta=doc.createElement('meta');meta.httpEquiv='Content-Security-Policy';
|
|
meta.content=`default-src 'none'; script-src 'nonce-${nonce}'; script-src-attr 'none'; style-src ${origin} 'unsafe-inline'; img-src ${origin}; connect-src 'none'; object-src 'none'; frame-src 'none'; base-uri 'none'; form-action 'none'`;
|
|
doc.head.prepend(meta);
|
|
for(const file of ['/static/vendor/reveal/reveal.css','/static/css/slides-frame.css']){const link=doc.createElement('link');link.rel='stylesheet';link.href=origin+file;doc.head.append(link);}
|
|
const colors=getComputedStyle(host),style=doc.createElement('style');
|
|
// Computed colors only; no authored CSS enters this isolated document.
|
|
style.textContent=`html,body,.reveal-viewport{background:${colors.backgroundColor==='rgba(0, 0, 0, 0)'?getComputedStyle(document.body).backgroundColor:colors.backgroundColor}!important;color:${colors.color}!important}`;doc.head.append(style);
|
|
const root=doc.createElement('div');root.className='reveal';const container=doc.createElement('div');container.className='slides';
|
|
for(const section of sections)container.append(doc.importNode(section,true));root.append(container);doc.body.append(root);
|
|
doc.body.dataset.token=token;doc.body.dataset.index=host.dataset.slideIndex||'0';
|
|
for(const file of ['/static/vendor/reveal/reveal.js','/static/js/slides-frame.js']){const script=doc.createElement('script');script.setAttribute('nonce',nonce);script.src=origin+file;doc.body.append(script);}
|
|
const frame=document.createElement('iframe');frame.className='slides-frame';frame.title=document.documentElement.lang.startsWith('ja')?'スライド':'Slides';
|
|
frame.setAttribute('sandbox','allow-scripts allow-popups');frame.setAttribute('allow','fullscreen');frame.referrerPolicy='no-referrer';
|
|
const fallback=document.createElement('div');fallback.className='slides-fallback';for(const section of sections)fallback.append(section);
|
|
// Keep a static projection available when the local runtime fails to load.
|
|
host.classList.add('slides-preview');host.replaceChildren(frame,fallback);
|
|
const listener=event=>{
|
|
if(event.source!==frame.contentWindow||event.data?.token!==token)return;
|
|
if(event.data.type==='slides-ready'){frame.dataset.ready='true';frame.dataset.count=String(sections.length);frame.hidden=false;fallback.hidden=true;clearTimeout(timer);}
|
|
if(['slides-index','slides-ready'].includes(event.data.type)&&Number.isInteger(event.data.index)&&event.data.index>=0&&event.data.index<sections.length)host.dataset.slideIndex=String(event.data.index);
|
|
};
|
|
window.addEventListener('message',listener);
|
|
const timer=setTimeout(()=>{if(frame.dataset.ready!=='true'){frame.hidden=true;fallback.hidden=false;}},8000);
|
|
views.set(host,{listener,timer});frame.srcdoc='<!doctype html>'+doc.documentElement.outerHTML;
|
|
}
|
|
window.PagesSlides={render,clear};
|
|
document.addEventListener('DOMContentLoaded',()=>{
|
|
const host=document.querySelector('[data-slides-viewer]');if(!host)return;
|
|
try{render(host,document.querySelector('[data-slides-source]').value,host.dataset.path);}catch{host.textContent=host.dataset.error;}
|
|
document.querySelector('[data-slides-fullscreen]')?.addEventListener('click',()=>{if(document.fullscreenElement)document.exitFullscreen?.();else host.requestFullscreen?.().catch(()=>{});});
|
|
});
|
|
})();
|