From e15a1ab1b3d2ca7db649ffab61c1f0ec057c3ac5 Mon Sep 17 00:00:00 2001 From: Frans de Jonge Date: Wed, 7 Mar 2018 10:22:49 +0100 Subject: [PATCH] [fix, spec] InputText:addChars() unicode handling (#3729) Also rename from `addChar` to `addChars` for clarity. Fixes #3703. --- frontend/ui/widget/inputtext.lua | 6 +-- frontend/ui/widget/physicalkeyboard.lua | 2 +- frontend/ui/widget/virtualkeyboard.lua | 2 +- spec/unit/inputtext_spec.lua | 57 +++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 spec/unit/inputtext_spec.lua diff --git a/frontend/ui/widget/inputtext.lua b/frontend/ui/widget/inputtext.lua index 7cef22784..fa6786fde 100644 --- a/frontend/ui/widget/inputtext.lua +++ b/frontend/ui/widget/inputtext.lua @@ -81,7 +81,7 @@ if Device.isTouchDevice() then if x > 0 and y > 0 then self.charpos = self.text_widget:moveCursor(x, y) if Device:hasClipboard() and Device.input.hasClipboardText() then - self:addChar(Device.input.getClipboardText()) + self:addChars(Device.input.getClipboardText()) end UIManager:setDirty(self.parent, function() return "ui", self.dimen @@ -247,13 +247,13 @@ function InputText:getKeyboardDimen() return self.keyboard.dimen end -function InputText:addChar(char) +function InputText:addChars(char) if self.enter_callback and char == '\n' then UIManager:scheduleIn(0.3, function() self.enter_callback() end) return end table.insert(self.charlist, self.charpos, char) - self.charpos = self.charpos + string.len(char) + self.charpos = self.charpos + #util.splitToChars(char) self:initTextBox(table.concat(self.charlist), true) end diff --git a/frontend/ui/widget/physicalkeyboard.lua b/frontend/ui/widget/physicalkeyboard.lua index 20e4a6d29..18f4f746f 100644 --- a/frontend/ui/widget/physicalkeyboard.lua +++ b/frontend/ui/widget/physicalkeyboard.lua @@ -119,7 +119,7 @@ function PhysicalKeyboard:onKeyPress(ev) if self.key_transformer then key = self.key_transformer[key] end - self.inputbox:addChar(key) + self.inputbox:addChars(key) end end diff --git a/frontend/ui/widget/virtualkeyboard.lua b/frontend/ui/widget/virtualkeyboard.lua index 2fa507d0b..00db9508e 100644 --- a/frontend/ui/widget/virtualkeyboard.lua +++ b/frontend/ui/widget/virtualkeyboard.lua @@ -307,7 +307,7 @@ end function VirtualKeyboard:addChar(key) logger.dbg("add char", key) - self.inputbox:addChar(key) + self.inputbox:addChars(key) end function VirtualKeyboard:delChar() diff --git a/spec/unit/inputtext_spec.lua b/spec/unit/inputtext_spec.lua new file mode 100644 index 000000000..ed63c1ccf --- /dev/null +++ b/spec/unit/inputtext_spec.lua @@ -0,0 +1,57 @@ +describe("InputText widget module", function() + local InputText + local equals + setup(function() + require("commonrequire") + InputText = require("ui/widget/inputtext") + + -- thanks to https://stackoverflow.com/a/32660766/2470572 + equals = function(o1, o2, ignore_mt) + if o1 == o2 then return true end + local o1Type = type(o1) + local o2Type = type(o2) + if o1Type ~= o2Type then return false end + if o1Type ~= 'table' then return false end + + if not ignore_mt then + local mt1 = getmetatable(o1) + if mt1 and mt1.__eq then + --compare using built in method + return o1 == o2 + end + end + + local keySet = {} + + for key1, value1 in pairs(o1) do + local value2 = o2[key1] + if value2 == nil or equals(value1, value2, ignore_mt) == false then + return false + end + keySet[key1] = true + end + + for key2, _ in pairs(o2) do + if not keySet[key2] then return false end + end + return true + end + end) + + describe("addChars()", function() + it("should add regular text", function() + InputText:initTextBox("") + InputText:addChars("a") + assert.is_true( equals({"a"}, InputText.charlist) ) + InputText:addChars("aa") + assert.is_true( equals({"a", "a", "a"}, InputText.charlist) ) + end) + it("should add unicode text", function() + InputText:initTextBox("") + InputText:addChars("Л") + assert.is_true( equals({"Л"}, InputText.charlist) ) + InputText:addChars("Луа") + assert.is_true( equals({"Л", "Л", "у", "а"}, InputText.charlist) ) + end) + end) +end)