Home | History | Annotate | Download | only in i18n
      1 // Copyright 2013 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 // limitations under the License.
     28 
     29 // ECMAScript 402 API implementation is broken into separate files for
     30 // each service. The build system combines them together into one
     31 // Intl namespace.
     32 
     33 /**
     34  * Initializes the given object so it's a valid Collator instance.
     35  * Useful for subclassing.
     36  */
     37 function initializeCollator(collator, locales, options) {
     38   if (collator.hasOwnProperty('__initializedIntlObject')) {
     39     throw new TypeError('Trying to re-initialize Collator object.');
     40   }
     41 
     42   if (options === undefined) {
     43     options = {};
     44   }
     45 
     46   var getOption = getGetOption(options, 'collator');
     47 
     48   var internalOptions = {};
     49 
     50   defineWEProperty(internalOptions, 'usage', getOption(
     51     'usage', 'string', ['sort', 'search'], 'sort'));
     52 
     53   var sensitivity = getOption('sensitivity', 'string',
     54                               ['base', 'accent', 'case', 'variant']);
     55   if (sensitivity === undefined && internalOptions.usage === 'sort') {
     56     sensitivity = 'variant';
     57   }
     58   defineWEProperty(internalOptions, 'sensitivity', sensitivity);
     59 
     60   defineWEProperty(internalOptions, 'ignorePunctuation', getOption(
     61     'ignorePunctuation', 'boolean', undefined, false));
     62 
     63   var locale = resolveLocale('collator', locales, options);
     64 
     65   // ICU can't take kb, kc... parameters through localeID, so we need to pass
     66   // them as options.
     67   // One exception is -co- which has to be part of the extension, but only for
     68   // usage: sort, and its value can't be 'standard' or 'search'.
     69   var extensionMap = parseExtension(locale.extension);
     70   setOptions(
     71       options, extensionMap, COLLATOR_KEY_MAP, getOption, internalOptions);
     72 
     73   var collation = 'default';
     74   var extension = '';
     75   if (extensionMap.hasOwnProperty('co') && internalOptions.usage === 'sort') {
     76     if (ALLOWED_CO_VALUES.indexOf(extensionMap.co) !== -1) {
     77       extension = '-u-co-' + extensionMap.co;
     78       // ICU can't tell us what the collation is, so save user's input.
     79       collation = extensionMap.co;
     80     }
     81   } else if (internalOptions.usage === 'search') {
     82     extension = '-u-co-search';
     83   }
     84   defineWEProperty(internalOptions, 'collation', collation);
     85 
     86   var requestedLocale = locale.locale + extension;
     87 
     88   // We define all properties C++ code may produce, to prevent security
     89   // problems. If malicious user decides to redefine Object.prototype.locale
     90   // we can't just use plain x.locale = 'us' or in C++ Set("locale", "us").
     91   // Object.defineProperties will either succeed defining or throw an error.
     92   var resolved = Object.defineProperties({}, {
     93     caseFirst: {writable: true},
     94     collation: {value: internalOptions.collation, writable: true},
     95     ignorePunctuation: {writable: true},
     96     locale: {writable: true},
     97     numeric: {writable: true},
     98     requestedLocale: {value: requestedLocale, writable: true},
     99     sensitivity: {writable: true},
    100     strength: {writable: true},
    101     usage: {value: internalOptions.usage, writable: true}
    102   });
    103 
    104   var internalCollator = %CreateCollator(requestedLocale,
    105                                          internalOptions,
    106                                          resolved);
    107 
    108   // Writable, configurable and enumerable are set to false by default.
    109   Object.defineProperty(collator, 'collator', {value: internalCollator});
    110   Object.defineProperty(collator, '__initializedIntlObject',
    111                         {value: 'collator'});
    112   Object.defineProperty(collator, 'resolved', {value: resolved});
    113 
    114   return collator;
    115 }
    116 
    117 
    118 /**
    119  * Constructs Intl.Collator object given optional locales and options
    120  * parameters.
    121  *
    122  * @constructor
    123  */
    124 %SetProperty(Intl, 'Collator', function() {
    125     var locales = arguments[0];
    126     var options = arguments[1];
    127 
    128     if (!this || this === Intl) {
    129       // Constructor is called as a function.
    130       return new Intl.Collator(locales, options);
    131     }
    132 
    133     return initializeCollator(toObject(this), locales, options);
    134   },
    135   ATTRIBUTES.DONT_ENUM
    136 );
    137 
    138 
    139 /**
    140  * Collator resolvedOptions method.
    141  */
    142 %SetProperty(Intl.Collator.prototype, 'resolvedOptions', function() {
    143     if (%_IsConstructCall()) {
    144       throw new TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR);
    145     }
    146 
    147     if (!this || typeof this !== 'object' ||
    148         this.__initializedIntlObject !== 'collator') {
    149       throw new TypeError('resolvedOptions method called on a non-object ' +
    150                           'or on a object that is not Intl.Collator.');
    151     }
    152 
    153     var coll = this;
    154     var locale = getOptimalLanguageTag(coll.resolved.requestedLocale,
    155                                        coll.resolved.locale);
    156 
    157     return {
    158       locale: locale,
    159       usage: coll.resolved.usage,
    160       sensitivity: coll.resolved.sensitivity,
    161       ignorePunctuation: coll.resolved.ignorePunctuation,
    162       numeric: coll.resolved.numeric,
    163       caseFirst: coll.resolved.caseFirst,
    164       collation: coll.resolved.collation
    165     };
    166   },
    167   ATTRIBUTES.DONT_ENUM
    168 );
    169 %FunctionSetName(Intl.Collator.prototype.resolvedOptions, 'resolvedOptions');
    170 %FunctionRemovePrototype(Intl.Collator.prototype.resolvedOptions);
    171 %SetNativeFlag(Intl.Collator.prototype.resolvedOptions);
    172 
    173 
    174 /**
    175  * Returns the subset of the given locale list for which this locale list
    176  * has a matching (possibly fallback) locale. Locales appear in the same
    177  * order in the returned list as in the input list.
    178  * Options are optional parameter.
    179  */
    180 %SetProperty(Intl.Collator, 'supportedLocalesOf', function(locales) {
    181     if (%_IsConstructCall()) {
    182       throw new TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR);
    183     }
    184 
    185     return supportedLocalesOf('collator', locales, arguments[1]);
    186   },
    187   ATTRIBUTES.DONT_ENUM
    188 );
    189 %FunctionSetName(Intl.Collator.supportedLocalesOf, 'supportedLocalesOf');
    190 %FunctionRemovePrototype(Intl.Collator.supportedLocalesOf);
    191 %SetNativeFlag(Intl.Collator.supportedLocalesOf);
    192 
    193 
    194 /**
    195  * When the compare method is called with two arguments x and y, it returns a
    196  * Number other than NaN that represents the result of a locale-sensitive
    197  * String comparison of x with y.
    198  * The result is intended to order String values in the sort order specified
    199  * by the effective locale and collation options computed during construction
    200  * of this Collator object, and will be negative, zero, or positive, depending
    201  * on whether x comes before y in the sort order, the Strings are equal under
    202  * the sort order, or x comes after y in the sort order, respectively.
    203  */
    204 function compare(collator, x, y) {
    205   return %InternalCompare(collator.collator, String(x), String(y));
    206 };
    207 
    208 
    209 addBoundMethod(Intl.Collator, 'compare', compare, 2);
    210