Parses the next tag that matches the specified tag name. Returns information on the next occurrence of the specified tag or null if none found. Name of the tags to parse (null to parse all tags). True if a tag was parsed or false if the end of the document was reached.
(out HtmlTag tag, string name = null)
| 109 | /// <param name="name">Name of the tags to parse (null to parse all tags).</param> |
| 110 | /// <returns>True if a tag was parsed or false if the end of the document was reached.</returns> |
| 111 | public bool ParseNext(out HtmlTag tag, string name = null) |
| 112 | { |
| 113 | tag = new HtmlTag(); |
| 114 | |
| 115 | // Loop until match is found or there are no more tags |
| 116 | while (MoveToNextTag()) |
| 117 | { |
| 118 | // Skip opening '<' |
| 119 | Move(); |
| 120 | |
| 121 | char c = Peek(); |
| 122 | if (c == '!' && Peek(1) == '-' && Peek(2) == '-') |
| 123 | { |
| 124 | // Skip over comments |
| 125 | const string endComment = "-->"; |
| 126 | _pos = _html.IndexOf(endComment, _pos, StringComparison.Ordinal); |
| 127 | NormalizePosition(); |
| 128 | Move(endComment.Length); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | // Skip leading slash |
| 133 | bool isLeadingSlash = c == '/'; |
| 134 | if (isLeadingSlash) |
| 135 | Move(); |
| 136 | |
| 137 | // Dont process if wrong slash is used. |
| 138 | if (c =='\\') |
| 139 | return false; |
| 140 | |
| 141 | // Parse tag |
| 142 | bool result = ParseTag(ref tag, name); |
| 143 | |
| 144 | // Because scripts may contain tag characters, we need special handling to skip over script contents |
| 145 | if (_scriptBegin) |
| 146 | { |
| 147 | const string endScript = "</script"; |
| 148 | _pos = _html.IndexOf(endScript, _pos, StringComparison.OrdinalIgnoreCase); |
| 149 | NormalizePosition(); |
| 150 | Move(endScript.Length); |
| 151 | SkipWhitespace(); |
| 152 | if (Peek() == '>') |
| 153 | Move(); |
| 154 | } |
| 155 | |
| 156 | if (result) |
| 157 | { |
| 158 | if (isLeadingSlash) |
| 159 | { |
| 160 | // Tag starts with '/' |
| 161 | tag.StartPosition--; |
| 162 | tag.IsLeadingSlash = true; |
| 163 | } |
| 164 | return true; |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 |