diff --git a/src/ssr.js b/src/ssr.js new file mode 100644 index 00000000..c58457ee --- /dev/null +++ b/src/ssr.js @@ -0,0 +1,155 @@ +import { encodeEntities, styleObjToCss, assign } from './util'; +import { options } from 'preact'; + +const VOID_ELEMENTS = /^(area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/; + +const UNSAFE_NAME = /[\s\n\\/='"\0<>]/; + +/** + * Render Preact JSX + Components to an HTML string. + * @param {import('preact').VNode|string|number|null|boolean} vnode any renderable value + * @param {object} context context object, forks throughout the tree + * @param {any} selectValue the current select value, passed down through the tree to set + */ +export default function renderToString(vnode, context, selectValue) { + if (vnode == null || vnode === false || vnode === true) { + return ''; + } + + if (typeof vnode !== 'object') { + return encodeEntities(vnode); + } + + context = context || {}; + + if (Array.isArray(vnode)) { + let s = ''; + for (let i=0; i String(s) - .replace(/&/g, '&') - .replace(//g, '>') - .replace(/"/g, '"'); +const ENC = {}; + +export function encodeEntities(s) { + s = String(s); + return ENC[s] || (ENC[s] = s + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"') + ); +} export let indent = (s, char) => String(s).replace(/(\n+)/g, '$1' + (char || '\t'));