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

// Inlines images in Css as data: urls if
//   the CSS rule containing the background/background-image property also has a "-one-image-inline: inline" property
//   OR the size of the image is less than or equal to 'sizeThreshold' AND the image doesn't have any other incoming relations
//
// Note: If provided, the queryObj argument must specify the set of Html assets to start from so that a fallback IE stylesheet can be created.

module.exports = function (queryObj, sizeThreshold) {
    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.cssRule.style.getPropertyValue('-one-image-inline') ||
                        (sizeThreshold >= 0 && cssImage.to.rawSrc.length <= sizeThreshold) && 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 = cssAsset.clone(htmlStyleInConditionalComment);

                    htmlStyleInConditionalComment.refreshHref();
                    // FIXME: Maybe a not-IE conditional comment should also be modelled as an inline-ish Html asset?
                    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 {<commit_msg>transforms.inlineCssImagesWithLegacyFallback: Changed the criterion for inlining slightly.<commit_after>var _ = require('underscore'),
    relations = require('../relations'),
    assets = require('../assets');

// Inlines images in Css as data: urls if
//   the CSS rule containing the background/background-image property also has a "-one-image-inline: inline" property
//   OR the size of the image is less than or equal to 'sizeThreshold' AND the Css asset only has that one relation to the image
//
// Note: If provided, the queryObj argument must specify the set of Html assets to start from so that a fallback IE stylesheet can be created.

module.exports = function (queryObj, sizeThreshold) {
    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.cssRule.style.getPropertyValue('-one-image-inline') ||
                        (sizeThreshold >= 0 && cssImage.to.rawSrc.length <= sizeThreshold) && assetGraph.findRelations({from: cssAsset, 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 = cssAsset.clone(htmlStyleInConditionalComment);

                    htmlStyleInConditionalComment.refreshHref();
                    // FIXME: Maybe a not-IE conditional comment should also be modelled as an inline-ish Html asset?
                    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 {
{'fraction_non_alphanumeric': 0.09706739838792659, 'fraction_numerical': 0.0010289830217801406, 'mean_word_length': 2.14902807775378, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4621423', 'n_tokens_mistral': 1435, 'n_tokens_neox': 1352, 'n_words': 349}