use of com.gargoylesoftware.htmlunit.javascript.host.event.Event in project htmlunit by HtmlUnit.
the class HtmlPage method initialize.
/**
* Initialize this page.
* @throws IOException if an IO problem occurs
* @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
* {@link com.gargoylesoftware.htmlunit.WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set
* to true.
*/
@Override
public void initialize() throws IOException, FailingHttpStatusCodeException {
final WebWindow enclosingWindow = getEnclosingWindow();
final boolean isAboutBlank = getUrl() == UrlUtils.URL_ABOUT_BLANK;
if (isAboutBlank) {
// a frame contains first a faked "about:blank" before its real content specified by src gets loaded
if (enclosingWindow instanceof FrameWindow && !((FrameWindow) enclosingWindow).getFrameElement().isContentLoaded()) {
return;
}
// save the URL that should be used to resolve relative URLs in this page
if (enclosingWindow instanceof TopLevelWindow) {
final TopLevelWindow topWindow = (TopLevelWindow) enclosingWindow;
final WebWindow openerWindow = topWindow.getOpener();
if (openerWindow != null && openerWindow.getEnclosedPage() != null) {
baseUrl_ = openerWindow.getEnclosedPage().getWebResponse().getWebRequest().getUrl();
}
}
}
if (!isAboutBlank) {
setReadyState(READY_STATE_INTERACTIVE);
getDocumentElement().setReadyState(READY_STATE_INTERACTIVE);
}
executeDeferredScriptsIfNeeded();
executeEventHandlersIfNeeded(Event.TYPE_DOM_DOCUMENT_LOADED);
loadFrames();
// see Node.initInlineFrameIfNeeded()
if (!isAboutBlank) {
if (hasFeature(FOCUS_BODY_ELEMENT_AT_START)) {
setElementWithFocus(getBody());
}
setReadyState(READY_STATE_COMPLETE);
getDocumentElement().setReadyState(READY_STATE_COMPLETE);
}
// frame initialization has a different order
boolean isFrameWindow = enclosingWindow instanceof FrameWindow;
boolean isFirstPageInFrameWindow = false;
if (isFrameWindow) {
isFrameWindow = ((FrameWindow) enclosingWindow).getFrameElement() instanceof HtmlFrame;
final History hist = enclosingWindow.getHistory();
if (hist.getLength() > 0 && UrlUtils.URL_ABOUT_BLANK == hist.getUrl(0)) {
isFirstPageInFrameWindow = hist.getLength() <= 2;
} else {
isFirstPageInFrameWindow = enclosingWindow.getHistory().getLength() < 2;
}
}
if (isFrameWindow && !isFirstPageInFrameWindow) {
executeEventHandlersIfNeeded(Event.TYPE_LOAD);
}
for (final FrameWindow frameWindow : getFrames()) {
if (frameWindow.getFrameElement() instanceof HtmlFrame) {
final Page page = frameWindow.getEnclosedPage();
if (page != null && page.isHtmlPage()) {
((HtmlPage) page).executeEventHandlersIfNeeded(Event.TYPE_LOAD);
}
}
}
if (!isFrameWindow) {
executeEventHandlersIfNeeded(Event.TYPE_LOAD);
if (!isAboutBlank && enclosingWindow.getWebClient().isJavaScriptEnabled() && hasFeature(EVENT_FOCUS_ON_LOAD)) {
final HtmlElement body = getBody();
if (body != null) {
final Event event = new Event((Window) enclosingWindow.getScriptableObject(), Event.TYPE_FOCUS);
body.fireEvent(event);
}
}
}
try {
while (!afterLoadActions_.isEmpty()) {
final PostponedAction action = afterLoadActions_.remove(0);
action.execute();
}
} catch (final IOException e) {
throw e;
} catch (final Exception e) {
throw new RuntimeException(e);
}
executeRefreshIfNeeded();
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.Event in project htmlunit by HtmlUnit.
the class HtmlElement method type.
/**
* Simulates typing the specified character while this element has focus, returning the page contained
* by this element's window after typing. Note that it may or may not be the same as the original page,
* depending on the JavaScript event handlers, etc. Note also that for some elements, typing <tt>'\n'</tt>
* submits the enclosed form.
*
* @param c the character you wish to simulate typing
* @param lastType is this the last character to type
* @return the page contained in the current window as returned by {@link WebClient#getCurrentWindow()}
* @exception IOException if an IO error occurs
*/
private Page type(final char c, final boolean lastType) throws IOException {
if (isDisabledElementAndDisabled()) {
return getPage();
}
// make enclosing window the current one
getPage().getWebClient().setCurrentWindow(getPage().getEnclosingWindow());
final HtmlPage page = (HtmlPage) getPage();
if (page.getFocusedElement() != this) {
focus();
}
final boolean isShiftNeeded = KeyboardEvent.isShiftNeeded(c, shiftPressed_);
final Event shiftDown;
final ScriptResult shiftDownResult;
if (isShiftNeeded) {
shiftDown = new KeyboardEvent(this, Event.TYPE_KEY_DOWN, KeyboardEvent.DOM_VK_SHIFT, true, ctrlPressed_, altPressed_);
shiftDownResult = fireEvent(shiftDown);
} else {
shiftDown = null;
shiftDownResult = null;
}
final Event keyDown = new KeyboardEvent(this, Event.TYPE_KEY_DOWN, c, shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
final ScriptResult keyDownResult = fireEvent(keyDown);
if (!keyDown.isAborted(keyDownResult)) {
final Event keyPress = new KeyboardEvent(this, Event.TYPE_KEY_PRESS, c, shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
final ScriptResult keyPressResult = fireEvent(keyPress);
if ((shiftDown == null || !shiftDown.isAborted(shiftDownResult)) && !keyPress.isAborted(keyPressResult)) {
doType(c, lastType);
}
}
final WebClient webClient = page.getWebClient();
if (this instanceof HtmlTextInput || this instanceof HtmlTextArea || this instanceof HtmlTelInput || this instanceof HtmlNumberInput || this instanceof HtmlSearchInput || this instanceof HtmlPasswordInput) {
fireEvent(new KeyboardEvent(this, Event.TYPE_INPUT, c, shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_));
}
HtmlElement eventSource = this;
if (!isAttachedToPage()) {
final BrowserVersion browserVersion = page.getWebClient().getBrowserVersion();
if (browserVersion.hasFeature(HTMLELEMENT_DETACH_ACTIVE_TRIGGERS_NO_KEYUP_EVENT)) {
eventSource = null;
} else {
eventSource = page.getBody();
}
}
if (eventSource != null) {
final Event keyUp = new KeyboardEvent(this, Event.TYPE_KEY_UP, c, shiftPressed_ || isShiftNeeded, ctrlPressed_, altPressed_);
eventSource.fireEvent(keyUp);
if (isShiftNeeded) {
final Event shiftUp = new KeyboardEvent(this, Event.TYPE_KEY_UP, KeyboardEvent.DOM_VK_SHIFT, false, ctrlPressed_, altPressed_);
eventSource.fireEvent(shiftUp);
}
}
final HtmlForm form = getEnclosingForm();
if (form != null && c == '\n' && isSubmittableByEnter()) {
final HtmlSubmitInput submit = form.getFirstByXPath(".//input[@type='submit']");
if (submit != null) {
return submit.click();
}
form.submit((SubmittableElement) this);
webClient.getJavaScriptEngine().processPostponedActions();
}
return webClient.getCurrentWindow().getEnclosedPage();
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.Event in project htmlunit by HtmlUnit.
the class HtmlLink method executeEvent.
private void executeEvent(final String type) {
final Object scriptable = getScriptableObject();
final HTMLLinkElement link = (HTMLLinkElement) scriptable;
final Event event = new Event(this, type);
link.executeEventLocally(event);
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.Event in project htmlunit by HtmlUnit.
the class FileReader method readAsArrayBuffer.
/**
* Reads the contents of the specified {@link Blob} or {@link File}.
* @param object the {@link Blob} or {@link File} from which to read
* @throws IOException if an error occurs
*/
@JsxFunction
public void readAsArrayBuffer(final Object object) throws IOException {
readyState_ = LOADING;
if (object instanceof Blob) {
final byte[] bytes = ((Blob) object).getBytes();
final NativeArrayBuffer buffer = new NativeArrayBuffer(bytes.length);
System.arraycopy(bytes, 0, buffer.getBuffer(), 0, bytes.length);
buffer.setParentScope(getParentScope());
buffer.setPrototype(ScriptableObject.getClassPrototype(getWindow(), buffer.getClassName()));
result_ = buffer;
}
readyState_ = DONE;
final Event event = new Event(this, Event.TYPE_LOAD);
fireEvent(event);
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.Event in project htmlunit by HtmlUnit.
the class FileReader method readAsText.
/**
* Reads the contents of the specified {@link Blob} or {@link File}.
* When the read operation is complete, the readyState is changed to DONE,
* the loaded event is triggered, and the result attribute contains the
* contents of the file as a text string.
* @param object the {@link Blob} or {@link File} from which to read
* @param encoding the encoding
* @throws IOException if an error occurs
*/
@JsxFunction
public void readAsText(final Object object, final Object encoding) throws IOException {
readyState_ = LOADING;
Charset charset = StandardCharsets.UTF_8;
if (encoding != null && !Undefined.isUndefined(encoding)) {
final String encAsString = Context.toString(encoding);
if (StringUtils.isNotBlank(encAsString)) {
try {
charset = Charsets.toCharset(encAsString.trim().toLowerCase(Locale.ROOT));
} catch (final UnsupportedCharsetException e) {
if (LOG.isWarnEnabled()) {
LOG.warn("FileReader readAsText was called with an unsupported encoding '" + encoding + "'. Using UTF-8 instead.");
}
}
}
}
if (object instanceof Blob) {
result_ = new String(((Blob) object).getBytes(), charset);
}
readyState_ = DONE;
final Event event = new Event(this, Event.TYPE_LOAD);
fireEvent(event);
}
Aggregations