public static List <FormDescr> GetAllForms(this HTMLBodyElement _) => FormDescr.FormsTypes .SelectMany(x => Document.GetElementsByClassName(x.Item1.FullNameWithoutGenerics()) .Where(y => x.Item2(y)) .Select(FormDescr.CreateFrom)) .ToList();
public void HtmlHasRightBodyElement() { var doc = new HTMLDocument(); var root = new HTMLHtmlElement(); doc.AppendChild(root); var body = new HTMLBodyElement(); root.AppendChild(body); Assert.AreEqual(body, doc.Body); }
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); }
public static FormDescr GetActiveFormOrNull(this HTMLBodyElement body) => body.GetAllForms().LastOrDefault(x => x.IsShown);