Page MenuHomePhabricator

Improper javascript array iteration causes problems
Closed, ResolvedPublic

Description

In wikibits.js on line 367 or so, there are two for loops that use the for..in
mechanism to loop through arrays. Since these are arrays and not objects they
should be looped through using plain for loops. Using for..in causes problems
when you include javascript libraries like older versions of prototype that mess
with Object.prototype.

To see the fun bugs it causes, try including prototype in your skin and look at
the edit toolbar.

To fix this, change:

for(i in mwEditButtons) {

to:

for (var i = 0; i < mwEditButtons.length; i++) {

and:

for(i in mwCustomEditButtons) {

to:

for (var i = 0; i < mwCustomEditButtons.length; i++) {

For further information about properly iterating arrays in javascript see:
http://simon.incutio.com/slides/2006/etech/javascript/js-reintroduction-notes.html#arrays


Version: 1.8.x
Severity: minor

Details

Reference
bz6684

Event Timeline

bzimport raised the priority of this task from to Low.Nov 21 2014, 9:20 PM
bzimport added a project: MediaWiki-Parser.
bzimport set Reference to bz6684.
bzimport added a subscriber: Unknown Object (MLST).

dto wrote:

Just wanted to say that I had to do the same, with the latest version of prototype.

By the way, this isn't because prototype messes with Object, but because it
messes with Array. (For example, the for...in loop on line 470 works fine
because ta is an Object, not an Array.)

dto wrote:

Patch for wikibits.js fixing protoype incompatibility

Not sure how things work around here...is a diff easier to commit than
scrolling to line 369?

Attached:

ayg wrote:

Attached patches should generally be in unified diff format. If a dev would
prefer to just manually scroll to the right lines, they can do that too.

dto wrote:

Attached patches should generally be in unified diff format.

Was my attachment in the right format?

Thanks for the patch Dan.

Applied in r19744.