git.iko.soy/typeable/pandoc
on master help sign in
data/pandoc.lua: fix deletion of nonexistent attributes

Fixes: #5569

commit7f9b32e36a8fdfe3efdcdb05b84cafd297641d85 authorAlbert Krewinkel <albert@zeitkraut.de> dateTue 11 Jun 2019, 19:39 (7 years ago) parent03b9f3d
2 files changed+10−1
data/pandoc.lua+4−1 test/lua/module/pandoc.lua+6
data/pandoc.lua+4−1
@@ -846,7 +846,10 @@ local AttributeList = {
846846
847847 __newindex = function (t, k, v)
848848 local cur, idx = List.find_if(t, assoc_key_equals(k))
849− if v == nil then
849+ if v == nil and not cur then
850+ -- deleted key does not exists in list
851+ return
852+ elseif v == nil then
850853 table.remove(t, idx)
851854 elseif cur then
852855 cur[2] = v
test/lua/module/pandoc.lua+6
@@ -62,6 +62,12 @@ return {
6262 local assoc_list = setmetatable(attributes, nil)
6363 assert.are_same(assoc_list, {{'b', '2'}})
6464 end),
65+ test('remains unchanged if deleted key did not exist', function ()
66+ local assoc_list = pandoc.List:new {{'alpha', 'x'}, {'beta', 'y'}}
67+ local attributes = pandoc.Attr('', {}, assoc_list:clone()).attributes
68+ attributes.a = nil
69+ assert.are_same(pandoc.List:new(attributes), assoc_list)
70+ end),
6571 test('gives key-value pairs when iterated-over', function ()
6672 local attributes = {width = '11', height = '22', name = 'test'}
6773 local attr = pandoc.Attr('', {}, attributes)