From 8d1d5ac84ca99e51980afe292ce245cc7b1b07f0 Mon Sep 17 00:00:00 2001 From: Jackmcbarn Date: Fri, 27 Jun 2014 12:13:10 -0400 Subject: [PATCH] Fix strange mw.html errors with numeric arguments Some functions in mw.html accept numbers as arguments, but later fail when constructing the string. This disallows numbers in attribute names, since they aren't valid anyway, and fixes the remainder of the cases to properly build the string. Bug: 67201 Change-Id: Ie7bcbb9d8df580dd8793681f78a8b0719d8a287a --- engines/LuaCommon/lualib/mw.html.lua | 6 +++--- tests/engines/LuaCommon/HtmlLibraryTests.lua | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/engines/LuaCommon/lualib/mw.html.lua b/engines/LuaCommon/lualib/mw.html.lua index 256fa9e1..7fd503b7 100644 --- a/engines/LuaCommon/lualib/mw.html.lua +++ b/engines/LuaCommon/lualib/mw.html.lua @@ -105,7 +105,7 @@ methodtable._build = function( t, ret ) if #t.styles > 0 then table.insert( ret, ' style="' ) for i, prop in ipairs( t.styles ) do - if type( prop ) == 'string' then -- added with cssText() + if type( prop ) ~= 'table' then -- added with cssText() table.insert( ret, htmlEncode( prop ) .. ';' ) else -- added with css() table.insert( @@ -216,8 +216,8 @@ methodtable.attr = function( t, name, val ) return t end - if type( name ) ~= 'string' and type( name ) ~= 'number' then - error( 'Invalid name given: The name must be either a string or a number' ) + if type( name ) ~= 'string' then + error( 'Invalid name given: The name must be a string' ) end if val ~= nil and type( val ) ~= 'string' and type( val ) ~= 'number' then error( 'Invalid value given: The value must be either a string or a number' ) diff --git a/tests/engines/LuaCommon/HtmlLibraryTests.lua b/tests/engines/LuaCommon/HtmlLibraryTests.lua index 3c61a5e0..de698288 100644 --- a/tests/engines/LuaCommon/HtmlLibraryTests.lua +++ b/tests/engines/LuaCommon/HtmlLibraryTests.lua @@ -158,8 +158,8 @@ local tests = { expect = { '
' } }, { name = 'mw.html.attr (invalid name 1)', func = testHelper, type='ToString', - args = { getEmptyTestDiv(), 'attr', true, 'bar' }, - expect = 'Invalid name given: The name must be either a string or a number' + args = { getEmptyTestDiv(), 'attr', 123, 'bar' }, + expect = 'Invalid name given: The name must be a string' }, { name = 'mw.html.attr (invalid name 2)', func = testHelper, args = { getEmptyTestDiv(), 'attr', 'ยงยงยงยง', 'foo' }, @@ -197,6 +197,10 @@ local tests = { args = { getEmptyTestDiv(), 'addClass', 'foo' }, expect = { '
' } }, + { name = 'mw.html.addClass (numeric argument)', func = testHelper, type='ToString', + args = { getEmptyTestDiv(), 'addClass', 123 }, + expect = { '
' } + }, { name = 'mw.html.addClass (invalid value)', func = testHelper, type='ToString', args = { getEmptyTestDiv(), 'addClass', {} }, expect = 'Invalid class given: The name must be either a string or a number' @@ -205,6 +209,10 @@ local tests = { args = { getEmptyTestDiv(), 'css', 'foo', 'bar' }, expect = { '
' } }, + { name = 'mw.html.css (numeric arguments)', func = testHelper, type='ToString', + args = { getEmptyTestDiv(), 'css', 123, 456 }, + expect = { '
' } + }, { name = 'mw.html.css (nil noop)', func = testHelper, type='ToString', args = { getEmptyTestDiv(), 'css', 'foo', nil }, expect = { '
' } @@ -233,6 +241,10 @@ local tests = { args = { getEmptyTestDiv(), 'cssText', 'Unit tests, ftw' }, expect = { '
' } }, + { name = 'mw.html.cssText (numeric argument)', func = testHelper, type='ToString', + args = { getEmptyTestDiv(), 'cssText', 123 }, + expect = { '
' } + }, { name = 'mw.html.cssText (invalid value)', func = testHelper, type='ToString', args = { getEmptyTestDiv(), 'cssText', {} }, expect = 'Invalid CSS given: Must be either a string or a number'