Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/model/input/userInputModel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,27 @@ function UserInputModel:init_visible(text)
self.visible:set_default_range()
end

--- Drop bytes that do not form valid UTF-8
--- @param s string
--- @return string
local function sanitize_utf8(s)
local r = s
local n, pos = utf8.len(r)
while not n do
r = string.sub(r, 1, pos - 1) .. string.sub(r, pos + 1)
n, pos = utf8.len(r)
end
return r
end

----------------
-- entered --
----------------

--- @param text string
function UserInputModel:add_text(text)
if type(text) == 'string' then
text = sanitize_utf8(text)
self:pop_selected_text()
local sl, cc = self:get_cursor_pos()
local cur_line = self:get_text_line(sl)
Expand Down Expand Up @@ -127,6 +141,7 @@ end
--- @param keep_cursor boolean
function UserInputModel:set_text(text, keep_cursor)
if type(text) == 'string' then
text = sanitize_utf8(text)
local lines = string.lines(text)
local n_added = #lines
if n_added == 1 then
Expand All @@ -136,7 +151,11 @@ function UserInputModel:set_text(text, keep_cursor)
self:_update_cursor(true)
end
elseif type(text) == 'table' then
self.entered = InputText(text)
local clean = {}
for i, l in ipairs(text) do
clean[i] = sanitize_utf8(l)
end
self.entered = InputText(clean)
end
self:text_change()
if not keep_cursor then
Expand Down
5 changes: 4 additions & 1 deletion src/view/input/userInputView.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ function UserInputView:render_input(input, status, time)
for l, s in ipairs(visible) do
local ln = l + vc.offset
local tl = string.ulen(s)
if not tl then return end
if not tl then
gfx.pop()
return
end

for c = 1, tl do
local char = string.usub(s, c, c)
Expand Down
28 changes: 28 additions & 0 deletions tests/input/user_input_model_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ describe("input model spec #input", function()
}
mock.mock_love(love)

describe('invalid UTF-8', function()
local invalid = 'abc\195('

it('add_text drops invalid bytes', function()
local model = UserInputModel(mockConf, luaEval)
model:add_text(invalid)
assert.same({ 'abc(' }, model:get_text())
end)

it('paste drops invalid bytes across lines', function()
local model = UserInputModel(mockConf, luaEval)
model:paste('abc\ndef' .. invalid)
assert.same({ 'abc', 'defabc(' }, model:get_text())
end)

it('set_text drops invalid bytes', function()
local model = UserInputModel(mockConf, luaEval)
model:set_text(invalid)
assert.same({ 'abc(' }, model:get_text())
end)

it('set_text drops invalid bytes in tables', function()
local model = UserInputModel(mockConf, luaEval)
model:set_text({ 'ok', invalid })
assert.same({ 'ok', 'abc(' }, model:get_text())
end)
end)

-----------------
-- ASCII --
-----------------
Expand Down
Loading