private Selector(Evaluator evaluator, iText.StyledXmlParser.Jsoup.Nodes.Element root) { Validate.NotNull(evaluator); Validate.NotNull(root); this.evaluator = evaluator; this.root = root; }
// /** // * Prepare to submit this form. A Connection object is created with the request set up from the form values. You // * can then set up other options (like user-agent, timeout, cookies), then execute it. // * @return a connection prepared from the values of this form. // * @throws IllegalArgumentException if the form's absolute action URL cannot be determined. Make sure you pass the // * document's base URI when parsing. // */ // public Connection submit() { // String action = hasAttr("action") ? absUrl("action") : baseUri(); // Validate.notEmpty(action, "Could not determine a form action URL for submit. Ensure you set a base URI when parsing."); // Connection.Method method = attr("method").toUpperCase().equals("POST") ? // Connection.Method.POST : Connection.Method.GET; // // return Jsoup.connect(action) // .data(formData()) // .method(method); // } /// <summary>Get the data that this form submits.</summary> /// <remarks> /// Get the data that this form submits. The returned list is a copy of the data, and changes to the contents of the /// list will not be reflected in the DOM. /// </remarks> /// <returns>a list of key vals</returns> public virtual IList <KeyVal> FormData() { List <KeyVal> data = new List <KeyVal>(); // iterate the form control elements and accumulate their values foreach (iText.StyledXmlParser.Jsoup.Nodes.Element el in elements) { if (!el.Tag().IsFormSubmittable()) { continue; } // contents are form listable, superset of submitable if (el.HasAttr("disabled")) { continue; } // skip disabled form inputs String name = el.Attr("name"); if (name.Length == 0) { continue; } String type = el.Attr("type"); if ("select".Equals(el.TagName())) { iText.StyledXmlParser.Jsoup.Select.Elements options = el.Select("option[selected]"); bool set = false; foreach (iText.StyledXmlParser.Jsoup.Nodes.Element option in options) { data.Add(KeyVal.Create(name, option.Val())); set = true; } if (!set) { iText.StyledXmlParser.Jsoup.Nodes.Element option = el.Select("option").First(); if (option != null) { data.Add(KeyVal.Create(name, option.Val())); } } } else { if ("checkbox".EqualsIgnoreCase(type) || "radio".EqualsIgnoreCase(type)) { // only add checkbox or radio if they have the checked attribute if (el.HasAttr("checked")) { String val = el.Val().Length > 0 ? el.Val() : "on"; data.Add(KeyVal.Create(name, val)); } } else { data.Add(KeyVal.Create(name, el.Val())); } } } return(data); }
/// <summary>Build a list of elements, by visiting root and every descendant of root, and testing it against the evaluator. /// </summary> /// <param name="eval">Evaluator to test elements against</param> /// <param name="root">root of tree to descend</param> /// <returns>list of matches; empty if none</returns> public static Elements Collect(Evaluator eval, iText.StyledXmlParser.Jsoup.Nodes.Element root) { Elements elements = new Elements(); new NodeTraversor(new Collector.Accumulator(root, elements, eval)).Traverse(root); return(elements); }
public virtual void DummyProcessingTestCorrectNested() { //Setup nodes iText.StyledXmlParser.Jsoup.Nodes.Element jsoupSVGRoot = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("svg"), ""); iText.StyledXmlParser.Jsoup.Nodes.Element jsoupSVGCircle = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("circle"), ""); iText.StyledXmlParser.Jsoup.Nodes.Element jsoupSVGPath = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("path"), ""); INode root = new JsoupElementNode(jsoupSVGRoot); root.AddChild(new JsoupElementNode(jsoupSVGCircle)); root.AddChild(new JsoupElementNode(jsoupSVGPath)); INode nestedSvg = new JsoupElementNode(jsoupSVGRoot); nestedSvg.AddChild(new JsoupElementNode(jsoupSVGCircle)); nestedSvg.AddChild(new JsoupElementNode(jsoupSVGCircle)); root.AddChild(nestedSvg); //Run DefaultSvgProcessor processor = new DefaultSvgProcessor(); ISvgConverterProperties props = new DummySvgConverterProperties(); ISvgNodeRenderer rootActual = processor.Process(root, props).GetRootRenderer(); //setup expected IBranchSvgNodeRenderer rootExpected = new DummyBranchSvgNodeRenderer("svg"); rootExpected.AddChild(new DummySvgNodeRenderer("circle")); rootExpected.AddChild(new DummySvgNodeRenderer("path")); IBranchSvgNodeRenderer nestedSvgRend = new DummyBranchSvgNodeRenderer("svg"); nestedSvgRend.AddChild(new DummySvgNodeRenderer("circle")); nestedSvgRend.AddChild(new DummySvgNodeRenderer("circle")); rootExpected.AddChild(nestedSvgRend); //Compare NUnit.Framework.Assert.AreEqual(rootActual, rootExpected); }
/// <summary>Wrap the supplied HTML around this node.</summary> /// <param name="html"> /// HTML to wrap around this element, e.g. /// <c><div class="head"></div></c> /// . Can be arbitrarily deep. /// </param> /// <returns>this node, for chaining.</returns> public virtual iText.StyledXmlParser.Jsoup.Nodes.Node Wrap(String html) { Validate.NotEmpty(html); iText.StyledXmlParser.Jsoup.Nodes.Element context = Parent() is iText.StyledXmlParser.Jsoup.Nodes.Element ? (iText.StyledXmlParser.Jsoup.Nodes.Element)Parent() : null; IList <iText.StyledXmlParser.Jsoup.Nodes.Node> wrapChildren = iText.StyledXmlParser.Jsoup.Parser.Parser.ParseFragment (html, context, BaseUri()); iText.StyledXmlParser.Jsoup.Nodes.Node wrapNode = wrapChildren[0]; if (wrapNode == null || !(wrapNode is iText.StyledXmlParser.Jsoup.Nodes.Element)) { // nothing to wrap with; noop return(null); } iText.StyledXmlParser.Jsoup.Nodes.Element wrap = (iText.StyledXmlParser.Jsoup.Nodes.Element)wrapNode; iText.StyledXmlParser.Jsoup.Nodes.Element deepest = GetDeepChild(wrap); parentNode.ReplaceChild(this, wrap); deepest.AddChildren(this); // remainder (unbalanced wrap, like <div></div><p></p> -- The <p> is remainder if (wrapChildren.Count > 0) { for (int i = 0; i < wrapChildren.Count; i++) { iText.StyledXmlParser.Jsoup.Nodes.Node remainder = wrapChildren[i]; remainder.parentNode.RemoveChild(remainder); wrap.AppendChild(remainder); } } return(this); }
/// <summary> /// Creates a new /// <see cref="JsoupElementNode"/> /// instance. /// </summary> /// <param name="element">the element</param> public JsoupElementNode(iText.StyledXmlParser.Jsoup.Nodes.Element element) : base(element) { this.element = element; this.attributes = new JsoupAttributes(element.Attributes()); this.lang = GetAttribute(CommonAttributeConstants.LANG); }
/// <summary> /// Get the string contents of the document's /// <c>title</c> /// element. /// </summary> /// <returns>Trimmed title, or empty string if none set.</returns> public virtual String Title() { // title is a preserve whitespace tag (for document output), but normalised here iText.StyledXmlParser.Jsoup.Nodes.Element titleEl = GetElementsByTag("title").First(); return(titleEl != null?iText.StyledXmlParser.Jsoup.Helper.StringUtil.NormaliseWhitespace(titleEl.Text()) .Trim() : ""); }
/// <summary>Normalise the document.</summary> /// <remarks> /// Normalise the document. This happens after the parse phase so generally does not need to be called. /// Moves any text content that is not in the body element into the body. /// </remarks> /// <returns>this document after normalisation</returns> public virtual iText.StyledXmlParser.Jsoup.Nodes.Document Normalise() { iText.StyledXmlParser.Jsoup.Nodes.Element htmlEl = FindFirstElementByTagName("html", this); if (htmlEl == null) { htmlEl = AppendElement("html"); } if (Head() == null) { htmlEl.PrependElement("head"); } if (Body() == null) { htmlEl.AppendElement("body"); } // pull text nodes out of root, html, and head els, and push into body. non-text nodes are already taken care // of. do in inverse order to maintain text order. NormaliseTextNodes(Head()); NormaliseTextNodes(htmlEl); NormaliseTextNodes(this); NormaliseStructure("head", htmlEl); NormaliseStructure("body", htmlEl); EnsureMetaCharsetElement(); return(this); }
// merge multiple <head> or <body> contents into one, delete the remainder, and ensure they are owned by <html> private void NormaliseStructure(String tag, iText.StyledXmlParser.Jsoup.Nodes.Element htmlEl) { Elements elements = this.GetElementsByTag(tag); iText.StyledXmlParser.Jsoup.Nodes.Element master = elements.First(); // will always be available as created above if not existent if (elements.Count > 1) { // dupes, move contents to master IList <iText.StyledXmlParser.Jsoup.Nodes.Node> toMove = new List <iText.StyledXmlParser.Jsoup.Nodes.Node>(); for (int i = 1; i < elements.Count; i++) { iText.StyledXmlParser.Jsoup.Nodes.Node dupe = elements[i]; foreach (iText.StyledXmlParser.Jsoup.Nodes.Node node in dupe.childNodes) { toMove.Add(node); } dupe.Remove(); } foreach (iText.StyledXmlParser.Jsoup.Nodes.Node dupe in toMove) { master.AppendChild(dupe); } } // ensure parented by <html> if (!master.Parent().Equals(htmlEl)) { htmlEl.AppendChild(master); } }
public virtual void DefaultProcessingCorrectlyNestedRenderersTest() { //Setup nodes iText.StyledXmlParser.Jsoup.Nodes.Element jsoupSVGRoot = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("svg"), ""); iText.StyledXmlParser.Jsoup.Nodes.Element jsoupSVGCircle = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("circle"), ""); iText.StyledXmlParser.Jsoup.Nodes.Element jsoupSVGPath = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("path"), ""); INode root = new JsoupElementNode(jsoupSVGRoot); root.AddChild(new JsoupElementNode(jsoupSVGCircle)); root.AddChild(new JsoupElementNode(jsoupSVGPath)); //Run DefaultSvgProcessor processor = new DefaultSvgProcessor(); SvgConverterProperties convProps = new SvgConverterProperties(); ISvgNodeRenderer rootRenderer = processor.Process(root, convProps).GetRootRenderer(); NUnit.Framework.Assert.IsTrue(rootRenderer is SvgTagSvgNodeRenderer); IList <ISvgNodeRenderer> children = ((SvgTagSvgNodeRenderer)rootRenderer).GetChildren(); NUnit.Framework.Assert.AreEqual(2, children.Count); NUnit.Framework.Assert.IsTrue(children[0] is CircleSvgNodeRenderer); NUnit.Framework.Assert.IsTrue(children[1] is PathSvgNodeRenderer); }
/// <summary> /// Ensures a meta charset (html) or xml declaration (xml) with the current /// encoding used. /// </summary> /// <remarks> /// Ensures a meta charset (html) or xml declaration (xml) with the current /// encoding used. This only applies with /// <see cref="UpdateMetaCharsetElement(bool)">updateMetaCharset</see> /// set to /// <tt>true</tt>, otherwise this method does nothing. /// <ul> /// <li>An exsiting element gets updated with the current charset</li> /// <li>If there's no element yet it will be inserted</li> /// <li>Obsolete elements are removed</li> /// </ul> /// <p><b>Elements used:</b></p> /// <ul> /// <li><b>Html:</b> <i><meta charset="CHARSET"></i></li> /// <li><b>Xml:</b> <i><?xml version="1.0" encoding="CHARSET"></i></li> /// </ul> /// </remarks> private void EnsureMetaCharsetElement() { if (updateMetaCharset) { Syntax syntax = OutputSettings().Syntax(); if (syntax == Syntax.html) { iText.StyledXmlParser.Jsoup.Nodes.Element metaCharset = Select("meta[charset]").First(); if (metaCharset != null) { metaCharset.Attr("charset", Charset().DisplayName()); } else { iText.StyledXmlParser.Jsoup.Nodes.Element head = Head(); if (head != null) { head.AppendElement("meta").Attr("charset", Charset().DisplayName()); } } // Remove obsolete elements Select("meta[name=charset]").Remove(); } else { if (syntax == Syntax.xml) { iText.StyledXmlParser.Jsoup.Nodes.Node node = ChildNodes()[0]; if (node is XmlDeclaration) { XmlDeclaration decl = (XmlDeclaration)node; if (decl.Name().Equals("xml")) { decl.Attr("encoding", Charset().DisplayName()); String version = decl.Attr("version"); if (version != null) { decl.Attr("version", "1.0"); } } else { decl = new XmlDeclaration("xml", baseUri, false); decl.Attr("version", "1.0"); decl.Attr("encoding", Charset().DisplayName()); PrependChild(decl); } } else { XmlDeclaration decl = new XmlDeclaration("xml", baseUri, false); decl.Attr("version", "1.0"); decl.Attr("encoding", Charset().DisplayName()); PrependChild(decl); } } } } }
public virtual void HandleAbsOnLocalhostFileUris() { Document doc = iText.StyledXmlParser.Jsoup.Jsoup.Parse("<a href='password'>One/a><a href='/var/log/messages'>Two</a>" , "file://localhost/etc/"); iText.StyledXmlParser.Jsoup.Nodes.Element one = doc.Select("a").First(); NUnit.Framework.Assert.AreEqual("file://localhost/etc/password", one.AbsUrl("href")); }
public virtual void AbsHandlesDotFromIndex() { Document doc = iText.StyledXmlParser.Jsoup.Jsoup.Parse("<a href='./one/two.html'>One</a>", "http://example.com" ); iText.StyledXmlParser.Jsoup.Nodes.Element a1 = doc.Select("a").First(); NUnit.Framework.Assert.AreEqual("http://example.com/one/two.html", a1.AbsUrl("href")); }
public virtual void NonExistingTagTest() { iText.StyledXmlParser.Jsoup.Nodes.Element nonExistingElement = new iText.StyledXmlParser.Jsoup.Nodes.Element (iText.StyledXmlParser.Jsoup.Parser.Tag.ValueOf("notAnExistingTag"), ""); IElementNode tag = new JsoupElementNode(nonExistingElement); fact.CreateSvgNodeRendererForTag(tag, null); }
public virtual void HasBeforeElementTest() { iText.StyledXmlParser.Jsoup.Nodes.Element element = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("before"), ""); IElementNode node = new JsoupElementNode(element); NUnit.Framework.Assert.IsTrue(CssPseudoElementUtil.HasBeforeAfterElements(node)); }
public virtual void TestMessageAddingChild() { iText.StyledXmlParser.Jsoup.Nodes.Element jsoupSVGRoot = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("svg"), ""); INode root = new JsoupElementNode(jsoupSVGRoot); root.AddChild(null); NUnit.Framework.Assert.AreEqual(0, root.ChildNodes().Count); }
/// <summary>Create a valid, empty shell of a document, suitable for adding more elements to.</summary> /// <param name="baseUri">baseUri of document</param> /// <returns>document with html, head, and body elements.</returns> public static iText.StyledXmlParser.Jsoup.Nodes.Document CreateShell(String baseUri) { Validate.NotNull(baseUri); iText.StyledXmlParser.Jsoup.Nodes.Document doc = new iText.StyledXmlParser.Jsoup.Nodes.Document(baseUri); iText.StyledXmlParser.Jsoup.Nodes.Element html = doc.AppendElement("html"); html.AppendElement("head"); html.AppendElement("body"); return(doc); }
public virtual void TestReplace() { Document doc = iText.StyledXmlParser.Jsoup.Jsoup.Parse("<p>One <span>two</span> three</p>"); iText.StyledXmlParser.Jsoup.Nodes.Element p = doc.Select("p").First(); iText.StyledXmlParser.Jsoup.Nodes.Element insert = doc.CreateElement("em").Text("foo"); p.ChildNode(1).ReplaceWith(insert); NUnit.Framework.Assert.AreEqual("One <em>foo</em> three", p.Html()); }
public virtual void OwnerDocument() { Document doc = iText.StyledXmlParser.Jsoup.Jsoup.Parse("<p>Hello"); iText.StyledXmlParser.Jsoup.Nodes.Element p = doc.Select("p").First(); NUnit.Framework.Assert.IsTrue(p.OwnerDocument() == doc); NUnit.Framework.Assert.IsTrue(doc.OwnerDocument() == doc); NUnit.Framework.Assert.IsNull(doc.Parent()); }
public virtual void UnwrapNoChildren() { Document doc = iText.StyledXmlParser.Jsoup.Jsoup.Parse("<div>One <span></span> Two</div>"); iText.StyledXmlParser.Jsoup.Nodes.Element span = doc.Select("span").First(); iText.StyledXmlParser.Jsoup.Nodes.Node node = span.Unwrap(); NUnit.Framework.Assert.AreEqual("<div>One Two</div>", TextUtil.StripNewlines(doc.Body().Html())); NUnit.Framework.Assert.IsTrue(node == null); }
public virtual void HandlesAbsPrefix() { Document doc = iText.StyledXmlParser.Jsoup.Jsoup.Parse("<a href=/foo>Hello</a>", "http://jsoup.org/"); iText.StyledXmlParser.Jsoup.Nodes.Element a = doc.Select("a").First(); NUnit.Framework.Assert.AreEqual("/foo", a.Attr("href")); NUnit.Framework.Assert.AreEqual("http://jsoup.org/foo", a.Attr("abs:href")); NUnit.Framework.Assert.IsTrue(a.HasAttr("abs:href")); }
public virtual void TestRemove() { Document doc = iText.StyledXmlParser.Jsoup.Jsoup.Parse("<p>One <span>two</span> three</p>"); iText.StyledXmlParser.Jsoup.Nodes.Element p = doc.Select("p").First(); p.ChildNode(0).Remove(); NUnit.Framework.Assert.AreEqual("two three", p.Text()); NUnit.Framework.Assert.AreEqual("<span>two</span> three", TextUtil.StripNewlines(p.Html())); }
private Selector(String query, iText.StyledXmlParser.Jsoup.Nodes.Element root) { Validate.NotNull(query); query = query.Trim(); Validate.NotEmpty(query); Validate.NotNull(root); this.evaluator = QueryParser.Parse(query); this.root = root; }
public virtual void HandlesAbsOnImage() { Document doc = iText.StyledXmlParser.Jsoup.Jsoup.Parse("<p><img src=\"/rez/osi_logo.png\" /></p>", "http://jsoup.org/" ); iText.StyledXmlParser.Jsoup.Nodes.Element img = doc.Select("img").First(); NUnit.Framework.Assert.AreEqual("http://jsoup.org/rez/osi_logo.png", img.Attr("abs:src")); NUnit.Framework.Assert.AreEqual(img.AbsUrl("src"), img.Attr("abs:src")); }
public virtual void LiteralAbsPrefix() { // if there is a literal attribute "abs:xxx", don't try and make absolute. Document doc = iText.StyledXmlParser.Jsoup.Jsoup.Parse("<a abs:href='odd'>One</a>"); iText.StyledXmlParser.Jsoup.Nodes.Element el = doc.Select("a").First(); NUnit.Framework.Assert.IsTrue(el.HasAttr("abs:href")); NUnit.Framework.Assert.AreEqual("odd", el.Attr("abs:href")); }
public virtual void TestMetaCharsetUpdateEnabledAfterCharsetChange() { Document doc = CreateHtmlDocument("dontTouch"); doc.Charset(EncodingUtil.GetEncoding(charsetUtf8)); iText.StyledXmlParser.Jsoup.Nodes.Element selectedElement = doc.Select("meta[charset]").First(); NUnit.Framework.Assert.AreEqual(charsetUtf8, selectedElement.Attr("charset")); NUnit.Framework.Assert.IsTrue(doc.Select("meta[name=charset]").IsEmpty()); }
public virtual void ElementNodeAttributeIsNotStylesheet() { iText.StyledXmlParser.Jsoup.Nodes.Element element = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("link"), ""); element.Attr(CommonAttributeConstants.REL, ""); JsoupElementNode elementNode = new JsoupElementNode(element); NUnit.Framework.Assert.IsFalse(CssUtils.IsStyleSheetLink(elementNode)); }
public virtual void RootTagTest() { iText.StyledXmlParser.Jsoup.Nodes.Element element = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag .ValueOf("dummy"), ""); IElementNode tag = new JsoupElementNode(element); ISvgNodeRenderer childRenderer = fact.CreateSvgNodeRendererForTag(tag, null); NUnit.Framework.Assert.IsTrue(childRenderer is DummySvgNodeRenderer); }
private void AddSiblingHtml(int index, String html) { Validate.NotNull(html); Validate.NotNull(parentNode); iText.StyledXmlParser.Jsoup.Nodes.Element context = Parent() is iText.StyledXmlParser.Jsoup.Nodes.Element ? (iText.StyledXmlParser.Jsoup.Nodes.Element)Parent() : null; IList<iText.StyledXmlParser.Jsoup.Nodes.Node> nodes = iText.StyledXmlParser.Jsoup.Parser.Parser.ParseFragment (html, context, BaseUri()); parentNode.AddChildren(index, nodes.ToArray(new iText.StyledXmlParser.Jsoup.Nodes.Node[nodes.Count])); }
public override bool Matches(iText.StyledXmlParser.Jsoup.Nodes.Element root, iText.StyledXmlParser.Jsoup.Nodes.Element element) { if (root == element) { return(false); } iText.StyledXmlParser.Jsoup.Nodes.Element prev = element.PreviousElementSibling(); return(prev != null && evaluator.Matches(root, prev)); }