strange variable behavior inside a loop

I just noticed this, I’m of the opinion that it’s a bug in Freeway’s
JavaScript engine, but I’d like to know if anyone else has a good
explanation for it before I get all huffy and file a bug.

###This doesn’t work:

var thisItem = fwDocument.fwTags.fwFind(fwItem);
var inlines = thisItem.fwFindAll(['div','img']);
for (i in inlines){
	SetCSSAttribute(inlines[i],'float','left');
	SetCSSAttribute(inlines[i],'vertical-align','top');
	if(inline.fwFindEnclosing('p')){
		var p = inlines[i].fwFindEnclosing('p');
		var d = p.fwAddEnclosing('',false);
		var c = p.fwFindAllContent();
		d.fwMove(c);
		p.fwDelete();
	}
}

###This does, yet I believe they should both work

var thisItem = fwDocument.fwTags.fwFind(fwItem);
var inlines = thisItem.fwFindAll(['div','img']);
for (i in inlines){
	var inline = inlines[i];
	SetCSSAttribute(inline,'float','left');
	SetCSSAttribute(inline,'vertical-align','top');
	if(inline.fwFindEnclosing('p')){
		var p = inline.fwFindEnclosing('p');
		var d = p.fwAddEnclosing('',false);
		var c = p.fwFindAllContent();
		d.fwMove(c);
		p.fwDelete();
	}
}

Here is setCSSAttribute, just for reference. If I don’t use this
function, then the result is the same for both, so the problem must be
in here somewhere.

function SetCSSAttribute(tag, attributeName, attributeValue) {
	// Sets a "CSS" attribute such as "position:absolute" in a tag value
	// Passing an attribute value of null removes that attribute entirely
	if (tag==null) return;
	var tagField = tag['style'];
	if (tagField == null){
		tag['style'] = '"'+attributeName+':'+attributeValue+'"';
	}else{
		var tagField = tagField.toString();
		var pairs = tagField.slice(1,-1).split(';');
		var out = new Array();
		if(attributeValue != null && tagField.indexOf(attributeName) < 0)
			out.push(attributeName+':'+attributeValue);
		for(i in pairs){
			pairs[i] = pairs[i].replace(/^s*(S*(s+S+)*)s*$/, "$1");
			//javascript equivalent of trim
			if(pairs[i].slice(0,pairs[i].indexOf(':')) == attributeName) {
				if(attributeValue != null)
					out.push(attributeName+':'+attributeValue);
			}else{
				out.push(pairs[i]);
			}
		}
		tag['style'] = fwQuote(out.join('; '));
	}
}

Can anyone see why using the iterator directly should fail, while
assigning the iterator to a local variable works?

Walter


actionsdev mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

What do you mean by ‘fail’? An error or just erroneous output?

I ran into what seemed like a bug the other day: When trying to iterate over properties of an action, effectively for(var i in this), the routine would crash the app when getting to certain properties. I was able to isolate the offending properties and work around it, but had not tried your method above of assigning the property to a variable before testing against it.

First, I would narrow this down to make sure the problem is in the iterator or the values being passed into the ‘inlines’ array. What are the values of each of the iterations of ‘i’? Run an alert for each instance of inlines[i] to see that those results are as expected. And again for the inline = inlines[i].

In this case, wouldn’t a loop using the array length be better for(var i=0;i<inlines.length;i++)? in case there are actual properties being added to the ‘inlines’ object. (I know the simpler solution would be inlines[i], but that would fail if the boolean equivalent of the object was false)

I have a feeling that the array returned by fwFindAll is not exactly a true array, the same as ‘arguments’ is not a true array either; but both behave pretty much as such when manipulated.


actionsdev mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Hey, great to see your shining face around here again! And yes, I
should do that, since I should know better than to treat an object as
an array.

Walter

On Jul 27, 2010, at 9:05 AM, Weaver wrote:

In this case, wouldn’t a loop using the array length be better
for(var i=0;i<inlines.length;i++)? in case there are actual
properties being added to the ‘inlines’ object. (I know the simpler
solution would be inlines[i], but that would fail if the boolean
equivalent of the object was false)


actionsdev mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options