Wednesday, April 8, 2009

Defective method in prototype: cumulativeOffSet

Prototype is one of the most popular javasctipt libraries, nevertheless it has faulty methods.
Based on the documentation, cumulativeOffSet is supposed to return the real position of an element from the top left corner of the document, it works fine in most cases, but when a border is added to a parent element, the element is displaced to a new position which prototype is unable to consider. However, other javascript libraries like jQuery or Gwt returns the correct position.

I've opened a prototype ticket reporting this wrong behaviour, and I've submited a patch which fixes it:
cumulativeOffset patch


Normal nested div


$('div2').cumulativeOffset()[0] -> x

Adding a 10px border to the parent element, the nested div is moved 10px in the document


$('div2').cumulativeOffset()[0] -> x
This is wrong because the correct value is (x+10)