use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection in project htmlunit by HtmlUnit.
the class Element method getElementsByTagName.
/**
* Returns all the descendant elements with the specified tag name.
* @param tagName the name to search for
* @return all the descendant elements with the specified tag name
*/
@JsxFunction
public HTMLCollection getElementsByTagName(final String tagName) {
if (elementsByTagName_ == null) {
elementsByTagName_ = new HashMap<>();
}
final String searchTagName;
final boolean caseSensitive;
final DomNode dom = getDomNodeOrNull();
if (dom == null) {
searchTagName = tagName.toLowerCase(Locale.ROOT);
caseSensitive = false;
} else {
final SgmlPage page = dom.getPage();
if (page != null && page.hasCaseSensitiveTagNames()) {
searchTagName = tagName;
caseSensitive = true;
} else {
searchTagName = tagName.toLowerCase(Locale.ROOT);
caseSensitive = false;
}
}
HTMLCollection collection = elementsByTagName_.get(searchTagName);
if (collection != null) {
return collection;
}
final DomNode node = getDomNodeOrDie();
if ("*".equals(tagName)) {
collection = new HTMLCollection(node, false) {
@Override
protected boolean isMatching(final DomNode nodeToMatch) {
return true;
}
};
} else {
collection = new HTMLCollection(node, false) {
@Override
protected boolean isMatching(final DomNode nodeToMatch) {
if (caseSensitive) {
return searchTagName.equals(nodeToMatch.getNodeName());
}
return searchTagName.equalsIgnoreCase(nodeToMatch.getNodeName());
}
};
}
elementsByTagName_.put(tagName, collection);
return collection;
}
use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection in project htmlunit by HtmlUnit.
the class Element method getElementsByClassName.
/**
* Returns all the descendant elements with the specified class.
* @param className the name to search for
* @return all the descendant elements with the specified class name
*/
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public HTMLCollection getElementsByClassName(final String className) {
final DomElement elt = getDomNodeOrDie();
final String[] classNames = CLASS_NAMES_SPLIT_PATTERN.split(className, 0);
return new HTMLCollection(elt, true) {
@Override
protected boolean isMatching(final DomNode node) {
if (!(node instanceof HtmlElement)) {
return false;
}
String classAttribute = ((HtmlElement) node).getAttributeDirect("class");
if (ATTRIBUTE_NOT_DEFINED == classAttribute) {
// probably better performance as most of elements won't have a class attribute
return false;
}
classAttribute = " " + classAttribute + " ";
for (final String aClassName : classNames) {
if (!classAttribute.contains(" " + aClassName + " ")) {
return false;
}
}
return true;
}
};
}
use of com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection in project htmlunit by HtmlUnit.
the class HTMLCollectionFrames method getElementsByName.
private Object getElementsByName(final HtmlPage page, final String name) {
// May be attempting to retrieve element(s) by name. IMPORTANT: We're using map-backed operations
// like getHtmlElementsByName() and getHtmlElementById() as much as possible, so as to avoid XPath
// overhead. We only use an XPath-based operation when we have to (where there is more than one
// matching element). This optimization appears to improve performance in certain situations by ~15%
// vs using XPath-based operations throughout.
final List<DomElement> elements = page.getElementsByName(name);
final boolean includeFormFields = getBrowserVersion().hasFeature(JS_WINDOW_FORMFIELDS_ACCESSIBLE_BY_NAME);
final Filter filter = new Filter(includeFormFields);
elements.removeIf(domElement -> !filter.matches(domElement));
if (elements.isEmpty()) {
return NOT_FOUND;
}
if (elements.size() == 1) {
return getScriptableFor(elements.get(0));
}
// Null must be changed to '' for proper collection initialization.
final String expElementName = "null".equals(name) ? "" : name;
return new HTMLCollection(page, true) {
@Override
protected List<DomNode> computeElements() {
final List<DomElement> expElements = page.getElementsByName(expElementName);
final List<DomNode> result = new ArrayList<>(expElements.size());
for (final DomElement domElement : expElements) {
if (filter.matches(domElement)) {
result.add(domElement);
}
}
return result;
}
@Override
protected EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
if ("name".equals(event.getName())) {
return EffectOnCache.RESET;
}
return EffectOnCache.NONE;
}
};
}
Aggregations