dom.js 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  1. /*
  2. Copyright (c) 2009, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.net/yui/license.txt
  5. version: 2.8.0r4
  6. */
  7. /**
  8. * The dom module provides helper methods for manipulating Dom elements.
  9. * @module dom
  10. *
  11. */
  12. (function() {
  13. // for use with generateId (global to save state if Dom is overwritten)
  14. YAHOO.env._id_counter = YAHOO.env._id_counter || 0;
  15. // internal shorthand
  16. var Y = YAHOO.util,
  17. lang = YAHOO.lang,
  18. UA = YAHOO.env.ua,
  19. trim = YAHOO.lang.trim,
  20. propertyCache = {}, // for faster hyphen converts
  21. reCache = {}, // cache className regexes
  22. RE_TABLE = /^t(?:able|d|h)$/i, // for _calcBorders
  23. RE_COLOR = /color$/i,
  24. // DOM aliases
  25. document = window.document,
  26. documentElement = document.documentElement,
  27. // string constants
  28. OWNER_DOCUMENT = 'ownerDocument',
  29. DEFAULT_VIEW = 'defaultView',
  30. DOCUMENT_ELEMENT = 'documentElement',
  31. COMPAT_MODE = 'compatMode',
  32. OFFSET_LEFT = 'offsetLeft',
  33. OFFSET_TOP = 'offsetTop',
  34. OFFSET_PARENT = 'offsetParent',
  35. PARENT_NODE = 'parentNode',
  36. NODE_TYPE = 'nodeType',
  37. TAG_NAME = 'tagName',
  38. SCROLL_LEFT = 'scrollLeft',
  39. SCROLL_TOP = 'scrollTop',
  40. GET_BOUNDING_CLIENT_RECT = 'getBoundingClientRect',
  41. GET_COMPUTED_STYLE = 'getComputedStyle',
  42. CURRENT_STYLE = 'currentStyle',
  43. CSS1_COMPAT = 'CSS1Compat',
  44. _BACK_COMPAT = 'BackCompat',
  45. _CLASS = 'class', // underscore due to reserved word
  46. CLASS_NAME = 'className',
  47. EMPTY = '',
  48. SPACE = ' ',
  49. C_START = '(?:^|\\s)',
  50. C_END = '(?= |$)',
  51. G = 'g',
  52. POSITION = 'position',
  53. FIXED = 'fixed',
  54. RELATIVE = 'relative',
  55. LEFT = 'left',
  56. TOP = 'top',
  57. MEDIUM = 'medium',
  58. BORDER_LEFT_WIDTH = 'borderLeftWidth',
  59. BORDER_TOP_WIDTH = 'borderTopWidth',
  60. // brower detection
  61. isOpera = UA.opera,
  62. isSafari = UA.webkit,
  63. isGecko = UA.gecko,
  64. isIE = UA.ie;
  65. /**
  66. * Provides helper methods for DOM elements.
  67. * @namespace YAHOO.util
  68. * @class Dom
  69. * @requires yahoo, event
  70. */
  71. Y.Dom = {
  72. CUSTOM_ATTRIBUTES: (!documentElement.hasAttribute) ? { // IE < 8
  73. 'for': 'htmlFor',
  74. 'class': CLASS_NAME
  75. } : { // w3c
  76. 'htmlFor': 'for',
  77. 'className': _CLASS
  78. },
  79. DOT_ATTRIBUTES: {},
  80. /**
  81. * Returns an HTMLElement reference.
  82. * @method get
  83. * @param {String | HTMLElement |Array} el Accepts a string to use as an ID for getting a DOM reference, an actual DOM reference, or an Array of IDs and/or HTMLElements.
  84. * @return {HTMLElement | Array} A DOM reference to an HTML element or an array of HTMLElements.
  85. */
  86. get: function(el) {
  87. var id, nodes, c, i, len, attr;
  88. if (el) {
  89. if (el[NODE_TYPE] || el.item) { // Node, or NodeList
  90. return el;
  91. }
  92. if (typeof el === 'string') { // id
  93. id = el;
  94. el = document.getElementById(el);
  95. attr = (el) ? el.attributes : null;
  96. if (el && attr && attr.id && attr.id.value === id) { // IE: avoid false match on "name" attribute
  97. return el;
  98. } else if (el && document.all) { // filter by name
  99. el = null;
  100. nodes = document.all[id];
  101. for (i = 0, len = nodes.length; i < len; ++i) {
  102. if (nodes[i].id === id) {
  103. return nodes[i];
  104. }
  105. }
  106. }
  107. return el;
  108. }
  109. if (YAHOO.util.Element && el instanceof YAHOO.util.Element) {
  110. el = el.get('element');
  111. }
  112. if ('length' in el) { // array-like
  113. c = [];
  114. for (i = 0, len = el.length; i < len; ++i) {
  115. c[c.length] = Y.Dom.get(el[i]);
  116. }
  117. return c;
  118. }
  119. return el; // some other object, just pass it back
  120. }
  121. return null;
  122. },
  123. getComputedStyle: function(el, property) {
  124. if (window[GET_COMPUTED_STYLE]) {
  125. return el[OWNER_DOCUMENT][DEFAULT_VIEW][GET_COMPUTED_STYLE](el, null)[property];
  126. } else if (el[CURRENT_STYLE]) {
  127. return Y.Dom.IE_ComputedStyle.get(el, property);
  128. }
  129. },
  130. /**
  131. * Normalizes currentStyle and ComputedStyle.
  132. * @method getStyle
  133. * @param {String | HTMLElement |Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
  134. * @param {String} property The style property whose value is returned.
  135. * @return {String | Array} The current value of the style property for the element(s).
  136. */
  137. getStyle: function(el, property) {
  138. return Y.Dom.batch(el, Y.Dom._getStyle, property);
  139. },
  140. // branching at load instead of runtime
  141. _getStyle: function() {
  142. if (window[GET_COMPUTED_STYLE]) { // W3C DOM method
  143. return function(el, property) {
  144. property = (property === 'float') ? property = 'cssFloat' :
  145. Y.Dom._toCamel(property);
  146. var value = el.style[property],
  147. computed;
  148. if (!value) {
  149. computed = el[OWNER_DOCUMENT][DEFAULT_VIEW][GET_COMPUTED_STYLE](el, null);
  150. if (computed) { // test computed before touching for safari
  151. value = computed[property];
  152. }
  153. }
  154. return value;
  155. };
  156. } else if (documentElement[CURRENT_STYLE]) {
  157. return function(el, property) {
  158. var value;
  159. switch(property) {
  160. case 'opacity' :// IE opacity uses filter
  161. value = 100;
  162. try { // will error if no DXImageTransform
  163. value = el.filters['DXImageTransform.Microsoft.Alpha'].opacity;
  164. } catch(e) {
  165. try { // make sure its in the document
  166. value = el.filters('alpha').opacity;
  167. } catch(err) {
  168. }
  169. }
  170. return value / 100;
  171. case 'float': // fix reserved word
  172. property = 'styleFloat'; // fall through
  173. default:
  174. property = Y.Dom._toCamel(property);
  175. value = el[CURRENT_STYLE] ? el[CURRENT_STYLE][property] : null;
  176. return ( el.style[property] || value );
  177. }
  178. };
  179. }
  180. }(),
  181. /**
  182. * Wrapper for setting style properties of HTMLElements. Normalizes "opacity" across modern browsers.
  183. * @method setStyle
  184. * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
  185. * @param {String} property The style property to be set.
  186. * @param {String} val The value to apply to the given property.
  187. */
  188. setStyle: function(el, property, val) {
  189. Y.Dom.batch(el, Y.Dom._setStyle, { prop: property, val: val });
  190. },
  191. _setStyle: function() {
  192. if (isIE) {
  193. return function(el, args) {
  194. var property = Y.Dom._toCamel(args.prop),
  195. val = args.val;
  196. if (el) {
  197. switch (property) {
  198. case 'opacity':
  199. if ( lang.isString(el.style.filter) ) { // in case not appended
  200. el.style.filter = 'alpha(opacity=' + val * 100 + ')';
  201. if (!el[CURRENT_STYLE] || !el[CURRENT_STYLE].hasLayout) {
  202. el.style.zoom = 1; // when no layout or cant tell
  203. }
  204. }
  205. break;
  206. case 'float':
  207. property = 'styleFloat';
  208. default:
  209. el.style[property] = val;
  210. }
  211. } else {
  212. }
  213. };
  214. } else {
  215. return function(el, args) {
  216. var property = Y.Dom._toCamel(args.prop),
  217. val = args.val;
  218. if (el) {
  219. if (property == 'float') {
  220. property = 'cssFloat';
  221. }
  222. el.style[property] = val;
  223. } else {
  224. }
  225. };
  226. }
  227. }(),
  228. /**
  229. * Gets the current position of an element based on page coordinates.
  230. * Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
  231. * @method getXY
  232. * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM
  233. * reference, or an Array of IDs and/or HTMLElements
  234. * @return {Array} The XY position of the element(s)
  235. */
  236. getXY: function(el) {
  237. return Y.Dom.batch(el, Y.Dom._getXY);
  238. },
  239. _canPosition: function(el) {
  240. return ( Y.Dom._getStyle(el, 'display') !== 'none' && Y.Dom._inDoc(el) );
  241. },
  242. _getXY: function() {
  243. if (document[DOCUMENT_ELEMENT][GET_BOUNDING_CLIENT_RECT]) {
  244. return function(node) {
  245. var scrollLeft, scrollTop, box, doc,
  246. off1, off2, mode, bLeft, bTop,
  247. floor = Math.floor, // TODO: round?
  248. xy = false;
  249. if (Y.Dom._canPosition(node)) {
  250. box = node[GET_BOUNDING_CLIENT_RECT]();
  251. doc = node[OWNER_DOCUMENT];
  252. scrollLeft = Y.Dom.getDocumentScrollLeft(doc);
  253. scrollTop = Y.Dom.getDocumentScrollTop(doc);
  254. xy = [floor(box[LEFT]), floor(box[TOP])];
  255. if (isIE && UA.ie < 8) { // IE < 8: viewport off by 2
  256. off1 = 2;
  257. off2 = 2;
  258. mode = doc[COMPAT_MODE];
  259. if (UA.ie === 6) {
  260. if (mode !== _BACK_COMPAT) {
  261. off1 = 0;
  262. off2 = 0;
  263. }
  264. }
  265. if ((mode === _BACK_COMPAT)) {
  266. bLeft = _getComputedStyle(doc[DOCUMENT_ELEMENT], BORDER_LEFT_WIDTH);
  267. bTop = _getComputedStyle(doc[DOCUMENT_ELEMENT], BORDER_TOP_WIDTH);
  268. if (bLeft !== MEDIUM) {
  269. off1 = parseInt(bLeft, 10);
  270. }
  271. if (bTop !== MEDIUM) {
  272. off2 = parseInt(bTop, 10);
  273. }
  274. }
  275. xy[0] -= off1;
  276. xy[1] -= off2;
  277. }
  278. if ((scrollTop || scrollLeft)) {
  279. xy[0] += scrollLeft;
  280. xy[1] += scrollTop;
  281. }
  282. // gecko may return sub-pixel (non-int) values
  283. xy[0] = floor(xy[0]);
  284. xy[1] = floor(xy[1]);
  285. } else {
  286. }
  287. return xy;
  288. };
  289. } else {
  290. return function(node) { // ff2, safari: manually calculate by crawling up offsetParents
  291. var docScrollLeft, docScrollTop,
  292. scrollTop, scrollLeft,
  293. bCheck,
  294. xy = false,
  295. parentNode = node;
  296. if (Y.Dom._canPosition(node) ) {
  297. xy = [node[OFFSET_LEFT], node[OFFSET_TOP]];
  298. docScrollLeft = Y.Dom.getDocumentScrollLeft(node[OWNER_DOCUMENT]);
  299. docScrollTop = Y.Dom.getDocumentScrollTop(node[OWNER_DOCUMENT]);
  300. // TODO: refactor with !! or just falsey
  301. bCheck = ((isGecko || UA.webkit > 519) ? true : false);
  302. // TODO: worth refactoring for TOP/LEFT only?
  303. while ((parentNode = parentNode[OFFSET_PARENT])) {
  304. xy[0] += parentNode[OFFSET_LEFT];
  305. xy[1] += parentNode[OFFSET_TOP];
  306. if (bCheck) {
  307. xy = Y.Dom._calcBorders(parentNode, xy);
  308. }
  309. }
  310. // account for any scrolled ancestors
  311. if (Y.Dom._getStyle(node, POSITION) !== FIXED) {
  312. parentNode = node;
  313. while ((parentNode = parentNode[PARENT_NODE]) && parentNode[TAG_NAME]) {
  314. scrollTop = parentNode[SCROLL_TOP];
  315. scrollLeft = parentNode[SCROLL_LEFT];
  316. //Firefox does something funky with borders when overflow is not visible.
  317. if (isGecko && (Y.Dom._getStyle(parentNode, 'overflow') !== 'visible')) {
  318. xy = Y.Dom._calcBorders(parentNode, xy);
  319. }
  320. if (scrollTop || scrollLeft) {
  321. xy[0] -= scrollLeft;
  322. xy[1] -= scrollTop;
  323. }
  324. }
  325. xy[0] += docScrollLeft;
  326. xy[1] += docScrollTop;
  327. } else {
  328. //Fix FIXED position -- add scrollbars
  329. if (isOpera) {
  330. xy[0] -= docScrollLeft;
  331. xy[1] -= docScrollTop;
  332. } else if (isSafari || isGecko) {
  333. xy[0] += docScrollLeft;
  334. xy[1] += docScrollTop;
  335. }
  336. }
  337. //Round the numbers so we get sane data back
  338. xy[0] = Math.floor(xy[0]);
  339. xy[1] = Math.floor(xy[1]);
  340. } else {
  341. }
  342. return xy;
  343. };
  344. }
  345. }(), // NOTE: Executing for loadtime branching
  346. /**
  347. * Gets the current X position of an element based on page coordinates. The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
  348. * @method getX
  349. * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
  350. * @return {Number | Array} The X position of the element(s)
  351. */
  352. getX: function(el) {
  353. var f = function(el) {
  354. return Y.Dom.getXY(el)[0];
  355. };
  356. return Y.Dom.batch(el, f, Y.Dom, true);
  357. },
  358. /**
  359. * Gets the current Y position of an element based on page coordinates. Element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
  360. * @method getY
  361. * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
  362. * @return {Number | Array} The Y position of the element(s)
  363. */
  364. getY: function(el) {
  365. var f = function(el) {
  366. return Y.Dom.getXY(el)[1];
  367. };
  368. return Y.Dom.batch(el, f, Y.Dom, true);
  369. },
  370. /**
  371. * Set the position of an html element in page coordinates, regardless of how the element is positioned.
  372. * The element(s) must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
  373. * @method setXY
  374. * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements
  375. * @param {Array} pos Contains X & Y values for new position (coordinates are page-based)
  376. * @param {Boolean} noRetry By default we try and set the position a second time if the first fails
  377. */
  378. setXY: function(el, pos, noRetry) {
  379. Y.Dom.batch(el, Y.Dom._setXY, { pos: pos, noRetry: noRetry });
  380. },
  381. _setXY: function(node, args) {
  382. var pos = Y.Dom._getStyle(node, POSITION),
  383. setStyle = Y.Dom.setStyle,
  384. xy = args.pos,
  385. noRetry = args.noRetry,
  386. delta = [ // assuming pixels; if not we will have to retry
  387. parseInt( Y.Dom.getComputedStyle(node, LEFT), 10 ),
  388. parseInt( Y.Dom.getComputedStyle(node, TOP), 10 )
  389. ],
  390. currentXY,
  391. newXY;
  392. if (pos == 'static') { // default to relative
  393. pos = RELATIVE;
  394. setStyle(node, POSITION, pos);
  395. }
  396. currentXY = Y.Dom._getXY(node);
  397. if (!xy || currentXY === false) { // has to be part of doc to have xy
  398. return false;
  399. }
  400. if ( isNaN(delta[0]) ) {// in case of 'auto'
  401. delta[0] = (pos == RELATIVE) ? 0 : node[OFFSET_LEFT];
  402. }
  403. if ( isNaN(delta[1]) ) { // in case of 'auto'
  404. delta[1] = (pos == RELATIVE) ? 0 : node[OFFSET_TOP];
  405. }
  406. if (xy[0] !== null) { // from setX
  407. setStyle(node, LEFT, xy[0] - currentXY[0] + delta[0] + 'px');
  408. }
  409. if (xy[1] !== null) { // from setY
  410. setStyle(node, TOP, xy[1] - currentXY[1] + delta[1] + 'px');
  411. }
  412. if (!noRetry) {
  413. newXY = Y.Dom._getXY(node);
  414. // if retry is true, try one more time if we miss
  415. if ( (xy[0] !== null && newXY[0] != xy[0]) ||
  416. (xy[1] !== null && newXY[1] != xy[1]) ) {
  417. Y.Dom._setXY(node, { pos: xy, noRetry: true });
  418. }
  419. }
  420. },
  421. /**
  422. * Set the X position of an html element in page coordinates, regardless of how the element is positioned.
  423. * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
  424. * @method setX
  425. * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
  426. * @param {Int} x The value to use as the X coordinate for the element(s).
  427. */
  428. setX: function(el, x) {
  429. Y.Dom.setXY(el, [x, null]);
  430. },
  431. /**
  432. * Set the Y position of an html element in page coordinates, regardless of how the element is positioned.
  433. * The element must be part of the DOM tree to have page coordinates (display:none or elements not appended return false).
  434. * @method setY
  435. * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
  436. * @param {Int} x To use as the Y coordinate for the element(s).
  437. */
  438. setY: function(el, y) {
  439. Y.Dom.setXY(el, [null, y]);
  440. },
  441. /**
  442. * Returns the region position of the given element.
  443. * The element must be part of the DOM tree to have a region (display:none or elements not appended return false).
  444. * @method getRegion
  445. * @param {String | HTMLElement | Array} el Accepts a string to use as an ID, an actual DOM reference, or an Array of IDs and/or HTMLElements.
  446. * @return {Region | Array} A Region or array of Region instances containing "top, left, bottom, right" member data.
  447. */
  448. getRegion: function(el) {
  449. var f = function(el) {
  450. var region = false;
  451. if ( Y.Dom._canPosition(el) ) {
  452. region = Y.Region.getRegion(el);
  453. } else {
  454. }
  455. return region;
  456. };
  457. return Y.Dom.batch(el, f, Y.Dom, true);
  458. },
  459. /**
  460. * Returns the width of the client (viewport).
  461. * @method getClientWidth
  462. * @deprecated Now using getViewportWidth. This interface left intact for back compat.
  463. * @return {Int} The width of the viewable area of the page.
  464. */
  465. getClientWidth: function() {
  466. return Y.Dom.getViewportWidth();
  467. },
  468. /**
  469. * Returns the height of the client (viewport).
  470. * @method getClientHeight
  471. * @deprecated Now using getViewportHeight. This interface left intact for back compat.
  472. * @return {Int} The height of the viewable area of the page.
  473. */
  474. getClientHeight: function() {
  475. return Y.Dom.getViewportHeight();
  476. },
  477. /**
  478. * Returns an array of HTMLElements with the given class.
  479. * For optimized performance, include a tag and/or root node when possible.
  480. * Note: This method operates against a live collection, so modifying the
  481. * collection in the callback (removing/appending nodes, etc.) will have
  482. * side effects. Instead you should iterate the returned nodes array,
  483. * as you would with the native "getElementsByTagName" method.
  484. * @method getElementsByClassName
  485. * @param {String} className The class name to match against
  486. * @param {String} tag (optional) The tag name of the elements being collected
  487. * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point.
  488. * This element is not included in the className scan.
  489. * @param {Function} apply (optional) A function to apply to each element when found
  490. * @param {Any} o (optional) An optional arg that is passed to the supplied method
  491. * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
  492. * @return {Array} An array of elements that have the given class name
  493. */
  494. getElementsByClassName: function(className, tag, root, apply, o, overrides) {
  495. tag = tag || '*';
  496. root = (root) ? Y.Dom.get(root) : null || document;
  497. if (!root) {
  498. return [];
  499. }
  500. var nodes = [],
  501. elements = root.getElementsByTagName(tag),
  502. hasClass = Y.Dom.hasClass;
  503. for (var i = 0, len = elements.length; i < len; ++i) {
  504. if ( hasClass(elements[i], className) ) {
  505. nodes[nodes.length] = elements[i];
  506. }
  507. }
  508. if (apply) {
  509. Y.Dom.batch(nodes, apply, o, overrides);
  510. }
  511. return nodes;
  512. },
  513. /**
  514. * Determines whether an HTMLElement has the given className.
  515. * @method hasClass
  516. * @param {String | HTMLElement | Array} el The element or collection to test
  517. * @param {String} className the class name to search for
  518. * @return {Boolean | Array} A boolean value or array of boolean values
  519. */
  520. hasClass: function(el, className) {
  521. return Y.Dom.batch(el, Y.Dom._hasClass, className);
  522. },
  523. _hasClass: function(el, className) {
  524. var ret = false,
  525. current;
  526. if (el && className) {
  527. current = Y.Dom._getAttribute(el, CLASS_NAME) || EMPTY;
  528. if (className.exec) {
  529. ret = className.test(current);
  530. } else {
  531. ret = className && (SPACE + current + SPACE).
  532. indexOf(SPACE + className + SPACE) > -1;
  533. }
  534. } else {
  535. }
  536. return ret;
  537. },
  538. /**
  539. * Adds a class name to a given element or collection of elements.
  540. * @method addClass
  541. * @param {String | HTMLElement | Array} el The element or collection to add the class to
  542. * @param {String} className the class name to add to the class attribute
  543. * @return {Boolean | Array} A pass/fail boolean or array of booleans
  544. */
  545. addClass: function(el, className) {
  546. return Y.Dom.batch(el, Y.Dom._addClass, className);
  547. },
  548. _addClass: function(el, className) {
  549. var ret = false,
  550. current;
  551. if (el && className) {
  552. current = Y.Dom._getAttribute(el, CLASS_NAME) || EMPTY;
  553. if ( !Y.Dom._hasClass(el, className) ) {
  554. Y.Dom.setAttribute(el, CLASS_NAME, trim(current + SPACE + className));
  555. ret = true;
  556. }
  557. } else {
  558. }
  559. return ret;
  560. },
  561. /**
  562. * Removes a class name from a given element or collection of elements.
  563. * @method removeClass
  564. * @param {String | HTMLElement | Array} el The element or collection to remove the class from
  565. * @param {String} className the class name to remove from the class attribute
  566. * @return {Boolean | Array} A pass/fail boolean or array of booleans
  567. */
  568. removeClass: function(el, className) {
  569. return Y.Dom.batch(el, Y.Dom._removeClass, className);
  570. },
  571. _removeClass: function(el, className) {
  572. var ret = false,
  573. current,
  574. newClass,
  575. attr;
  576. if (el && className) {
  577. current = Y.Dom._getAttribute(el, CLASS_NAME) || EMPTY;
  578. Y.Dom.setAttribute(el, CLASS_NAME, current.replace(Y.Dom._getClassRegex(className), EMPTY));
  579. newClass = Y.Dom._getAttribute(el, CLASS_NAME);
  580. if (current !== newClass) { // else nothing changed
  581. Y.Dom.setAttribute(el, CLASS_NAME, trim(newClass)); // trim after comparing to current class
  582. ret = true;
  583. if (Y.Dom._getAttribute(el, CLASS_NAME) === '') { // remove class attribute if empty
  584. attr = (el.hasAttribute && el.hasAttribute(_CLASS)) ? _CLASS : CLASS_NAME;
  585. el.removeAttribute(attr);
  586. }
  587. }
  588. } else {
  589. }
  590. return ret;
  591. },
  592. /**
  593. * Replace a class with another class for a given element or collection of elements.
  594. * If no oldClassName is present, the newClassName is simply added.
  595. * @method replaceClass
  596. * @param {String | HTMLElement | Array} el The element or collection to remove the class from
  597. * @param {String} oldClassName the class name to be replaced
  598. * @param {String} newClassName the class name that will be replacing the old class name
  599. * @return {Boolean | Array} A pass/fail boolean or array of booleans
  600. */
  601. replaceClass: function(el, oldClassName, newClassName) {
  602. return Y.Dom.batch(el, Y.Dom._replaceClass, { from: oldClassName, to: newClassName });
  603. },
  604. _replaceClass: function(el, classObj) {
  605. var className,
  606. from,
  607. to,
  608. ret = false,
  609. current;
  610. if (el && classObj) {
  611. from = classObj.from;
  612. to = classObj.to;
  613. if (!to) {
  614. ret = false;
  615. } else if (!from) { // just add if no "from"
  616. ret = Y.Dom._addClass(el, classObj.to);
  617. } else if (from !== to) { // else nothing to replace
  618. // May need to lead with DBLSPACE?
  619. current = Y.Dom._getAttribute(el, CLASS_NAME) || EMPTY;
  620. className = (SPACE + current.replace(Y.Dom._getClassRegex(from), SPACE + to)).
  621. split(Y.Dom._getClassRegex(to));
  622. // insert to into what would have been the first occurrence slot
  623. className.splice(1, 0, SPACE + to);
  624. Y.Dom.setAttribute(el, CLASS_NAME, trim(className.join(EMPTY)));
  625. ret = true;
  626. }
  627. } else {
  628. }
  629. return ret;
  630. },
  631. /**
  632. * Returns an ID and applies it to the element "el", if provided.
  633. * @method generateId
  634. * @param {String | HTMLElement | Array} el (optional) An optional element array of elements to add an ID to (no ID is added if one is already present).
  635. * @param {String} prefix (optional) an optional prefix to use (defaults to "yui-gen").
  636. * @return {String | Array} The generated ID, or array of generated IDs (or original ID if already present on an element)
  637. */
  638. generateId: function(el, prefix) {
  639. prefix = prefix || 'yui-gen';
  640. var f = function(el) {
  641. if (el && el.id) { // do not override existing ID
  642. return el.id;
  643. }
  644. var id = prefix + YAHOO.env._id_counter++;
  645. if (el) {
  646. if (el[OWNER_DOCUMENT] && el[OWNER_DOCUMENT].getElementById(id)) { // in case one already exists
  647. // use failed id plus prefix to help ensure uniqueness
  648. return Y.Dom.generateId(el, id + prefix);
  649. }
  650. el.id = id;
  651. }
  652. return id;
  653. };
  654. // batch fails when no element, so just generate and return single ID
  655. return Y.Dom.batch(el, f, Y.Dom, true) || f.apply(Y.Dom, arguments);
  656. },
  657. /**
  658. * Determines whether an HTMLElement is an ancestor of another HTML element in the DOM hierarchy.
  659. * @method isAncestor
  660. * @param {String | HTMLElement} haystack The possible ancestor
  661. * @param {String | HTMLElement} needle The possible descendent
  662. * @return {Boolean} Whether or not the haystack is an ancestor of needle
  663. */
  664. isAncestor: function(haystack, needle) {
  665. haystack = Y.Dom.get(haystack);
  666. needle = Y.Dom.get(needle);
  667. var ret = false;
  668. if ( (haystack && needle) && (haystack[NODE_TYPE] && needle[NODE_TYPE]) ) {
  669. if (haystack.contains && haystack !== needle) { // contains returns true when equal
  670. ret = haystack.contains(needle);
  671. }
  672. else if (haystack.compareDocumentPosition) { // gecko
  673. ret = !!(haystack.compareDocumentPosition(needle) & 16);
  674. }
  675. } else {
  676. }
  677. return ret;
  678. },
  679. /**
  680. * Determines whether an HTMLElement is present in the current document.
  681. * @method inDocument
  682. * @param {String | HTMLElement} el The element to search for
  683. * @param {Object} doc An optional document to search, defaults to element's owner document
  684. * @return {Boolean} Whether or not the element is present in the current document
  685. */
  686. inDocument: function(el, doc) {
  687. return Y.Dom._inDoc(Y.Dom.get(el), doc);
  688. },
  689. _inDoc: function(el, doc) {
  690. var ret = false;
  691. if (el && el[TAG_NAME]) {
  692. doc = doc || el[OWNER_DOCUMENT];
  693. ret = Y.Dom.isAncestor(doc[DOCUMENT_ELEMENT], el);
  694. } else {
  695. }
  696. return ret;
  697. },
  698. /**
  699. * Returns an array of HTMLElements that pass the test applied by supplied boolean method.
  700. * For optimized performance, include a tag and/or root node when possible.
  701. * Note: This method operates against a live collection, so modifying the
  702. * collection in the callback (removing/appending nodes, etc.) will have
  703. * side effects. Instead you should iterate the returned nodes array,
  704. * as you would with the native "getElementsByTagName" method.
  705. * @method getElementsBy
  706. * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
  707. * @param {String} tag (optional) The tag name of the elements being collected
  708. * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
  709. * @param {Function} apply (optional) A function to apply to each element when found
  710. * @param {Any} o (optional) An optional arg that is passed to the supplied method
  711. * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
  712. * @return {Array} Array of HTMLElements
  713. */
  714. getElementsBy: function(method, tag, root, apply, o, overrides, firstOnly) {
  715. tag = tag || '*';
  716. root = (root) ? Y.Dom.get(root) : null || document;
  717. if (!root) {
  718. return [];
  719. }
  720. var nodes = [],
  721. elements = root.getElementsByTagName(tag);
  722. for (var i = 0, len = elements.length; i < len; ++i) {
  723. if ( method(elements[i]) ) {
  724. if (firstOnly) {
  725. nodes = elements[i];
  726. break;
  727. } else {
  728. nodes[nodes.length] = elements[i];
  729. }
  730. }
  731. }
  732. if (apply) {
  733. Y.Dom.batch(nodes, apply, o, overrides);
  734. }
  735. return nodes;
  736. },
  737. /**
  738. * Returns the first HTMLElement that passes the test applied by the supplied boolean method.
  739. * @method getElementBy
  740. * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
  741. * @param {String} tag (optional) The tag name of the elements being collected
  742. * @param {String | HTMLElement} root (optional) The HTMLElement or an ID to use as the starting point
  743. * @return {HTMLElement}
  744. */
  745. getElementBy: function(method, tag, root) {
  746. return Y.Dom.getElementsBy(method, tag, root, null, null, null, true);
  747. },
  748. /**
  749. * Runs the supplied method against each item in the Collection/Array.
  750. * The method is called with the element(s) as the first arg, and the optional param as the second ( method(el, o) ).
  751. * @method batch
  752. * @param {String | HTMLElement | Array} el (optional) An element or array of elements to apply the method to
  753. * @param {Function} method The method to apply to the element(s)
  754. * @param {Any} o (optional) An optional arg that is passed to the supplied method
  755. * @param {Boolean} overrides (optional) Whether or not to override the scope of "method" with "o"
  756. * @return {Any | Array} The return value(s) from the supplied method
  757. */
  758. batch: function(el, method, o, overrides) {
  759. var collection = [],
  760. scope = (overrides) ? o : window;
  761. el = (el && (el[TAG_NAME] || el.item)) ? el : Y.Dom.get(el); // skip get() when possible
  762. if (el && method) {
  763. if (el[TAG_NAME] || el.length === undefined) { // element or not array-like
  764. return method.call(scope, el, o);
  765. }
  766. for (var i = 0; i < el.length; ++i) {
  767. collection[collection.length] = method.call(scope, el[i], o);
  768. }
  769. } else {
  770. return false;
  771. }
  772. return collection;
  773. },
  774. /**
  775. * Returns the height of the document.
  776. * @method getDocumentHeight
  777. * @return {Int} The height of the actual document (which includes the body and its margin).
  778. */
  779. getDocumentHeight: function() {
  780. var scrollHeight = (document[COMPAT_MODE] != CSS1_COMPAT || isSafari) ? document.body.scrollHeight : documentElement.scrollHeight,
  781. h = Math.max(scrollHeight, Y.Dom.getViewportHeight());
  782. return h;
  783. },
  784. /**
  785. * Returns the width of the document.
  786. * @method getDocumentWidth
  787. * @return {Int} The width of the actual document (which includes the body and its margin).
  788. */
  789. getDocumentWidth: function() {
  790. var scrollWidth = (document[COMPAT_MODE] != CSS1_COMPAT || isSafari) ? document.body.scrollWidth : documentElement.scrollWidth,
  791. w = Math.max(scrollWidth, Y.Dom.getViewportWidth());
  792. return w;
  793. },
  794. /**
  795. * Returns the current height of the viewport.
  796. * @method getViewportHeight
  797. * @return {Int} The height of the viewable area of the page (excludes scrollbars).
  798. */
  799. getViewportHeight: function() {
  800. var height = self.innerHeight, // Safari, Opera
  801. mode = document[COMPAT_MODE];
  802. if ( (mode || isIE) && !isOpera ) { // IE, Gecko
  803. height = (mode == CSS1_COMPAT) ?
  804. documentElement.clientHeight : // Standards
  805. document.body.clientHeight; // Quirks
  806. }
  807. return height;
  808. },
  809. /**
  810. * Returns the current width of the viewport.
  811. * @method getViewportWidth
  812. * @return {Int} The width of the viewable area of the page (excludes scrollbars).
  813. */
  814. getViewportWidth: function() {
  815. var width = self.innerWidth, // Safari
  816. mode = document[COMPAT_MODE];
  817. if (mode || isIE) { // IE, Gecko, Opera
  818. width = (mode == CSS1_COMPAT) ?
  819. documentElement.clientWidth : // Standards
  820. document.body.clientWidth; // Quirks
  821. }
  822. return width;
  823. },
  824. /**
  825. * Returns the nearest ancestor that passes the test applied by supplied boolean method.
  826. * For performance reasons, IDs are not accepted and argument validation omitted.
  827. * @method getAncestorBy
  828. * @param {HTMLElement} node The HTMLElement to use as the starting point
  829. * @param {Function} method - A boolean method for testing elements which receives the element as its only argument.
  830. * @return {Object} HTMLElement or null if not found
  831. */
  832. getAncestorBy: function(node, method) {
  833. while ( (node = node[PARENT_NODE]) ) { // NOTE: assignment
  834. if ( Y.Dom._testElement(node, method) ) {
  835. return node;
  836. }
  837. }
  838. return null;
  839. },
  840. /**
  841. * Returns the nearest ancestor with the given className.
  842. * @method getAncestorByClassName
  843. * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
  844. * @param {String} className
  845. * @return {Object} HTMLElement
  846. */
  847. getAncestorByClassName: function(node, className) {
  848. node = Y.Dom.get(node);
  849. if (!node) {
  850. return null;
  851. }
  852. var method = function(el) { return Y.Dom.hasClass(el, className); };
  853. return Y.Dom.getAncestorBy(node, method);
  854. },
  855. /**
  856. * Returns the nearest ancestor with the given tagName.
  857. * @method getAncestorByTagName
  858. * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
  859. * @param {String} tagName
  860. * @return {Object} HTMLElement
  861. */
  862. getAncestorByTagName: function(node, tagName) {
  863. node = Y.Dom.get(node);
  864. if (!node) {
  865. return null;
  866. }
  867. var method = function(el) {
  868. return el[TAG_NAME] && el[TAG_NAME].toUpperCase() == tagName.toUpperCase();
  869. };
  870. return Y.Dom.getAncestorBy(node, method);
  871. },
  872. /**
  873. * Returns the previous sibling that is an HTMLElement.
  874. * For performance reasons, IDs are not accepted and argument validation omitted.
  875. * Returns the nearest HTMLElement sibling if no method provided.
  876. * @method getPreviousSiblingBy
  877. * @param {HTMLElement} node The HTMLElement to use as the starting point
  878. * @param {Function} method A boolean function used to test siblings
  879. * that receives the sibling node being tested as its only argument
  880. * @return {Object} HTMLElement or null if not found
  881. */
  882. getPreviousSiblingBy: function(node, method) {
  883. while (node) {
  884. node = node.previousSibling;
  885. if ( Y.Dom._testElement(node, method) ) {
  886. return node;
  887. }
  888. }
  889. return null;
  890. },
  891. /**
  892. * Returns the previous sibling that is an HTMLElement
  893. * @method getPreviousSibling
  894. * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
  895. * @return {Object} HTMLElement or null if not found
  896. */
  897. getPreviousSibling: function(node) {
  898. node = Y.Dom.get(node);
  899. if (!node) {
  900. return null;
  901. }
  902. return Y.Dom.getPreviousSiblingBy(node);
  903. },
  904. /**
  905. * Returns the next HTMLElement sibling that passes the boolean method.
  906. * For performance reasons, IDs are not accepted and argument validation omitted.
  907. * Returns the nearest HTMLElement sibling if no method provided.
  908. * @method getNextSiblingBy
  909. * @param {HTMLElement} node The HTMLElement to use as the starting point
  910. * @param {Function} method A boolean function used to test siblings
  911. * that receives the sibling node being tested as its only argument
  912. * @return {Object} HTMLElement or null if not found
  913. */
  914. getNextSiblingBy: function(node, method) {
  915. while (node) {
  916. node = node.nextSibling;
  917. if ( Y.Dom._testElement(node, method) ) {
  918. return node;
  919. }
  920. }
  921. return null;
  922. },
  923. /**
  924. * Returns the next sibling that is an HTMLElement
  925. * @method getNextSibling
  926. * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
  927. * @return {Object} HTMLElement or null if not found
  928. */
  929. getNextSibling: function(node) {
  930. node = Y.Dom.get(node);
  931. if (!node) {
  932. return null;
  933. }
  934. return Y.Dom.getNextSiblingBy(node);
  935. },
  936. /**
  937. * Returns the first HTMLElement child that passes the test method.
  938. * @method getFirstChildBy
  939. * @param {HTMLElement} node The HTMLElement to use as the starting point
  940. * @param {Function} method A boolean function used to test children
  941. * that receives the node being tested as its only argument
  942. * @return {Object} HTMLElement or null if not found
  943. */
  944. getFirstChildBy: function(node, method) {
  945. var child = ( Y.Dom._testElement(node.firstChild, method) ) ? node.firstChild : null;
  946. return child || Y.Dom.getNextSiblingBy(node.firstChild, method);
  947. },
  948. /**
  949. * Returns the first HTMLElement child.
  950. * @method getFirstChild
  951. * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
  952. * @return {Object} HTMLElement or null if not found
  953. */
  954. getFirstChild: function(node, method) {
  955. node = Y.Dom.get(node);
  956. if (!node) {
  957. return null;
  958. }
  959. return Y.Dom.getFirstChildBy(node);
  960. },
  961. /**
  962. * Returns the last HTMLElement child that passes the test method.
  963. * @method getLastChildBy
  964. * @param {HTMLElement} node The HTMLElement to use as the starting point
  965. * @param {Function} method A boolean function used to test children
  966. * that receives the node being tested as its only argument
  967. * @return {Object} HTMLElement or null if not found
  968. */
  969. getLastChildBy: function(node, method) {
  970. if (!node) {
  971. return null;
  972. }
  973. var child = ( Y.Dom._testElement(node.lastChild, method) ) ? node.lastChild : null;
  974. return child || Y.Dom.getPreviousSiblingBy(node.lastChild, method);
  975. },
  976. /**
  977. * Returns the last HTMLElement child.
  978. * @method getLastChild
  979. * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
  980. * @return {Object} HTMLElement or null if not found
  981. */
  982. getLastChild: function(node) {
  983. node = Y.Dom.get(node);
  984. return Y.Dom.getLastChildBy(node);
  985. },
  986. /**
  987. * Returns an array of HTMLElement childNodes that pass the test method.
  988. * @method getChildrenBy
  989. * @param {HTMLElement} node The HTMLElement to start from
  990. * @param {Function} method A boolean function used to test children
  991. * that receives the node being tested as its only argument
  992. * @return {Array} A static array of HTMLElements
  993. */
  994. getChildrenBy: function(node, method) {
  995. var child = Y.Dom.getFirstChildBy(node, method),
  996. children = child ? [child] : [];
  997. Y.Dom.getNextSiblingBy(child, function(node) {
  998. if ( !method || method(node) ) {
  999. children[children.length] = node;
  1000. }
  1001. return false; // fail test to collect all children
  1002. });
  1003. return children;
  1004. },
  1005. /**
  1006. * Returns an array of HTMLElement childNodes.
  1007. * @method getChildren
  1008. * @param {String | HTMLElement} node The HTMLElement or an ID to use as the starting point
  1009. * @return {Array} A static array of HTMLElements
  1010. */
  1011. getChildren: function(node) {
  1012. node = Y.Dom.get(node);
  1013. if (!node) {
  1014. }
  1015. return Y.Dom.getChildrenBy(node);
  1016. },
  1017. /**
  1018. * Returns the left scroll value of the document
  1019. * @method getDocumentScrollLeft
  1020. * @param {HTMLDocument} document (optional) The document to get the scroll value of
  1021. * @return {Int} The amount that the document is scrolled to the left
  1022. */
  1023. getDocumentScrollLeft: function(doc) {
  1024. doc = doc || document;
  1025. return Math.max(doc[DOCUMENT_ELEMENT].scrollLeft, doc.body.scrollLeft);
  1026. },
  1027. /**
  1028. * Returns the top scroll value of the document
  1029. * @method getDocumentScrollTop
  1030. * @param {HTMLDocument} document (optional) The document to get the scroll value of
  1031. * @return {Int} The amount that the document is scrolled to the top
  1032. */
  1033. getDocumentScrollTop: function(doc) {
  1034. doc = doc || document;
  1035. return Math.max(doc[DOCUMENT_ELEMENT].scrollTop, doc.body.scrollTop);
  1036. },
  1037. /**
  1038. * Inserts the new node as the previous sibling of the reference node
  1039. * @method insertBefore
  1040. * @param {String | HTMLElement} newNode The node to be inserted
  1041. * @param {String | HTMLElement} referenceNode The node to insert the new node before
  1042. * @return {HTMLElement} The node that was inserted (or null if insert fails)
  1043. */
  1044. insertBefore: function(newNode, referenceNode) {
  1045. newNode = Y.Dom.get(newNode);
  1046. referenceNode = Y.Dom.get(referenceNode);
  1047. if (!newNode || !referenceNode || !referenceNode[PARENT_NODE]) {
  1048. return null;
  1049. }
  1050. return referenceNode[PARENT_NODE].insertBefore(newNode, referenceNode);
  1051. },
  1052. /**
  1053. * Inserts the new node as the next sibling of the reference node
  1054. * @method insertAfter
  1055. * @param {String | HTMLElement} newNode The node to be inserted
  1056. * @param {String | HTMLElement} referenceNode The node to insert the new node after
  1057. * @return {HTMLElement} The node that was inserted (or null if insert fails)
  1058. */
  1059. insertAfter: function(newNode, referenceNode) {
  1060. newNode = Y.Dom.get(newNode);
  1061. referenceNode = Y.Dom.get(referenceNode);
  1062. if (!newNode || !referenceNode || !referenceNode[PARENT_NODE]) {
  1063. return null;
  1064. }
  1065. if (referenceNode.nextSibling) {
  1066. return referenceNode[PARENT_NODE].insertBefore(newNode, referenceNode.nextSibling);
  1067. } else {
  1068. return referenceNode[PARENT_NODE].appendChild(newNode);
  1069. }
  1070. },
  1071. /**
  1072. * Creates a Region based on the viewport relative to the document.
  1073. * @method getClientRegion
  1074. * @return {Region} A Region object representing the viewport which accounts for document scroll
  1075. */
  1076. getClientRegion: function() {
  1077. var t = Y.Dom.getDocumentScrollTop(),
  1078. l = Y.Dom.getDocumentScrollLeft(),
  1079. r = Y.Dom.getViewportWidth() + l,
  1080. b = Y.Dom.getViewportHeight() + t;
  1081. return new Y.Region(t, r, b, l);
  1082. },
  1083. /**
  1084. * Provides a normalized attribute interface.
  1085. * @method setAttribute
  1086. * @param {String | HTMLElement} el The target element for the attribute.
  1087. * @param {String} attr The attribute to set.
  1088. * @param {String} val The value of the attribute.
  1089. */
  1090. setAttribute: function(el, attr, val) {
  1091. Y.Dom.batch(el, Y.Dom._setAttribute, { attr: attr, val: val });
  1092. },
  1093. _setAttribute: function(el, args) {
  1094. var attr = Y.Dom._toCamel(args.attr),
  1095. val = args.val;
  1096. if (el && el.setAttribute) {
  1097. if (Y.Dom.DOT_ATTRIBUTES[attr]) {
  1098. el[attr] = val;
  1099. } else {
  1100. attr = Y.Dom.CUSTOM_ATTRIBUTES[attr] || attr;
  1101. el.setAttribute(attr, val);
  1102. }
  1103. } else {
  1104. }
  1105. },
  1106. /**
  1107. * Provides a normalized attribute interface.
  1108. * @method getAttribute
  1109. * @param {String | HTMLElement} el The target element for the attribute.
  1110. * @param {String} attr The attribute to get.
  1111. * @return {String} The current value of the attribute.
  1112. */
  1113. getAttribute: function(el, attr) {
  1114. return Y.Dom.batch(el, Y.Dom._getAttribute, attr);
  1115. },
  1116. _getAttribute: function(el, attr) {
  1117. var val;
  1118. attr = Y.Dom.CUSTOM_ATTRIBUTES[attr] || attr;
  1119. if (el && el.getAttribute) {
  1120. val = el.getAttribute(attr, 2);
  1121. } else {
  1122. }
  1123. return val;
  1124. },
  1125. _toCamel: function(property) {
  1126. var c = propertyCache;
  1127. function tU(x,l) {
  1128. return l.toUpperCase();
  1129. }
  1130. return c[property] || (c[property] = property.indexOf('-') === -1 ?
  1131. property :
  1132. property.replace( /-([a-z])/gi, tU ));
  1133. },
  1134. _getClassRegex: function(className) {
  1135. var re;
  1136. if (className !== undefined) { // allow empty string to pass
  1137. if (className.exec) { // already a RegExp
  1138. re = className;
  1139. } else {
  1140. re = reCache[className];
  1141. if (!re) {
  1142. // escape special chars (".", "[", etc.)
  1143. className = className.replace(Y.Dom._patterns.CLASS_RE_TOKENS, '\\$1');
  1144. re = reCache[className] = new RegExp(C_START + className + C_END, G);
  1145. }
  1146. }
  1147. }
  1148. return re;
  1149. },
  1150. _patterns: {
  1151. ROOT_TAG: /^body|html$/i, // body for quirks mode, html for standards,
  1152. CLASS_RE_TOKENS: /([\.\(\)\^\$\*\+\?\|\[\]\{\}\\])/g
  1153. },
  1154. _testElement: function(node, method) {
  1155. return node && node[NODE_TYPE] == 1 && ( !method || method(node) );
  1156. },
  1157. _calcBorders: function(node, xy2) {
  1158. var t = parseInt(Y.Dom[GET_COMPUTED_STYLE](node, BORDER_TOP_WIDTH), 10) || 0,
  1159. l = parseInt(Y.Dom[GET_COMPUTED_STYLE](node, BORDER_LEFT_WIDTH), 10) || 0;
  1160. if (isGecko) {
  1161. if (RE_TABLE.test(node[TAG_NAME])) {
  1162. t = 0;
  1163. l = 0;
  1164. }
  1165. }
  1166. xy2[0] += l;
  1167. xy2[1] += t;
  1168. return xy2;
  1169. }
  1170. };
  1171. var _getComputedStyle = Y.Dom[GET_COMPUTED_STYLE];
  1172. // fix opera computedStyle default color unit (convert to rgb)
  1173. if (UA.opera) {
  1174. Y.Dom[GET_COMPUTED_STYLE] = function(node, att) {
  1175. var val = _getComputedStyle(node, att);
  1176. if (RE_COLOR.test(att)) {
  1177. val = Y.Dom.Color.toRGB(val);
  1178. }
  1179. return val;
  1180. };
  1181. }
  1182. // safari converts transparent to rgba(), others use "transparent"
  1183. if (UA.webkit) {
  1184. Y.Dom[GET_COMPUTED_STYLE] = function(node, att) {
  1185. var val = _getComputedStyle(node, att);
  1186. if (val === 'rgba(0, 0, 0, 0)') {
  1187. val = 'transparent';
  1188. }
  1189. return val;
  1190. };
  1191. }
  1192. if (UA.ie && UA.ie >= 8 && document.documentElement.hasAttribute) { // IE 8 standards
  1193. Y.Dom.DOT_ATTRIBUTES.type = true; // IE 8 errors on input.setAttribute('type')
  1194. }
  1195. })();
  1196. /**
  1197. * A region is a representation of an object on a grid. It is defined
  1198. * by the top, right, bottom, left extents, so is rectangular by default. If
  1199. * other shapes are required, this class could be extended to support it.
  1200. * @namespace YAHOO.util
  1201. * @class Region
  1202. * @param {Int} t the top extent
  1203. * @param {Int} r the right extent
  1204. * @param {Int} b the bottom extent
  1205. * @param {Int} l the left extent
  1206. * @constructor
  1207. */
  1208. YAHOO.util.Region = function(t, r, b, l) {
  1209. /**
  1210. * The region's top extent
  1211. * @property top
  1212. * @type Int
  1213. */
  1214. this.top = t;
  1215. /**
  1216. * The region's top extent
  1217. * @property y
  1218. * @type Int
  1219. */
  1220. this.y = t;
  1221. /**
  1222. * The region's top extent as index, for symmetry with set/getXY
  1223. * @property 1
  1224. * @type Int
  1225. */
  1226. this[1] = t;
  1227. /**
  1228. * The region's right extent
  1229. * @property right
  1230. * @type int
  1231. */
  1232. this.right = r;
  1233. /**
  1234. * The region's bottom extent
  1235. * @property bottom
  1236. * @type Int
  1237. */
  1238. this.bottom = b;
  1239. /**
  1240. * The region's left extent
  1241. * @property left
  1242. * @type Int
  1243. */
  1244. this.left = l;
  1245. /**
  1246. * The region's left extent
  1247. * @property x
  1248. * @type Int
  1249. */
  1250. this.x = l;
  1251. /**
  1252. * The region's left extent as index, for symmetry with set/getXY
  1253. * @property 0
  1254. * @type Int
  1255. */
  1256. this[0] = l;
  1257. /**
  1258. * The region's total width
  1259. * @property width
  1260. * @type Int
  1261. */
  1262. this.width = this.right - this.left;
  1263. /**
  1264. * The region's total height
  1265. * @property height
  1266. * @type Int
  1267. */
  1268. this.height = this.bottom - this.top;
  1269. };
  1270. /**
  1271. * Returns true if this region contains the region passed in
  1272. * @method contains
  1273. * @param {Region} region The region to evaluate
  1274. * @return {Boolean} True if the region is contained with this region,
  1275. * else false
  1276. */
  1277. YAHOO.util.Region.prototype.contains = function(region) {
  1278. return ( region.left >= this.left &&
  1279. region.right <= this.right &&
  1280. region.top >= this.top &&
  1281. region.bottom <= this.bottom );
  1282. };
  1283. /**
  1284. * Returns the area of the region
  1285. * @method getArea
  1286. * @return {Int} the region's area
  1287. */
  1288. YAHOO.util.Region.prototype.getArea = function() {
  1289. return ( (this.bottom - this.top) * (this.right - this.left) );
  1290. };
  1291. /**
  1292. * Returns the region where the passed in region overlaps with this one
  1293. * @method intersect
  1294. * @param {Region} region The region that intersects
  1295. * @return {Region} The overlap region, or null if there is no overlap
  1296. */
  1297. YAHOO.util.Region.prototype.intersect = function(region) {
  1298. var t = Math.max( this.top, region.top ),
  1299. r = Math.min( this.right, region.right ),
  1300. b = Math.min( this.bottom, region.bottom ),
  1301. l = Math.max( this.left, region.left );
  1302. if (b >= t && r >= l) {
  1303. return new YAHOO.util.Region(t, r, b, l);
  1304. } else {
  1305. return null;
  1306. }
  1307. };
  1308. /**
  1309. * Returns the region representing the smallest region that can contain both
  1310. * the passed in region and this region.
  1311. * @method union
  1312. * @param {Region} region The region that to create the union with
  1313. * @return {Region} The union region
  1314. */
  1315. YAHOO.util.Region.prototype.union = function(region) {
  1316. var t = Math.min( this.top, region.top ),
  1317. r = Math.max( this.right, region.right ),
  1318. b = Math.max( this.bottom, region.bottom ),
  1319. l = Math.min( this.left, region.left );
  1320. return new YAHOO.util.Region(t, r, b, l);
  1321. };
  1322. /**
  1323. * toString
  1324. * @method toString
  1325. * @return string the region properties
  1326. */
  1327. YAHOO.util.Region.prototype.toString = function() {
  1328. return ( "Region {" +
  1329. "top: " + this.top +
  1330. ", right: " + this.right +
  1331. ", bottom: " + this.bottom +
  1332. ", left: " + this.left +
  1333. ", height: " + this.height +
  1334. ", width: " + this.width +
  1335. "}" );
  1336. };
  1337. /**
  1338. * Returns a region that is occupied by the DOM element
  1339. * @method getRegion
  1340. * @param {HTMLElement} el The element
  1341. * @return {Region} The region that the element occupies
  1342. * @static
  1343. */
  1344. YAHOO.util.Region.getRegion = function(el) {
  1345. var p = YAHOO.util.Dom.getXY(el),
  1346. t = p[1],
  1347. r = p[0] + el.offsetWidth,
  1348. b = p[1] + el.offsetHeight,
  1349. l = p[0];
  1350. return new YAHOO.util.Region(t, r, b, l);
  1351. };
  1352. /////////////////////////////////////////////////////////////////////////////
  1353. /**
  1354. * A point is a region that is special in that it represents a single point on
  1355. * the grid.
  1356. * @namespace YAHOO.util
  1357. * @class Point
  1358. * @param {Int} x The X position of the point
  1359. * @param {Int} y The Y position of the point
  1360. * @constructor
  1361. * @extends YAHOO.util.Region
  1362. */
  1363. YAHOO.util.Point = function(x, y) {
  1364. if (YAHOO.lang.isArray(x)) { // accept input from Dom.getXY, Event.getXY, etc.
  1365. y = x[1]; // dont blow away x yet
  1366. x = x[0];
  1367. }
  1368. YAHOO.util.Point.superclass.constructor.call(this, y, x, y, x);
  1369. };
  1370. YAHOO.extend(YAHOO.util.Point, YAHOO.util.Region);
  1371. (function() {
  1372. /**
  1373. * Add style management functionality to DOM.
  1374. * @module dom
  1375. * @for Dom
  1376. */
  1377. var Y = YAHOO.util,
  1378. CLIENT_TOP = 'clientTop',
  1379. CLIENT_LEFT = 'clientLeft',
  1380. PARENT_NODE = 'parentNode',
  1381. RIGHT = 'right',
  1382. HAS_LAYOUT = 'hasLayout',
  1383. PX = 'px',
  1384. OPACITY = 'opacity',
  1385. AUTO = 'auto',
  1386. BORDER_LEFT_WIDTH = 'borderLeftWidth',
  1387. BORDER_TOP_WIDTH = 'borderTopWidth',
  1388. BORDER_RIGHT_WIDTH = 'borderRightWidth',
  1389. BORDER_BOTTOM_WIDTH = 'borderBottomWidth',
  1390. VISIBLE = 'visible',
  1391. TRANSPARENT = 'transparent',
  1392. HEIGHT = 'height',
  1393. WIDTH = 'width',
  1394. STYLE = 'style',
  1395. CURRENT_STYLE = 'currentStyle',
  1396. // IE getComputedStyle
  1397. // TODO: unit-less lineHeight (e.g. 1.22)
  1398. re_size = /^width|height$/,
  1399. re_unit = /^(\d[.\d]*)+(em|ex|px|gd|rem|vw|vh|vm|ch|mm|cm|in|pt|pc|deg|rad|ms|s|hz|khz|%){1}?/i,
  1400. ComputedStyle = {
  1401. get: function(el, property) {
  1402. var value = '',
  1403. current = el[CURRENT_STYLE][property];
  1404. if (property === OPACITY) {
  1405. value = Y.Dom.getStyle(el, OPACITY);
  1406. } else if (!current || (current.indexOf && current.indexOf(PX) > -1)) { // no need to convert
  1407. value = current;
  1408. } else if (Y.Dom.IE_COMPUTED[property]) { // use compute function
  1409. value = Y.Dom.IE_COMPUTED[property](el, property);
  1410. } else if (re_unit.test(current)) { // convert to pixel
  1411. value = Y.Dom.IE.ComputedStyle.getPixel(el, property);
  1412. } else {
  1413. value = current;
  1414. }
  1415. return value;
  1416. },
  1417. getOffset: function(el, prop) {
  1418. var current = el[CURRENT_STYLE][prop], // value of "width", "top", etc.
  1419. capped = prop.charAt(0).toUpperCase() + prop.substr(1), // "Width", "Top", etc.
  1420. offset = 'offset' + capped, // "offsetWidth", "offsetTop", etc.
  1421. pixel = 'pixel' + capped, // "pixelWidth", "pixelTop", etc.
  1422. value = '',
  1423. actual;
  1424. if (current == AUTO) {
  1425. actual = el[offset]; // offsetHeight/Top etc.
  1426. if (actual === undefined) { // likely "right" or "bottom"
  1427. value = 0;
  1428. }
  1429. value = actual;
  1430. if (re_size.test(prop)) { // account for box model diff
  1431. el[STYLE][prop] = actual;
  1432. if (el[offset] > actual) {
  1433. // the difference is padding + border (works in Standards & Quirks modes)
  1434. value = actual - (el[offset] - actual);
  1435. }
  1436. el[STYLE][prop] = AUTO; // revert to auto
  1437. }
  1438. } else { // convert units to px
  1439. if (!el[STYLE][pixel] && !el[STYLE][prop]) { // need to map style.width to currentStyle (no currentStyle.pixelWidth)
  1440. el[STYLE][prop] = current; // no style.pixelWidth if no style.width
  1441. }
  1442. value = el[STYLE][pixel];
  1443. }
  1444. return value + PX;
  1445. },
  1446. getBorderWidth: function(el, property) {
  1447. // clientHeight/Width = paddingBox (e.g. offsetWidth - borderWidth)
  1448. // clientTop/Left = borderWidth
  1449. var value = null;
  1450. if (!el[CURRENT_STYLE][HAS_LAYOUT]) { // TODO: unset layout?
  1451. el[STYLE].zoom = 1; // need layout to measure client
  1452. }
  1453. switch(property) {
  1454. case BORDER_TOP_WIDTH:
  1455. value = el[CLIENT_TOP];
  1456. break;
  1457. case BORDER_BOTTOM_WIDTH:
  1458. value = el.offsetHeight - el.clientHeight - el[CLIENT_TOP];
  1459. break;
  1460. case BORDER_LEFT_WIDTH:
  1461. value = el[CLIENT_LEFT];
  1462. break;
  1463. case BORDER_RIGHT_WIDTH:
  1464. value = el.offsetWidth - el.clientWidth - el[CLIENT_LEFT];
  1465. break;
  1466. }
  1467. return value + PX;
  1468. },
  1469. getPixel: function(node, att) {
  1470. // use pixelRight to convert to px
  1471. var val = null,
  1472. styleRight = node[CURRENT_STYLE][RIGHT],
  1473. current = node[CURRENT_STYLE][att];
  1474. node[STYLE][RIGHT] = current;
  1475. val = node[STYLE].pixelRight;
  1476. node[STYLE][RIGHT] = styleRight; // revert
  1477. return val + PX;
  1478. },
  1479. getMargin: function(node, att) {
  1480. var val;
  1481. if (node[CURRENT_STYLE][att] == AUTO) {
  1482. val = 0 + PX;
  1483. } else {
  1484. val = Y.Dom.IE.ComputedStyle.getPixel(node, att);
  1485. }
  1486. return val;
  1487. },
  1488. getVisibility: function(node, att) {
  1489. var current;
  1490. while ( (current = node[CURRENT_STYLE]) && current[att] == 'inherit') { // NOTE: assignment in test
  1491. node = node[PARENT_NODE];
  1492. }
  1493. return (current) ? current[att] : VISIBLE;
  1494. },
  1495. getColor: function(node, att) {
  1496. return Y.Dom.Color.toRGB(node[CURRENT_STYLE][att]) || TRANSPARENT;
  1497. },
  1498. getBorderColor: function(node, att) {
  1499. var current = node[CURRENT_STYLE],
  1500. val = current[att] || current.color;
  1501. return Y.Dom.Color.toRGB(Y.Dom.Color.toHex(val));
  1502. }
  1503. },
  1504. //fontSize: getPixelFont,
  1505. IEComputed = {};
  1506. IEComputed.top = IEComputed.right = IEComputed.bottom = IEComputed.left =
  1507. IEComputed[WIDTH] = IEComputed[HEIGHT] = ComputedStyle.getOffset;
  1508. IEComputed.color = ComputedStyle.getColor;
  1509. IEComputed[BORDER_TOP_WIDTH] = IEComputed[BORDER_RIGHT_WIDTH] =
  1510. IEComputed[BORDER_BOTTOM_WIDTH] = IEComputed[BORDER_LEFT_WIDTH] =
  1511. ComputedStyle.getBorderWidth;
  1512. IEComputed.marginTop = IEComputed.marginRight = IEComputed.marginBottom =
  1513. IEComputed.marginLeft = ComputedStyle.getMargin;
  1514. IEComputed.visibility = ComputedStyle.getVisibility;
  1515. IEComputed.borderColor = IEComputed.borderTopColor =
  1516. IEComputed.borderRightColor = IEComputed.borderBottomColor =
  1517. IEComputed.borderLeftColor = ComputedStyle.getBorderColor;
  1518. Y.Dom.IE_COMPUTED = IEComputed;
  1519. Y.Dom.IE_ComputedStyle = ComputedStyle;
  1520. })();
  1521. (function() {
  1522. /**
  1523. * Add style management functionality to DOM.
  1524. * @module dom
  1525. * @for Dom
  1526. */
  1527. var TO_STRING = 'toString',
  1528. PARSE_INT = parseInt,
  1529. RE = RegExp,
  1530. Y = YAHOO.util;
  1531. Y.Dom.Color = {
  1532. KEYWORDS: {
  1533. black: '000',
  1534. silver: 'c0c0c0',
  1535. gray: '808080',
  1536. white: 'fff',
  1537. maroon: '800000',
  1538. red: 'f00',
  1539. purple: '800080',
  1540. fuchsia: 'f0f',
  1541. green: '008000',
  1542. lime: '0f0',
  1543. olive: '808000',
  1544. yellow: 'ff0',
  1545. navy: '000080',
  1546. blue: '00f',
  1547. teal: '008080',
  1548. aqua: '0ff'
  1549. },
  1550. re_RGB: /^rgb\(([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\)$/i,
  1551. re_hex: /^#?([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})$/i,
  1552. re_hex3: /([0-9A-F])/gi,
  1553. toRGB: function(val) {
  1554. if (!Y.Dom.Color.re_RGB.test(val)) {
  1555. val = Y.Dom.Color.toHex(val);
  1556. }
  1557. if(Y.Dom.Color.re_hex.exec(val)) {
  1558. val = 'rgb(' + [
  1559. PARSE_INT(RE.$1, 16),
  1560. PARSE_INT(RE.$2, 16),
  1561. PARSE_INT(RE.$3, 16)
  1562. ].join(', ') + ')';
  1563. }
  1564. return val;
  1565. },
  1566. toHex: function(val) {
  1567. val = Y.Dom.Color.KEYWORDS[val] || val;
  1568. if (Y.Dom.Color.re_RGB.exec(val)) {
  1569. var r = (RE.$1.length === 1) ? '0' + RE.$1 : Number(RE.$1),
  1570. g = (RE.$2.length === 1) ? '0' + RE.$2 : Number(RE.$2),
  1571. b = (RE.$3.length === 1) ? '0' + RE.$3 : Number(RE.$3);
  1572. val = [
  1573. r[TO_STRING](16),
  1574. g[TO_STRING](16),
  1575. b[TO_STRING](16)
  1576. ].join('');
  1577. }
  1578. if (val.length < 6) {
  1579. val = val.replace(Y.Dom.Color.re_hex3, '$1$1');
  1580. }
  1581. if (val !== 'transparent' && val.indexOf('#') < 0) {
  1582. val = '#' + val;
  1583. }
  1584. return val.toLowerCase();
  1585. }
  1586. };
  1587. }());
  1588. YAHOO.register("dom", YAHOO.util.Dom, {version: "2.8.0r4", build: "2449"});