// view.js
// Pure view functions
/**
* Pure view functions for the application.
*
* Why pure functions returning HTML strings? Because Elm does it, tbh.
* - Keeps rendering logic stateless and easy to test.
* - Ensures the UI is always a direct function of state, which should in theory totally avoid bugs from incremental DOM updates.
* - Using template literals is minimal and browser-native, with no dependencies, and is fun.
*
* Why escape output?
* - Prevents XSS and ensures all user/content data is safely rendered.
*
* Why semantic/accessible HTML?
* - Ensures the app is usable for all users, including those using assistive tech, and is easy to reason about.
*/
/**
* Render the app UI as an HTML string
* @param {object} state
* @returns {string}
*/
export function view(state) {
return `
';
return html;
}
function escape(str) {
/**
* Escapes HTML special characters to prevent XSS.
*
* Why escape here? Keeps all rendering safe by default, so no accidental injection is possible.
*/
return String(str).replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
}
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}