-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathRegexParserUtils.cs
More file actions
198 lines (174 loc) · 7 KB
/
RegexParserUtils.cs
File metadata and controls
198 lines (174 loc) · 7 KB
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
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace TheArtOfDev.HtmlRenderer.Core.Parse
{
/// <summary>
/// Collection of regular expressions used when parsing
/// </summary>
internal static class RegexParserUtils
{
#region Fields and Consts
/// <summary>
/// Extracts the media types from a media at-rule; e.g. @media print, 3d, screen {
/// </summary>
public const string CssMediaTypes = @"@media[^\{\}]*\{";
/// <summary>
/// Extracts defined blocks in CSS.
/// WARNING: Blocks will include blocks inside at-rules.
/// </summary>
public const string CssBlocks = @"[^\{\}]*\{[^\{\}]*\}";
/// <summary>
/// Extracts a number; e.g. 5, 6, 7.5, 0.9
/// </summary>
public const string CssNumber = @"{[0-9]+|[0-9]*\.[0-9]+}";
/// <summary>
/// Extracts css percentages from the string; e.g. 100% .5% 5.4%
/// </summary>
public const string CssPercentage = @"([0-9]+|[0-9]*\.[0-9]+)\%";
/// <summary>
/// Extracts CSS lengths; e.g. 9px 3pt .89em
/// </summary>
public const string CssLength = @"([0-9]+|[0-9]*\.[0-9]+)(em|ex|px|in|cm|mm|pt|pc)";
/// <summary>
/// Extracts line-height values (normal, numbers, lengths, percentages)
/// </summary>
public const string CssLineHeight = "(normal|" + CssNumber + "|" + CssLength + "|" + CssPercentage + ")";
/// <summary>
/// Extracts font-family values
/// </summary>
public const string CssFontFamily = "(\"[^\"]*\"|'[^']*'|\\S+\\s*)(\\s*\\,\\s*(\"[^\"]*\"|'[^']*'|\\S+))*";
/// <summary>
/// Extracts CSS font-styles; e.g. normal italic oblique
/// </summary>
public const string CssFontStyle = "(normal|italic|oblique)";
/// <summary>
/// Extracts CSS font-variant values; e.g. normal, small-caps
/// </summary>
public const string CssFontVariant = "(normal|small-caps)";
/// <summary>
/// Extracts font-weight values; e.g. normal, bold, bolder...
/// </summary>
public const string CssFontWeight = "(normal|bold|bolder|lighter|100|200|300|400|500|600|700|800|900)";
/// <summary>
/// Exracts font sizes: xx-small, larger, small, 34pt, 30%, 2em
/// </summary>
public const string CssFontSize = "(" + CssLength + "|" + CssPercentage + "|xx-small|x-small|small|medium|large|x-large|xx-large|larger|smaller)";
/// <summary>
/// Gets the font-size[/line-height]? on the font shorthand property.
/// Check http://www.w3.org/TR/CSS21/fonts.html#font-shorthand
/// </summary>
public const string CssFontSizeAndLineHeight = CssFontSize + @"(\/" + CssLineHeight + @")?(\s|$)";
/// <summary>
/// the regexes cache that is used by the parser so not to create regex each time
/// </summary>
private static readonly Dictionary<string, Regex> _regexes = new Dictionary<string, Regex>();
#endregion
/// <summary>
/// Get CSS at rule from the given stylesheet.
/// </summary>
/// <param name="stylesheet">the stylesheet data to retrieve the rule from</param>
/// <param name="startIdx">the index to start the search for the rule, on return will be the value of the end of the found rule</param>
/// <returns>the found at rule or null if not exists</returns>
public static string GetCssAtRules(string stylesheet, ref int startIdx)
{
startIdx = stylesheet.IndexOf('@', startIdx);
if (startIdx > -1)
{
int count = 1;
int endIdx = stylesheet.IndexOf('{', startIdx);
if (endIdx > -1)
{
endIdx++; // to prevent IndexOutOfRangeException at line 113. When '}' is last character in 'stylesheet' variable
while (count > 0 && endIdx < stylesheet.Length)
{
if (stylesheet[endIdx] == '{')
{
count++;
}
else if (stylesheet[endIdx] == '}')
{
count--;
}
endIdx++;
}
if (endIdx < stylesheet.Length)
{
var atrule = stylesheet.Substring(startIdx, endIdx - startIdx + 1);
startIdx = endIdx;
return atrule;
}
}
}
return null;
}
/// <summary>
/// Extracts matches from the specified source
/// </summary>
/// <param name="regex">Regular expression to extract matches</param>
/// <param name="source">Source to extract matches</param>
/// <returns>Collection of matches</returns>
public static MatchCollection Match(string regex, string source)
{
var r = GetRegex(regex);
return r.Matches(source);
}
/// <summary>
/// Searches the specified regex on the source
/// </summary>
/// <param name="regex"></param>
/// <param name="source"></param>
/// <returns></returns>
public static string Search(string regex, string source)
{
int position;
return Search(regex, source, out position);
}
/// <summary>
/// Searches the specified regex on the source
/// </summary>
/// <param name="regex"></param>
/// <param name="source"></param>
/// <param name="position"> </param>
/// <returns></returns>
public static string Search(string regex, string source, out int position)
{
MatchCollection matches = Match(regex, source);
if (matches.Count > 0)
{
position = matches[0].Index;
return matches[0].Value;
}
else
{
position = -1;
}
return null;
}
/// <summary>
/// Get regex instance for the given regex string.
/// </summary>
/// <param name="regex">the regex string to use</param>
/// <returns>the regex instance</returns>
private static Regex GetRegex(string regex)
{
Regex r;
if (!_regexes.TryGetValue(regex, out r))
{
r = new Regex(regex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
_regexes[regex] = r;
}
return r;
}
}
}