This repository has been archived by the owner on Apr 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dominizer.cs
324 lines (315 loc) · 14.5 KB
/
Dominizer.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
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
/*
* Vha.AOML
* Copyright (C) 2010-2011 Remco van Oosterhout
* See Credits.txt for all aknowledgements.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License only.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Vha.AOML.DOM;
using Vha.Common;
namespace Vha.AOML
{
/// <summary>
/// This class allows you to transform a string of AOML into a DOM.Element tree
/// </summary>
public class Dominizer
{
public Parser Parser { get; private set; }
#region Constructors
public Dominizer()
{
this.Parser = new Parser();
this.Parser.Mode = ParserMode.Compatibility;
this.Parser.NewlineToBreak = true;
this.Parser.InvalidElementsToContent = false;
}
#endregion
public Element Parse(string aoml)
{
ContainerElement container = new ContainerElement();
// Parse AOML
NodeCollection nodes = this.Parser.Parse(aoml);
this.Parser.Sanitize(nodes);
this.Parser.Balance(nodes);
// Transform AOML into a DOM tree
Stack<Element> elements = new Stack<Element>();
elements.Push(container);
foreach (Node node in nodes)
{
Element element = null;
switch (node.Type)
{
case NodeType.Content:
// Content doesn't introduce a new level
// We'll simply add it as child at the current depth
ContentNode content = (ContentNode)node;
element = new TextElement(Web.UnescapeHtml(content.Value));
elements.Peek().Children.Add(element);
break;
case NodeType.Open:
OpenNode open = (OpenNode)node;
switch (open.Name)
{
case "br":
// Singular linebreak element
element = new BreakElement();
elements.Peek().Children.Add(element);
break;
case "img":
// Singular image element
element = this.CreateImageElement(open);
if (element != null)
elements.Peek().Children.Add(element);
break;
case "font":
element = this.CreateFontElement(open);
elements.Peek().Children.Add(element);
if (!open.Closed) { elements.Push(element); }
break;
case "a":
element = this.CreateLinkElement(open);
elements.Peek().Children.Add(element);
if (!open.Closed) { elements.Push(element); }
break;
case "u":
element = new UnderlineElement();
elements.Peek().Children.Add(element);
if (!open.Closed) { elements.Push(element); }
break;
case "i":
element = new ItalicElement();
elements.Peek().Children.Add(element);
if (!open.Closed) { elements.Push(element); }
break;
case "center":
case "left":
case "right":
case "div":
Alignment alignment = Alignment.Inherit;
if (open.Name == "center" || open.GetAttribute("align") == "center")
{
alignment = Alignment.Center;
}
else if (open.Name == "left" || open.GetAttribute("align") == "left")
{
alignment = Alignment.Left;
}
else if (open.Name == "right" || open.GetAttribute("align") == "right")
{
alignment = Alignment.Right;
}
element = new AlignElement(alignment);
elements.Peek().Children.Add(element);
if (!open.Closed) { elements.Push(element); }
break;
default:
throw new ArgumentException("Unexpected tag: " + open.Name);
}
break;
case NodeType.Close:
// Closing a node means consuming an element from the stack
// But first check whether the node matches the element on the stack
CloseNode close = (CloseNode)node;
switch (close.Name)
{
case "br":
throw new ArgumentException("Unexpected 'br' closing tag");
case "img":
throw new ArgumentException("Unexpected 'img' closing tag");
case "font":
if (elements.Peek().Type == ElementType.Color) { break; }
if (elements.Peek().Type == ElementType.Container) { break; }
throw new ArgumentException("Unexpected 'font' closing tag");
case "a":
if (elements.Peek().Type == ElementType.Link) { break; }
if (elements.Peek().Type == ElementType.Container) { break; }
throw new ArgumentException("Unexpected 'a' closing tag");
case "u":
if (elements.Peek().Type == ElementType.Underline) { break; }
throw new ArgumentException("Unexpected 'u' closing tag");
case "i":
if (elements.Peek().Type == ElementType.Italic) { break; }
throw new ArgumentException("Unexpected 'i' closing tag");
case "div":
case "center":
case "left":
case "right":
if (elements.Peek().Type == ElementType.Align) { break; }
throw new ArgumentException("Unexpected '" + close.Name + "' closing tag");
default:
throw new ArgumentException("Unexpected tag: " + close.Name);
}
if (elements.Count <= 1)
{
throw new ArgumentException("Unexpected closing tag");
}
// Go 1 step back down in the tree
elements.Pop();
break;
default:
throw new InvalidOperationException("Unexpected node type");
}
}
// Let's report our progress
if (container.Children.Count == 0)
{
return null;
}
if (container.Children.Count == 1)
{
return container.Children.ToArray()[0];
}
return container;
}
protected Element CreateImageElement(OpenNode node)
{
// Valid image?
string src = node.GetAttribute("src");
if (string.IsNullOrEmpty(src)) { return null; }
if (!src.Contains("://")) { return null; }
// Parse src value
string[] parts = src.Split(new string[] { "://" }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2) { return null; }
// - Determine type
ImageType type = ImageType.RDB;
if (parts[0].ToLower() == "rdb")
{
type = ImageType.RDB;
}
else if (parts[0].ToLower() == "tdb")
{
type = ImageType.TDB;
string prefix = "id:";
if (parts[1].StartsWith(prefix))
{
parts[1] = parts[1].Substring(prefix.Length);
}
}
else { return null; }
// Return image
return new ImageElement(type, parts[1]);
}
protected Element CreateFontElement(OpenNode node)
{
string color = node.GetAttribute("color");
if (string.IsNullOrEmpty(color)) { return new ContainerElement(); }
Color c = Color.FromString(color);
if (c == null) { return new ContainerElement(); }
return new ColorElement(c);
}
protected Element CreateLinkElement(OpenNode node)
{
// Valid link?
string href = node.GetAttribute("href");
if (string.IsNullOrEmpty(href) || !href.Contains("://"))
{
return new ContainerElement();
}
// Parse link
int index = href.IndexOf("://");
if (index <= 0)
{
return new ContainerElement();
}
string type = href.Substring(0, index).ToLower();
string argument = href.Substring(index + 3);
Link link = null;
Element windowElement = null;
// Verify header
Regex validCharacters = new Regex("^[a-zA-Z]+$");
if (!validCharacters.IsMatch(type) || argument.Length == 0)
{
// Invalid link
link = new InvalidLink(href);
}
else
{
// Generate link
switch (type)
{
case "text":
windowElement = Parse(argument);
if (windowElement == null) { break; }
link = new WindowLink(windowElement);
break;
case "charref":
index = argument.IndexOf("/");
if (index < 0) { break; }
index = argument.IndexOf("/", index + 1);
if (index <= 0) { break; }
argument = argument.Substring(index + 1);
windowElement = Parse(argument);
if (windowElement == null) { break; }
link = new WindowLink(windowElement);
break;
case "itemref":
string[] itemParts = argument.Split('/');
if (itemParts.Length != 3) { break; }
uint lowId, highId, ql;
if (!uint.TryParse(itemParts[0], out lowId)) { break; }
if (!uint.TryParse(itemParts[1], out highId)) { break; }
if (!uint.TryParse(itemParts[2], out ql)) { break; }
link = new ItemLink(lowId, highId, ql);
break;
case "itemid":
string[] entityParts = argument.Split('/');
// itemid:// can come with 4 arguments, but we have no use for the last 2
if (entityParts.Length != 2 && entityParts.Length != 4) { break; }
uint typeId, instanceId;
if (!uint.TryParse(entityParts[0], out typeId)) { break; }
if (!uint.TryParse(entityParts[1], out instanceId)) { break; }
link = new EntityLink(typeId, instanceId);
break;
case "chatcmd":
link = new CommandLink(argument);
break;
case "user":
link = new UserLink(argument);
break;
default:
try
{
// Attempt to create an 'OtherLink'
// this operation will throw an exception if the 'href' is significantly malformed
link = new OtherLink(href);
}
catch (UriFormatException)
{
// The 'href' is malformed, store as InvalidLink
link = new InvalidLink(href);
}
break;
}
}
if (link == null)
{
return new ContainerElement();
}
string style = node.GetAttribute("style");
if (style != null)
{
style = style.Replace(" ", "").ToLower();
if (!style.Contains("text-decoration:none"))
{
style = null;
}
}
return new LinkElement(link, style == null);
}
}
}