private DomElement ReadFreeText() { var para = new DomElement("p"); para.IsFreeText = true; var sb = new StringBuilder(); while(true) { var c = reader.Peek(); if (c == '<') { para.content = sb.ToString(); break; } else if (c == -1) { para.content = sb.ToString(); break; } else { reader.Read(); sb.Append((char)c); } } return para; }
private DomElement ReadElementBody(DomElement element) { while (true) { var c = reader.Peek(); var d1 = (char)c; if (c == -1) { //if (element.IsFreeText) break; } else if (c == '<') { reader.Read(); ReadWhiteSpace(); c = reader.Peek(); d1 = (char)c; if (c == '/') { reader.Read(); ReadWhiteSpace(); var tag = ReadWord(); if (tag != element.Tag) throw new Exception("Closing element not found"); ReadWhiteSpace(); c = reader.Peek(); d1 = (char)c; if (c != '>') throw new Exception(); reader.Read(); break; } else if (c == '!') { reader.Read(); var comment = "<!--"; var sbComment = new StringBuilder(); sbComment.Append("<!"); var length = comment.Length - sbComment.Length; for (int i = 0; i < length; ++i) { var c2 = reader.Read(); sbComment.Append((char)c2); } if (sbComment.ToString() != comment) throw new Exception(); var para = ReadComment(); element.Children.Add(para); } else { var innerElement = ReadElementHead(); if (!innerElement.IsVoidElement) innerElement = ReadElementBody(innerElement); element.Children.Add(innerElement); } } else { var para = ReadFreeText(); element.Children.Add(para); } } return element; }
private DomElement ReadElementHead() { var tag = ReadWord(); var element = new DomElement().GetElementFromString(tag); element = ReadAttrs(element); return element; }
private DomElement ReadAttrs(DomElement element) { while(true) { var c = reader.Peek(); if (c == '>') { reader.Read(); break; } else if (element.IsVoidElement && c == '/') { reader.Read(); c = reader.Peek(); if (c == '>') { reader.Read(); break; } } else if (IsWhiteSpace(c)) { ReadWhiteSpace(); } else if (IsTag(c)) { //reader.Read(); var tag = ReadWord().ToLower(); Expect('='); var c2 = Expect('"', '\''); //Expect(c2); if (tag == "id") { var word = ReadWord(); Expect(c2); element.ID = word; } else if (tag == "class") { var spaces = new int[] { ' ', '\t', '\n', '\r' }; while(true) { var word = ReadWord(); element.Class.Add(word); var expect = Expect(c2, ' ', '\t', '\n', '\r'); if (expect == c2) { break; } //else if (spaces.Contains(expect)) //{ // continue; //} //else // throw new Exception("Unrecognized character " + (char)expect); } } } //else if (c == '=') //{ //} //else if (c == '\'' || c == '"') //{ //} else { reader.Read(); } } return element; }
private DomElement parseDom() { var para = new DomElement("p"); para.IsFreeText = true; var dom = ReadElementBody(para); return dom; }
public Dom(string html) { //this.html = html; //html = "<sizzle>" + html + "</sizzle>"; this.reader = new StringReader(html); //parseDom(); this.DomContainer = parseDom(); }
private List<DomElement> GetElements(DomElement el, List<Selector.SelectorPart> sel) { var matchEls = new List<DomElement>(); //var s = sel.Selectors.First(); foreach(var child in el.Children) { if (child.IsMatch(sel)) { matchEls.Add(child); } else { matchEls.AddRange(GetElements(child, sel)); } } return matchEls; }
private List<DomElement> BeforeAfter(List<DomElement> children, string text, bool before) { var newParent = new List<DomElement>(); foreach (var child in children) { if (before && this.Elements.Contains(child)) { var para = new DomElement("p"); para.IsFreeText = true; para.content = text; newParent.Add(para); } var items = BeforeAfter(child.Children, text, before); child.Children = items; newParent.Add(child); if (!before && this.Elements.Contains(child)) { var para = new DomElement("p"); para.IsFreeText = true; para.content = text; newParent.Add(para); } } return newParent; }