| 1 | package ezvcard.util; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.junit.Assert.assertTrue; |
| 6 | |
| 7 | import org.jsoup.Jsoup; |
| 8 | import org.jsoup.nodes.Document; |
| 9 | import org.jsoup.nodes.Element; |
| 10 | import org.jsoup.select.Elements; |
| 11 | import org.junit.Test; |
| 12 | |
| 13 | /* |
| 14 | Copyright (c) 2012-2023, Michael Angstadt |
| 15 | All rights reserved. |
| 16 | |
| 17 | Redistribution and use in source and binary forms, with or without |
| 18 | modification, are permitted provided that the following conditions are met: |
| 19 | |
| 20 | 1. Redistributions of source code must retain the above copyright notice, this |
| 21 | list of conditions and the following disclaimer. |
| 22 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 23 | this list of conditions and the following disclaimer in the documentation |
| 24 | and/or other materials provided with the distribution. |
| 25 | |
| 26 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 27 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 28 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 29 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 30 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 31 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 32 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 33 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 35 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | |
| 37 | The views and conclusions contained in the software and documentation are those |
| 38 | of the authors and should not be interpreted as representing official policies, |
| 39 | either expressed or implied, of the FreeBSD Project. |
| 40 | */ |
| 41 | |
| 42 | /** |
| 43 | * @author Michael Angstadt |
| 44 | */ |
| 45 | public class HtmlUtilsTest { |
| 46 | @Test |
| 47 | public void isChildOf() { |
| 48 | //@formatter:off |
| 49 | String html = |
| 50 | "<html><body>" + |
| 51 | "<div id=\"one\"></div>" + |
| 52 | "<div id=\"two\">" + |
| 53 | "<div id=\"three\"></div>" + |
| 54 | "</div>" + |
| 55 | "</body></html>"; |
| 56 | //@formatter:on |
| 57 | Document document = Jsoup.parse(html); |
| 58 | |
| 59 | Element one = document.getElementById("one"); |
| 60 | Element two = document.getElementById("two"); |
| 61 | Element three = document.getElementById("three"); |
| 62 | |
| 63 | Elements elements = new Elements(one, two); |
| 64 | assertTrue(HtmlUtils.isChildOf(three, elements)); |
| 65 | |
| 66 | elements = new Elements(one, three); |
| 67 | assertFalse(HtmlUtils.isChildOf(two, elements)); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void toElement_without_base_url() { |
| 72 | Element element = HtmlUtils.toElement("<img src=\"image.png\" />"); |
| 73 | assertEquals(element.tagName(), "img"); |
| 74 | assertEquals("", element.absUrl("src")); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void toElement_with_base_url() { |
| 79 | Element element = HtmlUtils.toElement("<img src=\"image.png\" />", "http://example.com/"); |
| 80 | assertEquals(element.tagName(), "img"); |
| 81 | assertEquals("http://example.com/image.png", element.absUrl("src")); |
| 82 | } |
| 83 | } |