/* Drupal CKEditor 5 footnote widget: browser-ready bundle, no build required. */ (function () { const w = window; // Guard: CKEditor5 globals must exist. w.CKEditor5 = w.CKEditor5 || {}; w.CKEditor5.footnotepicker2 = w.CKEditor5.footnotepicker2 || {}; const { Plugin, ButtonView, Widget, toWidget, viewToModelPositionOutsideModelElement, DomEventObserver } = w.CKEDITOR || { ...w.CKEditor5.core, ...w.CKEditor5.ui, ...w.CKEditor5.widget, ...w.CKEditor5.engine }; if (!Plugin || !ButtonView || !Widget || !toWidget || !viewToModelPositionOutsideModelElement || !DomEventObserver) { // eslint-disable-next-line no-console console.warn('[bibcite_footnotes] CKEditor5 globals not found; FootnotePicker2 not registered.'); return; } // Sanitize ONLY the footnote body (not the article). // Allows: , , and plain text. function sanitizeFootnoteHtml(input) { if (!input) return ''; const allowedTags = new Set(['STRONG', 'EM', 'A']); const allowedAttrs = { A: new Set(['href', 'title', 'target', 'rel']), }; const template = document.createElement('template'); template.innerHTML = input; const cleanNode = (node) => { // Remove comments. if (node.nodeType === Node.COMMENT_NODE) { node.remove(); return; } // Text nodes are fine. if (node.nodeType === Node.TEXT_NODE) return; // Element nodes. if (node.nodeType === Node.ELEMENT_NODE) { // Strip disallowed elements but keep their text content. if (!allowedTags.has(node.tagName)) { const textNode = document.createTextNode(node.textContent || ''); node.replaceWith(textNode); return; } // Strip disallowed attributes. [...node.attributes].forEach((attr) => { const name = attr.name.toLowerCase(); const ok = allowedAttrs[node.tagName] && allowedAttrs[node.tagName].has(name); if (!ok) node.removeAttribute(attr.name); }); // Extra link safety. if (node.tagName === 'A') { const href = node.getAttribute('href') || ''; // Allow only http(s), mailto, and relative URLs. if (!/^(https?:|mailto:|\/)/i.test(href)) { node.removeAttribute('href'); } node.setAttribute('rel', 'noopener noreferrer'); } } // Recurse. [...node.childNodes].forEach(cleanNode); }; [...template.content.childNodes].forEach(cleanNode); return template.innerHTML; } function openFootnoteDialog(onSubmit, initial = {}) { const $ = w.jQuery; const Drupal = w.Drupal; // Fallback: prompt() if dialog isn't available. if (!Drupal || !Drupal.dialog || !$) { const reference = w.prompt('Reference ID (optional):', initial.reference || ''); const text = w.prompt('Free-form footnote text (optional):', initial.text || ''); const page = w.prompt('Page(s) (optional):', initial.page || ''); const value = w.prompt('Value (optional):', initial.value || ''); if ([reference, text, page, value].some(v => v === null)) return; onSubmit({reference: reference.trim(), text: text.trim(), page: page.trim(), value: value.trim()}); return; } const $wrapper = $('
'); // Row 1: reference selector $wrapper.append('
'); const $ref = $(` `); const references = w.drupalSettings?.bibcite_footnotes?.references || []; for (const [label, id] of references) { $ref.append($('