mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Scribunto
synced 2024-11-27 17:50:06 +00:00
Synchronize mw.text.nowiki() with wfEscapeWikiText in core
Added escapes for "!" and ";" as well as additional escapes at beginning and end of string. Bug: T168763 Co-Authored-By: vlakoff <vlakoff@gmail.com> Co-Authored-By: C. Scott Ananian <cananian@wikimedia.org> Depends-On: I34f2fa8c329e6f6771453b2f94dc4afbec31dac8 Change-Id: I6c9dcfdbbb2c6eff9414e24d3f2693ebe576505a
This commit is contained in:
parent
bce8a82d66
commit
6c340bff8d
|
@ -100,19 +100,32 @@ local nowikiRepl1 = {
|
|||
['{'] = '{',
|
||||
['|'] = '|',
|
||||
['}'] = '}',
|
||||
[';'] = ';',
|
||||
}
|
||||
|
||||
local nowikiRepl2 = {
|
||||
["\n!"] = "\n!", ["\r!"] = "\r!",
|
||||
["\n#"] = "\n#", ["\r#"] = "\r#",
|
||||
["\n*"] = "\n*", ["\r*"] = "\r*",
|
||||
["\n:"] = "\n:", ["\r:"] = "\r:",
|
||||
["\n;"] = "\n;", ["\r;"] = "\r;",
|
||||
["\n "] = "\n ", ["\r "] = "\r ",
|
||||
["\n\n"] = "\n ", ["\r\n"] = " \n",
|
||||
["\n\r"] = "\n ", ["\r\r"] = "\r ",
|
||||
["\n\t"] = "\n	", ["\r\t"] = "\r	",
|
||||
}
|
||||
|
||||
local nowikiRepl3 = {
|
||||
['+'] = '+',
|
||||
['-'] = '-',
|
||||
['_'] = '_',
|
||||
['~'] = '~',
|
||||
["\n"] = " ",
|
||||
["\r"] = " ",
|
||||
["\t"] = "	",
|
||||
}
|
||||
|
||||
local nowikiRepl4 = nowikiRepl3
|
||||
|
||||
local nowikiReplMagic = {}
|
||||
for sp, esc in pairs( {
|
||||
[' '] = ' ',
|
||||
|
@ -128,13 +141,20 @@ end
|
|||
|
||||
function mwtext.nowiki( s )
|
||||
-- string.gsub is safe here, because we're only caring about ASCII chars
|
||||
s = string.gsub( s, '["&\'<=>%[%]{|}]', nowikiRepl1 )
|
||||
s = string.gsub( s, '["&\'<=>%[%]{|};]', nowikiRepl1 )
|
||||
s = '\n' .. s
|
||||
s = string.gsub( s, '[\r\n][#*:; \n\r\t]', nowikiRepl2 )
|
||||
s = string.gsub( s, '[\r\n][!#*: \n\r\t]', nowikiRepl2 )
|
||||
s = string.gsub( s, '([\r\n])%-%-%-%-', '%1----' )
|
||||
s = string.sub( s, 2 )
|
||||
s = string.gsub( s, '!!', '!!' )
|
||||
s = string.gsub( s, '__', '__' )
|
||||
s = string.gsub( s, '://', '://' )
|
||||
s = string.gsub( s, '~~~', '~~~' )
|
||||
-- protect first and last character
|
||||
s = string.gsub( s, '^[%-+_~]', nowikiRepl3 )
|
||||
s = string.gsub( s, '[_~\r\n\t]$', nowikiRepl4 )
|
||||
-- technically, should only do these if $wgEnableMagicLinks, but
|
||||
-- it doesn't hurt to be safe
|
||||
s = string.gsub( s, 'ISBN%s', nowikiReplMagic )
|
||||
s = string.gsub( s, 'RFC%s', nowikiReplMagic )
|
||||
s = string.gsub( s, 'PMID%s', nowikiReplMagic )
|
||||
|
|
|
@ -82,12 +82,97 @@ local tests = {
|
|||
func = mw.text.nowiki,
|
||||
args = { '*"&\'<=>[]{|}#*:;\n*\n#\n:\n;\nhttp://example.com:80/\nRFC 123, ISBN 456' },
|
||||
expect = {
|
||||
'*"&'<=>[]{|}#*:;' ..
|
||||
'*"&'<=>[]{|}#*:;' ..
|
||||
'\n*\n#\n:\n;\nhttp://example.com:80/' ..
|
||||
'\nRFC 123, ISBN 456'
|
||||
}
|
||||
},
|
||||
|
||||
-- nowiki tests cases taken from wfEscapeWikiText test cases in core
|
||||
{ name = 'nowiki noescapes',
|
||||
func = mw.text.nowiki,
|
||||
args = { 'a' },
|
||||
expect = {
|
||||
'a'
|
||||
}
|
||||
},
|
||||
{ name = 'nowiki braces and brackets',
|
||||
func = mw.text.nowiki,
|
||||
args = { '[[WikiLink]] {{Template}} <html>' },
|
||||
expect = {
|
||||
'[[WikiLink]] {{Template}} <html>'
|
||||
}
|
||||
},
|
||||
{ name = 'nowiki quotes',
|
||||
func = mw.text.nowiki,
|
||||
args = { '"' .. "'" },
|
||||
expect = { '"'' },
|
||||
},
|
||||
{ name = 'nowiki tokens',
|
||||
func = mw.text.nowiki,
|
||||
args = { '{| {- {+ !! ~~~~~ __FOO__' },
|
||||
expect = {
|
||||
'{| {- {+ !! ~~~~~ __FOO__'
|
||||
},
|
||||
},
|
||||
{ name = 'nowiki start of line',
|
||||
func = mw.text.nowiki,
|
||||
args = { '* foo\n! bar\n# bat\n:baz\n pre\n----' },
|
||||
expect = {
|
||||
'* foo\n! bar\n# bat\n:baz\n pre\n----',
|
||||
},
|
||||
},
|
||||
{ name = 'nowiki paragraph separators',
|
||||
func = mw.text.nowiki,
|
||||
args = { 'a\n\n\n\nb' },
|
||||
expect = { 'a\n \n b' },
|
||||
},
|
||||
{ name = 'nowiki language converter',
|
||||
func = mw.text.nowiki,
|
||||
args = { '-{ foo ; bar }-' },
|
||||
expect = { '-{ foo ; bar }-' },
|
||||
},
|
||||
{ name = 'nowiki left-side context: |+',
|
||||
func = mw.text.nowiki,
|
||||
args = { '+ foo + bar' },
|
||||
expect = { '+ foo + bar' },
|
||||
},
|
||||
{ name = 'nowiki left-side context: |-',
|
||||
func = mw.text.nowiki,
|
||||
args = { '- foo - bar' },
|
||||
expect = { '- foo - bar' },
|
||||
},
|
||||
{ name = 'nowiki left-side context: __FOO__',
|
||||
func = mw.text.nowiki,
|
||||
args = { '_FOO__' },
|
||||
expect = { '_FOO__' },
|
||||
},
|
||||
{ name = 'nowiki left-side context: ~~~',
|
||||
func = mw.text.nowiki,
|
||||
args = { '~~ long string here' },
|
||||
expect = { '~~ long string here' },
|
||||
},
|
||||
{ name = 'nowiki left-side context: newlines',
|
||||
func = mw.text.nowiki,
|
||||
args = { '\n\n\nFoo' },
|
||||
expect = { ' \n Foo' },
|
||||
},
|
||||
{ name = 'nowiki right-side context: ~~~',
|
||||
func = mw.text.nowiki,
|
||||
args = { 'long string here ~~' },
|
||||
expect = { 'long string here ~~' },
|
||||
},
|
||||
{ name = 'nowiki right-side context: __FOO__',
|
||||
func = mw.text.nowiki,
|
||||
args = { '__FOO_' },
|
||||
expect = { '__FOO_' },
|
||||
},
|
||||
{ name = 'nowiki right-side context: newlines',
|
||||
func = mw.text.nowiki,
|
||||
args = { 'foo\n\n\n' },
|
||||
expect = { 'foo\n ' },
|
||||
},
|
||||
|
||||
{ name = 'tag, simple',
|
||||
func = mw.text.tag,
|
||||
args = { { name = 'b' } },
|
||||
|
|
Loading…
Reference in a new issue