!! Version 2 # MediaWiki 'Regex Fun' extension Parser test cases # @Author: Daniel Werner < danweetz@web.de > # @since 1.1 !! functionhooks regex !! endfunctionhooks # Templates for tests: # since template:! is needed by other tests, but creating it twice will fail but not having it at all when # running these tests only fill fail as well, we use the 'parsertest.regexfun' prefix to avoid duplicates. !! article Template:parsertest.regexfun:! !!text | !! endarticle !! article Template:parsertest.regexfun:(( !!text {{ !! endarticle !! article Template:parsertest.regexfun:)) !!text }} !! endarticle ## ## #regex ## !! test Simple regular expression #regex !! wikitext "{{#regex:abc 20,30,40 | /\d+/ }}" !! html

"20"

!! end !! test Simple replacement with #regex !! wikitext "{{#regex:abc 20,30,40 | %\d% | # }}" !! html

"abc ##,##,##"

!! end !! test Replace something with empty string !! wikitext "{{#regex:abc 20,30,40 | %\d% | }}" !! html

"abc ,,"

!! end !! test Nothing to replace !! wikitext "{{#regex:abc 20,30,40 | !foo! | xx }}" !! html

"abc 20,30,40"

!! end !! test 'i' flag test !! wikitext "{{#regex:TeSt | /test/i }}" !! html

"TeSt"

!! end !! test Safety test, script tag injection !! wikitext "{{#regex:| /.+/ }}" !! html

"<script>alert("foo")</script>"

!! end !! test Special chars !! wikitext "{{#regex: foo{{parsertest.regexfun:!}}oo | /(o.o)/ | $1 }}", "{{#regex_var: $1 }}" !! html

"foo|oo", "o|o"

!! end ## ## 'r' flag test ## !! test #regex test with 'r' flag where it should have no effect !! wikitext "{{#regex:abc 20,30,40 | /\d+/r }}" !! html

"20"

!! end !! test #regex with 'r' flag but no match should result into empty string instead of initial string in argument 1 !! wikitext "{{#regex:abc 20,30,40 | &foo&r | xx }}" !! html

""

!! end ## ## 'e' flag test ## !! test Safety test 'e' flag. This should always be filtered for not running into its php meaning! !! wikitext "{{#regex: text | /^.+ $/xe | $GLOBALS['wgDBuser'] }}" !! html

"$GLOBALS['wgDBuser']"

!! end !! article Template:parsertest.regexfun:11 !!text ELEVEN{{{1|}}} !! endarticle !! article Template:parsertest.regexfun:12 !!text TWELVE !! endarticle !! test 'e' modifier and new feature in 1.1, transform parameter doesn't have to be escaped !! wikitext "{{ #regex: 11 and 12 | /\d+/ier | {{parsertest.regexfun:$0|$0||}}\$0 }}" !! html

"ELEVEN11$0 and TWELVE$0"

!! end !! test 'e' does not work to abuse #regex for parsing its input. Only replacement parameter's wikitext will be parsed, not the text within the backrefs. !! wikitext "{{ #regex: {{parsertest.regexfun:((}}parsertest.regexfun:11{{parsertest.regexfun:!}}_foo{{parsertest.regexfun:))}} | /$.+^/e | $0 }}" !! html

"{{parsertest.regexfun:11|_foo}}"

!! end ## ## #regex_var ## !! test #regex_var after #regex with replacement !! wikitext "{{ #regex:Numbers: 1, 2, 3, 4 | /^\s*.+:\s*(\d+)\s*\,\s*(\d+)\s*\,\s*(\d+),\s*(\d+)\s*$/r | }}{{ #regex_var:1}},{{ #regex_var:2|foo}},{{ #regex_var:3|foo}},{{ #regex_var:4|foo}},{{ #regex_var:5}}" !! html

"1,2,3,4,"

!! end !! test Test without replacement in '#regex' before !! wikitext "{{ #regex: text | /(.+)/ }}_{{ #regex_var:0}}_{{ #regex_var:1}}" !! html

"text_text_text"

!! end !! test #regex_var after #regex with 'e' flag !! wikitext "{{ #regex:Numbers: 10, 20, 30 | /^\s*.+:\s*(\d+)\s*\,\s*(\d+)\s*\,\s*(\d+)\s*$/e | }}{{ #regex_var:1}},{{ #regex_var:2|foo}},{{ #regex_var:3|foo}},{{ #regex_var:4|foo}},{{ #regex_var:5}}" !! html

"10,20,30,foo,"

!! end !! test #regex_var again !! wikitext "{{ #regex:Numbers: 10, 20, 30 | /^\s*.+:\s*(\d+)\s*\,\s*(\d+)\s*\,\s*(\d+)\s*$/e | }}{{#regex_var:$1 + $2 = $3, right?}}" !! html

"10 + 20 = 30, right?"

!! end !! test #regex_var with old-style backrefs !! wikitext "{{ #regex:Numbers: 10, 20, 30 | /^\s*.+:\s*(\d+)\s*\,\s*(\d+)\s*\,\s*(\d+)\s*$/e | }}{{#regex_var:\1 + \2 = \3, right?}}" !! html

"10 + 20 = 30, right?"

!! end !! test #regex_var with $0 !! wikitext "{{ #regex:Numbers: 10, 20, 30 | /^\s*.+:\s*(\d+)\s*\,\s*(\d+)\s*\,\s*(\d+)\s*$/e | }}{{#regex_var:'$0' or '\0'?}}" !! html

"'Numbers: 10, 20, 30' or 'Numbers: 10, 20, 30'?"

!! end !! test #regex_var after invalid regex. Without/with default value. !! wikitext "{{ #regex:Numbers: 10, 20, 30 | /^\s*.+:\s*(\d+)\s*\,\s*(\d+)\s*\,\s*(\d+)\s*$/e | }}{{#if:{{#regex:}}}}{{ #regex_var:$0}}{{ #regex_var:_$0_}}", "{{#regex_var:$0|dflt1}}{{ #regex_var:_$0_|dflt2}}" !! html

"", "dflt1dflt2"

!! end !! article Template:parsertest.regexfun:regex_var-scope !!text {{#regex: {{{1}}} | {{{2}}} }} !! endarticle !! test Test of var scope, using a regex, then template call with regex inside, then 'regex_var' to get first regex value. Works in 1.1+ !! wikitext "{{#regex: 123 | /(.+)/ }}", "{{parsertest.regexfun:regex_var-scope | ABC | /(.+)/ }}", "{{#regex_var:$1}}" !! html

"123", "ABC", "123"

!! end ## ## #regexall ## !! test #regexall !! wikitext "{{#regexall:100,200,300 | /\d+/ }}" !! html

"100, 200, 300"

!! end !! test #regexall, sep '#', offset '1' !! wikitext "{{#regexall: 00,11,22,33 | /\d+/ | # | 1 }}" !! html

"11#22#33"

!! end !! test #regexall, sep empty, offset '1', limit '2' !! wikitext "{{#regexall: 00,11,22,33 | /\d+/ | | 1 | 2 }}" !! html

"1122"

!! end ## ## #regexquote ## !! test Input text escaped with #regexquote for use within regex, delimiter / by default !! wikitext "{{#regexquote: \#;239"%&§$,:.?// }}" !! html

"\\#;239"%&§\$,\:\.\?\/\/"

!! end !! test #regexquote parameter 2 test, delimiter '&' !! wikitext "{{#regexquote: \#;239"%&§$,:.?// | & }}" !! html

"\\#;239"%\&§\$,\:\.\?//"

!! end !! test #regexquote, wiki list character '#' as first one !! wikitext "{{#regexquote: #text }}" !! html

"\x23text"

!! end !! test #regexquote, wiki list character ';' as first one !! wikitext "{{#regexquote: ;text }}" !! html

"\x3btext"

!! end ## ## provoked errors, test with ParserFunction's #iferror ## !! functionhooks iferror !! endfunctionhooks !! test #regex with #iferror, no delimiter !! wikitext "{{#iferror: {{#regex:abc 20,30,40 }} | error | no-error }}" !! html

"error"

!! end !! test #regex with #iferror, invalid delimiter !! wikitext "{{#iferror: {{#regex:abc 20,30,40 | \d+ }} | error | no-error }}" !! html

"error"

!! end ## ## together with 'arrays' extension ## !! functionhooks arrayprint len !! endfunctionhooks !! test Using Arrays, using 'Regex Fun' externally, checking whether '#regex_var' will still work as expected. !! wikitext "{{ #regex: abcd | /.+/ }}", "{{ #arraydefine: b | 55555, 333, 4444, 1, 22, 999999999, 88888888, 4444 }}{{ #arraysearcharray: b | b | /\d+/e | 0 | -1 | {{#len:$0}} }}{{ #arrayprint: b }}", "{{ #arrayindex: b | 0 }}", "{{ #regex_var: $0 }}" !! html

"abcd", "5, 3, 4, 1, 2, 9, 8, 4", "5", "abcd"

!! end !! test Same as previous but with replace in '#regex' !! wikitext "{{ #regex: abcd | /(.+)/e | {{#len:$0}} }}", "{{ #arraydefine: b | 55555, 333, 4444, 1, 22, 999999999, 88888888, 4444 }}{{ #arraysearcharray: b | b | /\d+/e | 0 | -1 | {{#len:$0}} }}{{ #arrayprint: b }}", "{{ #arrayindex: b | 0 }}", "{{ #regex_var: $0+$1 }}" !! html

"4", "5, 3, 4, 1, 2, 9, 8, 4", "5", "abcd+abcd"

!! end