-
Notifications
You must be signed in to change notification settings - Fork 0
/
mucss.cs
190 lines (164 loc) · 5.95 KB
/
mucss.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace mucss
{
/// <summary>μCSS</summary>
public class Stylesheet
{
private string css;
private Dictionary<string,Selector> selectors = new Dictionary<string,Selector>();
#if DEBUG
/// <summary>Parsing log for debugging purposes. Available only when μCSS is compiled with the Debug configuration</summary>
public string log;
#endif
/// <summary>Initialize the μCSS and load the <paramref name="CSS"/> into it.</summary>
/// <param name="CSS">The cascade style sheets to be loaded</param>
public Stylesheet(string CSS)
{
css = CSS;
MatchCollection mc = new Regex(@"[\t\w\d:.#, ]*[\w]*\{[\t\w\r\n-: #;]*\}", RegexOptions.Singleline).Matches(css);
foreach (Match m in mc)
{
Selector sel = new Selector() { Declarations = new Dictionary<string,Declaration>() };
//finding selectors
string selbody = Regex.Replace(m.Value,@"\/\*[\w\d \r\n\t]*\*\/",""); //remove comments
selbody = Regex.Replace(selbody,@"[ \t]","");//remove spaces and tabs
sel.Pattern = Regex.Match(selbody, @"[\w\d-\(\)\[\]:.#, ]*{",RegexOptions.Singleline).Value.Replace("{", "");
sel.OuterCSS = selbody;
selbody = Regex.Replace(selbody, @"[\w\d:.#, ]*[\w]*{", "").Replace("}", ""); //remove selector
sel.InnerCSS = selbody;
//split declarations
string[] decs = selbody.Split(';');
//removing line breaks
for (int i = 0; i < decs.Length; i++)
{
decs[i] = decs[i].Replace("\n","").Replace("\r","");
}
//ennoble the internal arrays to a civilized struct
List<Declaration> declars = new List<Declaration>();
foreach (string declar in decs)
{
string[] parts = declar.Split(':');
if(parts.Length < 2) continue;
Declaration dec = new Declaration();
dec.Property = parts[0];
dec.Value = parts[1];
dec.InnerCSS = declar;
try {
sel.Declarations.Add(dec.Property, dec);
}
catch { }
}
RegisterSelector(sel);
}
}
/// <summary>Get all parsed selectors</summary>
public Dictionary<string, Selector> GetAllSelectors()
{
return selectors;
}
/// <summary>Gets CSS style for selector that can be found by <paramref name="Query"/></summary>
/// <param name="Query">The query which be used to find corresponding CSS Selector (regular expressions are supported)</param>
/// <returns>The CSS style</returns>
private Selector Get(string Query){
foreach (Selector s in selectors.Values)
{
if(Regex.IsMatch(s.Pattern,Query,RegexOptions.IgnoreCase))
return s;
}
throw new ArgumentOutOfRangeException("No style was found for " + Query);
}
/// <summary>Get a selector</summary>
/// <param name="Pattern">The pattern that corresponds the required selector</param>
/// <returns>The requested selector</returns>
/// <exception cref="System.ArgumentOutOfRangeException">An ArgumentOutOfRangeException will be thrown if the selector is't present.</exception>
public Selector this[string Pattern]{
get { return Get(Pattern); }
}
/// <summary>Get a declaration</summary>
/// <param name="Pattern">The pattern that corresponds the required selector</param>
/// <param name="Property">The property name that should be finded in the selector and to be returned</param>
/// <returns>The property declaration</returns>
/// <exception cref="System.ArgumentOutOfRangeException">An ArgumentOutOfRangeException will be thrown if the selector is't present.</exception>
/// <exception cref="System.Collections.Generic.KeyNotFoundException">An KeyNotFoundException will be thrown if the property cannot be found</exception>
public Declaration this[string Pattern, string Property]
{
get { return Get(Pattern).Declarations[Property]; }
}
/// <summary>Save the parsed selector(s) in the memory</summary>
/// <param name="s">The selector</param>
private void RegisterSelector(Selector s){
if (s.Pattern.IndexOf(',') > 0)
{
//if the selector matches namy patterns, all of they should be stored separately
string[] patterns = s.Pattern.Split(',');
#if DEBUG
log += "\r\nSplitted: " + s.Pattern + "{";
#endif
foreach (string pattern in patterns)
{
#if DEBUG
log += "\r\n\t" + pattern+";";
#endif
RegisterSelector(
new Selector()
{
Pattern = pattern, Declarations = s.Declarations, InnerCSS = s.InnerCSS, OuterCSS = s.OuterCSS
}
);
}
#if DEBUG
log += "\r\n}";
#endif
return;
}
if(!selectors.ContainsKey(s.Pattern)){
selectors.Add(s.Pattern, s);
#if DEBUG
log += "\r\nRegistered: " + s.Pattern;
#endif
}
else
{
//merge with existing
Selector existing = selectors[s.Pattern];
existing.InnerCSS += "\n" + s.InnerCSS;
existing.OuterCSS += "\n" + s.OuterCSS;
foreach (Declaration d in s.Declarations.Values)
{
try { existing.Declarations.Add(d.Property, d); }
catch { } //old good On Error Resume Next equivalent :-)
}
#if DEBUG
log += "\r\nMerged " + s.Pattern + " with " + existing.Pattern;
#endif
}
}
}
/// <summary>CSS selector (i.e. a:hover{})</summary>
public struct Selector
{
/// <summary>The pattern of this selector (i.e. "a:hover")</summary>
public string Pattern;
/// <summary>The stuff of this selector</summary>
public Dictionary<string, Declaration> Declarations;
/// <summary>The full inner content of the selector</summary>
public string InnerCSS;
/// <summary>The full body of the selector</summary>
public string OuterCSS;
}
/// <summary>CSS declarations (i.e. color: #ABCDEF)</summary>
public struct Declaration
{
/// <summary>The declaration's property name</summary>
public string Property;
/// <summary>The declaration's property value</summary>
public string Value;
/// <summary>The full content of the declaration</summary>
public string InnerCSS;
}
}