Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7223

Show
Ignore:
Timestamp:
07/24/07 17:41:27 (1 year ago)
Author:
sam
Message:

prototype: Add Element#identify, which returns the element's ID if it exists, or sets and returns a unique, auto-generated ID (of the form 'anonymous_element_' + auto-incremented digit) otherwise. Use this when you need to ensure an element has an ID. Closes #9012.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spinoffs/prototype/trunk/CHANGELOG

    r7222 r7223  
    11*SVN* 
     2 
     3* Add Element#identify, which returns the element's ID if it exists, or sets and returns a unique, auto-generated ID (of the form "anonymous_element_" + auto-incremented digit) otherwise.  Use this when you need to ensure an element has an ID.  Closes #9012.  [Jeff Watkins, sam, Tobie Langel] 
    24 
    35* Make Element#readAttribute work for cloned elements in IE. Closes #8481. [chem, Tobie Langel] 
  • spinoffs/prototype/trunk/src/dom.js

    r7222 r7223  
    232232    var args = $A(arguments), element = $(args.shift()); 
    233233    return Selector.findChildElements(element.parentNode, args).without(element); 
     234  }, 
     235   
     236  identify: function(element) { 
     237    element = $(element); 
     238    var id = element.readAttribute('id'), self = arguments.callee; 
     239    if (id) return id; 
     240    do { id = 'anonymous_element_' + self.counter++ } while ($(id)); 
     241    element.writeAttribute('id', id); 
     242    return id; 
    234243  }, 
    235244   
     
    598607}; 
    599608 
    600 Element.Methods.getElementsBySelector = Element.Methods.select
     609Element.Methods.identify.counter = 1
    601610 
    602611if (!document.getElementsByClassName) document.getElementsByClassName = function(instanceMethods){ 
     
    637646 
    638647Object.extend(Element.Methods, { 
     648  getElementsBySelector: Element.Methods.select, 
    639649  childElements: Element.Methods.immediateDescendants 
    640650}); 
  • spinoffs/prototype/trunk/test/unit/dom.html

    r7222 r7223  
    343343 
    344344<div id="unextended"></div> 
     345<div id="identification"> 
     346  <div id="predefined_id"></div> 
     347  <div></div> 
     348  <div></div> 
     349  <div></div> 
     350  <div id="anonymous_element_3"></div> 
     351</div> 
    345352 
    346353<!-- Tests follow --> 
     
    677684      }); 
    678685    }}, 
     686     
     687    testElementIdentify: function() {with(this) { 
     688      var parent = $('identification'); 
     689      assertEqual(parent.down().identify(), 'predefined_id'); 
     690      assertEqual(parent.down(1).identify(), 'anonymous_element_1'); 
     691      assertEqual(parent.down(2).identify(), 'anonymous_element_2'); 
     692      assertEqual(parent.down(3).identify(), 'anonymous_element_4'); 
     693    }}, 
    679694        
    680695    testElementClassNameMethod: function() {with(this) {