wikimedia-gadgets / twinkle

The English Wikipedia twinkle javascript helper
http://en.wikipedia.org/wiki/Wikipedia:Twinkle
Other
135 stars 147 forks source link

afd not transcluded properly if title of page contains "$1" #1993

Open 41922 opened 1 month ago

41922 commented 1 month ago

or so i think? https://en.wikipedia.org/w/index.php?title=Wikipedia:Articles_for_deletion/Log/2024_August_22&diff=prev&oldid=1241631405

NovemLinguae commented 1 month ago

There's probably an a.replace( b, c ) somewhere. $&, $1, $2, $3, $4, etc. are special characters in the "c" field and should be escaped. The fix here is probably to run some kind of escape algorithm on "c".

nardog commented 1 month ago

You can use mw.util.escapeRegExp(). Nvm, the replace not the search string.

NovemLinguae commented 1 month ago

Stack Overflow says the proper way to escape this is to add a dollar sign in front of every special string. So $&, $1, $2, $3, $4 -> $$&, $$1, $$2, $$3, $$4

https://stackoverflow.com/a/28102904/3480193

Someone's probably written a bulletproof function for this somewhere. I imagine it'd be something like...

function escapeSecondParameterOfRegexReplace( secondParameter ) {
    return secondParameter.replace( /(\$(?:\d+|&))/g, '$$$1' );
}

https://regex101.com/r/7yjT4v/1

Todo:

edit: here's some more special ones...

https://es5.github.io/#x15.5.4.11

image
NovemLinguae commented 1 month ago

Ah here we go.

https://stackoverflow.com/a/6969486/3480193

function escapeReplacement(string) {
    return string.replace(/\$/g, '$$$$');
}
siddharthvp commented 1 month ago

We have that function in morebits: Morebits.string.safeReplace.