Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#3803 closed defect (fixed)

Coding error with for loop n dojo.widget.RichText

Reported by: guest Owned by: liucougar
Priority: high Milestone:
Component: Editor Version: 0.9
Keywords: for loop wrong usage Cc:
Blocked By: Blocking:


I noticed this bug while using the Editor2 widget programtically on an imported dom element that i retrieved using ajax.

The error is in dojo/widget/RichText.js near line: 203

The current code is:

var formats = ["p", "pre", "address", "h1", "h2", "h3", "h4", "h5", "h6", "ol", "div", "ul"];
var localhtml = "";
for (var i=0;i<formats.length;i++) {
	if (formats[i].charAt(1) != "l") {
		localhtml += "<" + formats[i] + "><span>content</span></" + formats[i] + ">";
	} else {
		localhtml += "<" + formats[i] + "><li>content</li></" + formats[i] + ">";

The problem is in the for(var i in formats) loop.

The for in loop does not iterate over all the elements in the array: It iterates over all the properties of the formats object. See the ecmascript spec:

The format array object also has a property formats.length. This property is a function object and does not have an charAt function.

But within the loop the if the code tries to execute the charAt function:

if (formats[i].charAt(1) != "l") {...}

This yields an error in MSIE 7.0, MSIE 6.0 and the latest Opera version... I am not sure if the opera version complained about the same bug, but i think so.

The workaround

The workaround is easy: use the normal for loop:

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

So it is important NOT to use for-in loops when iterating over arrays!!!!

Greetings Tjerk Wolterink tjerk@…

I hate to use the GUEST account! Mail me : i want an account.. please!

Change History (3)

comment:1 Changed 12 years ago by Adam Peller

note: this has already been fixed in 0.9, but it's an easy fix for 0.4.4

comment:2 Changed 12 years ago by liucougar

Resolution: fixed
Status: newclosed

(In [9751]) fixes #3803 (dijit is already fixed)

comment:3 Changed 11 years ago by (none)

Milestone: 0.4.4

Milestone 0.4.4 deleted

Note: See TracTickets for help on using tickets.