OSDN Git Service

Version 0.6.49, fix for xnode.attr(name).
[pettanr/clientJs.git] / 0.6.x / js / dom / 14_XDomAttr.js
index a9566b0..af1b6ac 100644 (file)
@@ -1,134 +1,4 @@
 X.Dom.Attr = {\r
-       toName  : [],\r
-       toIndex : {\r
-               id           : 0,\r
-               className    : 1,\r
-               'class'      : 1, //\r
-               name         : 2,\r
-               title        : 3,\r
-               accessKey    : 4,\r
-               accesskey    : 4, //\r
-               tabIndex     : 5,\r
-               tabindex     : 5, //\r
-               dir          : 6,\r
-               lang         : 7,\r
-               'xml:lang'   : 8,\r
-               'xml:space'  : 9,\r
-               abbr         : 10,\r
-               accept       : 11,\r
-               acceptCharset : 12,\r
-               'accept-charset' : 12,\r
-               action       : 13,\r
-               align        : 14,\r
-               alink        : 15,\r
-               alt          : 16,\r
-               archive      : 17,\r
-               axis         : 18,\r
-               background   : 19,\r
-               bgColor      : 20,\r
-               bgcolor      : 20, //\r
-               border       : 21,\r
-               cellPadding  : 22,\r
-               cellpadding  : 22, //\r
-               cellSpacing  : 23,\r
-               cellspacing  : 23, //\r
-               ch           : 24,\r
-               'char'       : 24, //\r
-               chOff        : 25,\r
-               charoff      : 25, //\r
-               charset      : 26,\r
-               checked      : 27,\r
-               cite         : 28,\r
-               classid      : 29,\r
-               clear        : 30,\r
-               code         : 31,\r
-               codeBase     : 32,\r
-               codebase     : 32, //\r
-               codeType     : 33,\r
-               codetype     : 33, //\r
-               color        : 34,\r
-               cols         : 35,\r
-               colspan      : 36,\r
-               colSpan      : 36,\r
-               compact      : 37,\r
-               content      : 38,\r
-               coords       : 39,\r
-               data         : 40,\r
-               dateTime     : 41,\r
-               datetime     : 41, //\r
-               declare      : 42,\r
-               defer        : 43,\r
-               disabled     : 44,\r
-               enctype      : 45,\r
-               face         : 46,\r
-               htmlFor      : 47,\r
-               'for'        : 47, //\r
-               frame        : 48,\r
-               frameBorder  : 49,\r
-               frameborder  : 49, //\r
-               headers      : 50,\r
-               height       : 51,\r
-               href         : 52,\r
-               hreflang     : 53,\r
-               hspace       : 54,\r
-               httpEquiv    : 55,\r
-               'http-equiv' : 55, //\r
-               ismap        : 56,\r
-               isMap        : 56,\r
-               label        : 57,\r
-               language     : 58,\r
-               longDesc     : 59,\r
-               longdesc     : 59, //\r
-               marginheight : 60,\r
-               marginwidth  : 61,\r
-               maxLength    : 62,\r
-               maxlength    : 62, //\r
-               media        : 63,\r
-               method       : 64,\r
-               multiple     : 65,\r
-               noHref       : 66,\r
-               nohref       : 66,\r
-               noresize     : 67,\r
-               noshade      : 68,\r
-               nowrap       : 69,\r
-               object       : 71,\r
-               profile      : 72,\r
-               prompt       : 73,\r
-               readOnly     : 74,\r
-               readonly     : 74,\r
-               rel          : 75,\r
-               rev          : 76,\r
-               rows         : 77,\r
-               rowspan      : 78,\r
-               rowSpan      : 78,\r
-               rules        : 79,\r
-               scheme       : 80,\r
-               scope        : 81,\r
-               scrolling    : 82,\r
-               selected     : 83,\r
-               shape        : 84,\r
-               size         : 85,\r
-               span         : 86,\r
-               src          : 87,\r
-               standby      : 88,\r
-               start        : 89,\r
-               summary      : 90,\r
-               target       : 91,\r
-               text         : 92,\r
-               type         : 93,\r
-               useMap       : 94,\r
-               usemap       : 94,\r
-               valign       : 95,\r
-               value        : 96,\r
-               valueType    : 97,\r
-               valuetype    : 97,\r
-               version      : 98,\r
-               vlink        : 99,\r
-               vspace       : 100,\r
-               width        : 101,\r
-               wrap         : 102,\r
-               xmlns        : 103\r
-       },\r
        noValue : {\r
                checked  : 1,\r
                compact  : 1,\r
@@ -171,27 +41,39 @@ X.Dom.Attr = {
                valuetype        : 'valueType',\r
                checked          : 'defaultChecked'\r
        },\r
+       \r
+       HAS_VALUE : {\r
+               INPUT    : true,\r
+               TEXTAREA : true,\r
+               SELECT   : true\r
+       },\r
+       \r
        renameForTag : {},\r
+       // http://nanto.asablo.jp/blog/2005/10/29/123294\r
+       // checked -> defaultChecked\r
+       // 動的に生成した input 要素を文書ツリーに挿入する前に設定した checked 属性は反映されず、defaultChecked だと反映される\r
+       // 先頭にスペース\r
        objToAttrText : function( obj ){\r
-               var attrs = [], noValue = X.Dom.Attr.noValue, p;\r
+               var noValue = X.Dom.Attr.noValue,\r
+                       attrs = [ '' ], n = 0, p, v;\r
+               if( !obj ) return ''; // Opera7\r
                for( p in obj ){\r
-                       attrs[ attrs.length ] = noValue[ p ] ? p : [ p, '="', obj[ p ], '"' ].join( '' );\r
+                       v = obj[ p ];\r
+                       if( p === 'value' ){\r
+                               v = v.split( '"' ).join( '&quot;' ).split( '>' ).join( '&gt;' ).split( '<' ).join( '&lt;' );\r
+                       };\r
+                       attrs[ ++n ] = noValue[ p ] ? p : [ p, '="', v, '"' ].join( '' );\r
                };\r
-               return attrs.join( ' ' );\r
+               return 0 < n ? attrs.join( ' ' ) : '';\r
        }\r
 };\r
 \r
-(function( toIndex, toName, renameForDOM, renameForTag ){\r
+(function( renameForDOM, renameForTag ){\r
        var name, i;\r
-       for( name in toIndex ){\r
-               if( typeof ( i = toIndex[ name ] ) === 'number' ){\r
-                       toName[ i ] = name;\r
-               };\r
-       };\r
        for( name in renameForDOM ){\r
                renameForTag[ renameForDOM[ name ] ] = name;\r
        };\r
-})( X.Dom.Attr.toIndex, X.Dom.Attr.toName, X.Dom.Attr.renameForDOM, X.Dom.Attr.renameForTag );\r
+})( X.Dom.Attr.renameForDOM, X.Dom.Attr.renameForTag );\r
 \r
 \r
 \r
@@ -204,68 +86,89 @@ X.Dom.Attr = {
  * \r
  */\r
 X.Dom.Node.prototype.attr = function( nameOrObj /* v */ ){\r
-       var attrs = this._attrs,\r
-               node, v, name;\r
+       var attrs = this._attrs, newAttrs, f, p, elm, v;\r
        \r
        if( this._xnodeType !== 1 ) return this;\r
        \r
        if( nameOrObj && X.Type.isObject( nameOrObj ) ){\r
-               if( !attrs ) attrs = this._attrs = {};\r
-               node = this._ie4getRawNode ? this._ie4getRawNode() : this._rawNode;\r
+               attrs || ( attrs = this._attrs = {} );\r
+               newAttrs = this._newAttrs || ( this._newAttrs = {} );\r
+               \r
                for( p in nameOrObj ){\r
-                       this.__attr( node, attrs, p, nameOrObj[ p ] );\r
+                       if( this._setAttr( attrs, newAttrs, p, nameOrObj[ p ] ) === true ) f = true;\r
+               };\r
+               if( f ){\r
+                       this._attrText = false;\r
+                       this._dirty |= X.Dom.Dirty.ATTR;\r
+                       this._root && this._reserveUpdate();                    \r
                };\r
-               delete this._attrText; // = X.Dom.Attr.objToAttrText( attrs );\r
-               this._attrUpdated = true;\r
                return this;\r
        } else\r
        if( 1 < arguments.length ){\r
                // setter\r
-               // this._attrText use when Node.create()\r
-               if( !attrs ) attrs = this._attrs = {};\r
-               this.__attr(\r
-                       this._ie4getRawNode ? ( this._rawNode || this._ie4getRawNode() ) : this._rawNode,\r
-                       attrs, nameOrObj, arguments[ 1 ]\r
-               );\r
-               delete this._attrText; // = X.Dom.Attr.objToAttrText( attrs );\r
-               this._attrUpdated = true;\r
+               if( this._setAttr( attrs || ( this._attrs = {} ), this._newAttrs || ( this._newAttrs = {} ), nameOrObj, arguments[ 1 ] ) === true ){\r
+                       this._attrText = false;\r
+                       this._dirty |= X.Dom.Dirty.ATTR;\r
+                       this._root && this._reserveUpdate();\r
+               };\r
                return this;\r
        } else\r
        if( typeof nameOrObj === 'string' ){\r
                // getter\r
-               if( !attrs ) return;\r
-               \r
-               return attrs[ v ];\r
+               switch( nameOrObj ){\r
+                       case 'id' :\r
+                               return this._tag;\r
+                       case 'class' :\r
+                       case 'className' :\r
+                               return this._className;\r
+                       case 'tag' :\r
+                       case 'tagName' :\r
+                               return this._tag;\r
+                       case 'style' :\r
+                       case 'cssText' :\r
+                               return this.cssText();\r
+                       case 'value' :\r
+                       case 'selectedIndex' :\r
+                               if( X.Dom.Attr.HAS_VALUE[ this._tag ] ){\r
+                                       if( this._newAttrs && X.inObject( nameOrObj, this._newAttrs ) ) return this._newAttrs[ nameOrObj ];\r
+                                       if( elm = X.Dom.DOM_IE4 ? this._rawNode || this._ie4getRawNode() : this._rawNode ){\r
+                                               attrs[ nameOrObj ] = elm[ nameOrObj ]; // getAttribute( nameOrObj )?\r
+                                       };\r
+                                       return attrs[ nameOrObj ];\r
+                               };\r
+                               break;\r
+               };\r
+               return attrs && attrs[ X.Dom.Attr.renameForTag[ nameOrObj ] || nameOrObj ];\r
        };\r
 };\r
-X.Dom.Node.prototype.__attr = function( node, attrs, name, v ){\r
-       name = X.Dom.Attr.renameForTag[ name ] || name;\r
+X.Dom.Node.prototype._setAttr = function( attrs, newAttrs, name, v ){\r
+       if( name === 'UID' ) return;\r
+       if( name === 'id' ){\r
+               v = ( v !== 'ie4uid' + this._uid ) ? v : undefined;\r
+               if( v !== this._id ){\r
+                       this._id = v;\r
+                       this._dirty |= X.Dom.Dirty.ID;\r
+                       this._root && this._reserveUpdate();\r
+               };\r
+               return;\r
+       };      \r
+       if( name === 'class' ) return this.className( v );      \r
+       if( name === 'style' ) return this.cssText( v );\r
        \r
-       if( attrs[ name ] === v ) return;\r
-       if( name === 'style' ){\r
-               return this.cssText( v );\r
-       };\r
        if( name.indexOf( 'on' ) === 0 ){\r
                X.Notification.warn( 'xnode.attr("' + name + '") is wrong, xnode.listen() & xnode.unlisten().' );\r
                return;\r
        };\r
-       // className\r
-       if( name === 'class' ) return this.className( v );\r
-       // update node\r
-       if( node ){\r
-               this._ie4getRawNode ?\r
-                       v ? node.setAttribute( name, v ) : node.removeAttribute( name ) : // val\r
-                       ( node[ X.Dom.Attr.renameForDOM[ name ] || name ] = v || undefined ); // val\r
-       };\r
-       // id\r
-       if( name === 'id' ){\r
-               this._id = v === 'ie4uid' + this._uid ? undefined : v || undefined;\r
-               return;\r
-       };\r
-       // update this._attrs\r
-       if( !v ){\r
-               delete attrs[ name ];\r
+       \r
+       name = X.Dom.Attr.renameForTag[ name ] || name;\r
+       if( attrs[ name ] === v ) return;\r
+       \r
+       if( v == null ){\r
+               newAttrs[ name ] = undefined;\r
+               if( attrs.hasOwnProperty( name ) ) delete attrs[ name ];\r
        } else {\r
-               attrs[ name ] = v;\r
+               newAttrs[ name ] = attrs[ name ] = v;\r
        };\r
-};
\ No newline at end of file
+       return true;\r
+};\r
+\r