summaryrefslogtreecommitdiff
path: root/resources/jquery/jquery.byteLength.js
diff options
context:
space:
mode:
authorPierre Schmitz <pierre@archlinux.de>2011-12-03 13:29:22 +0100
committerPierre Schmitz <pierre@archlinux.de>2011-12-03 13:29:22 +0100
commitca32f08966f1b51fcb19460f0996bb0c4048e6fe (patch)
treeec04cc15b867bc21eedca904cea9af0254531a11 /resources/jquery/jquery.byteLength.js
parenta22fbfc60f36f5f7ee10d5ae6fe347340c2ee67c (diff)
Update to MediaWiki 1.18.0
* also update ArchLinux skin to chagnes in MonoBook * Use only css to hide our menu bar when printing
Diffstat (limited to 'resources/jquery/jquery.byteLength.js')
-rw-r--r--resources/jquery/jquery.byteLength.js19
1 files changed, 19 insertions, 0 deletions
diff --git a/resources/jquery/jquery.byteLength.js b/resources/jquery/jquery.byteLength.js
new file mode 100644
index 00000000..20fa5c8e
--- /dev/null
+++ b/resources/jquery/jquery.byteLength.js
@@ -0,0 +1,19 @@
+/**
+ * jQuery.byteLength
+ *
+ * Calculate the byte length of a string (accounting for UTF-8).
+ *
+ * @author Jan Paul Posma
+ */
+jQuery.byteLength = function( str ) {
+
+ // This basically figures out how many bytes a UTF-16 string (which is what js sees)
+ // will take in UTF-8 by replacing a 2 byte character with 2 *'s, etc, and counting that.
+ // Note, surrogate (\uD800-\uDFFF) characters are counted as 2 bytes, since there's two of them
+ // and the actual character takes 4 bytes in UTF-8 (2*2=4). Might not work perfectly in
+ // edge cases such as illegal sequences, but that should never happen.
+ return str
+ .replace( /[\u0080-\u07FF\uD800-\uDFFF]/g, '**' )
+ .replace( /[\u0800-\uD7FF\uE000-\uFFFF]/g, '***' )
+ .length;
+}