Opened 9 years ago

Closed 9 years ago

#12923 closed defect (fixed)

Strange do {}while(false) in dojo.attr()

Reported by: Richard Backhouse Owned by: Eugene Lazutkin
Priority: high Milestone: 1.7
Component: Core Version: 1.6.1
Keywords: Cc:
Blocked By: Blocking:

Description

dojo.attr() contains the following code :

if(args == 3){
	// setter
	do{
		if(propName == "style" && typeof value != "string"){ // inline'd type check
			// special case: setting a style
			d.style(node, value);
			break;
		}
		if(propName == "innerHTML"){
			// special case: assigning HTML
			//>>excludeStart("webkitMobile", kwArgs.webkitMobile);
			if(d.isIE && node.tagName.toLowerCase() in _roInnerHtml){
				d.empty(node);
				node.appendChild(d._toDom(value, node.ownerDocument));
			}else{
			//>>excludeEnd("webkitMobile");
				node[propName] = value;
			//>>excludeStart("webkitMobile", kwArgs.webkitMobile);
			}
			//>>excludeEnd("webkitMobile");
			break;
		}
		if(d.isFunction(value)){
			// special case: assigning an event handler
			// clobber if we can
			var attrId = d.attr(node, _attrId);
			if(!attrId){
				attrId = _ctr++;
				d.attr(node, _attrId, attrId);
			}
			if(!_evtHdlrMap[attrId]){
				_evtHdlrMap[attrId] = {};
			}
			var h = _evtHdlrMap[attrId][propName];
			if(h){
				d.disconnect(h);
			}else{
				try{
					delete node[propName];
				}catch(e){}
			}
			// ensure that event objects are normalized, etc.
			_evtHdlrMap[attrId][propName] = d.connect(node, propName, value);
			break;
		}
		if(forceProp || typeof value == "boolean"){
			// special case: forcing assignment to the property
			// special case: setting boolean to a property instead of attribute
			node[propName] = value;
			break;
		}
		// node's attribute
		node.setAttribute(attrName, value);
	}while(false);
	return node; // DomNode
}

It looks like the do/while is unnecessary and the code could be replaced with

if(args == 3){
	// setter
	if(propName == "style" && typeof value != "string"){ // inline'd type check
		// special case: setting a style
		d.style(node, value);
		return node;
	}
	if(propName == "innerHTML"){
		// special case: assigning HTML
		//>>excludeStart("webkitMobile", kwArgs.webkitMobile);
		if(d.isIE && node.tagName.toLowerCase() in _roInnerHtml){
			d.empty(node);
			node.appendChild(d._toDom(value, node.ownerDocument));
		}else{
		//>>excludeEnd("webkitMobile");
			node[propName] = value;
		//>>excludeStart("webkitMobile", kwArgs.webkitMobile);
		}
		//>>excludeEnd("webkitMobile");
		return node;
	}
	if(d.isFunction(value)){
		// special case: assigning an event handler
		// clobber if we can
		var attrId = d.attr(node, _attrId);
		if(!attrId){
			attrId = _ctr++;
			d.attr(node, _attrId, attrId);
		}
		if(!_evtHdlrMap[attrId]){
			_evtHdlrMap[attrId] = {};
		}
		var h = _evtHdlrMap[attrId][propName];
		if(h){
			d.disconnect(h);
		}else{
			try{
				delete node[propName];
			}catch(e){}
		}
		// ensure that event objects are normalized, etc.
		_evtHdlrMap[attrId][propName] = d.connect(node, propName, value);
		return node;
	}
	if(forceProp || typeof value == "boolean"){
		// special case: forcing assignment to the property
		// special case: setting boolean to a property instead of attribute
		node[propName] = value;
		return node;
	}
	// node's attribute
	node.setAttribute(attrName, value);
	return node; // DomNode
}

Change History (2)

comment:1 Changed 9 years ago by Chris Mitchell

Component: GeneralCore
Milestone: tbd1.7
Owner: set to Eugene Lazutkin

comment:2 Changed 9 years ago by Eugene Lazutkin

Resolution: fixed
Status: newclosed

(In [24991]) html: simplifying code in dojo.attr(), thx rbackhouse!, !strict, fixes #12923.

Note: See TracTickets for help on using tickets.