private void WriteTagAttributes(XmlWriter writer, IHTMLDOMNode htmlNode) { IEnumerable attributes = (htmlNode.attributes as IEnumerable).Cast <IHTMLDOMAttribute>().Where(a => a.specified); foreach (IHTMLDOMAttribute attribute in attributes) { string value = null; if (attribute.nodeValue is DBNull) { try { IHTMLDOMNode newNode = htmlNode.cloneNode(false); IHTMLElement newElement = newNode as IHTMLElement; XElement xmlElement = XElement.Parse(newElement.outerHTML); XAttribute xmlAttribute = xmlElement.Attribute(attribute.nodeName); value = xmlAttribute.Value; } catch (Exception) { } } else { value = attribute.nodeValue; } writer.WriteAttributeString(attribute.nodeName, value); } }
private static void OnDocCompleted(int procId, IHTMLDocument2 doc) { Console.WriteLine(procId.ToString() + ") OnDocCompleted called [Doc:" + doc.ToString() + "]"); //on document completion add our custom DIV //create sample element IHTMLElement elem = doc.createElement("div"); elem.innerHTML = "Hello from RemoteCOM"; elem.setAttribute("id", "RemoteCOM_SampleDiv", 0); //style it IHTMLStyle2 style2 = elem.style as IHTMLStyle2; style2.right = 0; elem.style.top = 0; style2.position = "absolute"; elem.style.border = "2px solid #FFFF00"; elem.style.background = "#FFFFC0"; elem.style.zIndex = 10000; elem.style.font = "bold 12px Helvetica"; elem.style.padding = "5px"; //insert new element into body IHTMLDOMNode bodyNode = doc.body as IHTMLDOMNode; IHTMLDOMNode elemNode = elem as IHTMLDOMNode; bodyNode.appendChild(elemNode); //remember to force release com objects Marshal.ReleaseComObject(elem); Marshal.ReleaseComObject(style2); Marshal.ReleaseComObject(elemNode); Marshal.ReleaseComObject(bodyNode); return; }
void checkLibrary(string url, IHTMLDOMNode div, string isbn, string title) { try { WebClient wc = new WebClient(); wc.Encoding = Encoding.UTF8; wc.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore); wc.DownloadStringCompleted += (s, e) => { XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(e.Result); XmlNode apiContinue = xdoc.SelectSingleNode("result/continue"); if (apiContinue.InnerText == "0") { addLink(xdoc, (IHTMLDOMNode)div, isbn, title); } else { XmlNode apiSession = xdoc.SelectSingleNode("result/session"); if (apiSession.InnerText.Length > 0) { string new_url = "http://api.calil.jp/check?appkey=" + appkey + "&session=" + apiSession.InnerText + "&format=xml"; checkLibrary(url, (IHTMLDOMNode)div, isbn, title); } } }; wc.DownloadStringAsync(new Uri(url)); } catch { CheckLibraryError(this, null); } }
private string CreateJSONData(IHTMLElement element) { StringBuilder sbJSON = new StringBuilder(); string strLanguage = System.Enum.GetName(typeof(AppSettings.CodeLanguages), wscript.settings.CodeLanguage); sbJSON.Append("{"); sbJSON.Append("\"CodeLanguage\": \"" + strLanguage + "\","); sbJSON.Append("\"tagName\": \"" + element.tagName + "\""); IHTMLDOMNode node = element as IHTMLDOMNode; foreach (IHTMLDOMAttribute attr in node.attributes) { if (attr.specified) { sbJSON.AppendFormat(",\"{0}\": \"{1}\"", attr.nodeName, System.Uri.EscapeDataString(attr.nodeValue.ToString())); } } if (element.innerText != null) { sbJSON.Append(",\"innerText\": \"" + System.Uri.EscapeDataString(element.innerText) + "\""); } sbJSON.Append("}"); return(sbJSON.ToString()); }
private void ParseNodes(IHTMLDOMNode domnode) { if (domnode == null) { return; } string str = domnode.nodeName; if (domnode == null) { return; } if (str == "INPUT" || str == "TEXTAREA" || str == "SELECT") { AddGridRow((IHTMLElement)domnode); } var nds = domnode.childNodes as IHTMLDOMChildrenCollection; foreach (IHTMLDOMNode childnd in nds) { string strdom = childnd.nodeName; if (strdom == "#text" || strdom == "#comment") { continue; } ParseNodes(childnd); } }
/// <summary> /// IHTMLDOMNode から HtmlNode を作成 /// </summary> /// <param name="node"></param> /// <returns></returns> protected HtmlNode LoadHtmlNode(IHTMLDOMNode node) { var nn = new HtmlNode(node.nodeName, node.nodeValue.ToString()); if (nn.TagName == "#text") { return(nn); } if (nn.TagName == "#comment") { // append comment tag. nn.TagName = "comment"; string v = System.Web.HttpUtility.HtmlEncode(nn.Value); nn.Children.Add(new HtmlNode("#text", v)); nn.Value = v; return(nn); } // append attributes IHTMLAttributeCollection attrs = node.attributes; if (attrs != null) { foreach (IHTMLDOMAttribute at in attrs) { if (at.specified) { string nodeValue = ""; if (at.nodeValue != null) { nodeValue = at.nodeValue.ToString(); } nn.Attrs.Add(new HtmlAttr { Key = at.nodeName, Value = nodeValue }); } } } var col = node.childNodes as IHTMLDOMChildrenCollection; if (col != null) { foreach (IHTMLDOMNode nd in col) { HtmlNode el = LoadHtmlNode(nd); el.Parent = nn; nn.Children.Add(el); } if (nn.Children.Count > 0 && nn.Children[0].TagName == "#text") { nn.Value = nn.Children[0].Value; } if (nn.Children.Count > 0 && nn.Children[0].TagName == "#comment") { nn.Value = nn.Children[0].Value; } } return(nn); }
/// <summary> /// Get the column of the current cell /// zero based /// </summary> /// <param name="cell"></param> /// <returns></returns> public int GetColIndex(IHTMLTableCell cell) { if (cell == null) { return(0); } int iret = 0; IHTMLTableCell tcell = cell; IHTMLDOMNode node = null; IHTMLElement elem = null; while (true) { node = tcell as IHTMLDOMNode; if (node == null) { break; } elem = PreviousSibling(node); if (elem == null) { break; } tcell = elem as IHTMLTableCell; if (tcell == null) { break; } iret += tcell.colSpan; } return(iret); }
//<a href="http://www.google.com" target="_blank">google</a> //Uses selection text public bool AppendAnchor(string href, string target) { if (m_pDoc2 == null) { return(false); } IHTMLElement elem = m_pDoc2.createElement("a") as IHTMLElement; if (elem == null) { return(false); } IHTMLAnchorElement aelem = elem as IHTMLAnchorElement; if (aelem == null) { return(false); } if (!string.IsNullOrEmpty(href)) { aelem.href = href; } if (!string.IsNullOrEmpty(target)) { aelem.target = target; } //Append to body DOM collection IHTMLDOMNode nd = (IHTMLDOMNode)elem; IHTMLDOMNode body = (IHTMLDOMNode)m_pDoc2.body; return(body.appendChild(nd) != null); }
/// <summary> /// Whether node is a free text node /// </summary> /// <param name="node">The node</param> protected bool IsFreeTextNode(IHTMLDOMNode node) { bool isTextNode = this.IsTextNode(node); bool hasSiblings = (node.previousSibling != null || node.nextSibling != null); return(isTextNode && hasSiblings); }
public void alignHeight(IfacesEnumsStructsClasses.IHTMLElement e) { // int h = getMaxHeightInRow(e); IfacesEnumsStructsClasses.IHTMLElement p = e.parentElement; if (p.getAttribute("ltype", 1) != null && p.getAttribute("ltype", 1).ToString() != "_colDIV") { return; } IHTMLDOMNode node = (IHTMLDOMNode)(p); for (int i = 0; i < node.childNodes.length; i++) { if (!(node.childNodes.item(i) is IfacesEnumsStructsClasses.IHTMLElement)) { continue; } IfacesEnumsStructsClasses.IHTMLElement ne = (IfacesEnumsStructsClasses.IHTMLElement)(node.childNodes.item(i)); if (ne.getAttribute("ltype", 1) != null && ne.getAttribute("ltype", 1).ToString() != "_cellDIV") { continue; } ne.style.height = e.offsetHeight.ToString() + "px"; } }
private IHTMLElement ScrubPostBodyRegionParentElements(IHTMLElement postBodyElement) { //Note: This method prevents the case where the post content consists of a single line of text, so surrounding elements //like that are part of the post content (like <p> and <b>) are improperly included in the template. //since the text-based find strategy returns the common parent element that contains the text, //it is possible for the element to be an inline element (like <b> or <i>) that entirely surrounds //the content. It would be improper to include these inline elements in the template, so we delete all inline //elements above the post body. try { IHTMLDocument2 postContentDoc = HTMLDocumentHelper.StringToHTMLDoc(mostRecentPost.Contents, this._blogHomepageUrl); IHTMLElement[] postContentsElements = FindText(HTMLDocumentHelper.HTMLToPlainText(postContentDoc.body.innerHTML), postContentDoc.body); if (postContentsElements.Length > 0) { IHTMLElement postContentsElement = postContentsElements[0]; if (postContentsElement is IHTMLBodyElement) { //there are no extraneous surrounding tags to clean up, so just return the current body element return(postBodyElement); } //postBodyElement and postContentsElement should now be the equivalent element in each document. //Now delete the parent elements of postContentsElement that appear in the postBodyElement DOM since //these will clearly be elements associated with the post, and not the template. ArrayList markedForDelete = new ArrayList(); while (!(postContentsElement is IHTMLBodyElement) && !(postBodyElement is IHTMLBodyElement)) { if (postContentsElement.tagName == postBodyElement.tagName) { markedForDelete.Add(postBodyElement); } postContentsElement = postContentsElement.parentElement; postBodyElement = postBodyElement.parentElement; } if (markedForDelete.Count > 0) { //delete all of the marked elements except the last one for (int i = markedForDelete.Count - 2; i >= 0; i--) { IHTMLDOMNode domNode = (IHTMLDOMNode)markedForDelete[i]; domNode.removeNode(false); } //delete the last node and return its parent as the new post body region element IHTMLElement lastMarkForDeleteElement = (IHTMLElement)markedForDelete[markedForDelete.Count - 1]; IHTMLElement newPostBodyElement = lastMarkForDeleteElement.parentElement; (lastMarkForDeleteElement as IHTMLDOMNode).removeNode(false); return(newPostBodyElement); } } } catch (Exception e) { //This is an error we should look at, but it should not abort template detection. Debug.Fail("Cleanup logic failed with an error", e.ToString()); } return(postBodyElement); }
public static void MergeXmlHeadToHtml(HTMLDocumentClass hDoc, IHTMLDOMNode hHead, XmlNode xHead) { for (int i = 0; i < xHead.ChildNodes.Count; i++) { CopyXmlNodeToHtml(hDoc, hHead, xHead.ChildNodes[i]); } }
public void resizeCol(IfacesEnumsStructsClasses.IHTMLElement e) { int w = getMaxWidthChildren(e); if (e.offsetWidth > w) { return; } IHTMLDOMNode node = (IHTMLDOMNode)(e); int ccount = 0; for (int i = 0; i < node.childNodes.length; i++) { if (!(node.childNodes.item(i) is IfacesEnumsStructsClasses.IHTMLElement)) { continue; } IfacesEnumsStructsClasses.IHTMLElement ne = (IfacesEnumsStructsClasses.IHTMLElement)(node.childNodes.item(i)); if (ne.getAttribute("ltype", 1) != null && ne.getAttribute("ltype", 1).ToString() != "_cellDIV") { continue; } float neww = (float)(ne.offsetWidth) / w * e.offsetWidth; IHTMLDOMNode ne_node = (IHTMLDOMNode)(ne); IfacesEnumsStructsClasses.IHTMLElement cc = (IfacesEnumsStructsClasses.IHTMLElement)(ne_node.firstChild); if (cc != null && cc.tagName == "DIV") { cc.style.width = ((int)neww).ToString() + "px"; } ne.style.width = ((int)neww).ToString() + "px"; } }
private static void AddElementsContainingText(string normalizedText, IHTMLElement fromElement, ArrayList elementList) { int elementCount = 0; IHTMLDOMNode fromNode = fromElement as IHTMLDOMNode; foreach (IHTMLDOMNode childNode in (IHTMLDOMChildrenCollection)fromNode.childNodes) { IHTMLElement child = childNode as IHTMLElement; if (child != null) { if (ContainsNormalizedText(normalizedText, child)) { elementCount++; AddElementsContainingText(normalizedText, child, elementList); } } } if (elementCount == 0) { if (ContainsNormalizedText(normalizedText, fromElement)) { elementList.Add(fromElement); } } }
private string Image(IHTMLDOMNode node) { containsImage_ = true; var source = processingHyperlink_.Length > 0 ? processingHyperlink_ : GetAttribute(node, "SRC"); if (processingBlockquote_) { if (handleImgInBlockQuoteException_) { // Image in Blockquote is omitted due to innerText. // Need to take it out from Blockquote manually. Console.WriteLine("> " + source + " in BlockQuote in " + currentPost_); } else { throw new ImageInBlockQuote(source); } } return(assetDirectory_ == null ? string.Format("![]({0})", source) : string.Format("![]({{{{site.assets_url}}}}{0})", DownloadFile(source))); }
private string Font(IHTMLDOMNode node) { var color = GetAttribute(node, "COLOR"); return(color.Length > 0 ? OuterText(node) : InnerText(node)); }
public NodeAdapter(object node) { _raw = node; _node = node as IHTMLDOMNode; _node2 = node as IHTMLDOMNode2; }
static private string GetAttribute(IHTMLDOMNode node, string name) { var attrs = node.attributes as IHTMLAttributeCollection2; var attr = attrs.getNamedItem(name); return(attr != null && attr.specified ? attr.nodeValue.ToString() : ""); }
/// <summary> /// We don't want to prune elements that have class, style, id, or event attributes. /// It seems like if the author went through the trouble to put these attributes /// on, we shouldn't trim. (Maybe we should even keep any element with any attributes?) /// </summary> private static bool HasInterestingAttributes(IHTMLDOMNode node) { IHTMLAttributeCollection attrs = node.attributes as IHTMLAttributeCollection; if (attrs != null) { foreach (IHTMLDOMAttribute attr in attrs) { if (attr.specified) { string attrName = attr.nodeName as string; if (attrName != null) { attrName = attrName.ToUpperInvariant(); switch (attrName) { case "CLASSNAME": case "CLASS": case "STYLE": case "ID": return(true); } return(attrName.StartsWith("on", StringComparison.OrdinalIgnoreCase)); } } } } return(false); }
private void RemoveById(string id) { HTMLDocument tmp = (HTMLDocument)canvas.Document.DomDocument; IHTMLDOMNode node = tmp.getElementById(id) as IHTMLDOMNode; node.parentNode.removeChild(node); }
/// <summary> /// 要素に対して明示的に指定された属性を取得する /// </summary> /// <remarks> /// HtmlElementが隠ぺいしているMSHTML内のインターフェースから直接取得するため、 /// HtmlElementでは取得できない属性も含まれる可能性がある。 /// HtmlElementから取得できるものだけを取得したい場合はExtractUsableAttributesを使用する。 /// </remarks> /// <param name="sourceElement">属性を取り出したい要素</param> /// <returns>属性名と属性値のペアリスト</returns> public static Dictionary <string, string> ExtractAttributes(HtmlElement sourceElement) { Dictionary <string, string> extractedAttributes = new Dictionary <string, string>(); if (sourceElement == null) { throw new ArgumentNullException("nullが指定されています"); } else if (sourceElement.TagName == "!" || sourceElement.TagName == "?") { // attributesコレクションを取り出せなくて例外が発生するため // 別途処理してしまう。 return(extractedAttributes); } else { IHTMLElement2 domElement = (IHTMLElement2)sourceElement.DomElement; IHTMLDOMNode node = (IHTMLDOMNode)domElement; IHTMLAttributeCollection attributes = (IHTMLAttributeCollection)node.attributes; foreach (IHTMLDOMAttribute attribute in attributes) { if (attribute.specified) { extractedAttributes.Add(attribute.nodeName.ToUpper(), attribute.nodeValue.ToString()); } } return(extractedAttributes); } }
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { HtmlDocument document = browser.Document; HtmlElement head = document.GetElementsByTagName("head")[0]; HTMLDocument mshtml = (HTMLDocument)document.DomDocument; IHTMLDOMNode msHead = null; foreach (IHTMLDOMNode item in mshtml.getElementsByTagName("head")) { msHead = item; } List <IHTMLDOMNode> origins = new List <IHTMLDOMNode>(); foreach (IHTMLDOMNode item in msHead.childNodes) { origins.Add(item); msHead.removeChild(item); } IHTMLStyleSheet sytle = mshtml.createStyleSheet(); sytle.cssText = Style; HtmlElement jquery = document.CreateElement("script"); ((IHTMLScriptElement)jquery.DomElement).text = Resources.jquery; HtmlElement script = document.CreateElement("script"); ((IHTMLScriptElement)script.DomElement).text = Script; head.AppendChild(jquery); head.AppendChild(script); foreach (IHTMLDOMNode item in origins) { msHead.appendChild(item); } UpdateTable(); }
private void webBrowser_Main_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (Config.HighlightMode == HLMode.Compatible) { HTMLDocument document = (HTMLDocument)webBrowser_Main.Document.DomDocument; IHTMLDOMNode bodyNode = (IHTMLDOMNode)webBrowser_Main.Document.Body.DomElement; for (int i = 0; i < answers.Length; i++) { int.TryParse(answers[i], out int judge); if (judge > 0 && judge <= 10) { continue; } HighLightingText(document, bodyNode, answers[i], i); } } else if (Config.HighlightMode == HLMode.Fast) { if (!highlighted) { webBrowser_Main.DocumentText = HighLightingTextFast(webBrowser_Main.DocumentText); highlighted = true; } } }
private string ToAttributeString(IHTMLDOMNode nd) { if (nd.attributes == null) { return(""); } StringBuilder sb = new StringBuilder(); foreach (IHTMLDOMAttribute att in nd.attributes) { if (att.nodeName == "style") { continue; } if (!att.specified) { continue; } if (att.nodeValue == null) { continue; } TreeNode childItem = new TreeNode(); sb.Append(" "); sb.Append(att.nodeName); sb.Append("="); sb.Append("\""); sb.Append(att.nodeValue); sb.Append("\""); } return(sb.ToString()); }
private void writeElement(string type, System.Windows.Forms.HtmlElement element) { sheet.WriteNextRow(); sheet.WriteTextCell(1, _writer.AddSharedString(type)); string attr = null, attrValue = null; IHTMLDOMNode node = element.DomElement as IHTMLDOMNode; IHTMLDOMAttribute2 domAttr = null; int columnIndex = 2; for (; columnIndex - 2 < _excelColumnHeader.Count; columnIndex++) { attr = _excelColumnHeader[columnIndex - 2] as string; if (string.IsNullOrEmpty(attr)) { attrValue = ""; } else { domAttr = node.attributes.getNamedItem(attr); if (domAttr != null) { attrValue = domAttr.value; } } if (attrValue != "null" && string.IsNullOrEmpty(attrValue) == false) { sheet.WriteTextCell(columnIndex, _writer.AddSharedString(attrValue)); } attrValue = null; domAttr = null; } }
private static IEnumerable <IHTMLDOMNode> GetChildren(IHTMLDOMNode node) { for (IHTMLDOMNode child = node.firstChild; child != null; child = child.nextSibling) { yield return(child); } }
public NodeAdapter( object node ) { _raw = node; _node = node as IHTMLDOMNode; _node2 = node as IHTMLDOMNode2; }
/// <summary> /// 创建子节点 /// </summary> /// <param name="parentNode"></param> /// <param name="domnode"></param> /// <returns></returns> private DomTreeNode NewDomNode(IHTMLDOMNode domnode) { string text = ""; if (Commentnode == domnode.nodeName) { if (domnode.nodeValue != null) { text = "<!" + domnode.nodeValue + ">"; } } else { text = string.Format("<{0}{1}>", domnode.nodeName.ToLower(), ToAttributeString(domnode)); } DomTreeNode treeNode = new DomTreeNode(); treeNode.Text = text; treeNode.Tag = domnode; if (!nodeHash.Contains(domnode)) { nodeHash.Add(domnode, treeNode); } return(treeNode); }
private void PopulateNode(IHTMLDOMNode node) { if (node == null) { return; } TreeNode parentTreeNode; IHTMLDOMNode parent; GetOrCreateParent(node, out parentTreeNode, out parent); if (parent == null) { return; } foreach (IHTMLDOMNode childNode in parent.childNodes) { if (childNode.nodeName == Commentnode) { continue; } if (childNode.nodeName == Textnode) { var val = childNode.nodeValue.ToString().Trim().Replace("\n", "").Replace("\t", ""); if (!string.IsNullOrEmpty(val)) { parentTreeNode.Text += string.Format("{0}", childNode.nodeValue); } } else { AddNewNode(parentTreeNode, childNode); } } PopulateNode(parent); }
private void log(TreeNode treeNode) { List <string> log = new List <string>(); log.Add(((IHTMLDOMNode)treeNode.Tag).nodeName); TreeNode pn = treeNode.Parent; while (pn != null) { IHTMLDOMNode n = (IHTMLDOMNode)pn.Tag; string prefix = ""; if (n.attributes != null) { IHTMLDOMAttribute id = n.attributes.item("id") as IHTMLDOMAttribute; if (id != null && id.nodeValue != null) { prefix = "[" + id.nodeValue.ToString() + "]"; } } log.Add(n.nodeName + prefix); pn = pn.Parent; } log.Reverse(); logger.Info(string.Join("/", log.ToArray())); }
void strip_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { ToolStripMenuItem item = e.ClickedItem as ToolStripMenuItem; IHTMLElement element = item.Tag as IHTMLElement; switch (item.Name) { case "copyHTML": Clipboard.SetText(element.outerHTML); break; case "copyInnerHTML": Clipboard.SetText(element.innerHTML); break; case "copyXPATH": Clipboard.SetText(XPathFinder.GetXPath(element, true)); break; case "delete": TreeNode node = this.SelectedNode; if (node != null) { IHTMLDOMNode domNode = node.Tag as IHTMLDOMNode; domNode.parentNode.removeChild(domNode); node.Remove(); } break; default: break; } }
public Data(IHTMLDOMNode pluginNode) { IHTMLDOMNode divHidden = pluginNode.firstChild.nextSibling.nextSibling.nextSibling; IHTMLDOMNode divPluginIdentifier = divHidden.firstChild; IHTMLDOMNode divTitle = divPluginIdentifier.nextSibling; IHTMLDOMNode divUrl = divTitle.nextSibling; IHTMLDOMNode divUsername = divUrl.nextSibling; IHTMLDOMNode divPassword = divUsername.nextSibling; IHTMLDOMNode divExtra = divPassword.nextSibling; IHTMLDOMNode divSubmit = divExtra.nextSibling; this.plugin_identifier = Utils.GetInnerText(divPluginIdentifier); this.title = Utils.GetInnerText(divTitle); this.url = Utils.GetInnerText(divUrl).Replace("&", "&"); this.username = Utils.GetInnerText(divUsername); this.password = Utils.decode64(Utils.GetInnerText(divPassword)); this.extra = Utils.GetInnerText(divExtra); this.usernameField = Utils.GetClassName(divUsername); this.passwordField = Utils.GetClassName(divPassword); this.extraField = Utils.GetClassName(divExtra); this.submitField = Utils.GetClassName(divSubmit); }
private void InsertDOMNodes(IHTMLDOMNode parentnode, TreeNode tree_node, int parentnode_level, int node_level) { string strtotlevel = ""; string strparentlevel = ""; string strnowlevel = ""; if (parentnode.hasChildNodes()) { IHTMLDOMChildrenCollection allchild = (IHTMLDOMChildrenCollection)parentnode.childNodes; int length = allchild.length; string strNode = ""; for (int i = 0; i < length; i++) { IHTMLDOMNode child_node = (IHTMLDOMNode)allchild.item(i); if (child_node.nodeName != "#comment") { if (child_node.nodeName != "#text") { strNode = "<" + (string)child_node.nodeName.ToString().ToUpper().Trim() + ">"; } else { if (child_node.nodeValue != null) { strNode = (string)child_node.nodeValue.ToString().ToUpper().Trim(); } else { continue; } } if (strNode != "") { TreeNode tempnode = tree_node.Nodes.Add(strNode); tempnode.Tag = child_node; strtotlevel = inttotlevel.ToString().PadLeft(4, '0'); //從根到目前層級 strparentlevel = (parentnode_level - 1).ToString().PadLeft(4, '0'); //父層級 strnowlevel = node_level.ToString().PadLeft(4, '0'); //目前層級序號 inttotlevel += 1; //tempnode.Tag = strtotlevel + "\t" + strparentlevel + "\t" + strnowlevel; node_level += 1; //tempnode.Text = tempnode.Tag + tempnode.Text; InsertDOMNodes(child_node, tempnode, inttotlevel, 1); } } } } }
/// <summary> /// Recursive method to walk the DOM, acounts for frames /// </summary> /// <param name="nd">Parent DOM node to walk</param> /// <param name="node">Parent tree node to populate</param> /// <returns></returns> private TreeNode parseNodes(IHTMLDOMNode nd, TreeNode node) { string str = nd.nodeName; TreeNode nextnode = null; //Add a new node to tree if (node != null) nextnode = node.Nodes.Add(str); else nextnode = treeDOM.Nodes.Add(str); //For each child, get children collection //And continue walking up and down the DOM try { //Frame? if (str == FRAMENODE) { //Get the nd.IWebBrowser2.IHTMLDocument3.documentelement and recurse IWebBrowser2 wb = (IWebBrowser2)nd; IHTMLDocument3 doc3 = (IHTMLDocument3)wb.Document; IHTMLDOMNode tempnode = (IHTMLDOMNode)doc3.documentElement; //get the comments for this node, if any IHTMLDOMChildrenCollection framends = (IHTMLDOMChildrenCollection)doc3.childNodes; foreach (IHTMLDOMNode tmpnd in framends) { str = tmpnd.nodeName; if (COMMENTNODE == str) { if (tmpnd.nodeValue != null) str += VALUESEPERATOR + tmpnd.nodeValue.ToString() + VALUESEPERATOR1; if (nextnode != null) nextnode.Nodes.Add(str); } } //parse document parseNodes(tempnode, nextnode); return nextnode; } //Get the DOM collection string strdom = string.Empty; IHTMLDOMChildrenCollection nds = (IHTMLDOMChildrenCollection)nd.childNodes; foreach (IHTMLDOMNode childnd in nds) { strdom = childnd.nodeName; //Attempt to extract text and comments if ((COMMENTNODE == strdom) || (TEXTNODE == strdom)) { if (childnd.nodeValue != null) strdom += VALUESEPERATOR + childnd.nodeValue.ToString() + VALUESEPERATOR1; //Add a new node to tree if (nextnode != null) nextnode.Nodes.Add(strdom); } else { if ((BODYNODE == strdom) && (str == BASENODE)) { //In MSDN, one of the inner FRAMEs BASE element //contains the BODY element??? //Do nothing } else parseNodes(childnd, nextnode); } } } catch (System.InvalidCastException icee) { Console.Write("\r\n InvalidCastException =" + icee.ToString() + "\r\nName =" + str + " \r\n"); } catch (Exception) //Anything else throw it { throw; } return nextnode; }
private static void AddElementsContainingText(IHTMLDOMNode node, string text, ArrayList list, Hashtable visitedNodes) { IHTMLElement element = node as IHTMLElement; if (element != null) { if (visitedNodes.ContainsKey(element.sourceIndex)) { return; } else visitedNodes.Add(element.sourceIndex, text); } IHTMLDOMChildrenCollection children = (IHTMLDOMChildrenCollection)node.childNodes; IHTMLDOMNode elementNode = null; foreach (IHTMLDOMNode childNode in children) { if (childNode.nodeType == HTMLDOMNodeTypes.TextNode) { if (TextNodeContainsText(childNode, text)) list.Add(node); } else AddElementsContainingText(childNode, text, list, visitedNodes); if (elementNode != null) break; } }
/// <summary> /// Determine whether a node can be considered whitespace. /// /// A node can be whitespace if: /// - It is a text node that does not contain non-whitespace chars /// - It is a tag that, if empty, is invisible; and does not /// have a class, style, or id /// /// But not if: /// - It is a tag that has children /// </summary> private static bool IsWhitespaceNode(IHTMLDOMNode node) { switch (node.nodeType) { case HTMLDocumentHelper.HTMLDOMNodeTypes.ElementNode: { // No element with descendants should be trimmed. // (If the descendants are all whitespace, they need to // have been pruned by now.) if (node.firstChild != null) return false; switch (node.nodeName.ToUpperInvariant()) { // These are all the tags that count as whitespace. // We might want to back off on some of these tags, // and just look for ones that are commonly generated // by our WYSIWYG editor. case "BR": case "P": case "DIV": case "BLOCKQUOTE": case "UL": case "OL": case "TABLE": case "FORM": case "SPAN": case "TT": case "I": case "B": case "U": case "S": case "STRIKE": case "BIG": case "SMALL": case "EM": case "STRONG": case "DFN": case "CODE": case "SAMP": case "KBD": case "VAR": case "CITE": case "ABBR": case "ACRONYM": case "SUB": case "SUP": return !HasInterestingAttributes(node); default: return false; } } case HTMLDocumentHelper.HTMLDOMNodeTypes.TextNode: { string s = node.nodeValue.ToString(); s = HtmlUtils.UnEscapeEntities(s, HtmlUtils.UnEscapeMode.NonMarkupText); foreach (char c in s) { if (!char.IsWhiteSpace(c)) return false; } return true; } default: // Any non-text non-element nodes will be kept return false; } }
public ElementNode(IHTMLDOMNode node) { this.node = node; document = (IHTMLDocument2) ((IHTMLDOMNode2) node).ownerDocument; }
/// <summary> /// Used as a part of HTML thinning to remove extraneous child nodes from an HTMLDOMNode /// </summary> /// <param name="node">The node whose children should be stripped</param> /// <returns>An HTML string with the DOMNodes cleaned out</returns> private static void StripChildNodes(IHTMLDOMNode node, StringBuilder escapedText, bool preserveImages, TickableProgressTick progress) { // is this a text node? If so, just get the text and return it if (node.nodeType == HTMLDocumentHelper.HTMLDOMNodeTypes.TextNode) escapedText.Append(HttpUtility.HtmlEncode(node.nodeValue.ToString())); else { progress.Tick(); bool tagStillOpen = false; ArrayList preserveTags = PreserveTags; if (preserveImages) preserveTags = PreserveTagsWithImages; // if we're in an element node (a tag) and we should preserve the tag, // append it to the returned text if (preserveTags.Contains(node.nodeName)) { // Append the opening tag element, with any extraneous // attributes stripped escapedText.Append("<" + node.nodeName); StripAttributes((IHTMLElement)node, escapedText); // if the element has no children, we can simply close out the tag if (!node.hasChildNodes()) { if (node.nodeName == HTMLTokens.IFrame) escapedText.Append("></" + node.nodeName + ">"); else escapedText.Append("/>"); } else // the element has children, leave the tag open { escapedText.Append(">"); tagStillOpen = true; } } else if (ReplaceTags.Contains(node.nodeName)) { // If there are no children, just emit the replacement tag if (!node.hasChildNodes()) { // Replace the tag escapedText.Append("<" + (string)ReplaceTags[node.nodeName] + "/>"); } else { if (!IsChildlessTag((string)ReplaceTags[node.nodeName])) { escapedText.Append("<" + (string)ReplaceTags[node.nodeName] + ">"); } // Since there are children, we're going to emit the replacement // tag at the end of this node tagStillOpen = true; } } if (node.firstChild != null) { StripChildNodes(node.firstChild, escapedText, preserveImages, progress); } // put a closing tag in for the current element (because we left it open in case of children) if (tagStillOpen) { if (PreserveTags.Contains(node.nodeName)) escapedText.Append("</" + node.nodeName + ">"); else if (ReplaceTags.Contains(node.nodeName)) { if (!IsChildlessTag((string)ReplaceTags[node.nodeName])) escapedText.Append("</" + (string)ReplaceTags[node.nodeName] + ">"); else escapedText.Append("<" + (string)ReplaceTags[node.nodeName] + "/>"); } } } if (node.nextSibling != null) { StripChildNodes(node.nextSibling, escapedText, preserveImages, progress); } }
/// <summary> /// Returns true if a text node contains the specified text. /// </summary> /// <param name="textNode"></param> /// <param name="text"></param> /// <returns></returns> private static bool TextNodeContainsText(IHTMLDOMNode textNode, string text) { Debug.Assert(textNode.nodeType == HTMLDOMNodeTypes.TextNode); return textNode.nodeValue.ToString().Trim().IndexOf(text.Trim(), StringComparison.OrdinalIgnoreCase) != -1; }
public BlockCommandExecutionContext(IHTMLElement rootElement) { //note: we need to insert a block element with a text node into the DOM and guarantee //and guarantee neither node gets orphaned as part of the command execution. _rootElement = GetEditableParent(rootElement); IHTMLDocument2 document = rootElement.document as IHTMLDocument2; _tempBlockElement = document.createElement("p"); _tempBlockElement.innerText = "tempHTML"; (_rootElement as IHTMLDOMNode).appendChild(_tempBlockElement as IHTMLDOMNode); _tempTextNode = (_tempBlockElement as IHTMLDOMNode).firstChild; }
public ElementNode(IHTMLDocument2 document) { this.document = document; node = (IHTMLDOMNode) ((IHTMLDocument3)document).documentElement; }
/// <summary> /// We don't want to prune elements that have class, style, id, or event attributes. /// It seems like if the author went through the trouble to put these attributes /// on, we shouldn't trim. (Maybe we should even keep any element with any attributes?) /// </summary> private static bool HasInterestingAttributes(IHTMLDOMNode node) { IHTMLAttributeCollection attrs = node.attributes as IHTMLAttributeCollection; if (attrs != null) { foreach (IHTMLDOMAttribute attr in attrs) { if (attr.specified) { string attrName = attr.nodeName as string; if (attrName != null) { attrName = attrName.ToUpperInvariant(); switch (attrName) { case "CLASSNAME": case "CLASS": case "STYLE": case "ID": return true; } return attrName.StartsWith("on", StringComparison.OrdinalIgnoreCase); } } } } return false; }
public ElementNode(ElementNode element) { node = element.node; document = element.document; }
/// <summary> /// Returns true if the node itself was pruned. /// </summary> public static bool TrimWhitespace(IHTMLDOMNode node, bool trimBegin, bool trimEnd) { if (node.nodeType == HTMLDocumentHelper.HTMLDOMNodeTypes.ElementNode) { if (trimBegin) { // Recurse in a forward depth-first direction, // until either all the children are pruned // or one of them isn't pruned. IHTMLDOMNode child; while (null != (child = node.firstChild)) { if (!TrimWhitespace(child, true, false)) break; } } if (trimEnd) { // Recurse in a backward depth-first direction. // Again, stop as soon as a child is not pruned. IHTMLDOMNode child; while (null != (child = LastChild(node))) { if (!TrimWhitespace(child, false, true)) break; } } } bool prune = IsWhitespaceNode(node); if (prune) node.parentNode.removeChild(node); return prune; }
internal WrappedWebElement(IeWrapper parent, IHTMLDOMNode node) { this.parent = parent; this.node = (IHTMLElement) node; }
/// <summary> /// Get the last child of a node. Would be great if there was /// a cheaper way to do this. /// </summary> private static IHTMLDOMNode LastChild(IHTMLDOMNode parent) { IHTMLDOMNode child = parent.firstChild; while (child != null && child.nextSibling != null) child = child.nextSibling; return child; }
private void ReplaceNodeTextWithNBSP(IHTMLDOMNode node) { const string NBSP = " "; string nodeText = (string)node.nodeValue; int length = nodeText.Length; StringBuilder sb = new StringBuilder(length * NBSP.Length); for (int i = 0; i < length; i++) { sb.Append(NBSP); } node.nodeValue = sb.ToString(); }
private static bool GetEleParentFrames(IHTMLDOMNode root, IHTMLDOMNode node, List<IHTMLDOMNode> frames) { bool flag = false; if (root == node) { return true; } bool flag2 = false; switch (root.nodeName.ToLower()) { case "frame": case "iframe": flag2 = true; break; } IHTMLDOMChildrenCollection childNodes = null; if (flag2) { IWebBrowser2 browser = root as IWebBrowser2; if (browser != null) { IHTMLDocument2 document = browser.Document as IHTMLDocument2; if (document != null) { IHTMLDOMNode parentElement = document.body.parentElement as IHTMLDOMNode; childNodes = parentElement.childNodes as IHTMLDOMChildrenCollection; } } } if (childNodes == null) { childNodes = root.childNodes as IHTMLDOMChildrenCollection; } if (childNodes == null) { return false; } for (int i = 0; i < childNodes.length; i++) { IHTMLDOMNode node3 = childNodes.item(i) as IHTMLDOMNode; if (GetEleParentFrames(node3, node, frames)) { if (flag2) { frames.Add(root); } flag = true; } } return flag; }
void addLoadingIcon(IHTMLDOMNode div) { HTMLDivElement loadingIconDivOuter = (HTMLDivElement)document.createElement("div"); loadingIconDivOuter.className = "libron_loading_icon_div_outer"; loadingIconDivOuter.style.cssText = "padding:7px;border:1px solid #cbc6bd;background:#e8e4db;-moz-border-radius:5px;-webkit-border-radius:5px;font-size:12px;"; HTMLDivElement loadingIconDiv = (HTMLDivElement)document.createElement("div"); loadingIconDiv.className = "libron_loading_icon_div"; loadingIconDiv.innerHTML = "<span style='color:#666;'>図書館を検索中</span> <image style='vertical-align:middle;' src='" + loadingIcon + "'><br /><br /><br />"; loadingIconDivOuter.appendChild((IHTMLDOMNode)loadingIconDiv); div.appendChild((IHTMLDOMNode)loadingIconDivOuter); }
/// <summary> /// IHTMLDOMNode から HtmlNode を作成 /// </summary> /// <param name="node"></param> /// <returns></returns> protected HtmlNode LoadHtmlNode(IHTMLDOMNode node) { var nn = new HtmlNode(node.nodeName, node.nodeValue.ToString()); if (nn.TagName == "#text") return nn; if (nn.TagName == "#comment") { // append comment tag. nn.TagName = "comment"; string v = System.Web.HttpUtility.HtmlEncode(nn.Value); nn.Children.Add(new HtmlNode("#text", v)); nn.Value = v; return nn; } // append attributes IHTMLAttributeCollection attrs = node.attributes; if (attrs != null) { foreach (IHTMLDOMAttribute at in attrs) { if (at.specified) { string nodeValue = ""; if (at.nodeValue != null) nodeValue = at.nodeValue.ToString(); nn.Attrs.Add(new HtmlAttr { Key = at.nodeName, Value = nodeValue }); } } } var col = node.childNodes as IHTMLDOMChildrenCollection; if (col != null) { foreach (IHTMLDOMNode nd in col) { HtmlNode el = LoadHtmlNode(nd); el.Parent = nn; nn.Children.Add(el); } if (nn.Children.Count > 0 && nn.Children[0].TagName == "#text") nn.Value = nn.Children[0].Value; if (nn.Children.Count > 0 && nn.Children[0].TagName == "#comment") nn.Value = nn.Children[0].Value; } return nn; }
void removeLoadingIcon(IHTMLDOMNode div) { for (int i = 0; i < ((IHTMLDOMChildrenCollection)div.childNodes).length; i++) { object elm = ((IHTMLDOMChildrenCollection)div.childNodes).item(i); if (elm.GetType().Name == "HTMLDivElementClass") { if (((HTMLDivElement)elm).className == "libron_loading_icon_div_outer") { div.removeChild((IHTMLDOMNode)elm); return; } } } }
private void ClearNodeContent(IHTMLDOMNode node) { foreach (IHTMLDOMNode childNode in node.childNodes as IHTMLDOMChildrenCollection) { switch (childNode.nodeType) { case HTMLDocumentHelper.HTMLDOMNodeTypes.ElementNode: ClearNodeContent(childNode); break; case HTMLDocumentHelper.HTMLDOMNodeTypes.TextNode: ReplaceNodeTextWithNBSP(childNode); break; } } }
void addLink(XmlDocument xdoc, IHTMLDOMNode div, string isbn, string title) { removeLoadingIcon((IHTMLDOMNode)div); XmlNode apibookstatus = xdoc.SelectSingleNode("result/books/book[@isbn='" + isbn + "']/system[@systemid='" + selectedSystemId + "']/status"); XmlNode libkeys = null; List<string> calil_library_links = new List<string>(); HTMLDivElement linkDivOuter = (HTMLDivElement)document.createElement("div"); HTMLDivElement linkDiv = (HTMLDivElement)document.createElement("div"); linkDiv.className = "libron_link_div"; linkDiv.style.cssText = "padding:7px;border:1px solid #cbc6bd;background:#e8e4db;-moz-border-radius:5px;-webkit-border-radius:5px;font-size:12px;"; string tweet_body = null; string twitter_link_inner = null; string libreq_link = ""; if (apibookstatus != null && apibookstatus.InnerText == "Error") { HTMLDivElement error_link = (HTMLDivElement)document.createElement("div"); error_link.innerHTML = "<span>エラーが発生しました <image src='" + ngIcon + "'></span>"; linkDiv.appendChild((IHTMLDOMNode)error_link); } else { XmlNode reserveurl = null; reserveurl = xdoc.SelectSingleNode("result/books/book[@isbn='" + isbn + "']/system[@systemid='" + selectedSystemId + "']/reserveurl"); libkeys = xdoc.SelectSingleNode("result/books/book[@isbn='" + isbn + "']/system[@systemid='" + selectedSystemId + "']/libkeys"); foreach (XmlNode libkey in libkeys.ChildNodes) { string calil_library_link = "<a href='http://calil.jp/library/search?s=" + selectedSystemId + "&k=" + HttpUtility.UrlEncode(libkey.InnerText) + "' target='_blank'>" + libkey.InnerText + "(" + libkey + ")" + "</a>"; calil_library_links.Add(calil_library_link); } if (calil_library_links.Count > 0) { HTMLDivElement ok_link = (HTMLDivElement)document.createElement("div"); if (reserveurl != null) { ok_link.innerHTML = "<span>» <a target='_blank' href='" + reserveurl.InnerText + "'>" + selectedSystemName + "で予約する</a> <image style='vertical-align:middle;' src='" + okIcon + "'></span>"; } else { ok_link.innerHTML = "<span style='color:#666666;'>" + selectedSystemName + "に蔵書あり <image style='vertical-align:middle;' src='" + okIcon + "'> " + string.Join("・", calil_library_links.ToArray()) + "</span>"; } linkDiv.appendChild((IHTMLDOMNode)ok_link); tweet_body = "「" + title + "」を" + selectedSystemName + "の図書館" + "で予約しました。 http://libron.net"; twitter_link_inner = "「予約したよ」とつぶやく"; } else { HTMLDivElement ng_message = (HTMLDivElement)document.createElement("div"); ng_message.innerHTML = "<span style='color:#666666;'>" + selectedSystemName + "には見つかりません <image style='vertical-align:middle;' src='" + ngIcon + "'></span>"; linkDiv.appendChild((IHTMLDOMNode)ng_message); tweet_body = "@libreq " + isbn + " " + selectedSystemName + " 「" + title + "」を図書館にリクエスト。 http://libreq.net"; libreq_link = " <a href='http://libreq.net/top/about' target='_blank'>[これは何?]</a>"; twitter_link_inner = "リクエストをつぶやく(借りられるようになったら通知を受け取れます)"; } } HTMLDivElement calil_link = (HTMLDivElement)document.createElement("div"); calil_link.style.marginTop = "3px"; string calil_url = "http://calil.jp/book/" + isbn; calil_link.innerHTML = "<span>» <a target='_blank' href='" + calil_url + "'>他の図書館で検索する(カーリル)</a> <image style='vertical-align:middle;' src='" + calilIcon + "'> </span>"; linkDiv.appendChild((IHTMLDOMNode)calil_link); if (tweet_body != null) { HTMLDivElement twitter_link = (HTMLDivElement)document.createElement("div"); twitter_link.style.marginTop = "3px"; string twitter_url = "http://twitter.com/home?status=" + HttpUtility.UrlEncode(tweet_body); twitter_link.innerHTML = "<span>» <a target='_blank' href='" + twitter_url + "'>" + twitter_link_inner + "</a>" + libreq_link + " <image style='vertical-align:middle;' src='" + twitterIcon + "'> </span>"; linkDiv.appendChild((IHTMLDOMNode)twitter_link); } linkDivOuter.appendChild((IHTMLDOMNode)linkDiv); div.appendChild((IHTMLDOMNode)linkDivOuter); }
public static string GetInnerText(IHTMLDOMNode node) { IHTMLElement elem = (IHTMLElement)node; return elem.innerText; }
void checkLibrary(string url, IHTMLDOMNode div, string isbn, string title) { try { WebClient wc = new WebClient(); wc.Encoding = Encoding.UTF8; wc.DownloadStringCompleted += (s, e) => { XmlDocument xdoc = new XmlDocument(); xdoc.LoadXml(e.Result); XmlNode apiContinue = xdoc.SelectSingleNode("result/continue"); if (apiContinue.InnerText == "0") { addLink(xdoc, (IHTMLDOMNode)div, isbn, title); } else { XmlNode apiSession = xdoc.SelectSingleNode("result/session"); if (apiSession.InnerText.Length > 0) { string new_url = "http://api.calil.jp/check?appkey=" + appkey + "&session=" + apiSession.InnerText + "&format=xml"; checkLibrary(url, (IHTMLDOMNode)div, isbn, title); } } }; wc.DownloadStringAsync(new Uri(url)); } catch { CheckLibraryError(this, null); } }
/// <summary> /// 得到node的所有父亲frames /// </summary> /// <param name="root"></param> /// <param name="elem"></param> /// <param name="frames"></param> /// <returns></returns> private static bool _getEleParentFrames(IHTMLDOMNode root, IHTMLDOMNode node, List<IHTMLDOMNode> frames) { if (root == node) return true; bool isFrame = false; string tag = root.nodeName.ToLower(); if (tag == "frame" || tag == "iframe") isFrame = true; IHTMLDOMChildrenCollection cs = null; if (isFrame) { IWebBrowser2 pwb = root as IWebBrowser2; if (pwb != null) { IHTMLDocument2 pdoc2 = pwb.Document as IHTMLDocument2; if (pdoc2 != null) { IHTMLDOMNode htmlElem = pdoc2.body.parentElement as IHTMLDOMNode; cs = htmlElem.childNodes as IHTMLDOMChildrenCollection; } } } if (cs == null) { cs = root.childNodes as IHTMLDOMChildrenCollection; } if (cs == null) return false; for (int idx = 0; idx < cs.length; idx++) { IHTMLDOMNode c = cs.item(idx) as IHTMLDOMNode; if (_getEleParentFrames(c, node, frames)) { if (isFrame) frames.Add(root); return true; } } return false; }
/// <summary> /// Returns the right neighbour which is a IHTMLElement in the HTML hierarchy /// </summary> /// <param name="node"></param> /// <returns></returns> public IHTMLElement NextSibiling(IHTMLDOMNode node) { IHTMLDOMNode pnode = node; while (true) { IHTMLDOMNode pnext = pnode.nextSibling; if (pnext == null) //No more break; //See if this is an HTMLElement IHTMLElement elem = pnext as IHTMLElement; if (elem != null) return elem; pnode = pnext; } return null; }
public static string GetClassName(IHTMLDOMNode node) { IHTMLElement elem = (IHTMLElement)node; return elem.className; }