Ejemplo n.º 1
0
        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"));
        }
Ejemplo n.º 2
0
        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"));
        }
Ejemplo n.º 3
0
        public virtual void TestMetaCharsetUpdateDisabledNoChanges()
        {
            Document doc         = CreateHtmlDocument("dontTouch");
            String   htmlCharset = "<html>\n" + " <head>\n" + "  <meta charset=\"dontTouch\">\n" + "  <meta name=\"charset\" content=\"dontTouch\">\n"
                                   + " </head>\n" + " <body></body>\n" + "</html>";

            NUnit.Framework.Assert.AreEqual(htmlCharset, doc.ToString());
            iText.StyledXmlParser.Jsoup.Nodes.Element selectedElement = doc.Select("meta[charset]").First();
            NUnit.Framework.Assert.IsNotNull(selectedElement);
            NUnit.Framework.Assert.AreEqual("dontTouch", selectedElement.Attr("charset"));
            selectedElement = doc.Select("meta[name=charset]").First();
            NUnit.Framework.Assert.IsNotNull(selectedElement);
            NUnit.Framework.Assert.AreEqual("dontTouch", selectedElement.Attr("content"));
        }
Ejemplo n.º 4
0
 /// <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>&lt;meta charset="CHARSET"&gt;</i></li>
 /// <li><b>Xml:</b> <i>&lt;?xml version="1.0" encoding="CHARSET"&gt;</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);
                 }
             }
         }
     }
 }
Ejemplo n.º 5
0
        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());
        }
Ejemplo n.º 6
0
        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"));
        }
Ejemplo n.º 7
0
        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));
        }
Ejemplo n.º 8
0
        public virtual void CompareIsStyleSheetLinkResult()
        {
            iText.StyledXmlParser.Jsoup.Nodes.Element element = new iText.StyledXmlParser.Jsoup.Nodes.Element(iText.StyledXmlParser.Jsoup.Parser.Tag
                                                                                                              .ValueOf("link"), "");
            element.Attr(CommonAttributeConstants.REL, CommonAttributeConstants.STYLESHEET);
            JsoupElementNode elementNode = new JsoupElementNode(element);
            bool             expected    = CssUtils.IsStyleSheetLink(elementNode);
            bool             actual      = HtmlUtils.IsStyleSheetLink(elementNode);

            NUnit.Framework.Assert.AreEqual(actual, expected);
        }
Ejemplo n.º 9
0
        public virtual void TestMetaCharsetUpdateIso8859()
        {
            Document doc = CreateHtmlDocument("changeThis");

            doc.UpdateMetaCharsetElement(true);
            doc.Charset(EncodingUtil.GetEncoding(charsetIso8859));
            String htmlCharsetISO = "<html>\n" + " <head>\n" + "  <meta charset=\"" + charsetIso8859 + "\">\n" + " </head>\n"
                                    + " <body></body>\n" + "</html>";

            NUnit.Framework.Assert.AreEqual(htmlCharsetISO, doc.ToString());
            iText.StyledXmlParser.Jsoup.Nodes.Element selectedElement = doc.Select("meta[charset]").First();
            NUnit.Framework.Assert.AreEqual(charsetIso8859, doc.Charset().Name());
            NUnit.Framework.Assert.AreEqual(charsetIso8859, selectedElement.Attr("charset"));
            NUnit.Framework.Assert.AreEqual(doc.Charset(), doc.OutputSettings().Charset());
        }