public void EditInternalCSSTest()
        {
            // Create an instance of an HTML document with specified content
            var content = "<div><p>Internal CSS</p><p>An internal CSS is used to define a style for a single HTML page</p></div>";

            using (var document = new HTMLDocument(content, "."))
            {
                var style = document.CreateElement("style");
                style.TextContent = ".frame1 { margin-top:50px; margin-left:50px; padding:20px; width:360px; height:90px; background-color:#a52a2a; font-family:verdana; color:#FFF5EE;} \r\n" +
                                    ".frame2 { margin-top:-90px; margin-left:160px; text-align:center; padding:20px; width:360px; height:100px; background-color:#ADD8E6;}";

                // Find the document header element and append the style element to the header
                var head = document.GetElementsByTagName("head").First();
                head.AppendChild(style);

                // Find the first paragraph element to inspect the styles
                var paragraph = (HTMLElement)document.GetElementsByTagName("p").First();
                paragraph.ClassName = "frame1";

                // Find the last paragraph element to inspect the styles
                var lastParagraph = (HTMLElement)document.GetElementsByTagName("p").Last();
                lastParagraph.ClassName = "frame2";

                // Set a color to the first paragraph
                paragraph.Style.FontSize  = "250%";
                paragraph.Style.TextAlign = "center";

                // Set a font-size to the last paragraph
                lastParagraph.Style.Color      = "#434343";
                lastParagraph.Style.FontSize   = "150%";
                lastParagraph.Style.FontFamily = "verdana";

                // Save the HTML document to a file
                document.Save(Path.Combine(OutputDir, "edit-internal-css.html"));

                // Create the instance of the PDF output device and render the document into this device
                using (var device = new PdfDevice(Path.Combine(OutputDir, "edit-internal-css.pdf")))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }

                Assert.True(document.QuerySelectorAll("style").Length > 0);

                Assert.True(File.Exists(Path.Combine(OutputDir, "edit-internal-css.pdf")));

                Assert.True(File.Exists(Path.Combine(OutputDir, "edit-internal-css.html")));
            }
        }
        public void EditInlineCSSTest()
        {
            // Create an instance of an HTML document with specified content
            var content = "<p>InlineCSS </p>";

            using (var document = new HTMLDocument(content, "."))
            {
                // Find the paragraph element to set a style
                var paragraph = (HTMLElement)document.GetElementsByTagName("p").First();

                // Set the style attribute
                paragraph.SetAttribute("style", "font-size:250%; font-family:verdana; color:#cd66aa");

                // Save the HTML document to a file
                document.Save(Path.Combine(OutputDir, "edit-inline-css.html"));

                // Create an instance of PDF output device and render the document into this device
                using (var device = new PdfDevice(Path.Combine(OutputDir, "edit-inline-css.pdf")))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }

                Assert.True(File.Exists(Path.Combine(OutputDir, "edit-inline-css.pdf")));
            }
        }
        public void UsingDOMTest()
        {
            // Create an instance of an HTML document
            using (var document = new HTMLDocument())
            {
                // Create a style element and assign the green color for all elements with class-name equals 'gr'.
                var style = document.CreateElement("style");
                style.TextContent = ".gr { color: green }";

                // Find the document header element and append style element to the header
                var head = document.GetElementsByTagName("head").First();
                head.AppendChild(style);

                // Create a paragraph element with class-name 'gr'.
                var p = (HTMLParagraphElement)document.CreateElement("p");
                p.ClassName = "gr";

                // Create a text node
                var text = document.CreateTextNode("Hello World!!");

                // Append the text node to the paragraph
                p.AppendChild(text);

                // Append the paragraph to the document body element
                document.Body.AppendChild(p);

                // Save the HTML document to a file
                document.Save(Path.Combine(OutputDir, "using-dom.html"));

                // Create an instance of the PDF output device and render the document into this device
                using (var device = new PdfDevice(Path.Combine(OutputDir, "using-dom.pdf")))
                {
                    // Render HTML to PDF
                    document.RenderTo(device);
                }

                Assert.True(File.Exists(Path.Combine(OutputDir, "using-dom.pdf")));
            }
        }
Exemplo n.º 4
0
        public void GetElementsByTagNameUsual()
        {
            var result = document.GetElementsByTagName("P");

            Assert.AreEqual(4, result.Length);
        }
Exemplo n.º 5
0
        static void CreateCssSelectorTest(String url, List <String> methods)
        {
            Console.Write("Loading " + url + " ... ");
            var client = new HttpClient();
            var result = client.GetAsync(url).Result;
            var source = result.Content.ReadAsStreamAsync().Result;

            HTMLDocument html = null;

            try { html = DocumentBuilder.Html(source); }
            catch { Console.WriteLine("error!!!"); return; }

            var title     = Sanatize(html.GetElementsByTagName("title")[0].TextContent);
            var content   = html.GetElementsByTagName("content")[0].InnerHTML.Trim().Replace("\"", "\"\"");
            var css       = html.GetElementsByTagName("css")[0].TextContent;
            var sheet     = CssParser.ParseStyleSheet(css);
            var selectors = new StringBuilder();
            var i         = 1;

            if (methods.Contains(title))
            {
                var ltr = 'A';

                while (methods.Contains(title + ltr.ToString()))
                {
                    ltr = (Char)((int)ltr + 1);
                }

                title += ltr.ToString();
            }

            foreach (var rule in sheet.CssRules)
            {
                if (rule is CSSStyleRule)
                {
                    selectors.Append(@"
	        var selectorINDEX = doc.QuerySelectorAll(""SELECTOR"");
	        Assert.AreEqual(0, selectorINDEX.Length);"
                                     .Replace("SELECTOR", ((CSSStyleRule)rule).SelectorText)
                                     .Replace("INDEX", i.ToString()));
                    i++;
                }
            }

            File.AppendAllText("test.cs", @"
        /// <summary>
        /// Test taken from URL
        /// </summary>
        public void TITLE()
        {
	        var source = @""HTML"";
	        var doc = DocumentBuilder.Html(source);
	        SELECTORS
        }
"
                               .Replace("URL", url)
                               .Replace("TITLE", title)
                               .Replace("HTML", content)
                               .Replace("SELECTORS", selectors.ToString())
                               );
            Console.WriteLine("success.");
            methods.Add(title);
        }
Exemplo n.º 6
0
        public void Merge(string[] htmlFiles, string outPath)
        {
            _htmlOutputPath = Path.GetDirectoryName(outPath);
            var temp = Path.Combine(_htmlOutputPath, "Output2");

            Directory.CreateDirectory(temp);

            for (var i = 0; i < htmlFiles.Length; i++)
            {
                var htmlFile           = htmlFiles[i];
                var fileName           = Path.GetFileName(htmlFile);
                var tempOutputFilePath = Path.Combine(temp, fileName);
                File.Copy(htmlFile, tempOutputFilePath);
                htmlFiles[i] = tempOutputFilePath;

                using (var htmlDocument = new HTMLDocument(htmlFiles[i]))
                {
                    HTMLDivElement   anchorDivContainer = (HTMLDivElement)htmlDocument.CreateElement("div");
                    HTMLBodyElement  htmlDocumentBody   = (HTMLBodyElement)htmlDocument.Body;
                    HTMLHeadElement  head             = (HTMLHeadElement)htmlDocument.GetElementsByTagName("head")[0];
                    HTMLStyleElement htmlStyleElement = (HTMLStyleElement)htmlDocument.CreateElement("style");

                    var resetEvent = new AutoResetEvent(false);
                    htmlStyleElement.OnLoad  += (sender, e) => { resetEvent.Set(); };
                    htmlStyleElement.OnError += (sender, e) => { resetEvent.Set(); };

                    var cssContent = htmlDocument.CreateTextNode(_content);
                    htmlStyleElement.AppendChild(cssContent);
                    head.AppendChild(htmlStyleElement);
                    resetEvent.WaitOne();

                    anchorDivContainer.SetAttribute("class", "container");
                    anchorDivContainer.SetAttribute("align", "center");

                    var paragraphElement = GetParagraphElement(htmlDocument, "Navigation");
                    anchorDivContainer.AppendChild(paragraphElement);

                    if (i != 0)
                    {
                        var anchorBackward = GetAnchorElement(htmlDocument, "Backward");
                        anchorBackward.Href = Path.GetFileName(htmlFiles[i - 1]);
                        anchorDivContainer.AppendChild(anchorBackward);
                    }
                    else
                    {
                        HTMLAnchorElement anchorBackward = GetAnchorElement(htmlDocument, "Backward");
                        anchorBackward.Href = Path.GetFileName(htmlFiles[htmlFiles.Length - 1]);
                        anchorDivContainer.AppendChild(anchorBackward);
                    }

                    if (i != htmlFiles.Length - 1)
                    {
                        HTMLAnchorElement anchorForward = GetAnchorElement(htmlDocument, "Forward");
                        anchorForward.Href = Path.GetFileName(htmlFiles[i + 1]);
                        anchorDivContainer.AppendChild(anchorForward);
                    }
                    else
                    {
                        HTMLAnchorElement anchorForward = GetAnchorElement(htmlDocument, "Forward");
                        anchorForward.Href = Path.GetFileName(htmlFiles[0]);
                        anchorDivContainer.AppendChild(anchorForward);
                    }

                    Node firstChild = htmlDocumentBody.FirstChild;
                    htmlDocumentBody.InsertBefore(anchorDivContainer, firstChild);
                    var outputPath = Path.Combine(_htmlOutputPath, fileName);
                    htmlDocument.Save(outputPath);
                    htmlDocument.Dispose();
                    htmlFiles[i] = outputPath;
                    ClearDirectory(temp);
                }
            }
            var name = Path.GetFileNameWithoutExtension(outPath);

            SaveDocument($"{name}.mhtml", htmlFiles[0], htmlFiles.Length);
        }