Bug 1257877 - Remove UTF-16 support from TextEncoder. r=hsivonen
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Thu, 24 Mar 2016 19:27:15 +0900
changeset 290288 cd3799dfd531b8a72becc4c2c78128fd052d8818
parent 290281 964a638d77fab2dbe92397c4cc9ea09fb344db06
child 290289 fbb40278805ff5a8aac8b1a1dbd8b0a180f82296
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershsivonen
bugs1257877
milestone48.0a1
Bug 1257877 - Remove UTF-16 support from TextEncoder. r=hsivonen
addon-sdk/source/test/test-buffer.js
dom/encoding/TextEncoder.cpp
dom/encoding/TextEncoder.h
dom/encoding/test/test_TextEncoder.js
dom/encoding/test/unit/test_misc.js
dom/encoding/test/unit/test_utf.js
dom/webidl/TextEncoder.webidl
--- a/addon-sdk/source/test/test-buffer.js
+++ b/addon-sdk/source/test/test-buffer.js
@@ -27,17 +27,17 @@
 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 // USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 const { Buffer, TextEncoder, TextDecoder } = require('sdk/io/buffer');
 const { safeMerge } = require('sdk/util/object');
 
-const ENCODINGS = ['utf-8', 'utf-16le', 'utf-16be'];
+const ENCODINGS = ['utf-8'];
 
 exports.testBufferMain = function (assert) {
   let b = Buffer('abcdef');
 
   // try to create 0-length buffers
   new Buffer('');
   new Buffer(0);
   // test encodings supported by node;
@@ -241,19 +241,16 @@ exports.testBufferCopy = function (asser
   // try to copy 0 bytes from past the end of the source buffer
   b.copy(new Buffer(1), 0, 2048, 2048);
 };
 
 exports.testBufferWrite = function (assert) {
   let b = Buffer(1024);
   b.fill(0);
 
-  assert.throws(() => {
-    b.write('test string', 0, 5, 'invalid');
-  }, RangeError, 'invalid encoding with buffer write throws');
   // try to write a 0-length string beyond the end of b
   assert.throws(function() {
     b.write('', 2048);
   }, RangeError, 'writing a 0-length string beyond buffer throws');
   // throw when writing to negative offset
   assert.throws(function() {
     b.write('a', -1);
   }, RangeError, 'writing negative offset on buffer throws');
--- a/dom/encoding/TextEncoder.cpp
+++ b/dom/encoding/TextEncoder.cpp
@@ -8,38 +8,20 @@
 #include "mozilla/dom/EncodingUtils.h"
 #include "mozilla/UniquePtrExtensions.h"
 #include "nsContentUtils.h"
 
 namespace mozilla {
 namespace dom {
 
 void
-TextEncoder::Init(const nsAString& aEncoding, ErrorResult& aRv)
+TextEncoder::Init()
 {
-  nsAutoString label(aEncoding);
-  EncodingUtils::TrimSpaceCharacters(label);
-
-  // Let encoding be the result of getting an encoding from label.
-  // If encoding is failure, or is none of utf-8, utf-16, and utf-16be,
-  // throw a RangeError (https://encoding.spec.whatwg.org/#dom-textencoder).
-  if (!EncodingUtils::FindEncodingForLabel(label, mEncoding)) {
-    aRv.ThrowRangeError<MSG_ENCODING_NOT_SUPPORTED>(label);
-    return;
-  }
-
-  if (!mEncoding.EqualsLiteral("UTF-8") &&
-      !mEncoding.EqualsLiteral("UTF-16LE") &&
-      !mEncoding.EqualsLiteral("UTF-16BE")) {
-    aRv.ThrowRangeError<MSG_DOM_ENCODING_NOT_UTF>();
-    return;
-  }
-
-  // Create an encoder object for mEncoding.
-  mEncoder = EncodingUtils::EncoderForEncoding(mEncoding);
+  // Create an encoder object for utf-8.
+  mEncoder = EncodingUtils::EncoderForEncoding(NS_LITERAL_CSTRING("UTF-8"));
 }
 
 void
 TextEncoder::Encode(JSContext* aCx,
                     JS::Handle<JSObject*> aObj,
                     const nsAString& aString,
                     JS::MutableHandle<JSObject*> aRetval,
                     ErrorResult& aRv)
@@ -87,14 +69,13 @@ TextEncoder::Encode(JSContext* aCx,
     aRv.Throw(rv);
   }
   aRetval.set(outView);
 }
 
 void
 TextEncoder::GetEncoding(nsAString& aEncoding)
 {
-  CopyASCIItoUTF16(mEncoding, aEncoding);
-  nsContentUtils::ASCIIToLower(aEncoding);
+  aEncoding.AssignLiteral("utf-8");
 }
 
 } // namespace dom
 } // namespace mozilla
--- a/dom/encoding/TextEncoder.h
+++ b/dom/encoding/TextEncoder.h
@@ -19,24 +19,20 @@ namespace dom {
 
 class TextEncoder final : public NonRefcountedDOMObject
 {
 public:
   // The WebIDL constructor.
 
   static TextEncoder*
   Constructor(const GlobalObject& aGlobal,
-              const nsAString& aEncoding,
               ErrorResult& aRv)
   {
     nsAutoPtr<TextEncoder> txtEncoder(new TextEncoder());
-    txtEncoder->Init(aEncoding, aRv);
-    if (aRv.Failed()) {
-      return nullptr;
-    }
+    txtEncoder->Init();
     return txtEncoder.forget();
   }
 
   TextEncoder()
   {
   }
 
   virtual
@@ -45,50 +41,40 @@ public:
 
   bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector)
   {
     return TextEncoderBinding::Wrap(aCx, this, aGivenProto, aReflector);
   }
 
 protected:
 
-  /**
-   * Validates provided encoding and throws an exception if invalid encoding.
-   * If no encoding is provided then mEncoding is default initialised to "utf-8".
-   *
-   * @param aEncoding    Optional encoding (case insensitive) provided.
-   *                     (valid values are "utf-8", "utf-16", "utf-16be")
-   *                     Default value is "utf-8" if no encoding is provided.
-   * @return aRv         EncodingError exception else null.
-   */
-  void Init(const nsAString& aEncoding, ErrorResult& aRv);
+  void Init();
 
 public:
   /**
    * Return the encoding name.
    *
    * @param aEncoding, current encoding.
    */
   void GetEncoding(nsAString& aEncoding);
 
   /**
-   * Encodes incoming utf-16 code units/ DOM string to the requested encoding.
+   * Encodes incoming utf-16 code units/ DOM string to utf-8.
    *
    * @param aCx        Javascript context.
    * @param aObj       the wrapper of the TextEncoder
    * @param aString    utf-16 code units to be encoded.
    * @return JSObject* The Uint8Array wrapped in a JS object.  Returned via
    *                   the aRetval out param.
    */
   void Encode(JSContext* aCx,
               JS::Handle<JSObject*> aObj,
               const nsAString& aString,
               JS::MutableHandle<JSObject*> aRetval,
               ErrorResult& aRv);
 private:
-  nsCString mEncoding;
   nsCOMPtr<nsIUnicodeEncoder> mEncoder;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif // mozilla_dom_textencoder_h_
--- a/dom/encoding/test/test_TextEncoder.js
+++ b/dom/encoding/test/test_TextEncoder.js
@@ -1,15 +1,24 @@
 /*
  * test_TextEncoder.js
  * bug 764234 tests
 */
 
 function runTextEncoderTests()
 {
+  test(testEncoderEncode, "testEncoderEncode");
+  test(testEncoderGetEncoding, "testEncoderGetEncoding");
+  test(testInvalidSequence, "testInvalidSequence");
+  test(testInputString, "testInputString");
+  test(testStreamingOptions, "testStreamingOptions");
+}
+
+function testEncoderEncode()
+{
   var data = "\u00a0\u0e01\u0e02\u0e03\u0e04\u0e05\u0e06\u0e07\u0e08\u0e09"
         + "\u0e0a\u0e0b\u0e0c\u0e0d\u0e0e\u0e0f\u0e10\u0e11\u0e12\u0e13\u0e14"
         + "\u0e15\u0e16\u0e17\u0e18\u0e19\u0e1a\u0e1b\u0e1c\u0e1d\u0e1e\u0e1f"
         + "\u0e20\u0e21\u0e22\u0e23\u0e24\u0e25\u0e26\u0e27\u0e28\u0e29\u0e2a"
         + "\u0e2b\u0e2c\u0e2d\u0e2e\u0e2f\u0e30\u0e31\u0e32\u0e33\u0e34\u0e35"
         + "\u0e36\u0e37\u0e38\u0e39\u0e3a\u0e3f\u0e40\u0e41\u0e42\u0e43\u0e44"
         + "\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c\u0e4d\u0e4e\u0e4f"
         + "\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56\u0e57\u0e58\u0e59\u0e5a"
@@ -41,118 +50,49 @@ function runTextEncoderTests()
                        0xB9, 0x89, 0xE0, 0xB9, 0x8A, 0xE0, 0xB9, 0x8B, 0xE0,
                        0xB9, 0x8C, 0xE0, 0xB9, 0x8D, 0xE0, 0xB9, 0x8E, 0xE0,
                        0xB9, 0x8F, 0xE0, 0xB9, 0x90, 0xE0, 0xB9, 0x91, 0xE0,
                        0xB9, 0x92, 0xE0, 0xB9, 0x93, 0xE0, 0xB9, 0x94, 0xE0,
                        0xB9, 0x95, 0xE0, 0xB9, 0x96, 0xE0, 0xB9, 0x97, 0xE0,
                        0xB9, 0x98, 0xE0, 0xB9, 0x99, 0xE0, 0xB9, 0x9A, 0xE0,
                        0xB9, 0x9B];
 
-  test(testEncoderGetEncoding, "testEncoderGetEncoding");
-  test(testInvalidSequence, "testInvalidSequence");
-  test(testEncodeUTF16ToUTF16, "testEncodeUTF16ToUTF16");
-  test(function() {
-    testConstructorEncodingOption(data, expectedString)
-  }, "testConstructorEncodingOption");
-  test(function() {
-    testEncodingValues(data, expectedString)
-  }, "testEncodingValues");
-  test(function() {
-    testInputString(data, expectedString)
-  }, "testInputString");
-  test(testStreamingOptions, "testStreamingOptions");
+  // valid encoding passed
+  testSingleString({input: data, expected: expectedString,
+    msg: "testing encoding with valid utf-8 encoding."});
 }
 
 function testInvalidSequence()
 {
   var data = "\u0e43\u0e44\ufffd\u0e45";
   var expectedString = [0xE0, 0xB9, 0x83, 0xE0, 0xB9, 0x84, 0xEF, 0xBF, 0xBD,
                         0xE0, 0xB9, 0x85];
 
   //Test null input string
-  testSingleString({encoding: "utf-8", input: data, expected: expectedString,
+  testSingleString({input: data, expected: expectedString,
     msg: "encoder with replacement character test."});
 }
 
-function testEncodeUTF16ToUTF16()
-{
-  var data = "\u0e43\u0e44\u0e45\u0e46\u0e47\u0e48\u0e49\u0e4a\u0e4b\u0e4c"
-             + "\u0e4d\u0e4e\u0e4f\u0e50\u0e51\u0e52\u0e53\u0e54\u0e55\u0e56"
-             + "\u0e57\u0e58\u0e59\u0e5a\u0e5b";
-
-  var expected = [0x43, 0x0E, 0x44, 0x0E, 0x45, 0x0E, 0x46, 0x0E, 0x47, 0x0E,
-                  0x48, 0x0E, 0x49, 0x0E, 0x4A, 0x0E, 0x4B, 0x0E, 0x4C, 0x0E,
-                  0x4D, 0x0E, 0x4E, 0x0E, 0x4F, 0x0E, 0x50, 0x0E, 0x51, 0x0E,
-                  0x52, 0x0E, 0x53, 0x0E, 0x54, 0x0E, 0x55, 0x0E, 0x56, 0x0E,
-                  0x57, 0x0E, 0x58, 0x0E, 0x59, 0x0E, 0x5A, 0x0E, 0x5B, 0x0E];
-
-  testSingleString({encoding: "Utf-16", input: data, expected: expected,
-    msg: "testing encoding from utf-16 to utf-16 zero."});
-}
-
-function testConstructorEncodingOption(aData, aExpectedString)
-{
-  function errorMessage(encoding) {
-    return `The given encoding '${String(encoding).trim()}' is not supported.`;
-  }
-
-  // valid encoding passed
-  var encoding = "UTF-8";
-  testSingleString({encoding: encoding, input: aData, expected: aExpectedString,
-    msg: "testing encoding with valid utf-8 encoding."});
-
-  // passing spaces for encoding
-  encoding = "   ";
-  testSingleString({encoding: encoding, input: aData, error: "RangeError",
-    errorMessage: errorMessage(encoding),
-    msg: "constructor encoding, spaces encoding test."});
-
-  // invalid encoding passed
-  encoding = "asdfasdf";
-  testSingleString({encoding: encoding, input: aData, error: "RangeError",
-    errorMessage: errorMessage(encoding),
-    msg: "constructor encoding, invalid encoding test."});
-
-  // null encoding passed
-  encoding = null;
-  testSingleString({encoding: encoding, input: aData, error: "RangeError",
-    errorMessage: errorMessage(encoding),
-    msg: "constructor encoding, \"null\" encoding test."});
-
-  // empty encoding passed
-  encoding = "";
-  testSingleString({encoding: encoding, input: aData, error: "RangeError",
-    errorMessage: errorMessage(encoding),
-    msg: "constructor encoding, empty encoding test."});
-}
-
-function testEncodingValues(aData, aExpectedString)
-{
-  var encoding = "ISO-8859-11";
-  testSingleString({encoding: aData, input: encoding, error: "RangeError",
-    msg: "encoder encoding values test."});
-}
-
-function testInputString(aData, aExpectedString)
+function testInputString()
 {
   //Test null input string
-  testSingleString({encoding: "utf-8", input: "", expected: [],
+  testSingleString({input: "", expected: [],
     msg: "encoder null input string test."});
 
   //Test spaces as input string
-  testSingleString({encoding: "utf-8", input: "  ", expected: [32, 32],
+  testSingleString({input: "  ", expected: [32, 32],
     msg: "spaces as input string."});
 }
 
 function testSingleString(test)
 {
   var outText;
   try {
     var stream = test.stream ? {stream: true} : null;
-    outText = (new TextEncoder(test.encoding)).encode(test.input, stream);
+    outText = (new TextEncoder()).encode(test.input, stream);
   } catch (e) {
     assert_equals(e.name, test.error, test.msg + " error thrown from the constructor.");
     if (test.errorMessage) {
       assert_equals(e.message, test.errorMessage, test.msg + " error thrown from the constructor.");
     }
     return;
   }
   assert_true(!test.error, test.msg);
@@ -218,47 +158,43 @@ function testStreamingOptions()
                    0xE0, 0xB9, 0x89, 0xE0, 0xB9, 0x8A, 0xE0, 0xB9, 0x8B,
                    0xE0, 0xB9, 0x8C, 0xE0, 0xB9, 0x8D, 0xE0, 0xB9, 0x8E,
                    0xE0, 0xB9, 0x8F, 0xE0, 0xB9, 0x90, 0xE0, 0xB9, 0x91,
                    0xE0, 0xB9, 0x92, 0xE0, 0xB9, 0x93, 0xE0, 0xB9, 0x94,
                    0xE0, 0xB9, 0x95, 0xE0, 0xB9, 0x96, 0xE0, 0xB9, 0x97,
                    0xE0, 0xB9, 0x98, 0xE0, 0xB9, 0x99, 0xE0, 0xB9, 0x9A,
                    0xE0, 0xB9, 0x9B]];
 
-  var expectedUTF16 = data.map(function(d) {
-    return new Uint8Array(new Uint16Array(arrayFromString(d)).buffer);
-  });
-
   // STREAMING TEST ONE: test streaming three valid strings with stream option
   // set to true for all three.
-  testArrayOfStrings({encoding: "utf-8", array: [
+  testArrayOfStrings({array: [
     {input: data[0], stream: true, expected: expected[0]},
     {input: data[1], stream: true, expected: expected[1]},
     {input: data[2], stream: true, expected: expected[2]},
   ], msg: "streaming test one."});
 
   // STREAMING TEST TWO: test streaming valid strings with stream option
   // streaming option: false from constructor, string 1 stream: true,
   // string 2 stream: false, string 3 stream: false
-  testArrayOfStrings({encoding: "utf-16", array: [
-    {input: data[0], stream: true, expected: expectedUTF16[0]},
-    {input: data[1], expected: expectedUTF16[1]},
-    {input: data[2], expected: expectedUTF16[2]},
+  testArrayOfStrings({array: [
+    {input: data[0], stream: true, expected: expected[0]},
+    {input: data[1], expected: expected[1]},
+    {input: data[2], expected: expected[2]},
   ], msg: "streaming test two."});
 }
 
 function arrayFromString(s) {
   return s.split('').map(function(c){return String.charCodeAt(c)});
 }
 
 function testArrayOfStrings(test)
 {
   var encoder;
   try {
-    encoder = new TextEncoder(test.encoding);
+    encoder = new TextEncoder();
   } catch (e) {
     assert_equals(e.name, test.error, test.msg);
     return;
   }
   assert_true(!test.error, test.msg);
 
   var array = test.array;
   for (var i = 0; i < array.length; i += 1) {
@@ -273,21 +209,11 @@ function testArrayOfStrings(test)
         return;
       }
     }
   }
 }
 
 function testEncoderGetEncoding()
 {
-  var labelEncodings = [
-    {encoding: "utf-8", labels: ["unicode-1-1-utf-8", "utf-8", "utf8"]},
-    {encoding: "utf-16le", labels: ["utf-16", "utf-16"]},
-    {encoding: "utf-16be", labels: ["utf-16be"]},
-  ];
-
-  for (var le of labelEncodings) {
-    for (var label of le.labels) {
-      var encoder = new TextEncoder(label);
-      assert_equals(encoder.encoding, le.encoding, label + " label encoding test.");
-    }
-  }
+  var encoder = new TextEncoder();
+  assert_equals(encoder.encoding, "utf-8", "TextEncoder encoding test.");
 }
--- a/dom/encoding/test/unit/test_misc.js
+++ b/dom/encoding/test/unit/test_misc.js
@@ -128,17 +128,27 @@ test(
   },
   "Encoding names"
 );
 
 test(
   function () {
     ["utf-8", "utf-16le", "utf-16be"].forEach(function (encoding) {
       var string = "\x00123ABCabc\x80\xFF\u0100\u1000\uFFFD\uD800\uDC00\uDBFF\uDFFF";
-      var encoded = new TextEncoder(encoding).encode(string);
+      var octets = {
+        "utf-16le": [0x00,0x00,0x31,0x00,0x32,0x00,0x33,0x00,0x41,0x00,0x42,0x00,
+                     0x43,0x00,0x61,0x00,0x62,0x00,0x63,0x00,0x80,0x00,0xFF,0x00,
+                     0x00,0x01,0x00,0x10,0xFD,0xFF,0x00,0xD8,0x00,0xDC,0xFF,0xDB,
+                     0xFF,0xDF],
+        "utf-16be": [0x00,0x00,0x00,0x31,0x00,0x32,0x00,0x33,0x00,0x41,0x00,0x42,
+                     0x00,0x43,0x00,0x61,0x00,0x62,0x00,0x63,0x00,0x80,0x00,0xFF,
+                     0x01,0x00,0x10,0x00,0xFF,0xFD,0xD8,0x00,0xDC,0x00,0xDB,0xFF,
+                     0xDF,0xFF]
+      };
+      var encoded = octets[encoding] || new TextEncoder(encoding).encode(string);
 
       for (var len = 1; len <= 5; ++len) {
         var out = "", decoder = new TextDecoder(encoding);
         for (var i = 0; i < encoded.length; i += len) {
           var sub = [];
           for (var j = i; j < encoded.length && j < i + len; ++j) {
             sub.push(encoded[j]);
           }
@@ -199,24 +209,17 @@ test(
     new TextDecoder("utf-16be").decode(new Uint8Array([0x00]));
   },
   "Non-fatal errors at EOF"
 );
 
 test(
   function () {
 
-    var utf_encodings = ["utf-8", "utf-16le", "utf-16be"];
+    var encodings = ["utf-8", "ibm866", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-8-i", "iso-8859-10", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8-r", "koi8-u", "macintosh", "windows-874", "windows-1250", "windows-1251", "windows-1252", "windows-1253", "windows-1254", "windows-1255", "windows-1256", "windows-1257", "windows-1258", "x-mac-cyrillic", "gbk", "gb18030", "big5", "euc-jp", "iso-2022-jp", "shift_jis", "euc-kr", "x-user-defined", "utf-16le", "utf-16be"];
 
-    var legacy_encodings = ["ibm866", "iso-8859-2", "iso-8859-3", "iso-8859-4", "iso-8859-5", "iso-8859-6", "iso-8859-7", "iso-8859-8", "iso-8859-8-i", "iso-8859-10", "iso-8859-13", "iso-8859-14", "iso-8859-15", "iso-8859-16", "koi8-r", "koi8-u", "macintosh", "windows-874", "windows-1250", "windows-1251", "windows-1252", "windows-1253", "windows-1254", "windows-1255", "windows-1256", "windows-1257", "windows-1258", "x-mac-cyrillic", "gbk", "gb18030", "big5", "euc-jp", "iso-2022-jp", "shift_jis", "euc-kr", "x-user-defined"];
-
-    utf_encodings.forEach(function(encoding) {
+    encodings.forEach(function(encoding) {
       assert_equals(new TextDecoder(encoding).encoding, encoding);
-      assert_equals(new TextEncoder(encoding).encoding, encoding);
-    });
-
-    legacy_encodings.forEach(function(encoding) {
-      assert_equals(new TextDecoder(encoding).encoding, encoding);
-      assert_throws({name: 'RangeError'}, function() { new TextEncoder(encoding); });
+      assert_equals(new TextEncoder(encoding).encoding, "utf-8");
     });
   },
-  "Non-UTF encodings supported only for decode, not encode"
+  "Non-UTF-8 encodings supported only for decode, not encode"
 );
--- a/dom/encoding/test/unit/test_utf.js
+++ b/dom/encoding/test/unit/test_utf.js
@@ -37,16 +37,38 @@ function encode_utf8(string) {
   var utf8 = unescape(encodeURIComponent(string));
   var octets = new Uint8Array(utf8.length), i;
   for (i = 0; i < utf8.length; i += 1) {
     octets[i] = utf8.charCodeAt(i);
   }
   return octets;
 }
 
+function encode_utf16le(string) {
+  var octets = new Uint8Array(string.length * 2);
+  var di = 0;
+  for (var i = 0; i < string.length; i++) {
+    var code = string.charCodeAt(i);
+    octets[di++] = code & 0xFF;
+    octets[di++] = code >> 8;
+  }
+  return octets;
+}
+
+function encode_utf16be(string) {
+  var octets = new Uint8Array(string.length * 2);
+  var di = 0;
+  for (var i = 0; i < string.length; i++) {
+    var code = string.charCodeAt(i);
+    octets[di++] = code >> 8;
+    octets[di++] = code & 0xFF;
+  }
+  return octets;
+}
+
 function decode_utf8(octets) {
   var utf8 = String.fromCharCode.apply(null, octets);
   return decodeURIComponent(escape(utf8));
 }
 
 // Helpers for test_utf_roundtrip.
 function cpname(n) {
   if (n+0 !== n)
@@ -89,35 +111,33 @@ function genblock(from, len) {
 
 function test_utf_roundtrip () {
   var MIN_CODEPOINT = 0;
   var MAX_CODEPOINT = 0x10FFFF;
   var BLOCK_SIZE = 0x1000;
 
   var block, block_tag, i, j, encoded, decoded, exp_encoded, exp_decoded;
 
-  var TE_U16LE = new TextEncoder("UTF-16LE");
   var TD_U16LE = new TextDecoder("UTF-16LE");
 
-  var TE_U16BE = new TextEncoder("UTF-16BE");
   var TD_U16BE = new TextDecoder("UTF-16BE");
 
-  var TE_U8    = new TextEncoder("UTF-8");
+  var TE_U8    = new TextEncoder();
   var TD_U8    = new TextDecoder("UTF-8");
 
   for (i = MIN_CODEPOINT; i < MAX_CODEPOINT; i += BLOCK_SIZE) {
     block_tag = cpname(i) + " - " + cpname(i + BLOCK_SIZE - 1);
     block = genblock(i, BLOCK_SIZE);
 
     // test UTF-16LE, UTF-16BE, and UTF-8 encodings against themselves
-    encoded = TE_U16LE.encode(block);
+    encoded = encode_utf16le(block);
     decoded = TD_U16LE.decode(encoded);
     assert_string_equals(block, decoded, "UTF-16LE round trip " + block_tag);
 
-    encoded = TE_U16BE.encode(block);
+    encoded = encode_utf16be(block);
     decoded = TD_U16BE.decode(encoded);
     assert_string_equals(block, decoded, "UTF-16BE round trip " + block_tag);
 
     encoded = TE_U8.encode(block);
     decoded = TD_U8.decode(encoded);
     assert_string_equals(block, decoded, "UTF-8 round trip " + block_tag);
 
     // test TextEncoder(UTF-8) against the older idiom
@@ -140,22 +160,22 @@ function test_utf_samples () {
     { encoding: "utf-16le",
       expected: [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xDB, 0xFD, 0xDF] },
     { encoding: "utf-16",
       expected: [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xDB, 0xFD, 0xDF] },
     { encoding: "utf-16be",
       expected: [0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34, 0xD8, 0x34, 0xDD, 0x1E, 0xDB, 0xFF, 0xDF, 0xFD] }
   ];
 
+  var encoded = new TextEncoder().encode(sample);
+  assert_array_equals(encoded, cases[0].expected,
+                      "expected equal encodings");
+
   cases.forEach(
     function(t) {
-      var encoded = new TextEncoder(t.encoding).encode(sample);
-      assert_array_equals(encoded, t.expected,
-                          "expected equal encodings - " + t.encoding);
-
       var decoded = new TextDecoder(t.encoding)
                         .decode(new Uint8Array(t.expected));
       assert_equals(decoded, sample,
                     "expected equal decodings - " + t.encoding);
     });
 }
 
 test(test_utf_samples,
--- a/dom/webidl/TextEncoder.webidl
+++ b/dom/webidl/TextEncoder.webidl
@@ -5,16 +5,16 @@
  *
  * The origin of this IDL file is
  * http://encoding.spec.whatwg.org/#interface-textencoder
  *
  * Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
-[Constructor(optional DOMString utfLabel = "utf-8"),
+[Constructor,
  Exposed=(Window,Worker,System)]
 interface TextEncoder {
   [Constant]
   readonly attribute DOMString encoding;
   [NewObject]
   Uint8Array encode(optional USVString input = "");
 };