ker/com/x-img2css/x-img2css.com.js
function zipSVG(libs, svgContent, src) {
    svgContent = svgContent
    // Remove space between two tags.
        .replace(/>[ \t\n\r]+</g, '><')
    // Replace double quotes with single quotes.
        .replace(/"/g, "'")
    // Replace many spaces by one single space.
        .replace(/[ \t\n\r]+/g, ' ');

    var svgTree = libs.parseHTML(svgContent);
    // We want to remove unused `id`attributes.
    // Such an attribute is used as long as there is a `xlink:href` attribute referencing it.
    var idsToKeep = {};
    // Remove Inkscape tags and <image>.
    libs.Tree.walk(svgTree, function(node) {
        if (node.type !== libs.Tree.TAG) return;
        var name = node.name.toLowerCase();
        if (name == 'flowroot') {
            libs.warning(
                "Please don't use <flowRoot> nor <flowRegion> in your SVG (\"" + src + "\")!\n"
                    + "If it was created with Inkscape, convert it in SVG 1.1 by opening the `Text` menu\n"
                    + "and selecting \"Convert to Text\".",
                src,
                svgContent,
                node.pos
            );
        }
        var nodeToDelete = startsWith(
            name,
            "metadata", "inkscape:", "sodipodi:", "image", "dc:", "cc:", "rdf:",
            "flowroot", "flowregion"
        );
        if (nodeToDelete) {
            node.type = libs.Tree.VOID;
            delete node.children;
        } else if (node.attribs) {
            var attribsToDelete = [];
            var attribName, attribValue;
            for (attribName in node.attribs) {
                if (
                    startsWith(
                        attribName,
                        'inkscape:', 'sodipodi:', 'xmlns:inkscape', 'xmlns:sodipodi',
                        'xmlns:dc', 'xmlns:cc', 'xmlns:rdf'
                    )
                ) {
                    // This is an attribute to delete.
                    attribsToDelete.push(attribName);
                } else {
                    // Look for references (`xlink:href`).
                    attribValue = node.attribs[attribName].trim();
                    if (attribName.toLowerCase() == 'xlink:href') {
                        if (attribValue.charAt(0) == '#') {
                            // Remember that this ID must not be deleted.
                            idsToKeep[attribValue.substr(1)] = 1;
                        }
                    } else {
                        // Look for local URLs: `url(#idToKeep)`.
                        var match;
                        while ((match = RX_LOCAL_URL.exec(attribValue))) {
                            idsToKeep[match[1]] = 1;
                        }
                    }
                }
            }
            attribsToDelete.forEach(function (attribName) {
                delete node.attribs[attribName];
            });
        }
    });

    // Removing emtpy <g> and <defs>.
    libs.Tree.walk(svgTree, function(node) {
        if (node.type != libs.Tree.TAG) return;
        var name = node.name.toLowerCase();
        if (node.attribs && node.attribs.id) {
            // Try to remove unused ID.
            if (!idsToKeep[node.attribs.id]) {
                delete node.attribs.id;
            }
        }
        if (['g', 'defs'].indexOf(name) > -1) {
            // If this tag has no child, we must delete it.
            var childrenCount = 0;
            (node.children || []).forEach(function (child) {
                if (child.type == libs.Tree.TEXT || child.type == libs.Tree.TAG) {
                    childrenCount++;
                }
            });
            if (childrenCount == 0) {
                // This tag is empty, remove it.
                delete node.children;
                node.type = libs.Tree.VOID;
            }
        }
    });

    var svg = libs.Tree.toString(svgTree);
    return svg;
    // The following code was used when we embeded the SVG in Data URI form.
    /*
     console.log(svg);
     console.log();
     console.log("Base64: " + (new Buffer(svg)).toString('base64').length);
     console.log("UTF-8:  " + encodeURIComponent(svg).length);
     console.log();
     var encodedSVG_base64 = (new Buffer(svg)).toString('base64');
     var encodedSVG_utf8 = encodeURIComponent(svg);
     if (encodedSVG_base64 < encodedSVG_utf8) {
     return '"data:image/svg+xml;base64,' + encodedSVG_base64 + '"';
     } else {
     return '"data:image/svg+xml;utf8,' + encodedSVG_utf8 + '"';
     }
     */
}
function zipSVG(libs, svgContent, src) {
    svgContent = svgContent
    // Remove space between two tags.
        .replace(/>[ \t\n\r]+</g, '><')
    // Replace double quotes with single quotes.
        .replace(/"/g, "'")
    // Replace many spaces by one single space.
        .replace(/[ \t\n\r]+/g, ' ');

    var svgTree = libs.parseHTML(svgContent);
    // We want to remove unused `id`attributes.
    // Such an attribute is used as long as there is a `xlink:href` attribute referencing it.
    var idsToKeep = {};
    // Remove Inkscape tags and <image>.
    libs.Tree.walk(svgTree, function(node) {
        if (node.type !== libs.Tree.TAG) return;
        var name = node.name.toLowerCase();
        if (name == 'flowroot') {
            libs.warning(
                "Please don't use <flowRoot> nor <flowRegion> in your SVG (\"" + src + "\")!\n"
                    + "If it was created with Inkscape, convert it in SVG 1.1 by opening the `Text` menu\n"
                    + "and selecting \"Convert to Text\".",
                src,
                svgContent,
                node.pos
            );
        }
        var nodeToDelete = startsWith(
            name,
            "metadata", "inkscape:", "sodipodi:", "image", "dc:", "cc:", "rdf:",
            "flowroot", "flowregion"
        );
        if (nodeToDelete) {
            node.type = libs.Tree.VOID;
            delete node.children;
        } else if (node.attribs) {
            var attribsToDelete = [];
            var attribName, attribValue;
            for (attribName in node.attribs) {
                if (
                    startsWith(
                        attribName,
                        'inkscape:', 'sodipodi:', 'xmlns:inkscape', 'xmlns:sodipodi',
                        'xmlns:dc', 'xmlns:cc', 'xmlns:rdf'
                    )
                ) {
                    // This is an attribute to delete.
                    attribsToDelete.push(attribName);
                } else {
                    // Look for references (`xlink:href`).
                    attribValue = node.attribs[attribName].trim();
                    if (attribName.toLowerCase() == 'xlink:href') {
                        if (attribValue.charAt(0) == '#') {
                            // Remember that this ID must not be deleted.
                            idsToKeep[attribValue.substr(1)] = 1;
                        }
                    } else {
                        // Look for local URLs: `url(#idToKeep)`.
                        var match;
                        while ((match = RX_LOCAL_URL.exec(attribValue))) {
                            idsToKeep[match[1]] = 1;
                        }
                    }
                }
            }
            attribsToDelete.forEach(function (attribName) {
                delete node.attribs[attribName];
            });
        }
    });

    // Removing emtpy <g> and <defs>.
    libs.Tree.walk(svgTree, function(node) {
        if (node.type != libs.Tree.TAG) return;
        var name = node.name.toLowerCase();
        if (node.attribs && node.attribs.id) {
            // Try to remove unused ID.
            if (!idsToKeep[node.attribs.id]) {
                delete node.attribs.id;
            }
        }
        if (['g', 'defs'].indexOf(name) > -1) {
            // If this tag has no child, we must delete it.
            var childrenCount = 0;
            (node.children || []).forEach(function (child) {
                if (child.type == libs.Tree.TEXT || child.type == libs.Tree.TAG) {
                    childrenCount++;
                }
            });
            if (childrenCount == 0) {
                // This tag is empty, remove it.
                delete node.children;
                node.type = libs.Tree.VOID;
            }
        }
    });

    var svg = libs.Tree.toString(svgTree);
    return svg;
    // The following code was used when we embeded the SVG in Data URI form.
    /*
     console.log(svg);
     console.log();
     console.log("Base64: " + (new Buffer(svg)).toString('base64').length);
     console.log("UTF-8:  " + encodeURIComponent(svg).length);
     console.log();
     var encodedSVG_base64 = (new Buffer(svg)).toString('base64');
     var encodedSVG_utf8 = encodeURIComponent(svg);
     if (encodedSVG_base64 < encodedSVG_utf8) {
     return '"data:image/svg+xml;base64,' + encodedSVG_base64 + '"';
     } else {
     return '"data:image/svg+xml;utf8,' + encodedSVG_utf8 + '"';
     }
     */
}
function zipSVG ( libs , svgContent , src ) { svgContent = svgContent // Remove space between two tags. . replace ( / >[ \t\n\r]+< / g , '><' ) // Replace double quotes with single quotes. . replace ( / " / g , "'" ) // Replace many spaces by one single space. . replace ( / [ \t\n\r]+ / g , ' ' ) ; var svgTree = libs . parseHTML ( svgContent ) ; // We want to remove unused `id`attributes. // Such an attribute is used as long as there is a `xlink:href` attribute referencing it. var idsToKeep = { } ; // Remove Inkscape tags and <image>. libs . Tree . walk ( svgTree , function ( node ) { if ( node . type !== libs . Tree . TAG ) return ; var name = node . name . toLowerCase ( ) ; if ( name == 'flowroot' ) { libs . warning ( "Please don't use <flowRoot> nor <flowRegion> in your SVG (\"" + src + "\")!\n" + "If it was created with Inkscape, convert it in SVG 1.1 by opening the `Text` menu\n" + "and selecting \"Convert to Text\"." , src , svgContent , node . pos ) ; } var nodeToDelete = startsWith ( name , "metadata" , "inkscape:" , "sodipodi:" , "image" , "dc:" , "cc:" , "rdf:" , "flowroot" , "flowregion" ) ; if ( nodeToDelete ) { node . type = libs . Tree . VOID ; delete node . children ; } else if ( node . attribs ) { var attribsToDelete = [ ] ; var attribName , attribValue ; for ( attribName in node . attribs ) { if ( startsWith ( attribName , 'inkscape:' , 'sodipodi:' , 'xmlns:inkscape' , 'xmlns:sodipodi' , 'xmlns:dc' , 'xmlns:cc' , 'xmlns:rdf' ) ) { // This is an attribute to delete. attribsToDelete . push ( attribName ) ; } else { // Look for references (`xlink:href`). attribValue = node . attribs [ attribName ] . trim ( ) ; if ( attribName . toLowerCase ( ) == 'xlink:href' ) { if ( attribValue . charAt ( 0 ) == '#' ) { // Remember that this ID must not be deleted. idsToKeep [ attribValue . substr ( 1 ) ] = 1 ; } } else { // Look for local URLs: `url(#idToKeep)`. var match ; while ( ( match = RX_LOCAL_URL . exec ( attribValue ) ) ) { idsToKeep [ match [ 1 ] ] = 1 ; } } } } attribsToDelete . forEach ( function ( attribName ) { delete node . attribs [ attribName ] ; } ) ; } } ) ; // Removing emtpy <g> and <defs>. libs . Tree . walk ( svgTree , function ( node ) { if ( node . type != libs . Tree . TAG ) return ; var name = node . name . toLowerCase ( ) ; if ( node . attribs && node . attribs . id ) { // Try to remove unused ID. if ( ! idsToKeep [ node . attribs . id ] ) { delete node . attribs . id ; } } if ( [ 'g' , 'defs' ] . indexOf ( name ) > - 1 ) { // If this tag has no child, we must delete it. var childrenCount = 0 ; ( node . children || [ ] ) . forEach ( function ( child ) { if ( child . type == libs . Tree . TEXT || child . type == libs . Tree . TAG ) { childrenCount ++ ; } } ) ; if ( childrenCount == 0 ) { // This tag is empty, remove it. delete node . children ; node . type = libs . Tree . VOID ; } } } ) ; var svg = libs . Tree . toString ( svgTree ) ; return svg ; // The following code was used when we embeded the SVG in Data URI form. /*
     console.log(svg);
     console.log();
     console.log("Base64: " + (new Buffer(svg)).toString('base64').length);
     console.log("UTF-8:  " + encodeURIComponent(svg).length);
     console.log();
     var encodedSVG_base64 = (new Buffer(svg)).toString('base64');
     var encodedSVG_utf8 = encodeURIComponent(svg);
     if (encodedSVG_base64 < encodedSVG_utf8) {
     return '"data:image/svg+xml;base64,' + encodedSVG_base64 + '"';
     } else {
     return '"data:image/svg+xml;utf8,' + encodedSVG_utf8 + '"';
     }
     */ }
Return the lightest SVG possible.
Return the lightest SVG possible .