public void Handle(TokenParser context, ICollection<Token> tokens, char ch) { if (HandleCore(context, tokens, ch) == false) { context.TokenBuilder.Append(ch); } }
protected override bool HandleCore(TokenParser context, ICollection<Token> tokens, char ch) { if (ch == '>') { Token token = context.SwitchState(TokenType.Text, new TextHandler()); tokens.Add(token); if (token.Type == TokenType.OpenTag && (token.Value == "script" || token.Value == "style")) { tokens.Add(context.SwitchState(TokenType.Text, new CDataHandler(string.Format("</{0}", token.Value)))); } return true; } if (ch == '/' || Char.IsWhiteSpace(ch)) { var handler = new AttibuteNameHandler(); Token token = context.SwitchState(TokenType.AttributeName, handler); if (token.Type == TokenType.OpenTag && (token.Value == "script" || token.Value == "style")) { handler.ReplaceNextTagOrTextTokenWithCData = string.Format("</{0}", token.Value); } tokens.Add(token); return true; } return false; }
protected override bool HandleCore(TokenParser context, ICollection<Token> tokens, char ch) { if (ch == '>') { tokens.Add(string.IsNullOrEmpty(ReplaceNextTagOrTextTokenWithCData) ? context.SwitchState(TokenType.Text, new TextHandler()) : context.SwitchState(TokenType.Text, new CDataHandler(ReplaceNextTagOrTextTokenWithCData))); return true; } return false; }
protected override bool HandleCore(TokenParser context, ICollection<Token> tokens, char ch) { if (chars.Contains(ch)) { tokens.Add(context.SwitchState(TokenType.AttributeName, new AttibuteNameHandler { ReplaceNextTagOrTextTokenWithCData = ReplaceNextTagOrTextTokenWithCData })); return true; } return base.HandleCore(context, tokens, ch); }
protected override bool HandleCore(TokenParser context, ICollection<Token> tokens, char ch) { if (state == State.AttributeValueBegin) { if (ch == '"' || ch == '\'') { tokens.Add(context.SwitchState(TokenType.AttributeValue, new AttibuteValueHandler(ch) { ReplaceNextTagOrTextTokenWithCData = ReplaceNextTagOrTextTokenWithCData })); return true; } if (!Char.IsWhiteSpace(ch)) { tokens.Add(context.SwitchState(TokenType.AttributeValue, new AttibuteValueHandler(' ', '\t', '\r', '\x00a0', '\x0085') { ReplaceNextTagOrTextTokenWithCData = ReplaceNextTagOrTextTokenWithCData })); return false; } return true; } if (ch == '=') { state = State.AttributeValueBegin; return true; } if (ch == '/' || Char.IsWhiteSpace(ch)) { tokens.Add(context.SwitchState(TokenType.AttributeName, new AttibuteNameHandler { ReplaceNextTagOrTextTokenWithCData = ReplaceNextTagOrTextTokenWithCData })); return true; } return base.HandleCore(context, tokens, ch); }
protected override bool HandleCore(TokenParser context, ICollection<Token> tokens, char ch) { if (closeToken[index] == ch) { index++; if (index >= closeToken.Length) { tokens.Add(context.SwitchState(TokenType.CloseTag, new TagHandler())); context.TokenBuilder.Append(closeToken.Substring(2)); } return true; } if (index > 0) { context.TokenBuilder.Append(closeToken.Substring(0, index)); index = 0; } return false; }
protected abstract bool HandleCore(TokenParser context, ICollection<Token> tokens, char ch);
public static HtmlNode Parse(string html) { IEnumerable<Token> tokens = new TokenParser().Parse(html); return TreeBuilder.Build(tokens); }