protected override void Process2(ref CombinerFilterContext data)
        {
            // This filter will execute only if the scripts haven't been combined,
            // since the combiner filter does the versioning as well.
            // This is done for performance reasons to avoid having to traverse the
            // document tree again.
            if (!data.CombineCss && data.VersionOnly)
            {
                string versionQueryString = data.CombinerService.GetVersionQueryString(data.JsVersion,
                                                                                       data.SharedVersion);
                if (CssNodes == null)
                {
                    return;
                }

                var cssNodes = CssNodes.ToList();
                foreach (HtmlNode css in cssNodes)
                {
                    string src            = css.Attributes["href"].Value;
                    string querySeparator = (src.IndexOf('?') > 0 ? "&" : "?");

                    css.Attributes["href"].Value += querySeparator + versionQueryString;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Replaces all local css tags on the page with a tag pointing to the combined resources
        /// of the tags removed. Appends the combined resources tag to the head of the document.
        /// Versions the resulting combined css tag (for performance reasons we're combining this operation in this filter).
        /// </summary>
        protected override void Process2(ref CombinerFilterContext data)
        {
            if (!data.CombineCss)
            {
                return;
            }

            if (CssNodes == null)
            {
                return;
            }

            var cssNodes = CssNodes.ToList();

            // Url pointing to the combined css of the tags removed
            // We're sending only distinct urls in order to eliminate duplicate loads of the same file...
            string[] combinedCssUrls =
                data.CombinerService.GetCombinedCssUrls(
                    data.RequestPath,
                    cssNodes.Select(n => n.Attributes["href"].Value).Distinct().ToList(),
                    data.CssVersion,
                    data.SharedVersion,
                    data.CombinedResourcesUrl,
                    data.MinifyCss,
                    data.PrependCdnHostToImages);

            // Remove all css tags
            foreach (HtmlNode css in cssNodes)
            {
                css.ParentNode.RemoveChild(css, false);
            }

            // Insert the new combined css tag into the head element
            var head = Doc.DocumentNode.SelectSingleNode("//head");

            if (head == null)
            {
                throw new ApplicationException("Head element in dom is null(TagsParser)");
            }

            var sb = new StringBuilder();

            foreach (string combinedCssUrl in combinedCssUrls)
            {
                sb.Append("<link rel='stylesheet' type='text/css' href='" + combinedCssUrl + "' />");
            }

            // We'll use this method of appending these nodes to the doc in order to avoid
            // having to keep track of the index in the collection when creating nodes explicitly;
            var temp = new HtmlDocument();

            temp.LoadHtml(sb.ToString());

            foreach (HtmlNode n in temp.DocumentNode.ChildNodes)
            {
                head.PrependChild(n);
            }
        }