示例#1
0
        public void ParseCss_WithStandardString_ExpectReadableProperties()
        {
            // Could read in a file here...
            // Arrange
            string css = "html{ background-color: #5a5eed; color: #FFFFFF; margin: 5px; } h2{ background-color: red }";

            // Act
            var stylesheet = new ExCSS.StylesheetParser().Parse(css);

            // Get the info out - long hand
            // var info = stylesheet.Children.First(c => ((ExCSS.StyleRule)c).SelectorText == "html") as ExCSS.StyleRule;
            // var selector = info.SelectorText;
            // var firstCssProperty = info.Style.BackgroundColor;

            // Get the info out - New way
            var info            = stylesheet.StyleRules.First() as ExCSS.StyleRule;
            var selector        = info.SelectorText;
            var backgroundColor = info.Style.BackgroundColor;
            var foregroundColor = info.Style.Color;
            var margin          = info.Style.Margin;

            // Assert
            Assert.Equal(@"html", selector);
            Assert.Equal(@"rgb(90, 94, 237)", backgroundColor);
            Assert.Equal(@"rgb(255, 255, 255)", foregroundColor);
            Assert.Equal(@"5px", margin);
        }
        public void WorkWithExCss()
        {
            // Could read in a file here...
            string css = "html{ background-color: #5a5eed; color: #FFFFFF; margin: 5px; } h2{ background-color: red }";

            var stylesheet = new ExCSS.StylesheetParser().Parse(css);

            // Get the info out - long hand
            //var info = stylesheet.Children.First(c => ((ExCSS.StyleRule)c).SelectorText == "html") as ExCSS.StyleRule;
            //var selector = info.SelectorText;
            //var firstCssProperty = info.Style.BackgroundColor;

            // Get the info out - New way
            var info            = stylesheet.StyleRules.First() as ExCSS.StyleRule;
            var selector        = info.SelectorText;
            var backgroundColor = info.Style.BackgroundColor;
            var foregroundColor = info.Style.Color;
            var margin          = info.Style.Margin;


            //// Create a new stylesheet
            var newParser = new ExCSS.StylesheetParser();

            ExCSS.StyleRule r = new ExCSS.StyleRule(newParser);
            r.SelectorText          = "h1";
            r.Style.BackgroundColor = "red";

            ExCSS.StyleRule r2 = new ExCSS.StyleRule(newParser);
            r2.SelectorText          = "h2";
            r2.Style.BackgroundColor = "green";

            var newstylesheet = r.ToCss() + System.Environment.NewLine + r2.ToCss();
        }
示例#3
0
        public void CreateStylesheet_WithCssProperties_ExpectStandardStringBack()
        {
            // Arrange
            string expectedResult = @"h1 { background-color: rgb(255, 0, 0) }" + Environment.NewLine
                                    + "h2 { background-color: rgb(0, 128, 0) }";

            var newParser = new ExCSS.StylesheetParser();

            ExCSS.StyleRule r = new ExCSS.StyleRule(newParser);
            r.SelectorText          = "h1";
            r.Style.BackgroundColor = "red";

            ExCSS.StyleRule r2 = new ExCSS.StyleRule(newParser);
            r2.SelectorText          = "h2";
            r2.Style.BackgroundColor = "green";

            // Act
            var newstylesheet = r.ToCss() + System.Environment.NewLine + r2.ToCss();

            // Assert
            Assert.Equal(expectedResult, newstylesheet);
        }
示例#4
0
        void Run()
        {
            if (File.Exists(outputName))
            {
                File.Delete(outputName);
            }

            var parser = new ExCSS.StylesheetParser();
            var css = File.ReadAllText("Site.css");
            var sheet = parser.Parse(css);

            var doc = new Document();
            //doc.DefaultPageSetup.Orientation = Orientation.Portrait;
            //doc.DefaultPageSetup.PageFormat = PageFormat.A4;
            //doc.DefaultPageSetup.FooterDistance = Unit.FromCentimeter(0.01);

            Func<string, byte[]> imageProc = s =>
                {
                    var i = System.Drawing.Image.FromFile(s);
                    var ms = new MemoryStream();
                    i.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    return ms.ToArray();
                };
            
            var section = doc.AddSection();
            var html = File.ReadAllText("example.html");
            section.AddHtml(sheet, html, new HtmlConverter(imageProc));


            var renderer = new PdfDocumentRenderer();
            renderer.Document = doc;
            renderer.RenderDocument();

            renderer.Save(outputName);
            Process.Start(outputName);
        }
示例#5
0
        public static void InlineCssIntoHtml(HtmlAgilityPack.HtmlDocument doc)
        {
            var styleTags = doc.DocumentNode.SelectNodes("descendant-or-self::style");

            if (styleTags != null)
            {
                //we only need to try and inline the css is there is any to inline in the first place

                var cssParser = new ExCSS.StylesheetParser();
                var styles = styleTags.Select(x => cssParser.Parse(x.InnerText));

                var allElements = doc.DocumentNode.SelectNodes("descendant-or-self::*");

                Dictionary<HtmlNode, List<ScopedDeclaration>> matches = new Dictionary<HtmlNode, List<ScopedDeclaration>>();

                foreach (var n in allElements)
                {
                    foreach (var r in styles.SelectMany(x => x.RuleSets))
                    {
                        var matchedSelectors = n.Matches(r.Selectors);

                        if (matchedSelectors.Any())
                        {

                            if (!matches.ContainsKey(n))
                            {
                                matches.Add(n, new List<ScopedDeclaration>());
                            }

                            var matchedDeclerations = r.Declarations.SelectMany(x => matchedSelectors.Select(y => new ScopedDeclaration(x, y)));
                            matches[n].AddRange(matchedDeclerations);

                            //TODO update the appending css rule to make sure the correctly prioritised rule is used
                            if (n.Attributes.Contains("style"))
                            {
                                var inlineStyles = n.Attributes["style"].Value;

                                var inline = cssParser.Parse("* { " + inlineStyles + "}")
                                                .RuleSets.Single()
                                                .Declarations
                                                .Select(x => new ScopedDeclaration(x));

                                matches[n].AddRange(inline);
                            }
                        }
                    }
                }

                //go through and update all styles
                foreach (var nodeRules in matches)
                {
                    var node = nodeRules.Key;

                    var declerations = nodeRules.Value.GroupBy(x => x.Decleration.Name).Select(x => x.OrderBy(r => r.Scope).First()).OrderBy(r => r.Scope);

                    StringBuilder stylesSB = new StringBuilder();
                    foreach (var d in declerations)
                    {
                        stylesSB.Append(d.Decleration.ToString());
                        stylesSB.Append(";");
                    }

                    if (!node.Attributes.Contains("style"))
                    {
                        node.Attributes.Add("style", stylesSB.ToString());
                    }
                    else
                    {
                        node.Attributes["style"].Value = stylesSB.ToString();
                    }
                }

            }
        }
示例#6
0
        public static void InlineCssIntoHtml(HtmlAgilityPack.HtmlDocument doc)
        {
            var styleTags = doc.DocumentNode.SelectNodes("descendant-or-self::style");

            if (styleTags != null)
            {
                //we only need to try and inline the css is there is any to inline in the first place

                var cssParser = new ExCSS.StylesheetParser();
                var styles    = styleTags.Select(x => cssParser.Parse(x.InnerText));


                var allElements = doc.DocumentNode.SelectNodes("descendant-or-self::*");


                Dictionary <HtmlNode, List <ScopedDeclaration> > matches = new Dictionary <HtmlNode, List <ScopedDeclaration> >();

                foreach (var n in allElements)
                {
                    foreach (var r in styles.SelectMany(x => x.RuleSets))
                    {
                        var matchedSelectors = n.Matches(r.Selectors);


                        if (matchedSelectors.Any())
                        {
                            if (!matches.ContainsKey(n))
                            {
                                matches.Add(n, new List <ScopedDeclaration>());
                            }

                            var matchedDeclerations = r.Declarations.SelectMany(x => matchedSelectors.Select(y => new ScopedDeclaration(x, y)));
                            matches[n].AddRange(matchedDeclerations);

                            //TODO update the appending css rule to make sure the correctly prioritised rule is used
                            if (n.Attributes.Contains("style"))
                            {
                                var inlineStyles = n.Attributes["style"].Value;

                                var inline = cssParser.Parse("* { " + inlineStyles + "}")
                                             .RuleSets.Single()
                                             .Declarations
                                             .Select(x => new ScopedDeclaration(x));

                                matches[n].AddRange(inline);
                            }
                        }
                    }
                }

                //go through and update all styles
                foreach (var nodeRules in matches)
                {
                    var node = nodeRules.Key;

                    var declerations = nodeRules.Value.GroupBy(x => x.Decleration.Name).Select(x => x.OrderBy(r => r.Scope).First()).OrderBy(r => r.Scope);


                    StringBuilder stylesSB = new StringBuilder();
                    foreach (var d in declerations)
                    {
                        stylesSB.Append(d.Decleration.ToString());
                        stylesSB.Append(";");
                    }

                    if (!node.Attributes.Contains("style"))
                    {
                        node.Attributes.Add("style", stylesSB.ToString());
                    }
                    else
                    {
                        node.Attributes["style"].Value = stylesSB.ToString();
                    }
                }
            }
        }