File size: 2,258 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformMarkup = exports.parseAttributes = exports.stripTags = exports.createTagRegex = void 0;
/** Create a tag matching regexp. */
function createTagRegex(tagName, flags) {
    return new RegExp(`/<!--[^]*?-->|<${tagName}(\\s[^]*?)?(?:>([^]*?)<\\/${tagName}>|\\/>)`, flags);
}
exports.createTagRegex = createTagRegex;
/** Strip script and style tags from markup. */
function stripTags(markup) {
    return markup
        .replace(createTagRegex('style', 'gi'), '')
        .replace(createTagRegex('script', 'gi'), '');
}
exports.stripTags = stripTags;
/** Transform an attribute string into a key-value object */
function parseAttributes(attributesStr) {
    return attributesStr
        .split(/\s+/)
        .filter(Boolean)
        .reduce((acc, attr) => {
        const [name, value] = attr.split('=');
        // istanbul ignore next
        acc[name] = value ? value.replace(/['"]/g, '') : true;
        return acc;
    }, {});
}
exports.parseAttributes = parseAttributes;
async function transformMarkup({ content, filename }, transformer, options = {}) {
    let { markupTagName = 'template' } = options;
    markupTagName = markupTagName.toLocaleLowerCase();
    const markupPattern = createTagRegex(markupTagName);
    const templateMatch = content.match(markupPattern);
    /** If no <template> was found, run the transformer over the whole thing */
    if (!templateMatch || templateMatch.index == null) {
        return transformer({
            content,
            markup: content,
            attributes: {},
            filename,
            options,
        });
    }
    const [fullMatch, attributesStr = '', templateCode] = templateMatch;
    const attributes = parseAttributes(attributesStr);
    /** Transform the found template code */
    let { code, map, dependencies } = await transformer({
        content: templateCode,
        markup: templateCode,
        attributes,
        filename,
        options,
    });
    code =
        content.slice(0, templateMatch.index) +
            code +
            content.slice(templateMatch.index + fullMatch.length);
    return { code, map, dependencies };
}
exports.transformMarkup = transformMarkup;