/// <summary> /// 内部函数:几个属性的Set的提取方法。 /// 功能:Set属性值。 /// </summary> /// <param name="key">属性名</param> /// <param name="value">属性值</param> private void SetValue(string key, string value) { XhtmlAttribute newItem = new XhtmlAttribute(key, value); XhtmlAttribute oldItem; if (this.Attributes.Contains(key, out oldItem)) { this.Attributes.Replace(newItem, oldItem); } else { this.Attributes.Add(newItem); } }
/// <summary> /// Jeelu.com重写。 /// </summary> public override bool Equals(object obj) { //return base.Equals(obj); XhtmlAttribute att = (XhtmlAttribute)obj; bool boolKey = false; bool boolValue = false; if (this.Key == att.Key) { boolKey = true; } if (this.Value == att.Value) { boolValue = true; } return(boolKey & boolValue); }
/// <summary> /// 设置具有指定名称的属性的值。 /// </summary> /// <param name="key">要创建或更改的属性的名称。这是限定名。如果该名称包含一个冒号,则将其解析为前缀和本地名称两个部分。</param> /// <param name="value">要为此属性设置的值。</param> public virtual void SetAttribute(string key, string value) { /// 如果是以下三种类型,不能写入属性 if (this is XhtmlText || this is XhtmlComment || this is XhtmlCData) { Debug.Fail(this.GetType().Name + " cannot SetAttribute!"); return; } XhtmlAttribute newXhtmlatt = new XhtmlAttribute(key, value); XmlAttribute attributeNode = this.GetAttributeNode(key); if (attributeNode == null) { XmlDocument doc = null; if (this.AsXmlNode is XmlDocument) { doc = (XmlDocument)this.AsXmlNode; } else { doc = this.AsXmlNode.OwnerDocument; } attributeNode = doc.CreateAttribute(key); attributeNode.Value = value; if (this.AsXmlNode is XmlDocument) { doc.DocumentElement.Attributes.Append(attributeNode); } else { this.AsXmlNode.Attributes.Append(attributeNode); } //this.Attributes.Add(newXhtmlatt); } else { attributeNode.Value = value; this.Attributes.Replace(newXhtmlatt, this.Attributes[key]); } }
/// <summary> /// 添加指定的Jeelu.XhtmlAttribute。如果该项已存在,则替换其值;如果不存在,则增加该项。 /// </summary> public virtual void SetAttribute(XhtmlAttribute attribute) { this.SetAttribute(attribute.Key, attribute.Value); }
/// <summary> /// This will parse a string containing HTML and will produce a domain tree. /// </summary> /// <param name="html">The HTML to be parsed</param> /// <param name="isRemoveEmptyElementText">The default mechanism will extract a pure DOM tree, which will contain many text nodes containing just whitespace (carriage returns etc.) However, with normal parsing, these are useless and only serve to complicate matters. Therefore, this option exists to automatically remove those empty text nodes.</param> /// <returns>A tree representing the elements</returns> public static XhtmlTCollection <XhtmlElement> Parse(string html, bool isRemoveEmptyElementText) { XhtmlTCollection <XhtmlElement> nodes = new XhtmlTCollection <XhtmlElement>(); html = PreprocessScript(html, "script"); html = PreprocessScript(html, "style"); html = RemoveComments(html); html = RemoveSGMLComments(html); StringCollection tokens = GetTokens(html); int index = 0; XhtmlSection element = null; while (index < tokens.Count) { if ("<".Equals(tokens[index])) { // Read open tag index++; if (index >= tokens.Count) { break; } string tag_name = tokens[index]; index++; element = new XhtmlSection(tag_name); // read the attributes and values while (index < tokens.Count && !">".Equals(tokens[index]) && !"/>".Equals(tokens[index])) { string attribute_name = tokens[index]; index++; if (index < tokens.Count && "=".Equals(tokens[index])) { index++; string attribute_value; if (index < tokens.Count) { attribute_value = tokens[index]; } else { attribute_value = null; } index++; XhtmlAttribute attribute = new XhtmlAttribute(attribute_name, XhtmlEncoder.Decode(attribute_value)); element.Attributes.Add(attribute); } else if (index < tokens.Count) { // Null-value attribute XhtmlAttribute attribute = new XhtmlAttribute(attribute_name, null); element.Attributes.Add(attribute); } } nodes.Add(element); if (index < tokens.Count && "/>".Equals(tokens[index])) { element.IsTerminated = true; index++; element = null; } else if (index < tokens.Count && ">".Equals(tokens[index])) { index++; } } else if (">".Equals(tokens[index])) { index++; } else if ("</".Equals(tokens[index])) { // Read close tag index++; if (index >= tokens.Count) { break; } string tag_name = tokens[index]; index++; int open_index = FindTagOpenNodeIndex(nodes, tag_name); if (open_index != -1) { MoveNodesDown(ref nodes, open_index + 1, (XhtmlElement)nodes[open_index]); } else { // Er, there is a close tag without an opening tag!! } // Skip to the end of this tag while (index < tokens.Count && !">".Equals(tokens[index])) { index++; } if (index < tokens.Count && ">".Equals(tokens[index])) { index++; } element = null; } else { // Read text string value = tokens[index]; if (isRemoveEmptyElementText) { value = RemoveWhitespace(value); } value = DecodeScript(value); if (isRemoveEmptyElementText && value.Length == 0) { // We do nothing } else { if (!(element != null && element.NoEscaping)) { value = XhtmlEncoder.Decode(value); } XhtmlText textNode = new XhtmlText(element, value); nodes.Add(textNode); //HtmlText node = new HtmlText(value); //nodes.Add(node); } index++; } } return(nodes); }