(template)
| 204 | } |
| 205 | |
| 206 | function compileTemplate(template) { |
| 207 | const regex = /<%([=_#-]?|_)?([\s\S]*?)([-_]?%>)/g |
| 208 | let cursor = 0 |
| 209 | let trimLeadingWhitespace = false |
| 210 | const lines = [] |
| 211 | |
| 212 | function appendText(value) { |
| 213 | if (!value) { |
| 214 | return |
| 215 | } |
| 216 | lines.push(` __append(${JSON.stringify(value)})`) |
| 217 | } |
| 218 | |
| 219 | for (const match of template.matchAll(regex)) { |
| 220 | let text = template.slice(cursor, match.index) |
| 221 | if (trimLeadingWhitespace) { |
| 222 | text = text.replace(/^\s*\r?\n?/, '') |
| 223 | trimLeadingWhitespace = false |
| 224 | } |
| 225 | if (match[1] === '_') { |
| 226 | text = text.replace(/\s*$/, '') |
| 227 | } |
| 228 | appendText(text) |
| 229 | |
| 230 | const marker = match[1] || '' |
| 231 | const code = match[2] |
| 232 | const close = match[3] |
| 233 | |
| 234 | if (marker === '=') { |
| 235 | lines.push(` __append(__escapeXML(${stripSemicolon(code.trim())}))`) |
| 236 | } else if (marker === '-') { |
| 237 | lines.push(` __append(${stripSemicolon(code.trim())})`) |
| 238 | } else if (marker !== '#') { |
| 239 | lines.push(code) |
| 240 | } |
| 241 | |
| 242 | trimLeadingWhitespace = close.startsWith('-') || close.startsWith('_') |
| 243 | cursor = match.index + match[0].length |
| 244 | } |
| 245 | |
| 246 | let tail = template.slice(cursor) |
| 247 | if (trimLeadingWhitespace) { |
| 248 | tail = tail.replace(/^\s*\r?\n?/, '') |
| 249 | } |
| 250 | appendText(tail) |
| 251 | |
| 252 | return lines.join('\n') |
| 253 | } |
| 254 | |
| 255 | function createTemplateRendererSource(renderers = templateRenderers) { |
| 256 | const entries = Array.from(renderers.entries()).sort(([a], [b]) => |
no test coverage detected