Ejemplo n.º 1
0
        public virtual void SvgCssResolverStylesheetTest()
        {
            iText.StyledXmlParser.Jsoup.Nodes.Element jsoupLink = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
                                                                                                                .ValueOf(SvgConstants.Tags.LINK), "");
            Attributes linkAttributes = jsoupLink.Attributes();

            linkAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute(SvgConstants.Attributes.XMLNS, "http://www.w3.org/1999/xhtml"
                                                                               ));
            linkAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute(SvgConstants.Attributes.REL, SvgConstants.Attributes
                                                                               .STYLESHEET));
            linkAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute(SvgConstants.Attributes.HREF, "styleSheetWithLinkStyle.css"
                                                                               ));
            linkAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("type", "text/css"));
            JsoupElementNode       node = new JsoupElementNode(jsoupLink);
            SvgConverterProperties scp  = new SvgConverterProperties();

            scp.SetBaseUri(baseUri);
            SvgProcessorContext          processorContext = new SvgProcessorContext(scp);
            SvgStyleResolver             sr           = new SvgStyleResolver(node, processorContext);
            IDictionary <String, String> attr         = sr.ResolveStyles(node, new SvgCssContext());
            IDictionary <String, String> expectedAttr = new Dictionary <String, String>();

            expectedAttr.Put(SvgConstants.Attributes.XMLNS, "http://www.w3.org/1999/xhtml");
            expectedAttr.Put(SvgConstants.Attributes.REL, SvgConstants.Attributes.STYLESHEET);
            expectedAttr.Put(SvgConstants.Attributes.HREF, "styleSheetWithLinkStyle.css");
            expectedAttr.Put("type", "text/css");
            // Attribute from external stylesheet
            expectedAttr.Put(SvgConstants.Attributes.FILL, "black");
            NUnit.Framework.Assert.AreEqual(expectedAttr, attr);
        }
Ejemplo n.º 2
0
        private Cleaner.ElementMeta CreateSafeElement(iText.StyledXmlParser.Jsoup.Nodes.Element sourceEl)
        {
            String     sourceTag = sourceEl.TagName();
            Attributes destAttrs = new Attributes();

            iText.StyledXmlParser.Jsoup.Nodes.Element dest = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
                                                                                                           .ValueOf(sourceTag), sourceEl.BaseUri(), destAttrs);
            int        numDiscarded = 0;
            Attributes sourceAttrs  = sourceEl.Attributes();

            foreach (iText.StyledXmlParser.Jsoup.Nodes.Attribute sourceAttr in sourceAttrs)
            {
                if (whitelist.IsSafeAttribute(sourceTag, sourceEl, sourceAttr))
                {
                    destAttrs.Put(sourceAttr);
                }
                else
                {
                    numDiscarded++;
                }
            }
            Attributes enforcedAttrs = whitelist.GetEnforcedAttributes(sourceTag);

            destAttrs.AddAll(enforcedAttrs);
            return(new Cleaner.ElementMeta(dest, numDiscarded));
        }
Ejemplo n.º 3
0
        public virtual void SvgCssResolverBasicAttributeTest()
        {
            iText.StyledXmlParser.Jsoup.Nodes.Element jsoupCircle = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
                                                                                                                  .ValueOf("circle"), "");
            Attributes circleAttributes = jsoupCircle.Attributes();

            circleAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("id", "circle1"));
            circleAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("cx", "95"));
            circleAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("cy", "95"));
            circleAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("rx", "53"));
            circleAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("ry", "53"));
            circleAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("style", "stroke-width:1.5;stroke:#da0000;"
                                                                                 ));
            AbstractCssContext cssContext = new SvgCssContext();
            INode circle = new JsoupElementNode(jsoupCircle);
            SvgProcessorContext          context  = new SvgProcessorContext(new SvgConverterProperties());
            ICssResolver                 resolver = new SvgStyleResolver(circle, context);
            IDictionary <String, String> actual   = resolver.ResolveStyles(circle, cssContext);
            IDictionary <String, String> expected = new Dictionary <String, String>();

            expected.Put("id", "circle1");
            expected.Put("cx", "95");
            expected.Put("cy", "95");
            expected.Put("rx", "53");
            expected.Put("ry", "53");
            expected.Put("stroke-width", "1.5");
            expected.Put("stroke", "#da0000");
            NUnit.Framework.Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 4
0
 private bool IsSameFormattingElement(iText.StyledXmlParser.Jsoup.Nodes.Element a, iText.StyledXmlParser.Jsoup.Nodes.Element
                                      b)
 {
     // same if: same namespace, tag, and attributes. Element.equals only checks tag, might in future check children
     return(a.NodeName().Equals(b.NodeName()) &&
            // a.namespace().equals(b.namespace()) &&
            a.Attributes().Equals(b.Attributes()));
 }
Ejemplo n.º 5
0
        public virtual void ParsesEmptyString()
        {
            String html = "<a />";

            iText.StyledXmlParser.Jsoup.Nodes.Element el = iText.StyledXmlParser.Jsoup.Jsoup.Parse(html).GetElementsByTag
                                                               ("a")[0];
            Attributes attr = el.Attributes();

            NUnit.Framework.Assert.AreEqual(0, attr.Size());
        }
Ejemplo n.º 6
0
        public virtual void HandlesNewLinesAndReturns()
        {
            String html = "<a\r\nfoo='bar\r\nqux'\r\nbar\r\n=\r\ntwo>One</a>";

            iText.StyledXmlParser.Jsoup.Nodes.Element el = iText.StyledXmlParser.Jsoup.Jsoup.Parse(html).Select("a").First
                                                               ();
            NUnit.Framework.Assert.AreEqual(2, el.Attributes().Size());
            NUnit.Framework.Assert.AreEqual("bar\r\nqux", el.Attr("foo"));
            NUnit.Framework.Assert.AreEqual("two", el.Attr("bar"));
        }
Ejemplo n.º 7
0
        // a.namespace().equals(b.namespace()) &&
        // todo: namespaces
        internal virtual void ReconstructFormattingElements()
        {
            iText.StyledXmlParser.Jsoup.Nodes.Element last = LastFormattingElement();
            if (last == null || OnStack(last))
            {
                return;
            }
            iText.StyledXmlParser.Jsoup.Nodes.Element entry = last;
            int  size = formattingElements.Count;
            int  pos  = size - 1;
            bool skip = false;

            while (true)
            {
                if (pos == 0)
                {
                    // step 4. if none before, skip to 8
                    skip = true;
                    break;
                }
                entry = formattingElements[--pos];
                // step 5. one earlier than entry
                if (entry == null || OnStack(entry))
                {
                    // step 6 - neither marker nor on stack
                    break;
                }
            }
            // jump to 8, else continue back to 4
            while (true)
            {
                if (!skip)
                {
                    // step 7: on later than entry
                    entry = formattingElements[++pos];
                }
                Validate.NotNull(entry);
                // should not occur, as we break at last element
                // 8. create new element from element, 9 insert into current node, onto stack
                skip = false;
                // can only skip increment from 4.
                iText.StyledXmlParser.Jsoup.Nodes.Element newEl = InsertStartTag(entry.NodeName());
                // todo: avoid fostering here?
                // newEl.namespace(entry.namespace()); // todo: namespaces
                newEl.Attributes().AddAll(entry.Attributes());
                // 10. replace entry with new entry
                formattingElements[pos] = newEl;
                // 11
                if (pos == size - 1)
                {
                    // if not last entry in list, jump to 7
                    break;
                }
            }
        }
Ejemplo n.º 8
0
        public virtual void OverrideDefaultStyleTest()
        {
            ICssResolver styleResolver = new SvgStyleResolver(new SvgProcessorContext(new SvgConverterProperties()));

            iText.StyledXmlParser.Jsoup.Nodes.Element svg = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
                                                                                                          .ValueOf("svg"), "");
            svg.Attributes().Put(SvgConstants.Attributes.STROKE, "white");
            INode svgNode = new JsoupElementNode(svg);
            IDictionary <String, String> resolvedStyles = styleResolver.ResolveStyles(svgNode, null);

            NUnit.Framework.Assert.AreEqual("white", resolvedStyles.Get(SvgConstants.Attributes.STROKE));
        }
Ejemplo n.º 9
0
        public virtual void SvgCssResolveMalformedXlinkTest()
        {
            iText.StyledXmlParser.Jsoup.Nodes.Element jsoupImage = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
                                                                                                                 .ValueOf("image"), "");
            iText.StyledXmlParser.Jsoup.Nodes.Attributes imageAttributes = jsoupImage.Attributes();
            imageAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("xlink:href", "htt://are/"));
            JsoupElementNode             node = new JsoupElementNode(jsoupImage);
            SvgStyleResolver             sr   = new SvgStyleResolver();
            IDictionary <String, String> attr = sr.ResolveStyles(node, new SvgCssContext());

            NUnit.Framework.Assert.AreEqual("htt://are/", attr.Get("xlink:href"));
        }
Ejemplo n.º 10
0
        public virtual void CanStartWithEq()
        {
            String html = "<a =empty />";

            iText.StyledXmlParser.Jsoup.Nodes.Element el = iText.StyledXmlParser.Jsoup.Jsoup.Parse(html).GetElementsByTag
                                                               ("a")[0];
            Attributes attr = el.Attributes();

            NUnit.Framework.Assert.AreEqual(1, attr.Size());
            NUnit.Framework.Assert.IsTrue(attr.HasKey("=empty"));
            NUnit.Framework.Assert.AreEqual("", attr.Get("=empty"));
        }
Ejemplo n.º 11
0
            public override bool Matches(iText.StyledXmlParser.Jsoup.Nodes.Element root, iText.StyledXmlParser.Jsoup.Nodes.Element
                                         element)
            {
                IList <iText.StyledXmlParser.Jsoup.Nodes.Attribute> values = element.Attributes().AsList();

                foreach (iText.StyledXmlParser.Jsoup.Nodes.Attribute attribute in values)
                {
                    if (attribute.Key.StartsWith(keyPrefix))
                    {
                        return(true);
                    }
                }
                return(false);
            }
Ejemplo n.º 12
0
        public virtual void SvgCssResolveHashXlinkTest()
        {
            iText.StyledXmlParser.Jsoup.Nodes.Element jsoupImage = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
                                                                                                                 .ValueOf("image"), "");
            iText.StyledXmlParser.Jsoup.Nodes.Attributes imageAttributes = jsoupImage.Attributes();
            imageAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("xlink:href", "#testid"));
            JsoupElementNode       node = new JsoupElementNode(jsoupImage);
            SvgConverterProperties scp  = new SvgConverterProperties();

            scp.SetBaseUri(baseUri);
            SvgProcessorContext          processorContext = new SvgProcessorContext(scp);
            SvgStyleResolver             sr   = new SvgStyleResolver(node, processorContext);
            IDictionary <String, String> attr = sr.ResolveStyles(node, new SvgCssContext());

            NUnit.Framework.Assert.AreEqual("#testid", attr.Get("xlink:href"));
        }
Ejemplo n.º 13
0
        public virtual void SvgCssResolverXlinkTest()
        {
            iText.StyledXmlParser.Jsoup.Nodes.Element jsoupImage = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
                                                                                                                 .ValueOf("image"), "");
            iText.StyledXmlParser.Jsoup.Nodes.Attributes imageAttributes = jsoupImage.Attributes();
            imageAttributes.Put(new iText.StyledXmlParser.Jsoup.Nodes.Attribute("xlink:href", "itis.jpg"));
            JsoupElementNode       node = new JsoupElementNode(jsoupImage);
            SvgConverterProperties scp  = new SvgConverterProperties();

            scp.SetBaseUri(baseUri);
            SvgProcessorContext          processorContext = new SvgProcessorContext(scp);
            SvgStyleResolver             sr   = new SvgStyleResolver(node, processorContext);
            IDictionary <String, String> attr = sr.ResolveStyles(node, new SvgCssContext());
            String fileName    = baseUri + "itis.jpg";
            String expectedURL = UrlUtil.ToNormalizedURI(fileName).ToString();

            NUnit.Framework.Assert.AreEqual(expectedURL, attr.Get("xlink:href"));
        }
Ejemplo n.º 14
0
        public virtual void ParsesRoughAttributeString()
        {
            String html = "<a id=\"123\" class=\"baz = 'bar'\" style = 'border: 2px'qux zim foo = 12 mux=18 />";

            // should be: <id=123>, <class=baz = 'bar'>, <qux=>, <zim=>, <foo=12>, <mux.=18>
            iText.StyledXmlParser.Jsoup.Nodes.Element el = iText.StyledXmlParser.Jsoup.Jsoup.Parse(html).GetElementsByTag
                                                               ("a")[0];
            Attributes attr = el.Attributes();

            NUnit.Framework.Assert.AreEqual(7, attr.Size());
            NUnit.Framework.Assert.AreEqual("123", attr.Get("id"));
            NUnit.Framework.Assert.AreEqual("baz = 'bar'", attr.Get("class"));
            NUnit.Framework.Assert.AreEqual("border: 2px", attr.Get("style"));
            NUnit.Framework.Assert.AreEqual("", attr.Get("qux"));
            NUnit.Framework.Assert.AreEqual("", attr.Get("zim"));
            NUnit.Framework.Assert.AreEqual("12", attr.Get("foo"));
            NUnit.Framework.Assert.AreEqual("18", attr.Get("mux"));
        }
Ejemplo n.º 15
0
        public virtual void ParsesBooleanAttributes()
        {
            String html = "<a normal=\"123\" boolean empty=\"\"></a>";

            iText.StyledXmlParser.Jsoup.Nodes.Element el = iText.StyledXmlParser.Jsoup.Jsoup.Parse(html).Select("a").First
                                                               ();
            NUnit.Framework.Assert.AreEqual("123", el.Attr("normal"));
            NUnit.Framework.Assert.AreEqual("", el.Attr("boolean"));
            NUnit.Framework.Assert.AreEqual("", el.Attr("empty"));
            IList <iText.StyledXmlParser.Jsoup.Nodes.Attribute> attributes = el.Attributes().AsList();

            NUnit.Framework.Assert.AreEqual(3, attributes.Count, "There should be 3 attribute present");
            // Assuming the list order always follows the parsed html
            NUnit.Framework.Assert.IsFalse(attributes[0] is BooleanAttribute, "'normal' attribute should not be boolean"
                                           );
            NUnit.Framework.Assert.IsTrue(attributes[1] is BooleanAttribute, "'boolean' attribute should be boolean");
            NUnit.Framework.Assert.IsFalse(attributes[2] is BooleanAttribute, "'empty' attribute should not be boolean"
                                           );
            NUnit.Framework.Assert.AreEqual(html, el.OuterHtml());
        }
Ejemplo n.º 16
0
        internal virtual void Insert(Token.Comment commentToken)
        {
            Comment comment = new Comment(commentToken.GetData(), baseUri);

            iText.StyledXmlParser.Jsoup.Nodes.Node insert = comment;
            if (commentToken.bogus)
            {
                // xml declarations are emitted as bogus comments (which is right for html, but not xml)
                // so we do a bit of a hack and parse the data as an element to pull the attributes out
                String data = comment.GetData();
                if (data.Length > 1 && (data.StartsWith("!") || data.StartsWith("?")))
                {
                    Document doc = iText.StyledXmlParser.Jsoup.Jsoup.Parse("<" + data.JSubstring(1, data.Length - 1) + ">", baseUri
                                                                           , iText.StyledXmlParser.Jsoup.Parser.Parser.XmlParser());
                    iText.StyledXmlParser.Jsoup.Nodes.Element el = doc.Child(0);
                    insert = new XmlDeclaration(el.TagName(), comment.BaseUri(), data.StartsWith("!"));
                    insert.Attributes().AddAll(el.Attributes());
                }
            }
            InsertNode(insert);
        }