Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | // Playground functionality - loaded as a separate script console.log('Loading playground functionality...'); // Safe component parser - converts JSON to Coherent.js components function parseComponentJSON(jsonString) { try { const parsed = JSON.parse(jsonString); return validateAndNormalizeComponent(parsed); } catch (error) { throw new Error(`Invalid JSON: ${error.message}`); } } function validateAndNormalizeComponent(obj) { if (obj === null || obj === undefined) { return null; } if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean') { return obj; } if (Array.isArray(obj)) { return obj.map(item => validateAndNormalizeComponent(item)); } if (typeof obj === 'object') { const result = {}; for (const [key, value] of Object.entries(obj)) { // Only allow safe HTML elements and properties if (isSafeElement(key) || isSafeProperty(key)) { result[key] = validateAndNormalizeComponent(value); } } return result; } return obj; } function isSafeElement(tagName) { const safeTags = [ 'div', 'span', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ul', 'ol', 'li', 'em', 'strong', 'a', 'img', 'br', 'section', 'article', 'header', 'footer', 'nav', 'main', 'button', 'input', 'select', 'option', 'textarea', 'form', 'table', 'tr', 'td', 'th', 'thead', 'tbody', 'tfoot' ]; return safeTags.includes(tagName.toLowerCase()); } function isSafeProperty(propName) { const safeProps = [ 'text', 'children', 'style', 'className', 'class', 'id', 'href', 'src', 'alt', 'title', 'placeholder', 'value', 'type', 'name', 'for', 'colspan', 'rowspan' ]; return safeProps.includes(propName); } // Simple browser-compatible renderToString function function browserRenderToString(component) { if (!component) return ''; if (typeof component === 'string') return component; if (typeof component === 'number' || typeof component === 'boolean') return String(component); if (Array.isArray(component)) return component.map(browserRenderToString).join(''); if (typeof component === 'object') { const tagName = Object.keys(component)[0]; const props = component[tagName] || {}; if (typeof props === 'string') return `<${tagName}>${props}</${tagName}>`; let html = `<${tagName}`; // Add attributes Object.keys(props).forEach(key => { if (key !== 'children' && key !== 'text') { html += ` ${key}="${props[key]}"`; } }); html += '>'; // Add text content if (props.text) { html += props.text; } // Add children if (props.children) { if (Array.isArray(props.children)) { html += props.children.map(browserRenderToString).join(''); } else { html += browserRenderToString(props.children); } } html += `</${tagName}>`; return html; } return ''; } // Simple DOM renderer function browserRenderToDOM(component, container) { if (!container) return; const html = browserRenderToString(component); container.innerHTML = html; } // Global playground function window.runPlaygroundComponent = function() { console.log('runPlaygroundComponent called!'); const codeEl = document.getElementById('code'); const outputEl = document.getElementById('output'); const previewEl = document.getElementById('preview'); const sourceEl = document.getElementById('source'); console.log('Elements found:', { codeEl, outputEl, previewEl, sourceEl }); if (!codeEl) { console.error('Code element not found!'); return; } const setStatus = (message) => { if (outputEl) { outputEl.textContent = message; outputEl.className = 'output-status'; } }; const setError = (message) => { if (outputEl) { outputEl.textContent = `Error: ${message}`; outputEl.className = 'output-error'; } if (previewEl) previewEl.innerHTML = ''; if (sourceEl) sourceEl.textContent = ''; }; const setSuccess = (component, html) => { if (outputEl) { outputEl.textContent = 'Component rendered successfully!'; outputEl.className = 'output-success'; } // Clear and render the preview if (previewEl) { try { browserRenderToDOM(component, previewEl); } catch (domError) { console.error('DOM render error:', domError); previewEl.innerHTML = html; // Fallback to HTML } } // Show the generated HTML source if (sourceEl) { sourceEl.textContent = html; } }; try { setStatus('Parsing component...'); const userInput = codeEl.value.trim(); if (!userInput) { setError('No component definition provided'); return; } console.log('User input:', userInput); // Parse the JSON component definition safely const component = parseComponentJSON(userInput); console.log('Parsed component:', component); if (!component) { setError('Component definition is empty'); return; } setStatus('Rendering component...'); // Render to HTML string using browser-compatible function const html = browserRenderToString(component); console.log('Generated HTML:', html); setSuccess(component, html); } catch (error) { console.error('Playground execution error:', error); setError(error.message); } }; console.log('Playground functionality loaded successfully!'); |