( html: string, tags: HtmlTagDescriptor[], prepend = false, )
| 1560 | } |
| 1561 | |
| 1562 | function injectToBody( |
| 1563 | html: string, |
| 1564 | tags: HtmlTagDescriptor[], |
| 1565 | prepend = false, |
| 1566 | ) { |
| 1567 | if (tags.length === 0) return html |
| 1568 | |
| 1569 | if (prepend) { |
| 1570 | // inject after body open |
| 1571 | if (bodyPrependInjectRE.test(html)) { |
| 1572 | return html.replace( |
| 1573 | bodyPrependInjectRE, |
| 1574 | (match, p1) => `${match}\n${serializeTags(tags, incrementIndent(p1))}`, |
| 1575 | ) |
| 1576 | } |
| 1577 | // if no there is no body tag, inject after head or fallback to prepend in html |
| 1578 | if (headInjectRE.test(html)) { |
| 1579 | return html.replace( |
| 1580 | headInjectRE, |
| 1581 | (match, p1) => `${match}\n${serializeTags(tags, p1)}`, |
| 1582 | ) |
| 1583 | } |
| 1584 | return prependInjectFallback(html, tags) |
| 1585 | } else { |
| 1586 | // inject before body close |
| 1587 | if (bodyInjectRE.test(html)) { |
| 1588 | return html.replace( |
| 1589 | bodyInjectRE, |
| 1590 | (match, p1) => `${serializeTags(tags, incrementIndent(p1))}${match}`, |
| 1591 | ) |
| 1592 | } |
| 1593 | // if no body tag is present, append to the html tag, or at the end of the file |
| 1594 | if (htmlInjectRE.test(html)) { |
| 1595 | return html.replace(htmlInjectRE, `${serializeTags(tags)}\n$&`) |
| 1596 | } |
| 1597 | return html + `\n` + serializeTags(tags) |
| 1598 | } |
| 1599 | } |
| 1600 | |
| 1601 | function prependInjectFallback(html: string, tags: HtmlTagDescriptor[]) { |
| 1602 | // prepend to the html tag, append after doctype, or the document start |
no test coverage detected