From: <asa...@us...> - 2017-08-11 07:02:42
|
Revision: 14782 http://sourceforge.net/p/htmlunit/code/14782 Author: asashour Date: 2017-08-11 07:02:39 +0000 (Fri, 11 Aug 2017) Log Message: ----------- Initial implementation of FileReader.readAsDataURL Modified Paths: -------------- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainer.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/file/FileReader.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/file/FileReaderTest.java Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainer.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainer.java 2017-08-10 19:00:58 UTC (rev 14781) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/event/EventListenersContainer.java 2017-08-11 07:02:39 UTC (rev 14782) @@ -31,6 +31,7 @@ import com.gargoylesoftware.htmlunit.html.DomNode; import com.gargoylesoftware.htmlunit.html.HtmlBody; import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.gargoylesoftware.htmlunit.javascript.host.Window; import net.sourceforge.htmlunit.corejs.javascript.Function; import net.sourceforge.htmlunit.corejs.javascript.NativeObject; @@ -220,7 +221,7 @@ private ScriptResult executeEventListeners(final boolean useCapture, final Event event, final Object[] args) { final DomNode node = jsNode_.getDomNodeOrNull(); // some event don't apply on all kind of nodes, for instance "blur" - if (node == null || !node.handles(event)) { + if (node != null && !node.handles(event)) { return null; } @@ -228,7 +229,8 @@ final List<Scriptable> listeners = getListeners(event.getType(), useCapture); if (listeners != null && !listeners.isEmpty()) { event.setCurrentTarget(jsNode_); - final HtmlPage page = (HtmlPage) node.getPage(); + + final HtmlPage page = (HtmlPage) ((Window) jsNode_.getParentScope()).getDomNodeOrDie(); // no need for a copy, listeners are copy on write for (final Scriptable listener : listeners) { Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/file/FileReader.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/file/FileReader.java 2017-08-10 19:00:58 UTC (rev 14781) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/file/FileReader.java 2017-08-11 07:02:39 UTC (rev 14782) @@ -14,6 +14,13 @@ */ package com.gargoylesoftware.htmlunit.javascript.host.file; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.file.Files; + +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.io.FileUtils; + import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstant; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor; @@ -20,6 +27,7 @@ import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter; +import com.gargoylesoftware.htmlunit.javascript.host.event.Event; import com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget; import net.sourceforge.htmlunit.corejs.javascript.Function; @@ -45,7 +53,8 @@ @JsxConstant public static final short DONE = 2; - private int readyState_; + private int readyState_ = EMPTY; + private Object result_; /** * Creates an instance. @@ -52,12 +61,12 @@ */ @JsxConstructor public FileReader() { - readyState_ = EMPTY; } /** - * Returns the {@code onload} event handler for this element. - * @return the {@code onload} event handler for this element + * Returns the current state of the reading operation. + * + * @return {@value #EMPTY}, {@value #LOADING}, or {@value #DONE} */ @JsxGetter public int getReadyState() { @@ -65,46 +74,71 @@ } /** - * Returns the {@code onload} event handler for this element. - * @return the {@code onload} event handler for this element + * Returns the file's contents. + * @return the file's contents */ @JsxGetter + public Object getResult() { + return result_; + } + + /** + * Reads the contents of the specified {@link Blob} or {@link File}. + * @param object the {@link Blob} or {@link File} from which to read + */ + @JsxFunction + public void readAsDataURL(final Object object) throws IOException { + setResult(object); + final Event event = new Event(this, Event.TYPE_LOAD); + fireEvent(event); + } + + private void setResult(final Object object) throws IOException { + readyState_ = LOADING; + final java.io.File file = ((File) object).getFile(); + final String contentType = Files.probeContentType(file.toPath()); + try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { + FileUtils.copyFile(file, bos); + + final byte[] bytes = bos.toByteArray(); + result_ = "data:" + contentType + ";base64," + new String(new Base64().encode(bytes)); + } + readyState_ = DONE; + } + + /** + * Returns the {@code onload} event handler for this {@link FileReader}. + * @return the {@code onload} event handler for this {@link FileReader} + */ + @JsxGetter public Function getOnload() { - return getEventHandler("load"); + return getEventHandler(Event.TYPE_LOAD); } /** - * Sets the {@code onload} event handler for this element. - * @param onload the {@code onload} event handler for this element + * Sets the {@code onload} event handler for this {@link FileReader}. + * @param onload the {@code onload} event handler for this {@link FileReader} */ @JsxSetter public void setOnload(final Object onload) { - setEventHandler("load", onload); + setEventHandler(Event.TYPE_LOAD, onload); } /** - * Returns the {@code onerror} event handler for this element. - * @return the {@code onerror} event handler for this element + * Returns the {@code onerror} event handler for this {@link FileReader}. + * @return the {@code onerror} event handler for this {@link FileReader} */ @JsxGetter public Function getOnerror() { - return getEventHandler("error"); + return getEventHandler(Event.TYPE_ERROR); } /** - * Sets the {@code onerror} event handler for this element. - * @param onerror the {@code onerror} event handler for this element + * Sets the {@code onerror} event handler for this {@link FileReader}. + * @param onerror the {@code onerror} event handler for this {@link FileReader} */ @JsxSetter public void setOnerror(final Object onerror) { - setEventHandler("error", onerror); + setEventHandler(Event.TYPE_ERROR, onerror); } - - /** - * Function readAsDataURL. - * @param file the file - */ - @JsxFunction - public void readAsDataURL(final File file) { - } } Modified: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/file/FileReaderTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/file/FileReaderTest.java 2017-08-10 19:00:58 UTC (rev 14781) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/file/FileReaderTest.java 2017-08-11 07:02:39 UTC (rev 14782) @@ -25,7 +25,6 @@ import com.gargoylesoftware.htmlunit.BrowserRunner; import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; -import com.gargoylesoftware.htmlunit.BrowserRunner.NotYetImplemented; import com.gargoylesoftware.htmlunit.WebDriverTestCase; import com.gargoylesoftware.htmlunit.html.HtmlPageTest; @@ -69,7 +68,6 @@ */ @Test @Alerts("data:text/plain;base64,SHRtbFVuaXQ=") - @NotYetImplemented public void readAsDataURL() throws Exception { final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ |