View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.html;
16  
17  import java.util.Iterator;
18  
19  import org.htmlunit.SimpleWebTestCase;
20  import org.junit.jupiter.api.Test;
21  import org.xml.sax.helpers.AttributesImpl;
22  
23  /**
24   * Tests for {@link DefaultElementFactory}.
25   *
26   * @author <a href="mailto:marvin.java@gmail.com">Marcos Vinicius B. de Souza</a>
27   * @author Marc Guillemot
28   * @author Ahmed Ashour
29   * @author Ronald Brill
30   * @since 1.2
31   */
32  public class DefaultElementFactoryTest extends SimpleWebTestCase {
33  
34      /**
35       * Test that the attribute order is the same as the provided one.
36       * @throws Exception if the test fails
37       */
38      @Test
39      public void attributeOrder() throws Exception {
40          // Construct the test page.
41          final String html = DOCTYPE_HTML
42                  + "<html><head><title>test page</title></head>\n"
43                  + "<body><div>test message</div></body></html>";
44  
45          // Load the test page.
46          final HtmlPage htmlPage = loadPage(html);
47  
48          // Creates the attributes of the 'anchor'.
49          final AttributesImpl atts = new AttributesImpl();
50          atts.addAttribute(null, "href", "href", null, "http://www.google.com");
51          atts.addAttribute(null, "tabindex", "tabindex", null, "2");
52          atts.addAttribute(null, "accesskey", "accesskey", null, "F");
53  
54          // Access the factory.
55          final DefaultElementFactory defaultElementFactory = new DefaultElementFactory();
56  
57          // Create a anchor element
58          final HtmlAnchor anchor = (HtmlAnchor) defaultElementFactory.createElement(htmlPage, "a", atts);
59  
60          verifyAttributes(anchor);
61      }
62  
63      /**
64       * @param anchor the anchor which attributes should be checked
65       */
66      private static void verifyAttributes(final HtmlAnchor anchor) {
67          // Get the attributes iterator
68          final Iterator<DomAttr> attributeEntriesIterator = anchor.getAttributesMap().values().iterator();
69  
70          // Verify if the attributes are in ascending order of name.
71          DomAttr htmlAttr = attributeEntriesIterator.next();
72          assertEquals("href", htmlAttr.getNodeName());
73          assertEquals("http://www.google.com", htmlAttr.getValue());
74  
75          htmlAttr = attributeEntriesIterator.next();
76          assertEquals("tabindex", htmlAttr.getNodeName());
77          assertEquals("2", htmlAttr.getValue());
78  
79          htmlAttr = attributeEntriesIterator.next();
80          assertEquals("accesskey", htmlAttr.getNodeName());
81          assertEquals("F", htmlAttr.getValue());
82      }
83  
84      /**
85       * Test the order of attributes.
86       * @throws Exception if the test fails
87       */
88      @Test
89      public void attributeOrderLive() throws Exception {
90          final String html = DOCTYPE_HTML
91              + "<html><body>\n"
92              + "<a href='http://www.google.com' tabindex='2' accesskey='F'>foo</a>\n"
93              + "</body></html>";
94          final HtmlPage page = loadPage(html);
95          final HtmlAnchor anchor = page.getAnchorByText("foo");
96  
97          verifyAttributes(anchor);
98      }
99  }