PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/dom4/1.1.1/dom4.max.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 333 lines | 278 code | 6 blank | 49 comment | 51 complexity | ad55fe9997b1309be7b473c0ab132063 MD5 | raw file
  1. /*!
  2. Copyright (C) 2013 by WebReflection
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. (function(window){'use strict';
  20. /* jshint loopfunc: true, noempty: false*/
  21. // http://www.w3.org/TR/dom/#element
  22. function textNodeIfString(node) {
  23. return typeof node === 'string' ? window.document.createTextNode(node) : node;
  24. }
  25. function mutationMacro(nodes) {
  26. if (nodes.length === 1) {
  27. return textNodeIfString(nodes[0]);
  28. }
  29. for (var
  30. fragment = window.document.createDocumentFragment(),
  31. list = slice.call(nodes),
  32. i = 0; i < nodes.length; i++
  33. ) {
  34. fragment.appendChild(textNodeIfString(list[i]));
  35. }
  36. return fragment;
  37. }
  38. for(var
  39. defineProperty = Object.defineProperty || function (object, property, descriptor) {
  40. object.__defineGetter__(property, descriptor.get);
  41. },
  42. indexOf = [].indexOf || function indexOf(value){
  43. var length = this.length;
  44. while(length--) {
  45. if (this[length] === value) {
  46. break;
  47. }
  48. }
  49. return length;
  50. },
  51. head,
  52. property,
  53. verifyToken,
  54. DOMTokenList,
  55. trim = /^\s+|\s+$/g,
  56. spaces = /\s+/,
  57. SPACE = '\x20',
  58. toggle = function toggle(token, force) {
  59. if (this.contains(token)) {
  60. if (!force) {
  61. // force is not true (either false or omitted)
  62. this.remove(token);
  63. }
  64. } else if(force === undefined || force) {
  65. force = true;
  66. this.add(token);
  67. }
  68. return !!force;
  69. },
  70. ElementPrototype = (window.Element || window.Node || window.HTMLElement).prototype,
  71. properties = [
  72. 'matches', (
  73. ElementPrototype.matchesSelector ||
  74. ElementPrototype.webkitMatchesSelector ||
  75. ElementPrototype.khtmlMatchesSelector ||
  76. ElementPrototype.mozMatchesSelector ||
  77. ElementPrototype.msMatchesSelector ||
  78. ElementPrototype.oMatchesSelector ||
  79. function matches(selector) {
  80. var parentNode = this.parentNode;
  81. return !!parentNode && -1 < indexOf.call(
  82. parentNode.querySelectorAll(selector),
  83. this
  84. );
  85. }
  86. ),
  87. 'closest', function closest(selector) {
  88. var parentNode = this, matches;
  89. while (
  90. // document has no .matches
  91. (matches = parentNode && parentNode.matches) &&
  92. !parentNode.matches(selector)
  93. ) {
  94. parentNode = parentNode.parentNode;
  95. }
  96. return matches ? parentNode : null;
  97. },
  98. 'prepend', function prepend() {
  99. var firstChild = this.firstChild,
  100. node = mutationMacro(arguments);
  101. if (firstChild) {
  102. this.insertBefore(node, firstChild);
  103. } else {
  104. this.appendChild(node);
  105. }
  106. },
  107. 'append', function append() {
  108. this.appendChild(mutationMacro(arguments));
  109. },
  110. 'before', function before() {
  111. var parentNode = this.parentNode;
  112. if (parentNode) {
  113. parentNode.insertBefore(
  114. mutationMacro(arguments), this
  115. );
  116. }
  117. },
  118. 'after', function after() {
  119. var parentNode = this.parentNode,
  120. nextSibling = this.nextSibling,
  121. node = mutationMacro(arguments);
  122. if (parentNode) {
  123. if (nextSibling) {
  124. parentNode.insertBefore(node, nextSibling);
  125. } else {
  126. parentNode.appendChild(node);
  127. }
  128. }
  129. },
  130. 'replace', function replace() {
  131. var parentNode = this.parentNode;
  132. if (parentNode) {
  133. parentNode.replaceChild(
  134. mutationMacro(arguments),
  135. this
  136. );
  137. }
  138. },
  139. 'remove', function remove() {
  140. var parentNode = this.parentNode;
  141. if (parentNode) {
  142. parentNode.removeChild(this);
  143. }
  144. }
  145. ],
  146. slice = properties.slice,
  147. i = properties.length; i; i -= 2
  148. ) {
  149. property = properties[i - 2];
  150. if (!(property in ElementPrototype)) {
  151. ElementPrototype[property] = properties[i - 1];
  152. }
  153. }
  154. // most likely an IE9 only issue
  155. // see https://github.com/WebReflection/dom4/issues/6
  156. if (!document.createElement('a').matches('a')) {
  157. ElementPrototype[property] = function(matches){
  158. return function (selector) {
  159. return matches.call(
  160. this.parentNode ?
  161. this :
  162. document.createDocumentFragment().appendChild(this),
  163. selector
  164. );
  165. };
  166. }(ElementPrototype[property]);
  167. }
  168. // http://www.w3.org/TR/dom/#domtokenlist
  169. // iOS 5.1 has completely screwed this property
  170. // classList in ElementPrototype is false
  171. // but it's actually there as getter
  172. if (!('classList' in document.documentElement)) {
  173. // http://www.w3.org/TR/domcore/#domtokenlist
  174. verifyToken = function (token) {
  175. if (!token) {
  176. throw 'SyntaxError';
  177. } else if (spaces.test(token)) {
  178. throw 'InvalidCharacterError';
  179. }
  180. return token;
  181. };
  182. DOMTokenList = function (node) {
  183. var className = node.className.replace(trim, '');
  184. if (className.length) {
  185. properties.push.apply(
  186. this,
  187. className.split(spaces)
  188. );
  189. }
  190. this._ = node;
  191. };
  192. DOMTokenList.prototype = {
  193. length: 0,
  194. add: function add() {
  195. for(var j = 0, token; j < arguments.length; j++) {
  196. token = arguments[j];
  197. if(!this.contains(token)) {
  198. properties.push.call(this, property);
  199. }
  200. }
  201. this._.className = '' + this;
  202. },
  203. contains: (function(indexOf){
  204. return function contains(token) {
  205. i = indexOf.call(this, property = verifyToken(token));
  206. return -1 < i;
  207. };
  208. }([].indexOf || function (token) {
  209. i = this.length;
  210. while(i-- && this[i] !== token){}
  211. return i;
  212. })),
  213. item: function item(i) {
  214. return this[i] || null;
  215. },
  216. remove: function remove() {
  217. for(var j = 0, token; j < arguments.length; j++) {
  218. token = arguments[j];
  219. if(this.contains(token)) {
  220. properties.splice.call(this, i, 1);
  221. }
  222. }
  223. this._.className = '' + this;
  224. },
  225. toggle: toggle,
  226. toString: function toString() {
  227. return properties.join.call(this, SPACE);
  228. }
  229. };
  230. defineProperty(ElementPrototype, 'classList', {
  231. get: function get() {
  232. return new DOMTokenList(this);
  233. },
  234. set: function(){}
  235. });
  236. } else {
  237. // iOS 5.1 and Nokia ASHA do not support multiple add or remove
  238. // trying to detect and fix that in here
  239. DOMTokenList = document.createElement('div').classList;
  240. DOMTokenList.add('a', 'b', 'a');
  241. if ('a\x20b' != DOMTokenList) {
  242. // no other way to reach original methods in iOS 5.1
  243. ElementPrototype = DOMTokenList.constructor.prototype;
  244. if (!('add' in ElementPrototype)) {
  245. // ASHA double fails in here
  246. ElementPrototype = window.DOMTokenList.prototype;
  247. }
  248. verifyToken = function (original) {
  249. return function () {
  250. var i = 0;
  251. while (i < arguments.length) {
  252. original.call(this, arguments[i++]);
  253. }
  254. };
  255. };
  256. ElementPrototype.add = verifyToken(ElementPrototype.add);
  257. ElementPrototype.remove = verifyToken(ElementPrototype.remove);
  258. // toggle is broken too ^_^ ... let's fix it
  259. ElementPrototype.toggle = toggle;
  260. }
  261. }
  262. if (!('head' in document)) {
  263. defineProperty(document, 'head', {
  264. get: function () {
  265. return head || (
  266. head = document.getElementsByTagName('head')[0]
  267. );
  268. }
  269. });
  270. }
  271. // http://www.w3.org/TR/dom/#customevent
  272. try{new window.CustomEvent('?');}catch(o_O){
  273. window.CustomEvent = function(
  274. eventName,
  275. defaultInitDict
  276. ){
  277. // the infamous substitute
  278. function CustomEvent(type, eventInitDict) {
  279. /*jshint eqnull:true */
  280. var event = document.createEvent(eventName);
  281. if (typeof type != 'string') {
  282. throw new Error('An event name must be provided');
  283. }
  284. if (eventName == 'Event') {
  285. event.initCustomEvent = initCustomEvent;
  286. }
  287. if (eventInitDict == null) {
  288. eventInitDict = defaultInitDict;
  289. }
  290. event.initCustomEvent(
  291. type,
  292. eventInitDict.bubbles,
  293. eventInitDict.cancelable,
  294. eventInitDict.detail
  295. );
  296. return event;
  297. }
  298. // attached at runtime
  299. function initCustomEvent(
  300. type, bubbles, cancelable, detail
  301. ) {
  302. /*jshint validthis:true*/
  303. this.initEvent(type, bubbles, cancelable);
  304. this.detail = detail;
  305. }
  306. // that's it
  307. return CustomEvent;
  308. }(
  309. // is this IE9 or IE10 ?
  310. // where CustomEvent is there
  311. // but not usable as construtor ?
  312. window.CustomEvent ?
  313. // use the CustomEvent interface in such case
  314. 'CustomEvent' : 'Event',
  315. // otherwise the common compatible one
  316. {
  317. bubbles: false,
  318. cancelable: false,
  319. detail: null
  320. }
  321. );
  322. }
  323. }(window));