<commit_before>var _ = require('underscore'),
    relations = require('../relations'),
    assets = require('../assets');

module.exports = function (queryObj) {
    return function inlineCssImages(assetGraph) {
        assetGraph.findAssets(_.extend({type: 'Html'}, queryObj)).forEach(function (htmlAsset) {
            assetGraph.findRelations({type: 'HtmlStyle', from: htmlAsset}).forEach(function (htmlStyle) {
                var cssAsset = htmlStyle.to,
                    cssImages = assetGraph.findRelations({from: cssAsset, to: {isImage: true, isInline: false}}),
                    cssImagesToInline = [];
                cssImages.forEach(function (cssImage) {
                    if (cssImage.to.rawSrc.length < 32768 * 3 / 4 - 50 && assetGraph.findRelations({to: cssImage.to}).length === 1) {
                        cssImagesToInline.push(cssImage);
                    }
                });
                if (cssImagesToInline.length > 0) {
                    var legacyStyleSheet = assetGraph.cloneAsset(cssAsset),
                        document = htmlAsset.parseTree,
                        conditionalCommentBody = new assets.Html({
                            text: ""
                        });

                    assetGraph.addAsset(conditionalCommentBody);
                    assetGraph.attachAndAddRelation(new relations.HtmlConditionalComment({
                        from: htmlAsset,
                        to: conditionalCommentBody,
                        condition: "IE"
                    }), 'before', htmlStyle);

                    var htmlStyleInConditionalComment = new relations.HtmlStyle({
                        to: legacyStyleSheet
                    });
                    htmlStyleInConditionalComment.attach(conditionalCommentBody, 'first');
                    assetGraph.refreshRelationHref(htmlStyleInConditionalComment);
                    htmlStyle.node.parentNode.insertBefore(document.createComment("[if !IE]>"), htmlStyle.node);
                    if (htmlStyle.node.nextSibling) {
                        htmlStyle.node.parentNode.insertBefore(document.createComment("<![endif]"), htmlStyle.node.nextSibling);
                    } else {
                        htmlStyle.node.parentNode.appendChild(document.createComment("<![endif]"));
                    }
                    cssImagesToInline.forEach(function (cssImage) {
                        assetGraph.inlineRelation(cssImage);
                    });
                }
            });
        });
    };
};
<commit_msg>transforms.inlineCssImagesWithLegacyFallback: Create and add the assets and relations in the right order. CssAlphaImageLoader are still broken, though.<commit_after>var _ = require('underscore'),
    relations = require('../relations'),
    assets = require('../assets');

module.exports = function (queryObj) {
    return function inlineCssImagesWithLegacyFallback(assetGraph) {
        assetGraph.findAssets(_.extend({type: 'Html'}, queryObj)).forEach(function (htmlAsset) {
            assetGraph.findRelations({type: 'HtmlStyle', from: htmlAsset}).forEach(function (htmlStyle) {
                var cssAsset = htmlStyle.to,
                    cssImages = assetGraph.findRelations({from: cssAsset, to: {isImage: true, isInline: false}}),
                    cssImagesToInline = [];
                cssImages.forEach(function (cssImage) {
                    if (cssImage.to.rawSrc.length < 32768 * 3 / 4 - 50 && assetGraph.findRelations({to: cssImage.to}).length === 1) {
                        cssImagesToInline.push(cssImage);
                    }
                });
                if (cssImagesToInline.length > 0) {
                    var document = htmlAsset.parseTree,
                        conditionalCommentBody = new assets.Html({text: ""});

                    assetGraph.addAsset(conditionalCommentBody);
                    assetGraph.attachAndAddRelation(new relations.HtmlConditionalComment({
                        from: htmlAsset,
                        to: conditionalCommentBody,
                        condition: "IE"
                    }), 'before', htmlStyle);

                    var htmlStyleInConditionalComment = new relations.HtmlStyle({
                        from: conditionalCommentBody
                    });
                    htmlStyleInConditionalComment.attach(conditionalCommentBody, 'first');

                    var legacyStyleSheet = assetGraph.cloneAsset(cssAsset, [htmlStyleInConditionalComment]);

                    assetGraph.refreshRelationHref(htmlStyleInConditionalComment);
                    htmlStyle.node.parentNode.insertBefore(document.createComment("[if !IE]>"), htmlStyle.node);
                    if (htmlStyle.node.nextSibling) {
                        htmlStyle.node.parentNode.insertBefore(document.createComment("<![endif]"), htmlStyle.node.nextSibling);
                    } else {
                        htmlStyle.node.parentNode.appendChild(document.createComment("<![endif]"));
                    }
                    cssImagesToInline.forEach(function (cssImage) {
                        assetGraph.inlineRelation(cssImage);
                    });
                }
            });
        });
    };
};

{'fraction_non_alphanumeric': 0.10508280982295831, 'fraction_numerical': 0.004188083000190367, 'mean_word_length': 1.7364583333333334, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2974968', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1191, 'n_words': 195}