///////////////////////////////////////////////////////////////////////////////// #region 񃧯 ///////////////////////////////////////////////////////////////////////////////// /// <summary> /// This constructs a new HTML element with the specified tag name. /// </summary> public CHtmlElement(string name) { System.Diagnostics.Debug.Assert(name != null && CHtmlUtil.ExistWhiteSpaceChar(name) == false); m_nodes = new CHtmlNodeCollection(this); m_name = name.Trim().ToLower(); }
/////////////////////////////////////////////////////////////////////////////// /// <summary> /// »ý¼ºÀÚ /// </summary> public DCssProperty(string name, string value) { System.Diagnostics.Debug.Assert(name != null && CHtmlUtil.ExistWhiteSpaceChar(name) == false); System.Diagnostics.Debug.Assert(value != null); m_propertyName = name.ToLower(); m_propertyValue = value.Trim(CHtmlUtil.WhiteSpaceCharsArray); }
/////////////////////////////////////////////////////////////////////////////// /// <summary> /// »ý¼ºÀÚ /// </summary> public CHtmlAttribute(string name, string value) { System.Diagnostics.Debug.Assert(name != null && CHtmlUtil.ExistWhiteSpaceChar(name) == false); System.Diagnostics.Debug.Assert(value != null); m_attributeName = name.Trim().ToLower(); m_attributeValue = value; }
///////////////////////////////////////////////////////////////////////////////// /// <summary> /// Àڽijëµå¸¦ Æ÷ÇÔÇÑ Àüü html ¹Ýȯ /// </summary> internal void TransformHTML(StringBuilder writer) { System.Diagnostics.Debug.Assert(writer != null); writer.Append(m_attributeName); writer.Append("=\""); writer.Append(CHtmlUtil.TranStrToHtmlText(m_attributeValue)); writer.Append("\""); }
///////////////////////////////////////////////////////////////////////////////// #region 分割 Token ///////////////////////////////////////////////////////////////////////////////// protected void ParseTokens() { List <StyleSheetToken> subTokens = new List <StyleSheetToken>(8); int index = 0; while (index < m_input.Length) { try { // While Space if (index < m_input.Length && CHtmlUtil.IsWhiteSpaceChar(m_input[index]) == true) { index += 1; } // CDO else if (index + 3 < m_input.Length && m_input.IndexOf("<!--", index, 4) != -1) { index += 4; } // CDC else if (index + 2 < m_input.Length && m_input.IndexOf("-->", index, 3) != -1) { index += 3; } // Comment else if (index + 1 < m_input.Length && m_input.IndexOf("/*", index, 2) != -1) { index = ParseComment(index, subTokens); } // At-Rule else if (index < m_input.Length && m_input[index].Equals('@') == true) { index = ParseAtRule(index, subTokens); } // RuleSet else { index = ParseRuleSet(index, subTokens); } m_tokens.AddRange(subTokens); } catch (ParseExeception) { System.Diagnostics.Debug.Write("ParseTokens parse exeception\n"); break; } } }
///////////////////////////////////////////////////////////////////////////////// /// <summary> /// /// </summary> /// <param name="htmlStream"></param> private Encoding DetectCharset(Stream inStream) { Encoding result = null; string charset = ""; long position = inStream.Position; System.IO.StreamReader reader = new System.IO.StreamReader(inStream); while (reader.EndOfStream == false) { string buffer = reader.ReadLine(); int index = buffer.IndexOf("charset"); if (index != -1 && buffer.Length > "charset".Length) { int startIndex = index + "charset".Length; while (startIndex < buffer.Length && CHtmlUtil.EqualesOfAnyChar(buffer[startIndex], " \r\n\t=\'\"<>")) { ++startIndex; } int endIndex = startIndex + 1; while (endIndex < buffer.Length && !CHtmlUtil.EqualesOfAnyChar(buffer[endIndex], " \r\n\t=\'\"<>")) { ++endIndex; } if (startIndex < buffer.Length && endIndex - startIndex > 0) { charset = buffer.Substring(startIndex, endIndex - startIndex); try { result = Encoding.GetEncoding(charset); break; } catch (Exception) { } } } } inStream.Position = position; return(result); }
///////////////////////////////////////////////////////////////////////////////// #region ///////////////////////////////////////////////////////////////////////////////// /// <summary> /// This will return the full HTML to represent this node (and all child nodes) /// </summary> internal override void TransformHTML(StringBuilder writer, int indentDepth) { System.Diagnostics.Debug.Assert(writer != null); writer.Append(CHtmlUtil.TranStrToHtmlText(m_text)); }
///////////////////////////////////////////////////////////////////////////// /// <summary> /// /// </summary> public void AssertValid() { System.Diagnostics.Debug.Assert(m_propertyName != null && CHtmlUtil.ExistWhiteSpaceChar(m_propertyName) == false, "Member variable, m_propertyName is invalid"); System.Diagnostics.Debug.Assert(m_propertyValue != null, "Member variable, m_propertyValue is invalid"); }
///////////////////////////////////////////////////////////////////////////////// /// <summary> /// /// </summary> /// <param name="input"></param> /// <returns></returns> public void ResolvePropertyList(string input, DCssPropertyCollection result) { System.Diagnostics.Debug.Assert(input != null); System.Diagnostics.Debug.Assert(result != null); PropertyListTokenType type = PropertyListTokenType.Name; int index = 0; string name = "", value = ""; while (index < input.Length) // PropertyList: S* declaration? [ ';' S* declaration? ]*; { // declaration: DELIM? property S* ':' S* value; int startIndex = 0; if (type == PropertyListTokenType.Name) { startIndex = input.IndexOfAny(":=".ToCharArray(), index); } else if (type == PropertyListTokenType.Value) { startIndex = input.IndexOf(';', index); } if (type == PropertyListTokenType.Name && startIndex != -1 && startIndex - index > 0 && (input[startIndex].Equals(':') || input[startIndex].Equals('='))) { name = input.Substring(index, startIndex - index); name = name.Trim(CHtmlUtil.WhiteSpaceCharsArray); if (name.Length > 0 && CHtmlUtil.ExistWhiteSpaceChar(name) == false) { type = PropertyListTokenType.Value; } } else if (type == PropertyListTokenType.Value && startIndex != -1 && startIndex - index > 0 && input[startIndex].Equals(';')) { value = input.Substring(index, startIndex - index); value = value.Trim(CHtmlUtil.WhiteSpaceCharsArray); if (value.Length > 0) { DCssProperty property = new DCssProperty(name, value); result.AddLast(property); } type = PropertyListTokenType.Name; } else if (type == PropertyListTokenType.Value && startIndex == -1) { value = input.Substring(index); value = value.Trim(CHtmlUtil.WhiteSpaceCharsArray); if (value.Length > 0) { DCssProperty property = new DCssProperty(name, value); result.AddLast(property); } type = PropertyListTokenType.Name; } else { type = PropertyListTokenType.Name; } if (startIndex == -1) { index = input.Length; } else { index = startIndex + 1; } } }
///////////////////////////////////////////////////////////////////////////////// #region ///////////////////////////////////////////////////////////////////////////////// /// <summary> /// /// </summary> /// <param name="htmlStream"></param> /// <returns></returns> private Encoding DetectCharset(CHtmlNodeCollection nodes) { Encoding result = null; string charset = ""; CHtmlNodeCollection metaNodes = new CHtmlNodeCollection(); CHtmlElement node = nodes["html"] as CHtmlElement; if (node != null) { node = node.Nodes["head"] as CHtmlElement; } if (node != null) { node.Nodes.FindByNameAttribute(metaNodes, "meta", "content", false); } for (int nodeIndex = 0, count = metaNodes.Count; nodeIndex < count; ++nodeIndex) { CHtmlElement metaElement = metaNodes[nodeIndex] as CHtmlElement; if (metaElement != null) { int index = -1; CHtmlAttributeCollection attributes = metaElement.Attributes.FindByName("content"); for (int attributeIndex = 0, attributeCount = attributes.Count; attributeIndex < attributeCount; ++attributeIndex) { CHtmlAttribute attribute = attributes[attributeIndex]; if ((index = attribute.Value.IndexOf("charset")) != -1) { string value = attribute.Value; int startIndex = index + 7; while (startIndex < value.Length && CHtmlUtil.EqualesOfAnyChar(value[startIndex], " =")) { ++startIndex; } int endIndex = startIndex + 1; while (endIndex < value.Length && !CHtmlUtil.EqualesOfAnyChar(value[endIndex], " ")) { ++endIndex; } if (startIndex < value.Length && endIndex - startIndex > 0) { charset = value.Substring(startIndex, endIndex - startIndex); try { result = Encoding.GetEncoding(charset); break; } catch (Exception) { } } } } } } return(result); }