OLD | NEW |
(Empty) | |
| 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 native function NativeJSCreateCollator(); |
| 39 |
| 40 if (collator.hasOwnProperty('__initializedIntlObject')) { |
| 41 throw new TypeError('Trying to re-initialize Collator object.'); |
| 42 } |
| 43 |
| 44 if (options === undefined) { |
| 45 options = {}; |
| 46 } |
| 47 |
| 48 var getOption = getGetOption(options, 'collator'); |
| 49 |
| 50 var internalOptions = {}; |
| 51 |
| 52 defineWEProperty(internalOptions, 'usage', getOption( |
| 53 'usage', 'string', ['sort', 'search'], 'sort')); |
| 54 |
| 55 var sensitivity = getOption('sensitivity', 'string', |
| 56 ['base', 'accent', 'case', 'variant']); |
| 57 if (sensitivity === undefined && internalOptions.usage === 'sort') { |
| 58 sensitivity = 'variant'; |
| 59 } |
| 60 defineWEProperty(internalOptions, 'sensitivity', sensitivity); |
| 61 |
| 62 defineWEProperty(internalOptions, 'ignorePunctuation', getOption( |
| 63 'ignorePunctuation', 'boolean', undefined, false)); |
| 64 |
| 65 var locale = resolveLocale('collator', locales, options); |
| 66 |
| 67 // ICU can't take kb, kc... parameters through localeID, so we need to pass |
| 68 // them as options. |
| 69 // One exception is -co- which has to be part of the extension, but only for |
| 70 // usage: sort, and its value can't be 'standard' or 'search'. |
| 71 var extensionMap = parseExtension(locale.extension); |
| 72 setOptions( |
| 73 options, extensionMap, COLLATOR_KEY_MAP, getOption, internalOptions); |
| 74 |
| 75 var collation = 'default'; |
| 76 var extension = ''; |
| 77 if (extensionMap.hasOwnProperty('co') && internalOptions.usage === 'sort') { |
| 78 if (ALLOWED_CO_VALUES.indexOf(extensionMap.co) !== -1) { |
| 79 extension = '-u-co-' + extensionMap.co; |
| 80 // ICU can't tell us what the collation is, so save user's input. |
| 81 collation = extensionMap.co; |
| 82 } |
| 83 } else if (internalOptions.usage === 'search') { |
| 84 extension = '-u-co-search'; |
| 85 } |
| 86 defineWEProperty(internalOptions, 'collation', collation); |
| 87 |
| 88 var requestedLocale = locale.locale + extension; |
| 89 |
| 90 // We define all properties C++ code may produce, to prevent security |
| 91 // problems. If malicious user decides to redefine Object.prototype.locale |
| 92 // we can't just use plain x.locale = 'us' or in C++ Set("locale", "us"). |
| 93 // Object.defineProperties will either succeed defining or throw an error. |
| 94 var resolved = Object.defineProperties({}, { |
| 95 caseFirst: {writable: true}, |
| 96 collation: {value: internalOptions.collation, writable: true}, |
| 97 ignorePunctuation: {writable: true}, |
| 98 locale: {writable: true}, |
| 99 numeric: {writable: true}, |
| 100 requestedLocale: {value: requestedLocale, writable: true}, |
| 101 sensitivity: {writable: true}, |
| 102 strength: {writable: true}, |
| 103 usage: {value: internalOptions.usage, writable: true} |
| 104 }); |
| 105 |
| 106 var internalCollator = NativeJSCreateCollator(requestedLocale, |
| 107 internalOptions, |
| 108 resolved); |
| 109 |
| 110 // Writable, configurable and enumerable are set to false by default. |
| 111 Object.defineProperty(collator, 'collator', {value: internalCollator}); |
| 112 Object.defineProperty(collator, '__initializedIntlObject', |
| 113 {value: 'collator'}); |
| 114 Object.defineProperty(collator, 'resolved', {value: resolved}); |
| 115 |
| 116 return collator; |
| 117 } |
| 118 |
| 119 |
| 120 /** |
| 121 * Constructs Intl.Collator object given optional locales and options |
| 122 * parameters. |
| 123 * |
| 124 * @constructor |
| 125 */ |
| 126 %SetProperty(Intl, 'Collator', function() { |
| 127 var locales = arguments[0]; |
| 128 var options = arguments[1]; |
| 129 |
| 130 if (!this || this === Intl) { |
| 131 // Constructor is called as a function. |
| 132 return new Intl.Collator(locales, options); |
| 133 } |
| 134 |
| 135 return initializeCollator(toObject(this), locales, options); |
| 136 }, |
| 137 ATTRIBUTES.DONT_ENUM |
| 138 ); |
| 139 |
| 140 |
| 141 /** |
| 142 * Collator resolvedOptions method. |
| 143 */ |
| 144 %SetProperty(Intl.Collator.prototype, 'resolvedOptions', function() { |
| 145 if (%_IsConstructCall()) { |
| 146 throw new TypeError(ORDINARY_FUNCTION_CALLED_AS_CONSTRUCTOR); |
| 147 } |
| 148 |
| 149 if (!this || typeof this !== 'object' || |
| 150 this.__initializedIntlObject !== 'collator') { |
| 151 throw new TypeError('resolvedOptions method called on a non-object ' + |
| 152 'or on a object that is not Intl.Collator.'); |
| 153 } |
| 154 |
| 155 var coll = this; |
| 156 var locale = getOptimalLanguageTag(coll.resolved.requestedLocale, |
| 157 coll.resolved.locale); |
| 158 |
| 159 return { |
| 160 locale: locale, |
| 161 usage: coll.resolved.usage, |
| 162 sensitivity: coll.resolved.sensitivity, |
| 163 ignorePunctuation: coll.resolved.ignorePunctuation, |
| 164 numeric: coll.resolved.numeric, |
| 165 caseFirst: coll.resolved.caseFirst, |
| 166 collation: coll.resolved.collation |
| 167 }; |
| 168 }, |
| 169 ATTRIBUTES.DONT_ENUM |
| 170 ); |
| 171 %FunctionRemovePrototype(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 %FunctionRemovePrototype(Intl.Collator.supportedLocalesOf); |
| 190 |
| 191 |
| 192 /** |
| 193 * When the compare method is called with two arguments x and y, it returns a |
| 194 * Number other than NaN that represents the result of a locale-sensitive |
| 195 * String comparison of x with y. |
| 196 * The result is intended to order String values in the sort order specified |
| 197 * by the effective locale and collation options computed during construction |
| 198 * of this Collator object, and will be negative, zero, or positive, depending |
| 199 * on whether x comes before y in the sort order, the Strings are equal under |
| 200 * the sort order, or x comes after y in the sort order, respectively. |
| 201 */ |
| 202 function compare(collator, x, y) { |
| 203 native function NativeJSInternalCompare(); |
| 204 return NativeJSInternalCompare(collator.collator, String(x), String(y)); |
| 205 }; |
| 206 |
| 207 |
| 208 addBoundMethod(Intl.Collator, 'compare', compare, 2); |
OLD | NEW |