2 using System.Collections.Generic;
\r
6 namespace NT2chCtrl.html
\r
8 public partial class HtmlElement
\r
10 private bool parse2chRes(string source, IAmbiguousSearch search)
\r
15 int length = source.Length;
\r
17 //StringElement sElem = null;
\r
18 HtmlElement hElem = null;
\r
20 //StringBuilder sb = new StringBuilder(source.Length);
\r
22 for (int idx1 = 0; idx1 < length; idx1++)
\r
24 idx2 = source.IndexOf('<', idx1);
\r
27 if (idx2 < length - 1)
\r
30 switch (source[idx2 + 1])
\r
34 idx3 = source.IndexOf(" href=", idx2 + 2, StringComparison.OrdinalIgnoreCase);
\r
35 if (idx3 == idx2 + 2)
\r
37 idx4 = source.IndexOf(">", idx3 + 6, StringComparison.OrdinalIgnoreCase);
\r
40 //sElem = new StringElement(this, source.Substring(textStart, idx2 - textStart));
\r
41 parse2chRes2(this, source, textStart, idx2, search);
\r
42 textStart = idx4 + 1;
\r
43 idx1 = textStart - 1;
\r
49 idx3 = source.IndexOf("r", idx2 + 2, StringComparison.OrdinalIgnoreCase);
\r
50 if (idx3 == idx2 + 2)
\r
52 idx4 = source.IndexOf(">", idx3 + 1, StringComparison.OrdinalIgnoreCase);
\r
55 //sElem = new StringElement(this, source.Substring(textStart, idx2 - textStart));
\r
56 parse2chRes2(this, source, textStart, idx2, search);
\r
57 hElem = new HtmlElement(this, "br");
\r
58 hElem.setClosed(true);
\r
59 textStart = idx4 + 1;
\r
60 idx1 = textStart - 1;
\r
65 idx3 = source.IndexOf("a>", idx2 + 2, StringComparison.OrdinalIgnoreCase);
\r
66 if (idx3 == idx2 + 2)
\r
68 //sElem = new StringElement(this, source.Substring(textStart, idx2 - textStart));
\r
69 parse2chRes2(this, source, textStart, idx2, search);
\r
70 textStart = idx3 + 2;
\r
71 idx1 = textStart - 1;
\r
83 } //while (textStart < length);
\r
85 if (textStart < length)
\r
87 //sElem = new StringElement(this, source.Substring(textStart, length - textStart));
\r
88 parse2chRes2(this, source, textStart, length, search);
\r
93 private static int findNonDisplayAscii(string source, int start, int end)
\r
95 int length = source.Length;
\r
96 length = Math.Min(length, end);
\r
97 for (int i = start; i < length; i++)
\r
100 if(c <= ' ' || c >= 0x7f){
\r
107 private bool parseEmphasisText(HtmlElement pElem, string source, int start, int end, IAmbiguousSearch search)
\r
110 HtmlAttribute hAttr;
\r
111 StringElement sElem;
\r
113 int textLength = end - start;
\r
115 string text = source.Substring(start, textLength);
\r
117 if (search == null)
\r
119 sElem = new StringElement(pElem, text);
\r
123 int findIdx, endIdx;
\r
125 if (!search.match(text, idx, out findIdx, out endIdx))
\r
129 sElem = new StringElement(pElem,
\r
130 text.Substring(idx, findIdx - idx));
\r
132 hElem = new HtmlElement(pElem, "span");
\r
133 hAttr = new HtmlAttribute("emphasis");
\r
134 hElem.addAttribute(hAttr);
\r
135 sElem = new StringElement(hElem,
\r
136 text.Substring(findIdx, endIdx - findIdx));
\r
137 hElem.setClosed(true);
\r
139 }while(idx < textLength);
\r
141 if (idx < textLength)
\r
143 sElem = new StringElement(pElem,
\r
144 text.Substring(idx, textLength - idx));
\r
149 private bool parse2chRes2(HtmlElement pElem, string source, int start, int end, IAmbiguousSearch search)
\r
152 int textStart = start;
\r
156 StringElement sElem;
\r
158 HtmlAttribute hAttr;
\r
162 for (int i = start; i < end; i++)
\r
173 else if (state == 1)
\r
188 if (state == 1 || state == 2 || state == 3)
\r
202 if (state == 2 || state == 3)
\r
205 //sElem = new StringElement(this,
\r
206 // source.Substring(textStart, textEnd - textStart));
\r
207 parseEmphasisText(this, source, textStart, textEnd, search);
\r
208 hElem = new HtmlElement(this, "span");
\r
209 hAttr = new HtmlAttribute("res-link");
\r
210 hElem.addAttribute(hAttr);
\r
211 hElem.setClosed(true);
\r
214 else if (state == 2)
\r
216 sElem = new StringElement(hElem, source.Substring(textEnd, textStart - textEnd));
\r
218 idx1 = source.IndexOf("ttp://", i, StringComparison.OrdinalIgnoreCase);
\r
219 if (idx1 == i || idx1 == (i + 1))
\r
224 prefix = string.Empty;
\r
225 idx2 = findNonDisplayAscii(source, idx1 + 6, end);
\r
226 sLink = source.Substring(i, idx2 - i);
\r
227 if (chkGraphicLink(sLink))
\r
229 hElem = new HtmlElement(this, "img");
\r
230 hAttr = new HtmlAttribute("src", sLink);
\r
231 hElem.addAttribute(hAttr);
\r
232 hElem.setClosed(true);
\r
234 hElem = new HtmlElement(this, "a");
\r
235 hAttr = new HtmlAttribute("href", prefix+sLink);
\r
236 hElem.addAttribute(hAttr);
\r
237 hElem.setClosed(true);
\r
238 sElem = new StringElement(hElem, sLink);
\r
244 idx1 = source.IndexOf("ttps://", i, StringComparison.OrdinalIgnoreCase);
\r
245 if (idx1 == i || idx1 == (i + 1))
\r
250 prefix = string.Empty;
\r
251 idx2 = findNonDisplayAscii(source, idx1 + 7, end);
\r
252 sLink = source.Substring(i, idx2 - i);
\r
253 if (chkGraphicLink(sLink))
\r
255 hElem = new HtmlElement(this, "img");
\r
256 hAttr = new HtmlAttribute("src", sLink);
\r
257 hElem.addAttribute(hAttr);
\r
258 hElem.setClosed(true);
\r
260 hElem = new HtmlElement(this, "a");
\r
261 hAttr = new HtmlAttribute("href", prefix+sLink);
\r
262 hElem.addAttribute(hAttr);
\r
263 hElem.setClosed(true);
\r
264 sElem = new StringElement(hElem, sLink);
\r
275 if (state == 2 || state == 3)
\r
278 //sElem = new StringElement(this,
\r
279 // source.Substring(textStart, textEnd - textStart));
\r
280 parseEmphasisText(this, source, textStart, textEnd, search);
\r
281 hElem = new HtmlElement(this, "span");
\r
282 hAttr = new HtmlAttribute("res-link");
\r
283 hElem.addAttribute(hAttr);
\r
284 hElem.setClosed(true);
\r
287 else if (state == 2)
\r
289 sElem = new StringElement(hElem, source.Substring(textEnd, textStart - textEnd));
\r
292 idx1 = source.IndexOf("sssp://", i, StringComparison.OrdinalIgnoreCase);
\r
293 if (idx1 == i || idx1 == (i + 1))
\r
295 idx2 = findNonDisplayAscii(source, idx1 + 7, end);
\r
296 sLink = "http" + source.Substring(i+4, idx2 - i - 4);
\r
297 if (chkGraphicLink(sLink))
\r
299 hElem = new HtmlElement(this, "img");
\r
300 hAttr = new HtmlAttribute("src", sLink);
\r
301 hElem.addAttribute(hAttr);
\r
302 hAttr = new HtmlAttribute("sssp");
\r
303 hElem.addAttribute(hAttr);
\r
304 hElem.setClosed(true);
\r
311 if (state == 2 || state == 3)
\r
313 //sElem = new StringElement(this,
\r
314 // source.Substring(textStart, textEnd - textStart));
\r
315 parseEmphasisText(this, source, textStart, textEnd, search);
\r
316 hElem = new HtmlElement(this, "span");
\r
317 hAttr = new HtmlAttribute("res-link");
\r
318 hElem.addAttribute(hAttr);
\r
319 hElem.setClosed(true);
\r
322 else if (state == 2)
\r
324 sElem = new StringElement(hElem, source.Substring(textEnd, textStart - textEnd));
\r
330 if (textStart < end)
\r
332 if (state == 2 || state == 3)
\r
334 //sElem = new StringElement(this,
\r
335 // source.Substring(textStart, textEnd - textStart));
\r
336 parseEmphasisText(this, source, textStart, textEnd, search);
\r
337 hElem = new HtmlElement(this, "span");
\r
338 hAttr = new HtmlAttribute("res-link");
\r
339 hElem.addAttribute(hAttr);
\r
340 hElem.setClosed(true);
\r
342 textStart = end - 1;
\r
343 else if (state == 2)
\r
345 sElem = new StringElement(hElem, source.Substring(textEnd, textStart - textEnd));
\r
348 sElem = new StringElement(this, source.Substring(end -1, 1));
\r
353 //sElem = new StringElement(this, source.Substring(textStart, end - textStart));
\r
354 parseEmphasisText(this, source, textStart, end, search);
\r
360 bool chkGraphicLink(string src)
\r
362 //if (!chkShowThumbnail())
\r
365 if (src == null || src.Length < 4)
\r
367 int idx = src.LastIndexOf('.');
\r
371 string suffix = src.Substring(idx + 1).ToLower();
\r
385 private bool parse2chRes_old(string source)
\r
388 if (source == null || source.Length == 0)
\r
393 string tagName, attrName=null, attrVal;
\r
394 StringElement sElem;
\r
395 HtmlAttribute hAttr;
\r
396 HtmlElement hElem = null;
\r
397 bool dquote = false;
\r
398 bool squote = false;
\r
401 int length = source.Length;
\r
402 for (int i = 0; i < length; i++)
\r
404 switch (HtmlParser.getCharToken(source[i]))
\r
406 case HtmlParser.CHAR_TOKEN.LT:
\r
407 if (state == 0 && textStart < i)
\r
409 sElem = new StringElement(
\r
410 this, source.Substring(textStart, i - textStart));
\r
413 textStart = length;
\r
415 case HtmlParser.CHAR_TOKEN.SLASH:
\r
418 else if (state == 6 && (dquote || squote))
\r
422 else if (state == 1)
\r
431 case HtmlParser.CHAR_TOKEN.GT:
\r
434 }if (state == 1 || state == 8){
\r
437 else if (state == 2)
\r
439 tagName = source.Substring(textStart, i - textStart);
\r
440 hElem = new HtmlElement(this, tagName);
\r
442 else if (state == 7)
\r
444 hElem.setClosed(true);
\r
446 else if (state == 9)
\r
448 tagName = source.Substring(textStart, i - textStart);
\r
449 int count = mChildren.Count;
\r
450 for (int j = count - 1; j >= 0; j--)
\r
452 HtmlElement child = mChildren[j];
\r
453 if (child.Closed())
\r
456 if (tagName.Equals(child.getTagName()))
\r
458 child.setClosed(true);
\r
461 child.mChildren.AddRange(
\r
462 mChildren.GetRange(j+1, count - 1 - j));
\r
463 mChildren.RemoveRange(j+1, count - 1 - j);
\r
473 case HtmlParser.CHAR_TOKEN.WHITESPACE:
\r
474 case HtmlParser.CHAR_TOKEN.NL:
\r
477 else if (state == 2)
\r
479 tagName = source.Substring(textStart, i - textStart);
\r
480 hElem = new HtmlElement(this, tagName);
\r
483 else if (state == 4)
\r
485 attrName = source.Substring(textStart, i - textStart);
\r
488 else if (state == 5)
\r
490 hAttr = new HtmlAttribute(attrName);
\r
491 hElem.addAttribute(hAttr);
\r
493 dquote = squote = false;
\r
495 else if (state == 6)
\r
497 attrVal = source.Substring(textStart, i - textStart);
\r
498 hAttr = new HtmlAttribute(attrName, attrVal);
\r
499 hElem.addAttribute(hAttr);
\r
501 dquote = squote = false;
\r
504 case HtmlParser.CHAR_TOKEN.EQUAL:
\r
505 if (state != 0 && state != 4 && state != 5)
\r
511 attrName = source.Substring(textStart, i - textStart);
\r
516 case HtmlParser.CHAR_TOKEN.DQUOTE:
\r
525 attrVal = source.Substring(textStart, i - textStart);
\r
526 hAttr = new HtmlAttribute(attrName, attrVal);
\r
527 hElem.addAttribute(hAttr);
\r
537 case HtmlParser.CHAR_TOKEN.SQUOTE:
\r
546 attrVal = source.Substring(textStart, i - textStart);
\r
547 hAttr = new HtmlAttribute(attrName, attrVal);
\r
548 hElem.addAttribute(hAttr);
\r
558 case HtmlParser.CHAR_TOKEN.ALPHA:
\r
561 else if (state == 1)
\r
566 else if (state == 3)
\r
571 else if (state == 8)
\r
576 else if(state == 7)
\r
589 if (state == 0 && textStart < length)
\r
591 sElem = new StringElement(
\r
592 this, source.Substring(textStart, length - textStart));
\r