// Parse-or-refuse for pasted autoresponder forms (ported from Branded Voice lib/formrestyle.ts, 2026-09-29). // Keeps the MECHANICS (action, method, hidden fields, visible text/email fields), replaces the SKIN, escapes // everything. Anything that cannot be rebuilt faithfully is REFUSED with a reason; member markup is never // injected into a page as-is. 'use strict'; const EMBED_MAX_CHARS = 20000; const escAttr = s => String(s).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); function attr(tag, name) { const m = tag.match(new RegExp('\\b' + name + '\\s*=\\s*"([^"]*)"', 'i')) || tag.match(new RegExp('\\b' + name + "\\s*=\\s*'([^']*)'", 'i')) || tag.match(new RegExp('\\b' + name + '\\s*=\\s*([^\\s"\'>]+)', 'i')); return m ? m[1] : null; } const hasFlag = (tag, name) => new RegExp('\\b' + name + '\\b', 'i').test(tag); function derivePlaceholder(type, name) { const n = name.toLowerCase(); if (type === 'email' || n.includes('email')) return 'Your best email address'; if (n.includes('first')) return 'Your first name'; if (n.includes('last')) return 'Your last name'; if (n.includes('name')) return 'Your name'; return 'Your answer'; } function problem(embed) { const src = String(embed || '').trim(); if (!src) return null; if (src.length > EMBED_MAX_CHARS) return 'Form code is too long. Paste only the
block your provider gives you.'; if (//gi); if (!forms || !forms.length) return 'No tag was found in the form code.'; if (forms.length > 1) return 'Form code contains more than one form. Paste just the opt-in form.'; const formTag = forms[0].match(/]*>/i)[0]; const action = attr(formTag, 'action'); if (!action || !/^https?:\/\//i.test(action)) return 'The form has no https:// action URL to submit to.'; if (/]*>/gi) || []; let visible = 0; for (const tag of inputs) { const type = (attr(tag, 'type') || 'text').toLowerCase(); if (type === 'hidden' || type === 'submit' || type === 'button' || type === 'image') continue; if (type === 'text' || type === 'email') { if (!attr(tag, 'name')) return 'A visible field in the form has no name, so submissions would not reach your list.'; visible++; continue; } return 'The form uses a "' + type + '" field, which cannot be restyled. Use a form with text and email fields only.'; } if (!visible) return 'The form has no email or text field for the visitor to fill in.'; if (visible > 4) return 'The form has more than four visible fields. Opt-in forms with up to four fields are supported.'; return null; } // returns { action, method, charset, target, hidden:[{name,value}], fields:[{type,name,placeholder,required,value}], buttonText } or null function parse(embed) { if (problem(embed)) return null; const src = String(embed).trim(); const formHtml = src.match(//gi)[0]; const formTag = formHtml.match(/]*>/i)[0]; const action = attr(formTag, 'action'); const method = (attr(formTag, 'method') || 'post').toLowerCase() === 'get' ? 'get' : 'post'; const charset = attr(formTag, 'accept-charset'); const target = attr(formTag, 'target'); const hidden = [], fields = []; let buttonText = null; for (const tag of formHtml.match(/]*>/gi) || []) { const type = (attr(tag, 'type') || 'text').toLowerCase(); const name = attr(tag, 'name') || ''; if (type === 'hidden') { if (name) hidden.push({ name, value: attr(tag, 'value') || '' }); continue; } if (type === 'submit' || type === 'button' || type === 'image') { const v = attr(tag, 'value'); if (v && type === 'submit') buttonText = v; continue; } fields.push({ type, name, placeholder: attr(tag, 'placeholder') || derivePlaceholder(type, name), required: hasFlag(tag, 'required'), value: attr(tag, 'value') || '' }); } const btn = formHtml.match(/]*>([\s\S]*?)<\/button>/i); if (btn && !buttonText) buttonText = btn[1].replace(/<[^>]+>/g, '').trim() || null; return { action, method, charset, target, hidden, fields, buttonText }; } function html(parsed, defaultButton) { const open = ''; const hidden = parsed.hidden.map(h => '').join('\n'); const inputs = parsed.fields.map(f => '').join('\n'); return [open, hidden, inputs, '', ''].join('\n'); } module.exports = { EMBED_MAX_CHARS, problem, parse, html };