use of com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent in project htmlunit by HtmlUnit.
the class HTMLDocument method getIt.
private Object getIt(final String name) {
final HtmlPage page = (HtmlPage) getDomNodeOrNull();
if (page == null) {
return NOT_FOUND;
}
final boolean forIDAndOrName = getBrowserVersion().hasFeature(HTMLDOCUMENT_GET_FOR_ID_AND_OR_NAME);
final boolean alsoFrames = getBrowserVersion().hasFeature(HTMLDOCUMENT_GET_ALSO_FRAMES);
// for performance
// we will calculate the elements to decide if we really have
// to really create a HTMLCollection or not
final List<DomNode> matchingElements = getItComputeElements(page, name, forIDAndOrName, alsoFrames);
final int size = matchingElements.size();
if (size == 0) {
return NOT_FOUND;
}
if (size == 1) {
final DomNode object = matchingElements.get(0);
if (alsoFrames && object instanceof BaseFrameElement) {
return ((BaseFrameElement) object).getEnclosedWindow().getScriptableObject();
}
return super.getScriptableFor(object);
}
return new HTMLCollection(page, matchingElements) {
@Override
protected List<DomNode> computeElements() {
return getItComputeElements(page, name, forIDAndOrName, alsoFrames);
}
@Override
protected EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
final String attributeName = event.getName();
if ("name".equals(attributeName) || (forIDAndOrName && "id".equals(attributeName))) {
return EffectOnCache.RESET;
}
return EffectOnCache.NONE;
}
@Override
protected HtmlUnitScriptable getScriptableFor(final Object object) {
if (alsoFrames && object instanceof BaseFrameElement) {
return ((BaseFrameElement) object).getEnclosedWindow().getScriptableObject();
}
return super.getScriptableFor(object);
}
};
}
use of com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent in project htmlunit by HtmlUnit.
the class HTMLDocument method getElementsByName.
/**
* {@inheritDoc}
*/
@Override
@JsxFunction({ FF, FF_ESR })
public HTMLCollection getElementsByName(final String elementName) {
implicitCloseIfNecessary();
if ("null".equals(elementName) || (elementName.isEmpty() && getBrowserVersion().hasFeature(HTMLDOCUMENT_ELEMENTS_BY_NAME_EMPTY))) {
return HTMLCollection.emptyCollection(getWindow().getDomNodeOrDie());
}
final HtmlPage page = getPage();
return new HTMLCollection(page, true) {
@Override
protected List<DomNode> computeElements() {
return new ArrayList<>(page.getElementsByName(elementName));
}
@Override
protected EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
if ("name".equals(event.getName())) {
return EffectOnCache.RESET;
}
return EffectOnCache.NONE;
}
};
}
use of com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent in project htmlunit by HtmlUnit.
the class HTMLFormElement method getElements.
/**
* Returns the value of the property {@code elements}.
* @return the value of this property
*/
@JsxGetter
public HTMLCollection getElements() {
final HtmlForm htmlForm = getHtmlForm();
return new HTMLCollection(htmlForm, false) {
private boolean filterChildrenOfNestedForms_;
@Override
protected List<DomNode> computeElements() {
final List<DomNode> response = super.computeElements();
// of the cases
if (filterChildrenOfNestedForms_) {
for (final Iterator<DomNode> iter = response.iterator(); iter.hasNext(); ) {
final HtmlElement field = (HtmlElement) iter.next();
if (field.getEnclosingForm() != htmlForm) {
iter.remove();
}
}
}
response.addAll(htmlForm.getLostChildren());
return response;
}
@Override
protected Object getWithPreemption(final String name) {
return HTMLFormElement.this.getWithPreemption(name);
}
@Override
public EffectOnCache getEffectOnCache(final HtmlAttributeChangeEvent event) {
return EffectOnCache.NONE;
}
@Override
protected boolean isMatching(final DomNode node) {
if (node instanceof HtmlForm) {
filterChildrenOfNestedForms_ = true;
return false;
}
return node instanceof HtmlInput || node instanceof HtmlButton || node instanceof HtmlTextArea || node instanceof HtmlSelect;
}
};
}
use of com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent 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