1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
//------------------------------------------------------------------------------
// <copyright file="SimpleParser.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System.Collections;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.RegularExpressions;
using System.Web.Util;
using System.Web.UI.MobileControls;
namespace System.Web.UI.Design.MobileControls.Util
{
[
System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand,
Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)
]
[Obsolete("The System.Web.Mobile.dll assembly has been deprecated and should no longer be used. For information about how to develop ASP.NET mobile applications, see http://go.microsoft.com/fwlink/?LinkId=157231.")]
internal class SimpleParser : BaseParser
{
private const int _stackInitialSize = 100;
private static Regex _unclosedTagRegex = null;
private const RegexOptions _options =
RegexOptions.Singleline | RegexOptions.Multiline;
private const String _pattern =
@"\G<(?<tagname>[\w:\.]+)" +
@"(" +
@"\s+(?<attrname>\w[-\w:]*)(" + // Attribute name
@"\s*=\s*""(?<attrval>[^""]*)""|" + // ="bar" attribute value
@"\s*=\s*'(?<attrval>[^']*)'|" + // ='bar' attribute value
@"\s*=\s*(?<attrval><%#.*?%>)|" + // =<%#expr%> attribute value
@"\s*=\s*(?!'|"")(?<attrval>[^\s=/>]*)(?!'|"")|" + // =bar attribute value
@"(?<attrval>\s*?)" + // no attrib value (with no '=')
@")" +
@")*" +
@"\s*(?<empty>)?>";
//@"\s*(?<empty>/)?>";
private static ElementTable _endTagOptionalElement = null;
private readonly static Regex _tagRegex = new TagRegex();
private readonly static Regex _directiveRegex = new DirectiveRegex();
private readonly static Regex _endtagRegex = new EndTagRegex();
private readonly static Regex _aspCodeRegex = new AspCodeRegex();
private readonly static Regex _aspExprRegex = new AspExprRegex();
private readonly static Regex _databindExprRegex = new DatabindExprRegex();
private readonly static Regex _commentRegex = new CommentRegex();
private readonly static Regex _includeRegex = new IncludeRegex();
private readonly static Regex _textRegex = new TextRegex();
// Regexes used in DetectSpecialServerTagError
private readonly static Regex _gtRegex = new GTRegex();
private readonly static Regex _ltRegex = new LTRegex();
private readonly static Regex _serverTagsRegex = new ServerTagsRegex();
private readonly static Regex _runatServerRegex = new RunatServerRegex();
/* Regex patterns
AspCodeRegex : \G<%(?!@)(?<code>.*?)%>
AspExprRegex : \G<%\s*?=(?<code>.*?)?%>
CommentRegex : \G<%--(([^-]*)-)*?-%>
DataBindExprRegex : \G<%#(?<code>.*?)?%>
DirectiveRegex : \G<%\s*@(\s*(?<attrname>\w+(?=\W))(\s*(?<equal>=)\s*"(?<attrval>[^"]*)"|\s*(?<equal>=)\s*'(?<attrval>[^']*)'|\s*(?<equal>=)\s*(?<attrval>[^\s%>]*)|(?<equal>)(?<attrval>\s*?)))*\s*?%>
EndTagRegex : \G</(?<tagname>[\w:\.]+)\s*>
GTRegex : [^%]>
IncludeRegex : \G<!--\s*#(?i:include)\s*(?<pathtype>[\w]+)\s*=\s*["']?(?<filename>[^\"']*?)["']?\s*-->
LTRegex : <
RunATServerRegex : runat\W*server
ServerTagsRegex : <%(?!#)(([^%]*)%)*?>
TagRegex : \G<(?<tagname>[\w:\.]+)(\s+(?<attrname>[-\w]+)(\s*=\s*"(?<attrval>[^"]*)"|\s*=\s*'(?<attrval>[^']*)'|\s*=\s*(?<attrval><%#.*?%>)|\s*=\s*(?<attrval>[^\s=/>]*)|(?<attrval>\s*?)))*\s*(?<empty>/)?>
TextRegex : \G[^<]+
//SimpleDirectiveRegex simpleDirectiveRegex = new SimpleDirectiveRegex();
*/
// static helper type should not be instantiated.
private SimpleParser() {
}
static SimpleParser()
{
_unclosedTagRegex = new Regex(_pattern, _options);
_endTagOptionalElement = new ElementTable();
/* following defnitions from MSDN Online WorkShop
http://msdn.microsoft.com/workshop/c-frame.htm#/workshop/author/default.asp
*/
_endTagOptionalElement.AddRange(
new String[] {
"area", "base", "basefront", "bgsound", "br",
"col", "colgroup", "dd", "dt", "embed", "frame",
"hr", "img", "input", "isindex", "li", "link",
"meta", "option", "p", "param", "rt"
});
}
/// <summary>
/// Simple parsing to check if input fragment is well-formed,
/// HTML elements that do not required end tags (i.e. <BR>)
/// will be ignored by this parser.
/// </summary>
/// <param name="text">
/// text being parsed
/// </param>
internal static bool IsWellFormed(String text)
{
int textPos = 0;
TagStack stack = new TagStack();
for (;;)
{
Match match = null;
// 1: scan for text up to the next tag.
if ((match = _textRegex.Match(text, textPos)).Success)
{
textPos = match.Index + match.Length;
}
// we might be done now
if (textPos == text.Length)
{
while (!stack.IsEmpty())
{
if (!IsEndTagOptional(stack.Pop()))
{
return false;
}
}
return true;
}
// First check if it's a unclosed tag (i.e. <mobile:Form >)
if ((match = _unclosedTagRegex.Match(text, textPos)).Success)
{
String startTag = match.Groups["tagname"].Value;
stack.Push(startTag);
}
// Check to see if it's a tag
else if ((match = _tagRegex.Match(text, textPos)).Success)
{
// skip
}
// Check to see if it's an end tag
else if ((match = _endtagRegex.Match(text, textPos)).Success)
{
String endTag = match.Groups["tagname"].Value;
bool matched = false;
while (!stack.IsEmpty())
{
String startTag = stack.Pop();
if (String.Compare(endTag, startTag, StringComparison.OrdinalIgnoreCase) != 0)
{
if (IsEndTagOptional(startTag))
{
continue;
}
// no match against start tag that requires an end tag
return false;
}
// we found a match here.
matched = true;
break;
}
if (!matched && stack.IsEmpty())
{
return false;
}
}
// Check to see if it's a directive (i.e. <%@ %> block)
else if ((match = _directiveRegex.Match(text, textPos)).Success)
{
// skip
}
// Check to see if it's a server side include
// e.g. <!-- #include file="foo.inc" -->
else if ((match = _includeRegex.Match(text, textPos)).Success)
{
// skip it
}
// Check to see if it's a comment (<%-- --%> block
// e.g. <!-- Blah! -->
else if ((match = _commentRegex.Match(text, textPos)).Success)
{
// skip
}
// Check to see if it's an asp expression block (i.e. <%= %> block)
else if ((match = _aspExprRegex.Match(text, textPos)).Success)
{
// skip
}
// Check to see if it's a databinding expression block (i.e. <%# %> block)
// This does not include <%# %> blocks used as values for
// attributes of server tags.
else if ((match = _databindExprRegex.Match(text, textPos)).Success)
{
// skip
}
// Check to see if it's an asp code block
else if ((match = _aspCodeRegex.Match(text, textPos)).Success)
{
// skip
}
// Did we process the block that started with a '<'?
if (match == null || !match.Success)
{
// Skip the '<'
textPos++;
}
else
{
textPos = match.Index + match.Length;
}
// we might be done now
if (textPos == text.Length)
{
while (!stack.IsEmpty())
{
if (!IsEndTagOptional(stack.Pop()))
{
return false;
}
}
return true;
}
}
}
private static bool IsEndTagOptional(String element)
{
return (_endTagOptionalElement.Contains(element));
}
/// <summary>
/// Private class used to store lowercase tags in a stack
/// return String.Empty if stack is empty
/// </summary>
[
System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand,
Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)
]
private class TagStack
{
private Stack _tagStack = null;
internal TagStack() : this(_stackInitialSize)
{
}
internal TagStack(int initialCapacity)
{
_tagStack = new Stack(initialCapacity);
}
internal void Push(String tagName)
{
_tagStack.Push(tagName.ToLower(CultureInfo.InvariantCulture));
}
internal String Pop()
{
if (IsEmpty())
{
return String.Empty;
}
return (String)_tagStack.Pop();
}
internal bool IsEmpty()
{
return (_tagStack.Count == 0);
}
}
/// <summary>
/// Private class used to store recognizable lowercase elements
/// return true if element is in the list, otherwise false
/// </summary>
[
System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand,
Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)
]
private class ElementTable
{
private Hashtable _table = null;
internal ElementTable() : this(_stackInitialSize)
{}
internal ElementTable(int initialCapacity)
{
_table = new Hashtable(initialCapacity, StringComparer.OrdinalIgnoreCase);
}
internal void Add(String key)
{
_table.Add(key, true);
}
internal bool Contains(String key)
{
return (_table.Contains(key));
}
internal void AddRange(String[] keysCollection)
{
foreach (String key in keysCollection)
{
this.Add(key);
}
}
}
}
}
|