dom.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 = exports.createElement("style");
  122. style.appendChild(doc.createTextNode(cssText));
  123. if (id)
  124. style.id = id;
  125. exports.getDocumentHead(doc).appendChild(style);
  126. }
  127. };
  128. exports.importCssStylsheet = function(uri, doc) {
  129. if (doc.createStyleSheet) {
  130. doc.createStyleSheet(uri);
  131. } else {
  132. var link = exports.createElement('link');
  133. link.rel = 'stylesheet';
  134. link.href = uri;
  135. exports.getDocumentHead(doc).appendChild(link);
  136. }
  137. };
  138. exports.getInnerWidth = function(element) {
  139. return (
  140. parseInt(exports.computedStyle(element, "paddingLeft"), 10) +
  141. parseInt(exports.computedStyle(element, "paddingRight"), 10) +
  142. element.clientWidth
  143. );
  144. };
  145. exports.getInnerHeight = function(element) {
  146. return (
  147. parseInt(exports.computedStyle(element, "paddingTop"), 10) +
  148. parseInt(exports.computedStyle(element, "paddingBottom"), 10) +
  149. element.clientHeight
  150. );
  151. };
  152. exports.scrollbarWidth = function(document) {
  153. var inner = exports.createElement("ace_inner");
  154. inner.style.width = "100%";
  155. inner.style.minWidth = "0px";
  156. inner.style.height = "200px";
  157. inner.style.display = "block";
  158. var outer = exports.createElement("ace_outer");
  159. var style = outer.style;
  160. style.position = "absolute";
  161. style.left = "-10000px";
  162. style.overflow = "hidden";
  163. style.width = "200px";
  164. style.minWidth = "0px";
  165. style.height = "150px";
  166. style.display = "block";
  167. outer.appendChild(inner);
  168. var body = document.documentElement;
  169. body.appendChild(outer);
  170. var noScrollbar = inner.offsetWidth;
  171. style.overflow = "scroll";
  172. var withScrollbar = inner.offsetWidth;
  173. if (noScrollbar == withScrollbar) {
  174. withScrollbar = outer.clientWidth;
  175. }
  176. body.removeChild(outer);
  177. return noScrollbar-withScrollbar;
  178. };
  179. if (typeof document == "undefined") {
  180. exports.importCssString = function() {};
  181. return;
  182. }
  183. if (window.pageYOffset !== undefined) {
  184. exports.getPageScrollTop = function() {
  185. return window.pageYOffset;
  186. };
  187. exports.getPageScrollLeft = function() {
  188. return window.pageXOffset;
  189. };
  190. }
  191. else {
  192. exports.getPageScrollTop = function() {
  193. return document.body.scrollTop;
  194. };
  195. exports.getPageScrollLeft = function() {
  196. return document.body.scrollLeft;
  197. };
  198. }
  199. if (window.getComputedStyle)
  200. exports.computedStyle = function(element, style) {
  201. if (style)
  202. return (window.getComputedStyle(element, "") || {})[style] || "";
  203. return window.getComputedStyle(element, "") || {};
  204. };
  205. else
  206. exports.computedStyle = function(element, style) {
  207. if (style)
  208. return element.currentStyle[style];
  209. return element.currentStyle;
  210. };
  211. /*
  212. * Optimized set innerHTML. This is faster than plain innerHTML if the element
  213. * already contains a lot of child elements.
  214. *
  215. * See http://blog.stevenlevithan.com/archives/faster-than-innerhtml for details
  216. */
  217. exports.setInnerHtml = function(el, innerHtml) {
  218. var element = el.cloneNode(false);//document.createElement("div");
  219. element.innerHTML = innerHtml;
  220. el.parentNode.replaceChild(element, el);
  221. return element;
  222. };
  223. if ("textContent" in document.documentElement) {
  224. exports.setInnerText = function(el, innerText) {
  225. el.textContent = innerText;
  226. };
  227. exports.getInnerText = function(el) {
  228. return el.textContent;
  229. };
  230. }
  231. else {
  232. exports.setInnerText = function(el, innerText) {
  233. el.innerText = innerText;
  234. };
  235. exports.getInnerText = function(el) {
  236. return el.innerText;
  237. };
  238. }
  239. exports.getParentWindow = function(document) {
  240. return document.defaultView || document.parentWindow;
  241. };
  242. });