Ejemplo n.º 1
0
        private string ExpandImports(string cssContent, string parentUrl)
        {
            var imports = Regex.CssImportPattern.Matches(cssContent);
            var filter  = RRContainer.Current.GetAllInstances <IFilter>().FirstOrDefault(x => (x is CssFilter));

            foreach (Match match in imports)
            {
                var url = match.Groups["url"].Value;
                if (filter != null && filter.IgnoreTarget(new CssJsFilterContext(null, url, match.ToString())))
                {
                    continue;
                }
                var absoluteUrl   = RelativeToAbsoluteUtility.ToAbsolute(parentUrl, url);
                var importContent = WebClientWrapper.DownloadString <CssResource>(absoluteUrl);
                importContent = MakeRelativeUrlsAbsoluteAndRemoveComments(importContent, absoluteUrl);
                importContent = ExpandImports(importContent, absoluteUrl);
                var media = match.Groups["media"];
                if (media.Success)
                {
                    importContent = WrapMedia(importContent, media.Value);
                }
                cssContent = cssContent.Replace(match.ToString(), importContent);
            }
            return(cssContent);
        }
Ejemplo n.º 2
0
        public void WillMakeRelativeUrlAbsolute()
        {
            var result =
                RelativeToAbsoluteUtility.ToAbsolute("http://blogs.msdn.com/themes/blogs/MSDN2/css/MSDNblogs.css",
                                                     "../../../MSDN2/Images/MSDN/contentpane.png");

            Assert.Equal("http://blogs.msdn.com/themes/msdn2/Images/MSDN/contentpane.png", result, StringComparer.OrdinalIgnoreCase);
        }
Ejemplo n.º 3
0
        public void WillUseContentHostIfUrlIsLocal()
        {
            var config = new Mock <IRRConfiguration>();

            config.Setup(x => x.ContentHost).Returns("http://content");
            RRContainer.Current.Configure(x => x.For <IRRConfiguration>().Use(config.Object));

            var result =
                RelativeToAbsoluteUtility.ToAbsolute("http://blogs.msdn.com/themes/blogs/MSDN2/css/MSDNblogs.css",
                                                     "http://blogs.msdn.com/MSDN2/Images/MSDN/contentpane.png");

            Assert.Equal("http://content/msdn2/Images/MSDN/contentpane.png", result, StringComparer.OrdinalIgnoreCase);
            RRContainer.Current = null;
        }
Ejemplo n.º 4
0
        public void WillForwardNewUrlToListenerIfNoContentHost_obsolete()
        {
            Registry.AbsoluteUrlTransformer = (x, y) =>
            {
                var newUrlHost = new Uri(y).Host;
                return(y.Replace(newUrlHost, "funny." + new Uri(x).Host));
            };

            var result =
                RelativeToAbsoluteUtility.ToAbsolute("http://blogs.msdn.com/themes/blogs/MSDN2/css/MSDNblogs.css",
                                                     "../../../MSDN2/Images/MSDN/contentpane.png");

            Assert.Equal("http://funny.blogs.msdn.com/themes/msdn2/Images/MSDN/contentpane.png", result, StringComparer.OrdinalIgnoreCase);
            Registry.AbsoluteUrlTransformer = null;
        }
Ejemplo n.º 5
0
        private string MakeRelativeUrlsAbsoluteAndRemoveComments(string originalCss, string parentCssUrl)
        {
            originalCss = Regex.CssCommentPattern.Replace(originalCss, string.Empty);
            var matches = Regex.ImageUrlPattern.Matches(originalCss);

            foreach (Match match in matches)
            {
                var url = match.Groups["url"].Value.Replace("'", "").Replace("\"", "").Trim();
                if (url.Length <= 0 || url.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }
                var newUrl = RelativeToAbsoluteUtility.ToAbsolute(parentCssUrl, url);
                originalCss = originalCss.Replace(match.Value, match.Value.Replace(url, newUrl));
            }
            return(originalCss);
        }
Ejemplo n.º 6
0
        private string Transform <T>(string preTransform) where T : IResourceType
        {
            var noCommentTransform = Regex.HtmlCommentPattern.Replace(preTransform, string.Empty);
            var resource           = RRContainer.Current.GetInstance <T>();
            var matches            = resource.ResourceRegex.Matches(noCommentTransform);

            if (matches.Count > 0)
            {
                var urls = new StringBuilder();
                var transformableMatches = new List <string>();
                foreach (var match in matches)
                {
                    var  strMatch = match.ToString();
                    var  urlMatch = Regex.UrlPattern.Match(strMatch);
                    bool matched  = false;
                    if (urlMatch.Success)
                    {
                        var url = RelativeToAbsoluteUtility.ToAbsolute(config.BaseAddress == null ? context.Request.Url : new Uri(config.BaseAddress), urlMatch.Groups["url"].Value);
                        if ((resource.TagValidator == null || resource.TagValidator(strMatch, url)) && (RRContainer.Current.GetAllInstances <IFilter>().Where(x => (x is CssFilter && typeof(T) == typeof(CssResource)) || (x is JavascriptFilter && typeof(T) == typeof(JavaScriptResource))).FirstOrDefault(y => y.IgnoreTarget(new CssJsFilterContext(context.Request, url, strMatch))) == null))
                        {
                            matched = true;
                            urls.Append(url);
                            urls.Append(GetMedia(strMatch));
                            urls.Append("::");
                            transformableMatches.Add(strMatch);
                        }
                    }
                    if (!matched && transformableMatches.Count > 0)
                    {
                        preTransform = DoTransform <T>(preTransform, urls, transformableMatches, noCommentTransform);
                        urls.Length  = 0;
                        transformableMatches.Clear();
                    }
                }
                if (transformableMatches.Count > 0)
                {
                    preTransform = DoTransform <T>(preTransform, urls, transformableMatches, noCommentTransform);
                    urls.Length  = 0;
                    transformableMatches.Clear();
                }
            }
            return(preTransform);
        }
Ejemplo n.º 7
0
        public void WillForwardNewUrlToListener_obsolete()
        {
            var config = new Mock <IRRConfiguration>();

            config.Setup(x => x.ContentHost).Returns("http://contenthost");
            RRContainer.Current.Configure(x => x.For <IRRConfiguration>().Use(config.Object));
            Registry.AbsoluteUrlTransformer = (x, y) =>
            {
                var newUrlHost = new Uri(y).Host;
                return(y.Replace(newUrlHost, newUrlHost + "." + new Uri(x).Host));
            };

            var result =
                RelativeToAbsoluteUtility.ToAbsolute("http://blogs.msdn.com/themes/blogs/MSDN2/css/MSDNblogs.css",
                                                     "../../../MSDN2/Images/MSDN/contentpane.png");

            Assert.Equal("http://contenthost.blogs.msdn.com/themes/msdn2/Images/MSDN/contentpane.png", result, StringComparer.OrdinalIgnoreCase);
            RRContainer.Current             = null;
            Registry.AbsoluteUrlTransformer = null;
        }
Ejemplo n.º 8
0
        private string ExpandImports(string cssContent, string parentUrl)
        {
            var imports = Regex.CssImportPattern.Matches(cssContent);

            foreach (Match match in imports)
            {
                var url           = match.Groups["url"].Value;
                var absoluteUrl   = RelativeToAbsoluteUtility.ToAbsolute(parentUrl, url);
                var importContent = webClientWrapper.DownloadString <CssResource>(absoluteUrl);
                importContent = ProcessSprites(importContent, absoluteUrl);
                importContent = ExpandImports(importContent, absoluteUrl);
                var media = match.Groups["media"];
                if (media.Success)
                {
                    importContent = WrapMedia(importContent, media.Value);
                }
                cssContent = cssContent.Replace(match.ToString(), importContent);
            }
            return(cssContent);
        }
        public BackgroundImageClass(string originalClassString, string parentCssUrl)
        {
            var offsetExplicitelySet = new bool[2];

            OriginalClassString = originalClassString;
            var match = Regex.ImageUrlPattern.Match(originalClassString);

            if (match.Success)
            {
                OriginalImageUrl = match.Groups["url"].Value.Replace("'", "").Replace("\"", "").Trim();
                if (OriginalImageUrl.Length > 0)
                {
                    ImageUrl            = RelativeToAbsoluteUtility.ToAbsolute(parentCssUrl, OriginalImageUrl);
                    OriginalClassString = OriginalClassString.Replace(OriginalImageUrl, ImageUrl);
                }
            }
            var repeatMatch = Regex.RepeatPattern.Matches(originalClassString);

            if (repeatMatch.Count > 0)
            {
                Repeat = (RepeatStyle)Enum.Parse(typeof(RepeatStyle), repeatMatch[repeatMatch.Count - 1].Value.Replace("-", ""), true);
            }
            var widthMatch = Regex.WidthPattern.Matches(originalClassString);

            if (widthMatch.Count > 0)
            {
                Width = Int32.Parse(widthMatch[widthMatch.Count - 1].Groups["width"].Value);
            }

            var heightMatch = Regex.HeightPattern.Matches(originalClassString);

            if (heightMatch.Count > 0)
            {
                Height = Int32.Parse(heightMatch[heightMatch.Count - 1].Groups["height"].Value);
            }

            if (Width != null || Height != null)
            {
                var paddingMatches = Regex.PaddingPattern.Matches(originalClassString);
                if (paddingMatches.Count > 0)
                {
                    var padVals = new int[4];
                    foreach (var pads in from Match paddingMatch in paddingMatches select GetPadding(paddingMatch))
                    {
                        if (pads[0] != null)
                        {
                            padVals[0] = (int)pads[0];
                        }
                        if (pads[1] != null)
                        {
                            padVals[1] = (int)pads[1];
                        }
                        if (pads[2] != null)
                        {
                            padVals[2] = (int)pads[2];
                        }
                        if (pads[3] != null)
                        {
                            padVals[3] = (int)pads[3];
                        }
                    }
                    if (Width != null)
                    {
                        if (padVals[1] < 0 || padVals[3] < 0)
                        {
                            Width = null;
                        }
                        else
                        {
                            Width += (padVals[1] + padVals[3]);
                        }
                    }
                    if (Height != null)
                    {
                        if (padVals[0] < 0 || padVals[2] < 0)
                        {
                            Height = null;
                        }
                        else
                        {
                            Height += (padVals[0] + padVals[2]);
                        }
                    }
                }
            }

            var offsetMatches = Regex.OffsetPattern.Matches(originalClassString);

            if (offsetMatches.Count > 0)
            {
                foreach (Match offsetMatch in offsetMatches)
                {
                    SetOffsets(offsetMatch, offsetExplicitelySet);
                }
            }
            if (XOffset.PositionMode == PositionMode.Direction && !offsetExplicitelySet[1])
            {
                YOffset = new Position()
                {
                    PositionMode = PositionMode.Direction
                }
            }
            ;
            if (YOffset.PositionMode == PositionMode.Direction && !offsetExplicitelySet[0])
            {
                XOffset = new Position()
                {
                    PositionMode = PositionMode.Direction
                }
            }
            ;
        }