// These commands resolve Wiki paths only. They never run shell commands or open // user-supplied URL schemes, and all writes remain in the authorized editor. (() => { function resolvePath(value, current = '/') { if (/^[a-z][a-z0-9+.-]*:/i.test(value) || value.startsWith('//') || /[\\\u0000-\u001f\u007f]/.test(value)) { throw new Error('invalid_path'); } const parts = (value.startsWith('/') ? [] : current.split('/').filter(Boolean)); for (const part of value.split('/')) { if (!part || part === '.') continue; if (part === '..') parts.pop(); else parts.push(part); } return '/' + parts.join('/'); } function destination(value, current = '/') { value = value.trim(); let command = 'cd'; const match = /^(cd|vi|view)(?:\s+(.*))?$/s.exec(value); if (match) { command = match[1]; value = match[2] || (command === 'cd' ? '/' : current); } const path = resolvePath(value, current); // Each segment is literal. In particular #, ?, % and Unicode belong to the // Path, not URL syntax; never decode user input a second time. const encoded = path.split('/').map(encodeURIComponent).join('/'); return {command, path, href: command === 'vi' ? '/e/-' + encoded : command === 'view' ? encoded : '/v/browser' + encoded}; } if (typeof module !== 'undefined' && module.exports) module.exports = {resolvePath, destination}; if (typeof document === 'undefined') return; window.PagesWikiPaths = {resolvePath, destination}; const root = document.querySelector('[data-browser-address]'); if (!root) return; const current = root.dataset.path; const input = root.querySelector('input'); const error = document.getElementById('browser-address-error'); const navigate = value => { try { const target = destination(value, current); error.textContent = ''; location.assign(target.href); } catch (_) { error.textContent = root.dataset.invalidLabel; input.focus(); } }; root.addEventListener('submit', event => { event.preventDefault(); navigate('cd ' + input.value); }); input.addEventListener('keydown', event => { if (event.key === 'Escape') { input.value = current; error.textContent = ''; input.blur(); } }); input.addEventListener('input', () => { error.textContent = ''; }); document.querySelector('[data-browser-up]')?.addEventListener('click', () => navigate('cd ..')); const dialog = document.querySelector('[data-browser-create-dialog]'); const name = dialog.querySelector('[name="path"]'); const createError = dialog.querySelector('[role="alert"]'); let mode = 'vi'; document.querySelectorAll('[data-browser-create]').forEach(button => button.addEventListener('click', () => { mode = button.dataset.browserCreate; dialog.querySelector('h2').textContent = button.title; dialog.querySelector('[type="submit"]').textContent = mode === 'vi' ? root.dataset.editLabel : root.dataset.openLabel; name.value = ''; name.setCustomValidity(''); createError.textContent = ''; dialog.querySelector('[data-browser-format]').hidden = mode !== 'vi'; dialog.showModal(); name.focus(); })); dialog.querySelector('[data-browser-create-cancel]').addEventListener('click', () => dialog.close()); name.addEventListener('input', () => { name.setCustomValidity(''); createError.textContent = ''; }); dialog.querySelector('form').addEventListener('submit', event => { event.preventDefault(); try { if (!name.value.trim()) throw new Error('invalid_path'); const path = resolvePath(name.value.trim(), current); if (mode === 'vi' && path === '/') throw new Error('invalid_path'); let href = destination(mode + ' ' + path, current).href; if (mode === 'vi' && dialog.querySelector('[name="format"]').value === 'plain') href += '?edittype=plain'; location.assign(href); } catch (_) { createError.textContent = root.dataset.invalidLabel; name.setCustomValidity(root.dataset.invalidLabel); name.reportValidity(); } }); })();