Răsfoiți Sursa

Keyboard Handling [8/8]: Introduce substituteCodepoint() to replace code points which don't have a keysym with ones that do

For now, the only code points this is done for are {s, S, t, T} with comma below (used in Romanian),
 which are replaced by {s, S, t, T} Cedilla.
jalf 11 ani în urmă
părinte
comite
466a09f0f3
2 a modificat fișierele cu 34 adăugiri și 2 ștergeri
  1. 19 2
      include/keyboard.js
  2. 15 0
      tests/test.helper.js

+ 19 - 2
include/keyboard.js

@@ -1,6 +1,22 @@
 var kbdUtil = (function() {
     "use strict";
 
+    function substituteCodepoint(cp) {
+        // Any Unicode code points which do not have corresponding keysym entries
+        // can be swapped out for another code point by adding them to this table
+        var substitutions = {
+            // {S,s} with comma below -> {S,s} with cedilla
+            0x218 : 0x15e,
+            0x219 : 0x15f,
+            // {T,t} with comma below -> {T,t} with cedilla
+            0x21a : 0x162,
+            0x21b : 0x163
+        };
+
+        var sub = substitutions[cp];
+        return sub ? sub : cp;
+    };
+
     function isMac() {
         return navigator && !!(/macintosh/i).exec(navigator.appVersion);
     }
@@ -156,7 +172,7 @@ var kbdUtil = (function() {
         }
 
         if (codepoint) {
-            var res = keysyms.fromUnicode(codepoint);
+            var res = keysyms.fromUnicode(substituteCodepoint(codepoint));
             if (res) {
                 return res;
             }
@@ -258,7 +274,8 @@ var kbdUtil = (function() {
         getKey : getKey,
         getKeysym : getKeysym,
         keysymFromKeyCode : keysymFromKeyCode,
-        nonCharacterKey : nonCharacterKey
+        nonCharacterKey : nonCharacterKey,
+        substituteCodepoint : substituteCodepoint
     };
 })();
 

+ 15 - 0
tests/test.helper.js

@@ -42,6 +42,18 @@ describe('Helpers', function() {
         });
     });
 
+    describe('substituteCodepoint', function() {
+        it('should replace characters which don\'t have a keysym', function() {
+            expect(kbdUtil.substituteCodepoint('Ș'.charCodeAt())).to.equal('Ş'.charCodeAt());
+            expect(kbdUtil.substituteCodepoint('ș'.charCodeAt())).to.equal('ş'.charCodeAt());
+            expect(kbdUtil.substituteCodepoint('Ț'.charCodeAt())).to.equal('Ţ'.charCodeAt());
+            expect(kbdUtil.substituteCodepoint('ț'.charCodeAt())).to.equal('ţ'.charCodeAt());
+        });
+        it('should pass other characters through unchanged', function() {
+            expect(kbdUtil.substituteCodepoint('T'.charCodeAt())).to.equal('T'.charCodeAt());
+        });
+    });
+
     describe('nonCharacterKey', function() {
         it('should  recognize the right keys', function() {
             expect(kbdUtil.nonCharacterKey({keyCode: 0xd}), 'enter').to.be.defined;
@@ -77,6 +89,9 @@ describe('Helpers', function() {
             expect(kbdUtil.getKeysym({which: 0x43, shiftKey: false})).to.have.property('keysym', 0x63);
             expect(kbdUtil.getKeysym({which: 0x43, shiftKey: true})).to.have.property('keysym', 0x43);
         });
+        it('should substitute where applicable', function() {
+            expect(kbdUtil.getKeysym({char : 'Ș'})).to.have.property('keysym', 0x1aa);
+        });
     });
 
     describe('Modifier Sync', function() { // return a list of fake events necessary to fix modifier state