node_modules/svelte2tsx/index.js
/**
 * Handle various kinds of attributes and make them conform to JSX.
 * - {x}   --->    x={x}
 * - x="{..}"   --->    x={..}
 * - lowercase DOM attributes
 * - multi-value handling
 */
Handle various kinds of attributes and make them conform to JSX.
Handle various kinds of attributes and make them conform to JSX .
function handleAttribute(htmlx, str, attr, parent, preserveCase) {
    let transformedFromDirectiveOrNamespace = false;
    const transformAttributeCase = (name) => {
        if (!preserveCase && !svgAttributes.find((x) => x == name)) {
            return name.toLowerCase();
        }
        else {
            return name;
        }
    };
    if (parent.type == 'Element') {
        const sapperLinkActions = ['sapper:prefetch', 'sapper:noscroll'];
        const sveltekitLinkActions = ['sveltekit:prefetch', 'sveltekit:noscroll'];
        if ((attr.value !== true &&
            !(attr.value.length &&
                attr.value.length == 1 &&
                attr.value[0].type == 'AttributeShorthand')) ||
            sapperLinkActions.includes(attr.name) ||
            sveltekitLinkActions.includes(attr.name)) {
            let name = transformAttributeCase(attr.name);
            const colonIndex = name.indexOf(':');
            if (colonIndex >= 0) {
                const parts = name.split(':');
                name = parts[0] + parts[1][0].toUpperCase() + parts[1].substring(1);
            }
            str.overwrite(attr.start, attr.start + attr.name.length, name);
            transformedFromDirectiveOrNamespace = true;
        }
    }
    if (parent.type === 'InlineComponent' && attr.name.startsWith('--') && attr.value !== true) {
        str.prependRight(attr.start, '{...__sveltets_cssProp({"');
        buildTemplateString(attr, str, htmlx, '": `', '`})}');
        return;
    }
    if (attr.value === true) {
        if (parent.type === 'Element' &&
            !transformedFromDirectiveOrNamespace &&
            parent.name !== '!DOCTYPE') {
            str.overwrite(attr.start, attr.end, transformAttributeCase(attr.name));
        }
        return;
    }
    if (attr.value.length == 0)
        return; 
    if (attr.value.length == 1) {
        const attrVal = attr.value[0];
        if (attr.name == 'slot') {
            str.remove(attr.start, attr.end);
            return;
        }
        if (attrVal.type == 'AttributeShorthand') {
            let attrName = attrVal.expression.name;
            if (parent.type == 'Element') {
                attrName = transformAttributeCase(attrName);
            }
            str.appendRight(attr.start, `${attrName}=`);
            return;
        }
        const equals = htmlx.lastIndexOf('=', attrVal.start);
        const sanitizedName = sanitizeLeadingChars(attr.name);
        if (sanitizedName !== attr.name) {
            str.overwrite(attr.start, equals, sanitizedName);
        }
        if (attrVal.type == 'Text') {
            const endsWithQuote = htmlx.lastIndexOf('"', attrVal.end) === attrVal.end - 1 ||
                htmlx.lastIndexOf("'", attrVal.end) === attrVal.end - 1;
            const needsQuotes = attrVal.end == attr.end && !endsWithQuote;
            const hasBrackets = htmlx.lastIndexOf('}', attrVal.end) === attrVal.end - 1 ||
                htmlx.lastIndexOf('}"', attrVal.end) === attrVal.end - 1 ||
                htmlx.lastIndexOf("}'", attrVal.end) === attrVal.end - 1;
            const needsNumberConversion = !hasBrackets &&
                parent.type === 'Element' &&
                numberOnlyAttributes.has(attr.name.toLowerCase()) &&
                !isNaN(attrVal.data);
            if (needsNumberConversion) {
                if (needsQuotes) {
                    str.prependRight(equals + 1, '{');
                    str.appendLeft(attr.end, '}');
                }
                else {
                    str.overwrite(equals + 1, equals + 2, '{');
                    str.overwrite(attr.end - 1, attr.end, '}');
                }
            }
            else if (needsQuotes) {
                str.prependRight(equals + 1, '"');
                str.appendLeft(attr.end, '"');
            }
            return;
        }
        if (attrVal.type == 'MustacheTag') {
            if (attrVal.end != attr.end) {
                str.remove(attrVal.start - 1, attrVal.start);
                str.remove(attr.end - 1, attr.end);
            }
            return;
        }
        return;
    }
    buildTemplateString(attr, str, htmlx, '={`', '`}');
}
function handleAttribute ( htmlx , str , attr , parent , preserveCase ) { let transformedFromDirectiveOrNamespace = false ; const transformAttributeCase = ( name ) => { if ( ! preserveCase && ! svgAttributes . find ( ( x ) => x == name ) ) { return name . toLowerCase ( ) ; } else { return name ; } } ; if ( parent . type == 'Element' ) { const sapperLinkActions = [ 'sapper:prefetch' , 'sapper:noscroll' ] ; const sveltekitLinkActions = [ 'sveltekit:prefetch' , 'sveltekit:noscroll' ] ; if ( ( attr . value !== true && ! ( attr . value . length && attr . value . length == 1 && attr . value [ 0 ] . type == 'AttributeShorthand' ) ) || sapperLinkActions . includes ( attr . name ) || sveltekitLinkActions . includes ( attr . name ) ) { let name = transformAttributeCase ( attr . name ) ; const colonIndex = name . indexOf ( ':' ) ; if ( colonIndex >= 0 ) { const parts = name . split ( ':' ) ; name = parts [ 0 ] + parts [ 1 ] [ 0 ] . toUpperCase ( ) + parts [ 1 ] . substring ( 1 ) ; } str . overwrite ( attr . start , attr . start + attr . name . length , name ) ; transformedFromDirectiveOrNamespace = true ; } } if ( parent . type === 'InlineComponent' && attr . name . startsWith ( '--' ) && attr . value !== true ) { str . prependRight ( attr . start , '{...__sveltets_cssProp({"' ) ; buildTemplateString ( attr , str , htmlx , '": `' , '`})}' ) ; return ; } if ( attr . value === true ) { if ( parent . type === 'Element' && ! transformedFromDirectiveOrNamespace && parent . name !== '!DOCTYPE' ) { str . overwrite ( attr . start , attr . end , transformAttributeCase ( attr . name ) ) ; } return ; } if ( attr . value . length == 0 ) return ; if ( attr . value . length == 1 ) { const attrVal = attr . value [ 0 ] ; if ( attr . name == 'slot' ) { str . remove ( attr . start , attr . end ) ; return ; } if ( attrVal . type == 'AttributeShorthand' ) { let attrName = attrVal . expression . name ; if ( parent . type == 'Element' ) { attrName = transformAttributeCase ( attrName ) ; } str . appendRight ( attr . start , ` ${ attrName } ` ) ; return ; } const equals = htmlx . lastIndexOf ( '=' , attrVal . start ) ; const sanitizedName = sanitizeLeadingChars ( attr . name ) ; if ( sanitizedName !== attr . name ) { str . overwrite ( attr . start , equals , sanitizedName ) ; } if ( attrVal . type == 'Text' ) { const endsWithQuote = htmlx . lastIndexOf ( '"' , attrVal . end ) === attrVal . end - 1 || htmlx . lastIndexOf ( "'" , attrVal . end ) === attrVal . end - 1 ; const needsQuotes = attrVal . end == attr . end && ! endsWithQuote ; const hasBrackets = htmlx . lastIndexOf ( '}' , attrVal . end ) === attrVal . end - 1 || htmlx . lastIndexOf ( '}"' , attrVal . end ) === attrVal . end - 1 || htmlx . lastIndexOf ( "}'" , attrVal . end ) === attrVal . end - 1 ; const needsNumberConversion = ! hasBrackets && parent . type === 'Element' && numberOnlyAttributes . has ( attr . name . toLowerCase ( ) ) && ! isNaN ( attrVal . data ) ; if ( needsNumberConversion ) { if ( needsQuotes ) { str . prependRight ( equals + 1 , '{' ) ; str . appendLeft ( attr . end , '}' ) ; } else { str . overwrite ( equals + 1 , equals + 2 , '{' ) ; str . overwrite ( attr . end - 1 , attr . end , '}' ) ; } } else if ( needsQuotes ) { str . prependRight ( equals + 1 , '"' ) ; str . appendLeft ( attr . end , '"' ) ; } return ; } if ( attrVal . type == 'MustacheTag' ) { if ( attrVal . end != attr . end ) { str . remove ( attrVal . start - 1 , attrVal . start ) ; str . remove ( attr . end - 1 , attr . end ) ; } return ; } return ; } buildTemplateString ( attr , str , htmlx , '={`' , '`}' ) ; }
Handle various kinds of attributes and make them conform to JSX.
Handle various kinds of attributes and make them conform to JSX .
//if we are on an "element" we are case insensitive, lowercase to match our JSX // skip Attribute shorthand, that is handled below //strip ":" from out attribute name and uppercase the next letter to convert to jsx attribute // Custom CSS property //we are a bare attribute //wut? //handle single value //if the end doesn't line up, we are wrapped in quotes // We have multiple attribute values, so we build a template string out of them.