From: <rb...@us...> - 2017-12-27 16:53:38
|
Revision: 15043 http://sourceforge.net/p/htmlunit/code/15043 Author: rbri Date: 2017-12-27 16:53:36 +0000 (Wed, 27 Dec 2017) Log Message: ----------- TextEncoder/TextDecoder impl added Modified Paths: -------------- trunk/htmlunit/src/changes/changes.xml trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/TextDecoder.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/TextEncoder.java trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/arrays/Uint8Array.java Added Paths: ----------- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/TextDecoderTest.java trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/TextEncoderTest.java Modified: trunk/htmlunit/src/changes/changes.xml =================================================================== --- trunk/htmlunit/src/changes/changes.xml 2017-12-26 10:49:19 UTC (rev 15042) +++ trunk/htmlunit/src/changes/changes.xml 2017-12-27 16:53:36 UTC (rev 15043) @@ -8,6 +8,9 @@ <body> <release version="2.29" date="xx, 2017" description="Bugfixes, Chrome 63, WebStart support"> + <action type="add" dev="rbri"> + JavaScript: TextEncoder/TextDecoder impl added. + </action> <action type="fix" dev="rbri" issue="1940" due-to="Atsushi Nakagawa"> JavaScript: MutationObserver fires callbacks asynchronously </action> Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/TextDecoder.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/TextDecoder.java 2017-12-26 10:49:19 UTC (rev 15042) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/TextDecoder.java 2017-12-27 16:53:36 UTC (rev 15043) @@ -17,22 +17,391 @@ import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME; import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; + import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter; +import com.gargoylesoftware.htmlunit.javascript.host.arrays.ArrayBuffer; +import com.gargoylesoftware.htmlunit.javascript.host.arrays.ArrayBufferView; +import net.sourceforge.htmlunit.corejs.javascript.Context; +import net.sourceforge.htmlunit.corejs.javascript.ScriptRuntime; +import net.sourceforge.htmlunit.corejs.javascript.Undefined; + /** * A JavaScript object for {@code TextDecoder}. * * @author Ahmed Ashour + * @author Ronald Brill */ @JsxClass({CHROME, FF}) public class TextDecoder extends SimpleScriptable { + private static java.util.Map<String, Charset> ENCODINGS_; + private static java.util.Map<String, String> ENCODING_NAMES_; + private Charset encoding_ = StandardCharsets.UTF_8; + static { + ENCODINGS_ = new HashMap<>(); + ENCODING_NAMES_ = new HashMap<>(); + + Charset charset = StandardCharsets.UTF_8; + ENCODINGS_.put("utf-8", charset); + ENCODINGS_.put("utf8", charset); + ENCODINGS_.put("unicode-1-1-utf-8", charset); + + charset = Charset.forName("big5"); + ENCODINGS_.put("big5", charset); + ENCODINGS_.put("big5-hkscs", charset); + ENCODINGS_.put("cn-big5", charset); + ENCODINGS_.put("csbig5", charset); + ENCODINGS_.put("x-x-big5", charset); + + charset = Charset.forName("euc-jp"); + ENCODINGS_.put("cseucpkdfmtjapanese", charset); + ENCODINGS_.put("euc-jp", charset); + ENCODINGS_.put("x-euc-jp", charset); + + charset = Charset.forName("euc-kr"); + ENCODINGS_.put("cseuckr", charset); + ENCODINGS_.put("csksc56011987", charset); + ENCODINGS_.put("euc-kr", charset); + ENCODINGS_.put("iso-ir-149", charset); + ENCODINGS_.put("korean", charset); + ENCODINGS_.put("ks_c_5601-1987", charset); + ENCODINGS_.put("ks_c_5601-1989", charset); + ENCODINGS_.put("ksc5601", charset); + ENCODINGS_.put("ksc_5601", charset); + ENCODINGS_.put("windows-949", charset); + + charset = Charset.forName("gb18030"); + ENCODINGS_.put("gb18030", charset); + + charset = Charset.forName("gbk"); + ENCODINGS_.put("chinese", charset); + ENCODINGS_.put("csgb2312", charset); + ENCODINGS_.put("csiso58gb231280", charset); + ENCODINGS_.put("gb2312", charset); + ENCODINGS_.put("gb_2312", charset); + ENCODINGS_.put("gb_2312-80", charset); + ENCODINGS_.put("gbk", charset); + ENCODINGS_.put("iso-ir-58", charset); + ENCODINGS_.put("x-gbk", charset); + + charset = Charset.forName("ibm866"); + ENCODINGS_.put("866", charset); + ENCODINGS_.put("cp866", charset); + ENCODINGS_.put("csibm866", charset); + ENCODINGS_.put("ibm866", charset); + + charset = Charset.forName("iso-2022-jp"); + ENCODINGS_.put("csiso2022jp", charset); + ENCODINGS_.put("iso-2022-jp", charset); + + charset = Charset.forName("iso-8859-2"); + ENCODINGS_.put("csisolatin2", charset); + ENCODINGS_.put("iso-8859-2", charset); + ENCODINGS_.put("iso-ir-101", charset); + ENCODINGS_.put("iso8859-2", charset); + ENCODINGS_.put("iso88592", charset); + ENCODINGS_.put("iso_8859-2", charset); + ENCODINGS_.put("iso_8859-2:1987", charset); + ENCODINGS_.put("l2", charset); + ENCODINGS_.put("latin2", charset); + + charset = Charset.forName("iso-8859-3"); + ENCODINGS_.put("csisolatin3", charset); + ENCODINGS_.put("iso-8859-3", charset); + ENCODINGS_.put("iso-ir-109", charset); + ENCODINGS_.put("iso8859-3", charset); + ENCODINGS_.put("iso88593", charset); + ENCODINGS_.put("iso_8859-3", charset); + ENCODINGS_.put("iso_8859-3:1988", charset); + ENCODINGS_.put("l3", charset); + ENCODINGS_.put("latin3", charset); + + charset = Charset.forName("iso-8859-4"); + ENCODINGS_.put("csisolatin4", charset); + ENCODINGS_.put("iso-8859-4", charset); + ENCODINGS_.put("iso-ir-110", charset); + ENCODINGS_.put("iso8859-4", charset); + ENCODINGS_.put("iso88594", charset); + ENCODINGS_.put("iso_8859-4", charset); + ENCODINGS_.put("iso_8859-4:1988", charset); + ENCODINGS_.put("l4", charset); + ENCODINGS_.put("latin4", charset); + + charset = Charset.forName("iso-8859-5"); + ENCODINGS_.put("csisolatincyrillic", charset); + ENCODINGS_.put("cyrillic", charset); + ENCODINGS_.put("iso-8859-5", charset); + ENCODINGS_.put("iso-ir-144", charset); + ENCODINGS_.put("iso88595", charset); + ENCODINGS_.put("iso_8859-5", charset); + ENCODINGS_.put("iso_8859-5:1988", charset); + + charset = Charset.forName("iso-8859-6"); + ENCODINGS_.put("arabic", charset); + ENCODINGS_.put("asmo-708", charset); + ENCODINGS_.put("csiso88596e", charset); + ENCODINGS_.put("csiso88596i", charset); + ENCODINGS_.put("csisolatinarabic", charset); + ENCODINGS_.put("ecma-114", charset); + ENCODINGS_.put("iso-8859-6", charset); + ENCODINGS_.put("iso-8859-6-e", charset); + ENCODINGS_.put("iso-8859-6-i", charset); + ENCODINGS_.put("iso-ir-127", charset); + ENCODINGS_.put("iso8859-6", charset); + ENCODINGS_.put("iso88596", charset); + ENCODINGS_.put("iso_8859-6", charset); + ENCODINGS_.put("iso_8859-6:1987", charset); + + charset = Charset.forName("iso-8859-7"); + ENCODINGS_.put("csisolatingreek", charset); + ENCODINGS_.put("ecma-118", charset); + ENCODINGS_.put("elot_928", charset); + ENCODINGS_.put("greek", charset); + ENCODINGS_.put("greek8", charset); + ENCODINGS_.put("iso-8859-7", charset); + ENCODINGS_.put("iso-ir-126", charset); + ENCODINGS_.put("iso8859-7", charset); + ENCODINGS_.put("iso88597", charset); + ENCODINGS_.put("iso_8859-7", charset); + ENCODINGS_.put("iso_8859-7:1987", charset); + ENCODINGS_.put("sun_eu_greek", charset); + + charset = Charset.forName("iso-8859-8"); + ENCODINGS_.put("csiso88598e", charset); + ENCODINGS_.put("csisolatinhebrew", charset); + ENCODINGS_.put("hebrew", charset); + ENCODINGS_.put("iso-8859-8", charset); + ENCODINGS_.put("iso-8859-8-e", charset); + ENCODINGS_.put("iso-ir-138", charset); + ENCODINGS_.put("iso8859-8", charset); + ENCODINGS_.put("iso88598", charset); + ENCODINGS_.put("iso_8859-8", charset); + ENCODINGS_.put("iso_8859-8:1988", charset); + ENCODINGS_.put("visual", charset); + +// charset = Charset.forName("iso-8859-8-i"); +// ENCODINGS_.put("csiso88598i", charset); +// ENCODINGS_.put("iso-8859-8-i", charset); +// ENCODINGS_.put("logical", charset); + +// charset = Charset.forName("iso-8859-10"); +// ENCODINGS_.put("csisolatin6", charset); +// ENCODINGS_.put("iso-8859-10", charset); +// ENCODINGS_.put("iso-ir-157", charset); +// ENCODINGS_.put("iso8859-10", charset); +// ENCODINGS_.put("iso885910", charset); +// ENCODINGS_.put("l6", charset); +// ENCODINGS_.put("latin6", charset); + + charset = Charset.forName("iso-8859-13"); + ENCODINGS_.put("iso-8859-13", charset); + ENCODINGS_.put("iso8859-13", charset); + ENCODINGS_.put("iso885913", charset); + +// charset = Charset.forName("iso-8859-14"); +// ENCODINGS_.put("iso-8859-14", charset); +// ENCODINGS_.put("iso8859-14", charset); +// ENCODINGS_.put("iso885914", charset); + + charset = Charset.forName("iso-8859-15"); + ENCODINGS_.put("csisolatin9", charset); + ENCODINGS_.put("iso-8859-15", charset); + ENCODINGS_.put("iso8859-15", charset); + ENCODINGS_.put("iso885915", charset); + ENCODINGS_.put("l9", charset); + // ENCODINGS_.put("latin9", charset); + +// charset = Charset.forName("iso-8859-16"); +// ENCODINGS_.put("iso-8859-16", charset); + + charset = Charset.forName("koi8-r"); + ENCODINGS_.put("cskoi8r", charset); + ENCODINGS_.put("koi", charset); + ENCODINGS_.put("koi8", charset); + ENCODINGS_.put("koi8-r", charset); + ENCODINGS_.put("koi8_r", charset); + + charset = Charset.forName("koi8-u"); + ENCODINGS_.put("koi8-u", charset); + + charset = Charset.forName("shift_jis"); + ENCODINGS_.put("csshiftjis", charset); + ENCODINGS_.put("ms_kanji", charset); + ENCODINGS_.put("shift-jis", charset); + ENCODINGS_.put("shift_jis", charset); + ENCODINGS_.put("sjis", charset); + ENCODINGS_.put("windows-31j", charset); + ENCODINGS_.put("x-sjis", charset); + + charset = Charset.forName("utf-16be"); + ENCODINGS_.put("utf-16be", charset); + + charset = Charset.forName("utf-16le"); + ENCODINGS_.put("utf-16", charset); + ENCODINGS_.put("utf-16le", charset); + + charset = Charset.forName("windows-1250"); + ENCODINGS_.put("cp1250", charset); + ENCODINGS_.put("windows-1250", charset); + ENCODINGS_.put("x-cp1250", charset); + + charset = Charset.forName("windows-1251"); + ENCODINGS_.put("cp1251", charset); + ENCODINGS_.put("windows-1251", charset); + ENCODINGS_.put("x-cp1251", charset); + + charset = Charset.forName("windows-1252"); + ENCODINGS_.put("ansi_x3.4-1968", charset); + ENCODINGS_.put("ascii", charset); + ENCODINGS_.put("cp1252", charset); + ENCODINGS_.put("cp819", charset); + ENCODINGS_.put("csisolatin1", charset); + ENCODINGS_.put("ibm819", charset); + ENCODINGS_.put("iso-8859-1", charset); + ENCODINGS_.put("iso-ir-100", charset); + ENCODINGS_.put("iso8859-1", charset); + ENCODINGS_.put("iso88591", charset); + ENCODINGS_.put("iso_8859-1", charset); + ENCODINGS_.put("iso_8859-1:1987", charset); + ENCODINGS_.put("l1", charset); + ENCODINGS_.put("latin1", charset); + ENCODINGS_.put("us-ascii", charset); + ENCODINGS_.put("windows-1252", charset); + ENCODINGS_.put("x-cp1252", charset); + + charset = Charset.forName("windows-1253"); + ENCODINGS_.put("cp1253", charset); + ENCODINGS_.put("windows-1253", charset); + ENCODINGS_.put("x-cp1253", charset); + + charset = Charset.forName("windows-1254"); + ENCODINGS_.put("cp1254", charset); + ENCODINGS_.put("csisolatin5", charset); + ENCODINGS_.put("iso-8859-9", charset); + ENCODINGS_.put("iso-ir-148", charset); + ENCODINGS_.put("iso8859-9", charset); + ENCODINGS_.put("iso88599", charset); + ENCODINGS_.put("iso_8859-9", charset); + ENCODINGS_.put("iso_8859-9:1989", charset); + ENCODINGS_.put("l5", charset); + ENCODINGS_.put("latin5", charset); + ENCODINGS_.put("windows-1254", charset); + ENCODINGS_.put("x-cp1254", charset); + + charset = Charset.forName("windows-1255"); + ENCODINGS_.put("cp1255", charset); + ENCODINGS_.put("windows-1255", charset); + ENCODINGS_.put("x-cp1255", charset); + + charset = Charset.forName("windows-1256"); + ENCODINGS_.put("cp1256", charset); + ENCODINGS_.put("windows-1256", charset); + ENCODINGS_.put("x-cp1256", charset); + + charset = Charset.forName("windows-1257"); + ENCODINGS_.put("cp1257", charset); + ENCODINGS_.put("windows-1257", charset); + ENCODINGS_.put("x-cp1257", charset); + + charset = Charset.forName("windows-1258"); + ENCODINGS_.put("cp1258", charset); + ENCODINGS_.put("windows-1258", charset); + ENCODINGS_.put("x-cp1258", charset); + + charset = Charset.forName("x-windows-874"); + ENCODING_NAMES_.put("x-windows-874", "windows-874"); + ENCODINGS_.put("dos-874", charset); + ENCODINGS_.put("iso-8859-11", charset); + ENCODINGS_.put("iso8859-11", charset); + ENCODINGS_.put("iso885911", charset); + ENCODINGS_.put("tis-620", charset); + ENCODINGS_.put("windows-874", charset); + + charset = Charset.forName("x-MacCyrillic"); + ENCODING_NAMES_.put("x-MacCyrillic", "x-mac-cyrillic"); + ENCODINGS_.put("x-mac-cyrillic", charset); + ENCODINGS_.put("x-mac-ukrainian", charset); + + charset = Charset.forName("x-MacRoman"); + ENCODING_NAMES_.put("x-MacRoman", "macintosh"); + ENCODINGS_.put("csmacintosh", charset); + ENCODINGS_.put("mac", charset); + ENCODINGS_.put("macintosh", charset); + ENCODINGS_.put("x-mac-roman", charset); + +// charset = Charset.forName("x-user-defined"); +// ENCODINGS_.put("x-user-defined", charset); + } + /** + * Ctor. + */ + public TextDecoder() { + } + + /** * Creates an instance. + * @param encoding the encoding */ @JsxConstructor - public TextDecoder() { + public TextDecoder(final Object encoding) { + if (Undefined.instance == encoding) { + return; + } + + final String enc = Context.toString(encoding); + final Charset charset = ENCODINGS_.get(enc); + if (charset != null) { + encoding_ = charset; + return; + } + throw ScriptRuntime.typeError("Argument 1 '" + enc + "' is not a supported encoding"); } + + /** + * @return always "utf-8" + */ + @JsxGetter + public String getEncoding() { + String name = encoding_.name(); + name = ENCODING_NAMES_.get(name); + if (name != null) { + return name; + } + return encoding_.name().toLowerCase(); + } + + /** + * @param buffer to be decoded + * @return returns the decoded string + */ + @JsxFunction + public String decode(final Object buffer) { + if (Undefined.instance == buffer) { + return ""; + } + + ArrayBuffer arrayBuffer = null; + if (buffer instanceof ArrayBuffer) { + arrayBuffer = (ArrayBuffer) buffer; + } + else if (buffer instanceof ArrayBufferView) { + arrayBuffer = ((ArrayBufferView) buffer).getBuffer(); + } + + if (arrayBuffer != null) { + return new String(arrayBuffer.getBytes(), encoding_); + } + + throw ScriptRuntime.typeError("Argument 1 of TextDecoder.decode could not be" + + " converted to any of: ArrayBufferView, ArrayBuffer."); + } } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/TextEncoder.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/TextEncoder.java 2017-12-26 10:49:19 UTC (rev 15042) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/TextEncoder.java 2017-12-27 16:53:36 UTC (rev 15043) @@ -17,14 +17,23 @@ import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME; import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF; +import java.nio.charset.StandardCharsets; + import com.gargoylesoftware.htmlunit.javascript.SimpleScriptable; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction; +import com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter; +import com.gargoylesoftware.htmlunit.javascript.host.arrays.Uint8Array; +import net.sourceforge.htmlunit.corejs.javascript.Context; +import net.sourceforge.htmlunit.corejs.javascript.Undefined; + /** * A JavaScript object for {@code TextEncoder}. * * @author Ahmed Ashour + * @author Ronald Brill */ @JsxClass({CHROME, FF}) public class TextEncoder extends SimpleScriptable { @@ -35,4 +44,34 @@ @JsxConstructor public TextEncoder() { } + + /** + * @return always "utf-8" + */ + @JsxGetter + public String getEncoding() { + return "utf-8"; + } + + /** + * @param toEncode the string to encode + * @return returns a Uint8Array containing the text given encoded . + */ + @JsxFunction + public Uint8Array encode(final Object toEncode) { + if (Undefined.instance == toEncode) { + return new Uint8Array(new byte[0], getWindow()); + } + + final String txt; + if (toEncode == null) { + txt = "null"; + } + else { + txt = Context.toString(toEncode); + } + + final byte[] bytes = txt.getBytes(StandardCharsets.UTF_8); + return new Uint8Array(bytes, getWindow()); + } } Modified: trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/arrays/Uint8Array.java =================================================================== --- trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/arrays/Uint8Array.java 2017-12-26 10:49:19 UTC (rev 15042) +++ trunk/htmlunit/src/main/java/com/gargoylesoftware/htmlunit/javascript/host/arrays/Uint8Array.java 2017-12-27 16:53:36 UTC (rev 15043) @@ -17,6 +17,7 @@ import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstant; import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor; +import com.gargoylesoftware.htmlunit.javascript.host.Window; import net.sourceforge.htmlunit.corejs.javascript.Scriptable; @@ -31,6 +32,32 @@ @JsxClass public class Uint8Array extends ArrayBufferViewBase { + /** + * Ctor. + */ + public Uint8Array() { + super(); + } + + /** + * Ctor. + * + * @param bytes the bytes to store + * @param window the context + */ + public Uint8Array(final byte[] bytes, final Window window) { + this(); + setPrototype(window.getPrototype(getClass())); + setParentScope(getParentScope()); + + setByteLength(bytes.length); + + final ArrayBuffer buffer = new ArrayBuffer(bytes); + buffer.setPrototype(window.getPrototype(buffer.getClass())); + buffer.setParentScope(getParentScope()); + setBuffer(buffer); + } + /** The size, in bytes, of each array element. */ @JsxConstant public static final int BYTES_PER_ELEMENT = 1; @@ -70,5 +97,4 @@ protected int getBytesPerElement() { return BYTES_PER_ELEMENT; } - } Added: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/TextDecoderTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/TextDecoderTest.java (rev 0) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/TextDecoderTest.java 2017-12-27 16:53:36 UTC (rev 15043) @@ -0,0 +1,796 @@ +/* + * Copyright (c) 2002-2017 Gargoyle Software Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gargoylesoftware.htmlunit.javascript.host; + +import static com.gargoylesoftware.htmlunit.BrowserRunner.TestedBrowser.CHROME; +import static com.gargoylesoftware.htmlunit.BrowserRunner.TestedBrowser.FF; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.gargoylesoftware.htmlunit.BrowserRunner; +import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; +import com.gargoylesoftware.htmlunit.BrowserRunner.NotYetImplemented; +import com.gargoylesoftware.htmlunit.WebDriverTestCase; + +/** + * Tests for {@link TextDecoder}. + * + * @author Ronald Brill + * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder">TextDecoder() - Web APIs | MDN</a> + */ +@RunWith(BrowserRunner.class) +public class TextDecoderTest extends WebDriverTestCase { + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = {"utf-8", "utf-8", "utf-8", "utf-8"}, + IE = "no TextDecoder") + public void encoding() throws Exception { + final String html = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function doTest() {\n" + + " if (typeof TextDecoder === 'undefined') {\n" + + " alert('no TextDecoder');\n" + + " return;\n" + + " };\n" + + " var enc = new TextDecoder();\n" + + " alert(enc.encoding);\n" + + + " enc = new TextDecoder(undefined);\n" + + " alert(enc.encoding);\n" + + + " enc = new TextDecoder('utf-8');\n" + + " alert(enc.encoding);\n" + + + " enc = new TextDecoder('utf8');\n" + + " alert(enc.encoding);\n" + + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='doTest()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "utf-8", + IE = "no TextDecoder") + public void encoding_utf8() throws Exception { + encoding("unicode-1-1-utf-8"); + encoding("utf-8"); + encoding("utf8"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "ibm866", + IE = "no TextDecoder") + public void encoding_ibm866() throws Exception { + encoding("866"); + encoding("cp866"); + encoding("csibm866"); + encoding("ibm866"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-2", + IE = "no TextDecoder") + public void encoding_iso_8859_2() throws Exception { + encoding("csisolatin2"); + encoding("iso-8859-2"); + encoding("iso-ir-101"); + encoding("iso8859-2"); + encoding("iso88592"); + encoding("iso_8859-2"); + encoding("iso_8859-2:1987"); + encoding("l2"); + encoding("latin2"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-3", + IE = "no TextDecoder") + public void encoding_iso_8859_3() throws Exception { + encoding("csisolatin3"); + encoding("iso-8859-3"); + encoding("iso-ir-109"); + encoding("iso8859-3"); + encoding("iso88593"); + encoding("iso_8859-3"); + encoding("iso_8859-3:1988"); + encoding("l3"); + encoding("latin3"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-4", + IE = "no TextDecoder") + public void encoding_iso_8859_4() throws Exception { + encoding("csisolatin4"); + encoding("iso-8859-4"); + encoding("iso-ir-110"); + encoding("iso8859-4"); + encoding("iso88594"); + encoding("iso_8859-4"); + encoding("iso_8859-4:1988"); + encoding("l4"); + encoding("latin4"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-5", + IE = "no TextDecoder") + public void encoding_iso_8859_5() throws Exception { + encoding("csisolatincyrillic"); + encoding("cyrillic"); + encoding("iso-8859-5"); + encoding("iso-ir-144"); + encoding("iso88595"); + encoding("iso_8859-5"); + encoding("iso_8859-5:1988"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-6", + IE = "no TextDecoder") + public void encoding_iso_8859_6() throws Exception { + encoding("arabic"); + encoding("asmo-708"); + encoding("csiso88596e"); + encoding("csiso88596i"); + encoding("csisolatinarabic"); + encoding("ecma-114"); + encoding("iso-8859-6"); + encoding("iso-8859-6-e"); + encoding("iso-8859-6-i"); + encoding("iso-ir-127"); + encoding("iso8859-6"); + encoding("iso88596"); + encoding("iso_8859-6"); + encoding("iso_8859-6:1987"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-7", + IE = "no TextDecoder") + public void encoding_iso_8859_7() throws Exception { + encoding("csisolatingreek"); + encoding("ecma-118"); + encoding("elot_928"); + encoding("greek"); + encoding("greek8"); + encoding("iso-8859-7"); + encoding("iso-ir-126"); + encoding("iso8859-7"); + encoding("iso88597"); + encoding("iso_8859-7"); + encoding("iso_8859-7:1987"); + encoding("sun_eu_greek"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-8", + IE = "no TextDecoder") + public void encoding_iso_8859_8() throws Exception { + encoding("csiso88598e"); + encoding("csisolatinhebrew"); + encoding("hebrew"); + encoding("iso-8859-8"); + encoding("iso-8859-8-e"); + encoding("iso-ir-138"); + encoding("iso8859-8"); + encoding("iso88598"); + encoding("iso_8859-8"); + encoding("iso_8859-8:1988"); + encoding("visual"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-8-i", + IE = "no TextDecoder") + @NotYetImplemented({CHROME, FF}) + public void encoding_iso_8859_8i() throws Exception { + encoding("csiso88598i"); + encoding("iso-8859-8-i"); + encoding("logical"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-10", + IE = "no TextDecoder") + @NotYetImplemented({CHROME, FF}) + public void encoding_iso_8859_10() throws Exception { + encoding("csisolatin6"); + encoding("iso-8859-10"); + encoding("iso-ir-157"); + encoding("iso8859-10"); + encoding("iso885910"); + encoding("l6"); + encoding("latin6"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-13", + IE = "no TextDecoder") + public void encoding_iso_8859_13() throws Exception { + encoding("iso-8859-13"); + encoding("iso8859-13"); + encoding("iso885913"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-14", + IE = "no TextDecoder") + @NotYetImplemented({CHROME, FF}) + public void encoding_iso_8859_14() throws Exception { + encoding("iso-8859-14"); + encoding("iso8859-14"); + encoding("iso885914"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-15", + IE = "no TextDecoder") + public void encoding_iso_8859_15() throws Exception { + encoding("csisolatin9"); + encoding("iso-8859-15"); + encoding("iso8859-15"); + encoding("iso885915"); + encoding("l9"); + // encoding("latin9"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "exception", + IE = "no TextDecoder") + public void encoding_iso_8859_15_ex() throws Exception { + encoding("latin9"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-8859-16", + IE = "no TextDecoder") + @NotYetImplemented({CHROME, FF}) + public void encoding_iso_8859_16() throws Exception { + encoding("iso-8859-16"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "koi8-r", + IE = "no TextDecoder") + public void encoding_koi8_r() throws Exception { + encoding("cskoi8r"); + encoding("koi"); + encoding("koi8"); + encoding("koi8-r"); + encoding("koi8_r"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "koi8-u", + IE = "no TextDecoder") + public void encoding_koi8_u() throws Exception { + encoding("koi8-u"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "macintosh", + IE = "no TextDecoder") + public void encoding_macintosh() throws Exception { + encoding("csmacintosh"); + encoding("mac"); + encoding("macintosh"); + encoding("x-mac-roman"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "windows-874", + IE = "no TextDecoder") + public void encoding_windows_874() throws Exception { + encoding("dos-874"); + encoding("iso-8859-11"); + encoding("iso8859-11"); + encoding("iso885911"); + encoding("tis-620"); + encoding("windows-874"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "windows-1250", + IE = "no TextDecoder") + public void encoding_windows_1250() throws Exception { + encoding("cp1250"); + encoding("windows-1250"); + encoding("x-cp1250"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "windows-1251", + IE = "no TextDecoder") + public void encoding_windows_1251() throws Exception { + encoding("cp1251"); + encoding("windows-1251"); + encoding("x-cp1251"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "windows-1252", + IE = "no TextDecoder") + public void encoding_windows_1252() throws Exception { + encoding("ansi_x3.4-1968"); + encoding("ascii"); + encoding("cp1252"); + encoding("cp819"); + encoding("csisolatin1"); + encoding("ibm819"); + encoding("iso-8859-1"); + encoding("iso-ir-100"); + encoding("iso8859-1"); + encoding("iso88591"); + encoding("iso_8859-1"); + encoding("iso_8859-1:1987"); + encoding("l1"); + encoding("latin1"); + encoding("us-ascii"); + encoding("windows-1252"); + encoding("x-cp1252"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "windows-1253", + IE = "no TextDecoder") + public void encoding_windows_1253() throws Exception { + encoding("cp1253"); + encoding("windows-1253"); + encoding("x-cp1253"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "windows-1254", + IE = "no TextDecoder") + public void encoding_windows_1254() throws Exception { + encoding("cp1254"); + encoding("csisolatin5"); + encoding("iso-8859-9"); + encoding("iso-ir-148"); + encoding("iso8859-9"); + encoding("iso88599"); + encoding("iso_8859-9"); + encoding("iso_8859-9:1989"); + encoding("l5"); + encoding("latin5"); + encoding("windows-1254"); + encoding("x-cp1254"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "windows-1255", + IE = "no TextDecoder") + public void encoding_windows_1255() throws Exception { + encoding("cp1255"); + encoding("windows-1255"); + encoding("x-cp1255"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "windows-1256", + IE = "no TextDecoder") + public void encoding_windows_1256() throws Exception { + encoding("cp1256"); + encoding("windows-1256"); + encoding("x-cp1256"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "windows-1257", + IE = "no TextDecoder") + public void encoding_windows_1257() throws Exception { + encoding("cp1257"); + encoding("windows-1257"); + encoding("x-cp1257"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "windows-1258", + IE = "no TextDecoder") + public void encoding_windows_1258() throws Exception { + encoding("cp1258"); + encoding("windows-1258"); + encoding("x-cp1258"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "x-mac-cyrillic", + IE = "no TextDecoder") + public void encoding_x_mac_cyrillic() throws Exception { + encoding("x-mac-cyrillic"); + encoding("x-mac-ukrainian"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "gbk", + IE = "no TextDecoder") + public void encoding_gbk() throws Exception { + encoding("chinese"); + encoding("csgb2312"); + encoding("csiso58gb231280"); + encoding("gb2312"); + encoding("gb_2312"); + encoding("gb_2312-80"); + encoding("gbk"); + encoding("iso-ir-58"); + encoding("x-gbk"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "gb18030", + IE = "no TextDecoder") + public void encoding_gb18030() throws Exception { + encoding("gb18030"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "exception", + IE = "no TextDecoder") + public void encoding_hz_gb_2312() throws Exception { + encoding("hz-gb-2312"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "big5", + IE = "no TextDecoder") + public void encoding_big5() throws Exception { + encoding("big5"); + encoding("big5-hkscs"); + encoding("cn-big5"); + encoding("csbig5"); + encoding("x-x-big5"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "euc-jp", + IE = "no TextDecoder") + public void encoding_euc_jp() throws Exception { + encoding("cseucpkdfmtjapanese"); + encoding("euc-jp"); + encoding("x-euc-jp"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "iso-2022-jp", + IE = "no TextDecoder") + public void encoding_iso_2022_jp() throws Exception { + encoding("csiso2022jp"); + encoding("iso-2022-jp"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "shift_jis", + IE = "no TextDecoder") + public void encoding_shift_jis() throws Exception { + encoding("csshiftjis"); + encoding("ms_kanji"); + encoding("shift-jis"); + encoding("shift_jis"); + encoding("sjis"); + encoding("windows-31j"); + encoding("x-sjis"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "euc-kr", + IE = "no TextDecoder") + public void encoding_euc_kr() throws Exception { + encoding("cseuckr"); + encoding("csksc56011987"); + encoding("euc-kr"); + encoding("iso-ir-149"); + encoding("korean"); + encoding("ks_c_5601-1987"); + encoding("ks_c_5601-1989"); + encoding("ksc5601"); + encoding("ksc_5601"); + encoding("windows-949"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "exception", + IE = "no TextDecoder") + public void encoding_iso_2022_kr() throws Exception { + encoding("csiso2022kr"); + encoding("iso-2022-kr"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "utf-16be", + IE = "no TextDecoder") + public void encoding_utf_16be() throws Exception { + encoding("utf-16be"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "utf-16le", + IE = "no TextDecoder") + public void encoding_utf_16le() throws Exception { + encoding("utf-16"); + encoding("utf-16le"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "x-user-defined", + IE = "no TextDecoder") + @NotYetImplemented({CHROME, FF}) + public void encoding_x_user_defined() throws Exception { + encoding("x-user-defined"); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "exception", + IE = "no TextDecoder") + public void encoding_replacement() throws Exception { + encoding("iso-2022-cn"); + encoding("iso-2022-cn-ext"); + } + + private void encoding(final String encoding) throws Exception { + final String html = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function doTest() {\n" + + " if (typeof TextDecoder === 'undefined') {\n" + + " alert('no TextDecoder');\n" + + " return;\n" + + " };\n" + + " try {\n" + + " enc = new TextDecoder('" + encoding + "');\n" + + " alert(enc.encoding);\n" + + " } catch(e) { alert('exception'); }\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='doTest()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = {"0", "8", "72", "116"}, + IE = "no TextEncoder") + public void encode() throws Exception { + final String html = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function doTest() {\n" + + " if (typeof TextEncoder === 'undefined') {\n" + + " alert('no TextEncoder');\n" + + " return;\n" + + " };\n" + + " var enc = new TextEncoder();\n" + + " var encoded = enc.encode('');\n" + + " alert(encoded.length);\n" + + + " encoded = enc.encode('HtmlUnit');\n" + + " alert(encoded.length);\n" + + " alert(encoded[0]);\n" + + " alert(encoded[encoded.length - 1]);\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='doTest()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = {"HtmlUnit"}, + IE = "no TextDecoder") + public void decode() throws Exception { + final String html = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function doTest() {\n" + + " if (typeof TextDecoder === 'undefined') {\n" + + " alert('no TextDecoder');\n" + + " return;\n" + + " };\n" + + " var enc = new TextEncoder();\n" + + " var encoded = enc.encode('HtmlUnit');\n" + + + " var dec = new TextDecoder('utf-8');\n" + + " var decoded = dec.decode(encoded);\n" + + " alert(decoded);\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='doTest()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = {"", "exception"}, + IE = "no TextDecoder") + public void decode2() throws Exception { + final String html = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function doTest() {\n" + + " if (typeof TextDecoder === 'undefined') {\n" + + " alert('no TextDecoder');\n" + + " return;\n" + + " };\n" + + " var dec = new TextDecoder('utf-8');\n" + + " try {\n" + + " alert(dec.decode(undefined));\n" + + " } catch(e) { alert('exception'); }\n" + + + " try {\n" + + " alert(dec.decode(null));\n" + + " } catch(e) { alert('exception'); }\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='doTest()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } +} Property changes on: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/TextDecoderTest.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/TextEncoderTest.java =================================================================== --- trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/TextEncoderTest.java (rev 0) +++ trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/TextEncoderTest.java 2017-12-27 16:53:36 UTC (rev 15043) @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2002-2017 Gargoyle Software Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gargoylesoftware.htmlunit.javascript.host; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.gargoylesoftware.htmlunit.BrowserRunner; +import com.gargoylesoftware.htmlunit.BrowserRunner.Alerts; +import com.gargoylesoftware.htmlunit.WebDriverTestCase; + +/** + * Tests for {@link TextEncoder}. + * + * @author Ronald Brill + * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder">TextEncoder() - Web APIs | MDN</a> + */ +@RunWith(BrowserRunner.class) +public class TextEncoderTest extends WebDriverTestCase { + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = "utf-8", + IE = "no TextEncoder") + public void encoding() throws Exception { + final String html = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function doTest() {\n" + + " if (typeof TextEncoder === 'undefined') {\n" + + " alert('no TextEncoder');\n" + + " return;\n" + + " };\n" + + " var enc = new TextEncoder();\n" + + " alert(enc.encoding);\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='doTest()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = {"0", "8", "72", "116"}, + IE = "no TextEncoder") + public void encode() throws Exception { + final String html = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function doTest() {\n" + + " if (typeof TextEncoder === 'undefined') {\n" + + " alert('no TextEncoder');\n" + + " return;\n" + + " };\n" + + " var enc = new TextEncoder();\n" + + " var encoded = enc.encode('');\n" + + " alert(encoded.length);\n" + + + " encoded = enc.encode('HtmlUnit');\n" + + " alert(encoded.length);\n" + + " alert(encoded[0]);\n" + + " alert(encoded[encoded.length - 1]);\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='doTest()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } + + /** + * @throws Exception on test failure + */ + @Test + @Alerts(DEFAULT = {"0", "0", "4"}, + IE = "no TextEncoder") + public void encode2() throws Exception { + final String html = "<html>\n" + + "<head>\n" + + " <script>\n" + + " function doTest() {\n" + + " if (typeof TextEncoder === 'undefined') {\n" + + " alert('no TextEncoder');\n" + + " return;\n" + + " };\n" + + " var enc = new TextEncoder();\n" + + " var encoded = enc.encode();\n" + + " alert(encoded.length);\n" + + + " var encoded = enc.encode(undefined);\n" + + " alert(encoded.length);\n" + + + " var encoded = enc.encode(null);\n" + + " alert(encoded.length);\n" + + " }\n" + + " </script>\n" + + "</head>\n" + + "<body onload='doTest()'>\n" + + "</body></html>"; + + loadPageWithAlerts2(html); + } +} Property changes on: trunk/htmlunit/src/test/java/com/gargoylesoftware/htmlunit/javascript/host/TextEncoderTest.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property |