dom.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var XHTML_NS = "http://www.w3.org/1999/xhtml";
  33. exports.getDocumentHead = function(doc) {
  34. if (!doc)
  35. doc = document;
  36. return doc.head || doc.getElementsByTagName("head")[0] || doc.documentElement;
  37. }
  38. exports.createElement = function(tag, ns) {
  39. return document.createElementNS ?
  40. document.createElementNS(ns || XHTML_NS, tag) :
  41. document.createElement(tag);
  42. };
  43. exports.hasCssClass = function(el, name) {
  44. var classes = (el.className || "").split(/\s+/g);
  45. return classes.indexOf(name) !== -1;
  46. };
  47. /*
  48. * Add a CSS class to the list of classes on the given node
  49. */
  50. exports.addCssClass = function(el, name) {
  51. if (!exports.hasCssClass(el, name)) {
  52. el.className += " " + name;
  53. }
  54. };
  55. /*
  56. * Remove a CSS class from the list of classes on the given node
  57. */
  58. exports.removeCssClass = function(el, name) {
  59. var classes = el.className.split(/\s+/g);
  60. while (true) {
  61. var index = classes.indexOf(name);
  62. if (index == -1) {
  63. break;
  64. }
  65. classes.splice(index, 1);
  66. }
  67. el.className = classes.join(" ");
  68. };
  69. exports.toggleCssClass = function(el, name) {
  70. var classes = el.className.split(/\s+/g), add = true;
  71. while (true) {
  72. var index = classes.indexOf(name);
  73. if (index == -1) {
  74. break;
  75. }
  76. add = false;
  77. classes.splice(index, 1);
  78. }
  79. if(add)
  80. classes.push(name);
  81. el.className = classes.join(" ");
  82. return add;
  83. };
  84. /*
  85. * Add or remove a CSS class from the list of classes on the given node
  86. * depending on the value of <tt>include</tt>
  87. */
  88. exports.setCssClass = function(node, className, include) {
  89. if (include) {
  90. exports.addCssClass(node, className);
  91. } else {
  92. exports.removeCssClass(node, className);
  93. }
  94. };
  95. exports.hasCssString = function(id, doc) {
  96. var index = 0, sheets;
  97. doc = doc || document;
  98. if (doc.createStyleSheet && (sheets = doc.styleSheets)) {
  99. while (index < sheets.length)
  100. if (sheets[index++].owningElement.id === id) return true;
  101. } else if ((sheets = doc.getElementsByTagName("style"))) {
  102. while (index < sheets.length)
  103. if (sheets[index++].id === id) return true;
  104. }
  105. return false;
  106. };
  107. exports.importCssString = function importCssString(cssText, id, doc) {
  108. doc = doc || document;
  109. // If style is already imported return immediately.
  110. if (id && exports.hasCssString(id, doc))
  111. return null;
  112. var style;
  113. if (id)
  114. cssText += "\n/*# sourceURL=ace/css/" + id + " */";
  115. if (doc.createStyleSheet) {
  116. style = doc.createStyleSheet();
  117. style.cssText = cssText;
  118. if (id)
  119. style.owningElement.id = id;
  120. } else {
  121. style = doc.createElementNS
  122. ? doc.createElementNS(XHTML_NS, "style")
  123. : doc.createElement("style");
  124. style.appendChild(doc.createTextNode(cssText));
  125. if (id)
  126. style.id = id;
  127. exports.getDocumentHead(doc).appendChild(style);
  128. }
  129. };
  130. exports.importCssStylsheet = function(uri, doc) {
  131. if (doc.createStyleSheet) {
  132. doc.createStyleSheet(uri);
  133. } else {
  134. var link = exports.createElement('link');
  135. link.rel = 'stylesheet';
  136. link.href = uri;
  137. exports.getDocumentHead(doc).appendChild(link);
  138. }
  139. };
  140. exports.getInnerWidth = function(element) {
  141. return (
  142. parseInt(exports.computedStyle(element, "paddingLeft"), 10) +
  143. parseInt(exports.computedStyle(element, "paddingRight"), 10) +
  144. element.clientWidth
  145. );
  146. };
  147. exports.getInnerHeight = function(element) {
  148. return (
  149. parseInt(exports.computedStyle(element, "paddingTop"), 10) +
  150. parseInt(exports.computedStyle(element, "paddingBottom"), 10) +
  151. element.clientHeight
  152. );
  153. };
  154. exports.scrollbarWidth = function(document) {
  155. var inner = exports.createElement("ace_inner");
  156. inner.style.width = "100%";
  157. inner.style.minWidth = "0px";
  158. inner.style.height = "200px";
  159. inner.style.display = "block";
  160. var outer = exports.createElement("ace_outer");
  161. var style = outer.style;
  162. style.position = "absolute";
  163. style.left = "-10000px";
  164. style.overflow = "hidden";
  165. style.width = "200px";
  166. style.minWidth = "0px";
  167. style.height = "150px";
  168. style.display = "block";
  169. outer.appendChild(inner);
  170. var body = document.documentElement;
  171. body.appendChild(outer);
  172. var noScrollbar = inner.offsetWidth;
  173. style.overflow = "scroll";
  174. var withScrollbar = inner.offsetWidth;
  175. if (noScrollbar == withScrollbar) {
  176. withScrollbar = outer.clientWidth;
  177. }
  178. body.removeChild(outer);
  179. return noScrollbar-withScrollbar;
  180. };
  181. if (typeof document == "undefined") {
  182. exports.importCssString = function() {};
  183. return;
  184. }
  185. if (window.pageYOffset !== undefined) {
  186. exports.getPageScrollTop = function() {
  187. return window.pageYOffset;
  188. };
  189. exports.getPageScrollLeft = function() {
  190. return window.pageXOffset;
  191. };
  192. }
  193. else {
  194. exports.getPageScrollTop = function() {
  195. return document.body.scrollTop;
  196. };
  197. exports.getPageScrollLeft = function() {
  198. return document.body.scrollLeft;
  199. };
  200. }
  201. if (window.getComputedStyle)
  202. exports.computedStyle = function(element, style) {
  203. if (style)
  204. return (window.getComputedStyle(element, "") || {})[style] || "";
  205. return window.getComputedStyle(element, "") || {};
  206. };
  207. else
  208. exports.computedStyle = function(element, style) {
  209. if (style)
  210. return element.currentStyle[style];
  211. return element.currentStyle;
  212. };
  213. /*
  214. * Optimized set innerHTML. This is faster than plain innerHTML if the element
  215. * already contains a lot of child elements.
  216. *
  217. * See http://blog.stevenlevithan.com/archives/faster-than-innerhtml for details
  218. */
  219. exports.setInnerHtml = function(el, innerHtml) {
  220. var element = el.cloneNode(false);//document.createElement("div");
  221. element.innerHTML = innerHtml;
  222. el.parentNode.replaceChild(element, el);
  223. return element;
  224. };
  225. if ("textContent" in document.documentElement) {
  226. exports.setInnerText = function(el, innerText) {
  227. el.textContent = innerText;
  228. };
  229. exports.getInnerText = function(el) {
  230. return el.textContent;
  231. };
  232. }
  233. else {
  234. exports.setInnerText = function(el, innerText) {
  235. el.innerText = innerText;
  236. };
  237. exports.getInnerText = function(el) {
  238. return el.innerText;
  239. };
  240. }
  241. exports.getParentWindow = function(document) {
  242. return document.defaultView || document.parentWindow;
  243. };
  244. });