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
This commit is contained in:
Jackmcbarn 2014-06-27 12:13:10 -04:00
parent bf39827980
commit 8d1d5ac84c
2 changed files with 17 additions and 5 deletions

View file

@ -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' )

View file

@ -158,8 +158,8 @@ local tests = {
expect = { '<div ab="cd" foo="bar"></div>' }
},
{ 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 = { '<div class="foo"></div>' }
},
{ name = 'mw.html.addClass (numeric argument)', func = testHelper, type='ToString',
args = { getEmptyTestDiv(), 'addClass', 123 },
expect = { '<div class="123"></div>' }
},
{ 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 = { '<div style="foo:bar;"></div>' }
},
{ name = 'mw.html.css (numeric arguments)', func = testHelper, type='ToString',
args = { getEmptyTestDiv(), 'css', 123, 456 },
expect = { '<div style="123:456;"></div>' }
},
{ name = 'mw.html.css (nil noop)', func = testHelper, type='ToString',
args = { getEmptyTestDiv(), 'css', 'foo', nil },
expect = { '<div></div>' }
@ -233,6 +241,10 @@ local tests = {
args = { getEmptyTestDiv(), 'cssText', 'Unit tests, ftw' },
expect = { '<div style="Unit tests, ftw;"></div>' }
},
{ name = 'mw.html.cssText (numeric argument)', func = testHelper, type='ToString',
args = { getEmptyTestDiv(), 'cssText', 123 },
expect = { '<div style="123;"></div>' }
},
{ 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'