Skip to content

Commit

Permalink
use the correct tag name for constructing new elements (issue #127)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Nov 13, 2024
1 parent d18d662 commit 2c357f8
Show file tree
Hide file tree
Showing 515 changed files with 128 additions and 7,261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -533,13 +533,13 @@ public Element createElement(final String tagName) throws DOMException {
// owner document and a tag name. Use the constructor to instantiate
// a new object and return it.
try {
return htmlHolder.ctr_.newInstance(this, htmlHolder.tagName_);
return htmlHolder.ctr_.newInstance(this, tagName);
}
catch (final Exception e) {
throw new IllegalStateException("HTM15 Tag '" + tagName + "' associated with an Element class that failed to construct.\n" + tagName, e);
throw new IllegalStateException("HTM15 Tag '" + tagName + "' associated with an Element class that failed to construct.", e);
}
}
return new HTMLElementImpl(this, tagName.toUpperCase(Locale.ENGLISH));
return new HTMLElementImpl(this, tagName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ public void attrEndingWithCRAtEndOfStream() {
*/
@Test
public void invalidProcessingInstruction() throws Exception {
doTest("<html><?9 ?></html>", "<HTML/>");
doTest("<html><?9 ?></html>", "<html/>");
}

/**
* See <a href="http://sourceforge.net/support/tracker.php?aid=2828534">Bug 2828534</a>.
*/
@Test
public void invalidAttributeName() throws Exception {
doTest("<html 9='id'></html>", "<HTML/>");
doTest("<html 9='id'></html>", "<html/>");
}

private static void doTest(final String html, final String expected) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2002-2009 Andy Clark, Marc Guillemot
* Copyright (c) 2017-2024 Ronald Brill
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.cyberneko.xerces.dom;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.StringReader;

import org.htmlunit.cyberneko.html.dom.HTMLDocumentImpl;
import org.htmlunit.cyberneko.parsers.DOMParser;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;

/**
* Unit tests for {@link HTMLDocumentImpl}.
*
* @author Ronald Brill
*/
public class HTMLDocumentImplTest {

@Test
public void tagName() throws Exception {
final String html = "<HTML><head></head>"
+ "<bODy>"
+ "<DIv>abc</DIv>"
+ "</bodY></HTML>";

DOMParser parser = new DOMParser(HTMLDocumentImpl.class);
// parser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");
// parser.setProperty("http://cyberneko.org/html/properties/names/attrs", "lower");

// parser.setProperty("http://cyberneko.org/html/properties/names/elems", "upper");
// parser.setProperty("http://cyberneko.org/html/properties/names/attrs", "upper");

parser.parse(new InputSource(new StringReader(html)));
Document doc = parser.getDocument();

Element htmlElem = doc.getDocumentElement();
assertEquals("HTML", htmlElem.getTagName());

Element headElem = (Element) htmlElem.getChildNodes().item(0);
assertEquals("head", headElem.getTagName());

Element bodyElem = (Element) htmlElem.getChildNodes().item(1);
assertEquals("bODy", bodyElem.getTagName());

Element divElem = (Element) bodyElem.getChildNodes().item(0);
assertEquals("DIv", divElem.getTagName());
}

@Test
public void tagNameLower() throws Exception {
final String html = "<HTML><head></head>"
+ "<bODy>"
+ "<DIv>abc</DIv>"
+ "</bodY></HTML>";

DOMParser parser = new DOMParser(HTMLDocumentImpl.class);
parser.setProperty("http://cyberneko.org/html/properties/names/elems", "lower");

parser.parse(new InputSource(new StringReader(html)));
Document doc = parser.getDocument();

Element htmlElem = doc.getDocumentElement();
assertEquals("html", htmlElem.getTagName());

Element headElem = (Element) htmlElem.getChildNodes().item(0);
assertEquals("head", headElem.getTagName());

Element bodyElem = (Element) htmlElem.getChildNodes().item(1);
assertEquals("body", bodyElem.getTagName());

Element divElem = (Element) bodyElem.getChildNodes().item(0);
assertEquals("div", divElem.getTagName());
}

@Test
public void tagNameUpper() throws Exception {
final String html = "<HTML><head></head>"
+ "<bODy>"
+ "<DIv>abc</DIv>"
+ "</bodY></HTML>";

DOMParser parser = new DOMParser(HTMLDocumentImpl.class);
parser.setProperty("http://cyberneko.org/html/properties/names/elems", "upper");

parser.parse(new InputSource(new StringReader(html)));
Document doc = parser.getDocument();

Element htmlElem = doc.getDocumentElement();
assertEquals("HTML", htmlElem.getTagName());

Element headElem = (Element) htmlElem.getChildNodes().item(0);
assertEquals("HEAD", headElem.getTagName());

Element bodyElem = (Element) htmlElem.getChildNodes().item(1);
assertEquals("BODY", bodyElem.getTagName());

Element divElem = (Element) bodyElem.getChildNodes().item(0);
assertEquals("DIV", divElem.getTagName());
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 2c357f8

Please sign in to comment.