Пример #1
0
        public void DontRemoveUsedRuleInResultWhenOnlyClass()
        {
            var service = new RequiredCssOnlyService();

            var cssContent  = "body {\r\nbackground-color: white; color: black;\r\n} .card { border:solid 1px green; }\r\n";
            var htmlContent = @"<div class=""card""><h1>Card header</h1><p>this is a card</h1></div>";

            var result = service.RemoveUnusedRules(cssContent, htmlContent);

            Assert.IsTrue(result.Contains(".card{"));
        }
        public void NoCommentsResult()
        {
            var service = new RequiredCssOnlyService();

            var cssContent  = "/*\r\n * # TESTING MULTILINE COMMENT #  \r\n */\r\nbody {\r\nbackground-color: white; color: black;\r\n} /* We don't know what this should illustrate */ .card + h1\r\n{ font-size: 20px }\r\n";
            var htmlContent = "";

            var result = service.RemoveUnusedRules(cssContent, htmlContent);

            Assert.IsFalse(result.Contains("/*") && result.Contains("*/"));
        }
        public void NoNewLineResult()
        {
            var service = new RequiredCssOnlyService();

            var cssContent  = "body {\r\nbackground-color: white; color: black;\r\n} .card + h1\r\n{ font-size: 20px }\r\n";
            var htmlContent = "";

            var result = service.RemoveUnusedRules(cssContent, htmlContent);

            Assert.IsFalse(result.Contains("\r") && result.Contains("\n"));
        }
Пример #4
0
        static void Main(string[] args)
        {
            const string folder = @"C:\code\RequiredCssOnlyApp\";

            RequiredCssOnlyService service = new RequiredCssOnlyService();

            var pageName = "page.html";
            var html     = System.IO.File.ReadAllText(folder + pageName, System.Text.Encoding.UTF8);

            var linkMatches = Regex.Matches(html, "(?<link><link.*href=\"(?<url>[^\"]+)\"[^>]*>)");

            foreach (Match linkMatch in linkMatches)
            {
                var linkGroup = linkMatch.Groups["link"];
                if (!linkGroup.Success)
                {
                    continue;
                }

                var urlGroup = linkMatch.Groups["url"];
                if (!urlGroup.Success)
                {
                    continue;
                }

                if (string.IsNullOrEmpty(urlGroup.Value) || !urlGroup.Value.EndsWith(".css"))
                {
                    continue;
                }

                var fileName = urlGroup.Value;

                var cssContent   = System.IO.File.ReadAllText(folder + fileName, System.Text.Encoding.UTF8);
                var resultingCss = service.RemoveUnusedRules(cssContent, html);

                System.IO.File.WriteAllText(folder + fileName.Replace(".css", "-out.css"), resultingCss, System.Text.Encoding.UTF8);

                if (!string.IsNullOrEmpty(resultingCss))
                {
                    resultingCss = $"<style>{resultingCss}</style>";
                }

                html = html.Replace(linkGroup.Value, resultingCss);
            }

            System.IO.File.WriteAllText(folder + pageName.Replace(".html", "-out.html"), html, System.Text.Encoding.UTF8);
        }