Skip to content

Commit

Permalink
HtmlElement: deprecate .getHtmlElementsByTagName() and .getHtmlElemen…
Browse files Browse the repository at this point in the history
…tsByTagNames()

git-svn-id: http://svn.code.sf.net/p/htmlunit/code/trunk/htmlunit@12258 5f5364db-9458-4db8-a492-e30667be6df6
  • Loading branch information
asashour committed Mar 31, 2016
1 parent b6d60b5 commit d1697e8
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 44 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<body>
<release version="2.21" date="???" description="FF45, Bugfixes">
<action type="update" dev="asashour">
HtmlElement: deprecate .getHtmlElementsByTagName() and .getHtmlElementsByTagNames().
</action>
<action type="add" dev="asashour">
JavaScript: add .labels property for HTMLButtonElement, HTMLInputElement, HTMLKeygenElement,
HTMLMeterElement, HTMLOutputElement, HTMLProgressElement, HTMLSelectElement and HTMLTextAreaElement.
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/com/gargoylesoftware/htmlunit/WebAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,14 @@ public static void assertInputDoesNotContainValue(final HtmlPage page, final Str
public static void assertAllTabIndexAttributesSet(final HtmlPage page) {
final List<String> tags =
Arrays.asList(new String[] {"a", "area", "button", "input", "object", "select", "textarea"});
for (final HtmlElement element : page.getDocumentElement().getHtmlElementsByTagNames(tags)) {
final Short tabIndex = element.getTabIndex();
if (tabIndex == null || tabIndex == HtmlElement.TAB_INDEX_OUT_OF_BOUNDS) {
final String s = element.getAttribute("tabindex");
throw new AssertionError("Illegal value for tab index: '" + s + "'.");

for (final String tag : tags) {
for (final HtmlElement element : page.getDocumentElement().getElementsByTagName(tag)) {
final Short tabIndex = element.getTabIndex();
if (tabIndex == null || tabIndex == HtmlElement.TAB_INDEX_OUT_OF_BOUNDS) {
final String s = element.getAttribute("tabindex");
throw new AssertionError("Illegal value for tab index: '" + s + "'.");
}
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/com/gargoylesoftware/htmlunit/html/DomElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,13 +398,23 @@ public DomAttr getAttributeNodeNS(final String namespaceURI, final String localN
*/
@Override
public DomNodeList<HtmlElement> getElementsByTagName(final String tagName) {
return new AbstractDomNodeList<HtmlElement>(this) {
return getElementsByTagNameImpl(tagName);
}

/**
* This should be {@link #getElementsByTagName(String)}, but is separate because of the type erasure in Java.
* @param name The name of the tag to match on
* @return A list of matching elements.
*/
<E extends HtmlElement> DomNodeList<E> getElementsByTagNameImpl(final String tagName) {
return new AbstractDomNodeList<E>(this) {
@Override
protected List<HtmlElement> provideElements() {
final List<HtmlElement> res = new LinkedList<>();
for (HtmlElement elem : getDomNode().getHtmlElementDescendants()) {
@SuppressWarnings("unchecked")
protected List<E> provideElements() {
final List<E> res = new LinkedList<>();
for (final HtmlElement elem : getDomNode().getHtmlElementDescendants()) {
if (elem.getLocalName().equalsIgnoreCase(tagName)) {
res.add(elem);
res.add((E) elem);
}
}
return res;
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/com/gargoylesoftware/htmlunit/html/HtmlElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,13 @@ public Attr setAttributeNode(final Attr attribute) {
* Returns the HTML elements that are descendants of this element and that have one of the specified tag names.
* @param tagNames the tag names to match (case-insensitive)
* @return the HTML elements that are descendants of this element and that have one of the specified tag name
* @deprecated as of 2.21, please use {@link #getElementsByTagName(String)}
*/
@Deprecated
public final List<HtmlElement> getHtmlElementsByTagNames(final List<String> tagNames) {
final List<HtmlElement> list = new ArrayList<>();
for (final String tagName : tagNames) {
list.addAll(getHtmlElementsByTagName(tagName));
list.addAll(getElementsByTagName(tagName));
}
return list;
}
Expand All @@ -267,18 +269,12 @@ public final List<HtmlElement> getHtmlElementsByTagNames(final List<String> tagN
* @param tagName the tag name to match (case-insensitive)
* @param <E> the sub-element type
* @return the HTML elements that are descendants of this element and that have the specified tag name
* @deprecated as of 2.21, please use {@link #getElementsByTagName(String)}
*/
@Deprecated
@SuppressWarnings("unchecked")
public final <E extends HtmlElement> List<E> getHtmlElementsByTagName(final String tagName) {
final List<E> list = new ArrayList<>();
final String lowerCaseTagName = tagName.toLowerCase(Locale.ROOT);
final Iterable<HtmlElement> iterable = getHtmlElementDescendants();
for (final HtmlElement element : iterable) {
if (lowerCaseTagName.equals(element.getTagName())) {
list.add((E) element);
}
}
return list;
return (List<E>) getElementsByTagName(tagName);
}

/**
Expand Down Expand Up @@ -982,7 +978,7 @@ public final <E extends HtmlElement> List<E> getElementsByAttribute(
*/
public final HtmlElement appendChildIfNoneExists(final String tagName) {
final HtmlElement child;
final List<HtmlElement> children = getHtmlElementsByTagName(tagName);
final List<HtmlElement> children = getElementsByTagName(tagName);
if (children.isEmpty()) {
// Add a new child and return it.
child = (HtmlElement) ((HtmlPage) getPage()).createElement(tagName);
Expand All @@ -1002,7 +998,7 @@ public final HtmlElement appendChildIfNoneExists(final String tagName) {
* @param i the index of the child to remove
*/
public final void removeChild(final String tagName, final int i) {
final List<HtmlElement> children = getHtmlElementsByTagName(tagName);
final List<HtmlElement> children = getElementsByTagName(tagName);
if (i >= 0 && i < children.size()) {
children.get(i).remove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private HtmlImage findReferencingImage() {
String name = getNameAttribute();
if (null != page && StringUtils.isNotBlank(name)) {
name = "#" + name.trim();
for (HtmlElement elem : page.getDocumentElement().getHtmlElementsByTagName("img")) {
for (HtmlElement elem : page.getDocumentElement().getElementsByTagName("img")) {
final HtmlImage image = (HtmlImage) elem;
if (name.equals(image.getUseMapAttribute())) {
return image;
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/com/gargoylesoftware/htmlunit/html/HtmlPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@
import java.util.SortedSet;
import java.util.TreeSet;

import net.sourceforge.htmlunit.corejs.javascript.Context;
import net.sourceforge.htmlunit.corejs.javascript.Script;
import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -83,6 +78,11 @@
import com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument;
import com.gargoylesoftware.htmlunit.protocol.javascript.JavaScriptURLConnection;

import net.sourceforge.htmlunit.corejs.javascript.Context;
import net.sourceforge.htmlunit.corejs.javascript.Script;
import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;

/**
* A representation of an HTML page returned from a server.
* <p>
Expand Down Expand Up @@ -628,7 +628,7 @@ public HtmlAnchor getAnchorByHref(final String href) throws ElementNotFoundExcep
* @return the list of {@link HtmlAnchor} in this page
*/
public List<HtmlAnchor> getAnchors() {
return getDocumentElement().getHtmlElementsByTagName("a");
return getDocumentElement().getElementsByTagNameImpl("a");
}

/**
Expand Down Expand Up @@ -667,7 +667,7 @@ public HtmlForm getFormByName(final String name) throws ElementNotFoundException
* @return all the forms in this page
*/
public List<HtmlForm> getForms() {
return getDocumentElement().getHtmlElementsByTagName("form");
return getDocumentElement().getElementsByTagNameImpl("form");
}

/**
Expand Down Expand Up @@ -1380,7 +1380,7 @@ private void executeDeferredScriptsIfNeeded() {
}
if (hasFeature(JS_DEFERRED)) {
final HtmlElement doc = getDocumentElement();
final List<HtmlElement> elements = doc.getHtmlElementsByTagName("script");
final List<HtmlElement> elements = doc.getElementsByTagName("script");
for (final HtmlElement e : elements) {
if (e instanceof HtmlScript) {
final HtmlScript script = (HtmlScript) e;
Expand All @@ -1397,7 +1397,7 @@ private void executeDeferredScriptsIfNeeded() {
*/
private void setReadyStateOnDeferredScriptsIfNeeded() {
if (getWebClient().getOptions().isJavaScriptEnabled() && hasFeature(JS_DEFERRED)) {
final List<HtmlElement> elements = getDocumentElement().getHtmlElementsByTagName("script");
final List<HtmlElement> elements = getDocumentElement().getElementsByTagName("script");
for (final HtmlElement e : elements) {
if (e instanceof HtmlScript) {
final HtmlScript script = (HtmlScript) e;
Expand Down Expand Up @@ -1810,18 +1810,18 @@ static boolean isMappedElement(final Document document, final String attributeNa
}

private void calculateBase() {
final List<HtmlBase> baseElements = getDocumentElement().getHtmlElementsByTagName("base");
final List<HtmlElement> baseElements = getDocumentElement().getElementsByTagName("base");
switch (baseElements.size()) {
case 0:
base_ = null;
break;

case 1:
base_ = baseElements.get(0);
base_ = (HtmlBase) baseElements.get(0);
break;

default:
base_ = baseElements.get(0);
base_ = (HtmlBase) baseElements.get(0);
notifyIncorrectness("Multiple 'base' detected, only the first is used.");
}
}
Expand Down Expand Up @@ -1870,7 +1870,7 @@ protected List<HtmlMeta> getMetaTags(final String httpEquiv) {
return Collections.emptyList(); // weird case, for instance if document.documentElement has been removed
}
final String nameLC = httpEquiv.toLowerCase(Locale.ROOT);
final List<HtmlMeta> tags = getDocumentElement().getHtmlElementsByTagName("meta");
final List<HtmlMeta> tags = getDocumentElement().getElementsByTagNameImpl("meta");
for (final Iterator<HtmlMeta> iter = tags.iterator(); iter.hasNext();) {
final HtmlMeta element = iter.next();
if (!nameLC.equals(element.getHttpEquivAttribute().toLowerCase(Locale.ROOT))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public List<HtmlOption> getSelectedOptions() {
* @return all of the options in this select element
*/
public List<HtmlOption> getOptions() {
return Collections.unmodifiableList(this.<HtmlOption>getHtmlElementsByTagName("option"));
return Collections.unmodifiableList(this.<HtmlOption>getElementsByTagNameImpl("option"));
}

/**
Expand All @@ -155,15 +155,15 @@ public List<HtmlOption> getOptions() {
* @return the option specified by the index
*/
public HtmlOption getOption(final int index) {
return this.<HtmlOption>getHtmlElementsByTagName("option").get(index);
return this.<HtmlOption>getElementsByTagNameImpl("option").get(index);
}

/**
* Returns the number of options.
* @return the number of options
*/
public int getOptionSize() {
return getHtmlElementsByTagName("option").size();
return getElementsByTagName("option").size();
}

/**
Expand All @@ -172,7 +172,7 @@ public int getOptionSize() {
* @param newLength the new length property value
*/
public void setOptionSize(final int newLength) {
final List<HtmlElement> elementList = getHtmlElementsByTagName("option");
final List<HtmlElement> elementList = getElementsByTagName("option");

for (int i = elementList.size() - 1; i >= newLength; i--) {
elementList.get(i).remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public HTMLTableElement() {
*/
@JsxGetter
public Object getCaption() {
final List<HtmlElement> captions = getDomNodeOrDie().getHtmlElementsByTagName("caption");
final List<HtmlElement> captions = getDomNodeOrDie().getElementsByTagName("caption");
if (captions.isEmpty()) {
return null;
}
Expand Down Expand Up @@ -98,7 +98,7 @@ public void setCaption(final Object o) {
*/
@JsxGetter
public Object getTFoot() {
final List<HtmlElement> tfoots = getDomNodeOrDie().getHtmlElementsByTagName("tfoot");
final List<HtmlElement> tfoots = getDomNodeOrDie().getElementsByTagName("tfoot");
if (tfoots.isEmpty()) {
return null;
}
Expand Down Expand Up @@ -130,7 +130,7 @@ public void setTFoot(final Object o) {
*/
@JsxGetter
public Object getTHead() {
final List<HtmlElement> theads = getDomNodeOrDie().getHtmlElementsByTagName("thead");
final List<HtmlElement> theads = getDomNodeOrDie().getElementsByTagName("thead");
if (theads.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public void asTextWithMultipleSelect() throws Exception {
+ "</select>\n"
+ "</form></body></html>";
final HtmlPage page = loadPage(html);
final HtmlSelect select = (HtmlSelect) page.getDocumentElement().getHtmlElementsByTagName("select").get(0);
final HtmlSelect select = (HtmlSelect) page.getDocumentElement().getElementsByTagName("select").get(0);
assertEquals("foo\nbar\nbaz", select.asText());
}

Expand Down

0 comments on commit d1697e8

Please sign in to comment.