private string PreprocessScript(string input, string tag_name) { StringBuilder stringBuilder = new StringBuilder(); int i = 0; int length = tag_name.Length; while (i < input.Length) { bool flag = false; if (i + length + 1 < input.Length && input.Substring(i, length + 1).ToLower().Equals("<" + tag_name)) { while (i < input.Length) { if (input.Substring(i, 1).Equals(">")) { stringBuilder.Append(">"); i++; } else { if (i + 1 >= input.Length || !input.Substring(i, 2).Equals("/>")) { if (input.Substring(i, 1).Equals("\"")) { stringBuilder.Append("\""); i++; while (i < input.Length && !input.Substring(i, 1).Equals("\"")) { stringBuilder.Append(input.Substring(i, 1)); i++; } if (i < input.Length) { i++; stringBuilder.Append("\""); } } else if (input.Substring(i, 1).Equals("'")) { stringBuilder.Append("'"); i++; while (i < input.Length && !input.Substring(i, 1).Equals("'")) { stringBuilder.Append(input.Substring(i, 1)); i++; } if (i < input.Length) { i++; stringBuilder.Append("'"); } } else { stringBuilder.Append(input.Substring(i, 1)); i++; } continue; } stringBuilder.Append("/>"); i += 2; flag = true; } break; } if (i >= input.Length) { goto IL_314; } if (!flag) { StringBuilder stringBuilder2 = new StringBuilder(); while (i + length + 3 < input.Length && !input.Substring(i, length + 3).ToLower().Equals("</" + tag_name + ">")) { stringBuilder2.Append(input.Substring(i, 1)); i++; } stringBuilder.Append(HtmlParser.EncodeScript(stringBuilder2.ToString())); stringBuilder.Append("</" + tag_name + ">"); if (i + length + 3 < input.Length) { i += length + 3; } } goto IL_301; } stringBuilder.Append(input.Substring(i, 1)); i++; IL_301 :; } IL_314 : return(stringBuilder.ToString()); }
public HtmlNodeCollection Parse(string html) { HtmlNodeCollection htmlNodeCollection = new HtmlNodeCollection(null); html = this.PreprocessScript(html, "script"); html = this.PreprocessScript(html, "style"); html = this.RemoveComments(html); html = this.RemoveSGMLComments(html); StringCollection tokens = this.GetTokens(html); int i = 0; HtmlElement htmlElement = null; HtmlNodeCollection result; while (i < tokens.Count) { if ("<".Equals(tokens[i])) { i++; if (i >= tokens.Count) { result = htmlNodeCollection; return(result); } string name = tokens[i]; i++; htmlElement = new HtmlElement(name); while (i < tokens.Count && !">".Equals(tokens[i]) && !"/>".Equals(tokens[i])) { string name2 = tokens[i]; i++; if (i < tokens.Count && "=".Equals(tokens[i])) { i++; string value; if (i < tokens.Count) { value = tokens[i]; } else { value = null; } i++; HtmlAttribute attribute = new HtmlAttribute(name2, HtmlEncoder.DecodeValue(value)); htmlElement.Attributes.Add(attribute); } else if (i < tokens.Count) { HtmlAttribute attribute = new HtmlAttribute(name2, null); htmlElement.Attributes.Add(attribute); } } htmlNodeCollection.Add(htmlElement); if (i < tokens.Count && "/>".Equals(tokens[i])) { htmlElement.IsTerminated = true; i++; htmlElement = null; } else if (i < tokens.Count && ">".Equals(tokens[i])) { i++; } } else if (">".Equals(tokens[i])) { i++; } else if ("</".Equals(tokens[i])) { i++; if (i >= tokens.Count) { result = htmlNodeCollection; return(result); } string name = tokens[i]; i++; int num = this.FindTagOpenNodeIndex(htmlNodeCollection, name); if (num != -1) { this.MoveNodesDown(ref htmlNodeCollection, num + 1, (HtmlElement)htmlNodeCollection[num]); } while (i < tokens.Count && !">".Equals(tokens[i])) { i++; } if (i < tokens.Count && ">".Equals(tokens[i])) { i++; } htmlElement = null; } else { string text = tokens[i]; if (this.mRemoveEmptyElementText) { text = this.RemoveWhitespace(text); } text = HtmlParser.DecodeScript(text); if (!this.mRemoveEmptyElementText || text.Length != 0) { if (htmlElement == null || !htmlElement.NoEscaping) { text = HtmlEncoder.DecodeValue(text); } HtmlText node = new HtmlText(text); htmlNodeCollection.Add(node); } i++; } } result = htmlNodeCollection; return(result); }