Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

MutationObserver.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import _WeakMap from "babel-runtime/core-js/weak-map";
  2. /**
  3. * @license
  4. * Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
  5. * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
  6. * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
  7. * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
  8. * Code distributed by Google as part of the polymer project is also
  9. * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
  10. */
  11. // @version 0.7.22
  12. (function (global) {
  13. if (global.JsMutationObserver) {
  14. return;
  15. }
  16. var registrationsTable = new _WeakMap();
  17. var setImmediate;
  18. if (/Trident|Edge/.test(navigator.userAgent)) {
  19. setImmediate = setTimeout;
  20. } else if (window.setImmediate) {
  21. setImmediate = window.setImmediate;
  22. } else {
  23. var setImmediateQueue = [];
  24. var sentinel = String(Math.random());
  25. window.addEventListener("message", function (e) {
  26. if (e.data === sentinel) {
  27. var queue = setImmediateQueue;
  28. setImmediateQueue = [];
  29. queue.forEach(function (func) {
  30. func();
  31. });
  32. }
  33. });
  34. setImmediate = function setImmediate(func) {
  35. setImmediateQueue.push(func);
  36. window.postMessage(sentinel, "*");
  37. };
  38. }
  39. var isScheduled = false;
  40. var scheduledObservers = [];
  41. function scheduleCallback(observer) {
  42. scheduledObservers.push(observer);
  43. if (!isScheduled) {
  44. isScheduled = true;
  45. setImmediate(dispatchCallbacks);
  46. }
  47. }
  48. function wrapIfNeeded(node) {
  49. return window.ShadowDOMPolyfill && window.ShadowDOMPolyfill.wrapIfNeeded(node) || node;
  50. }
  51. function dispatchCallbacks() {
  52. isScheduled = false;
  53. var observers = scheduledObservers;
  54. scheduledObservers = [];
  55. observers.sort(function (o1, o2) {
  56. return o1.uid_ - o2.uid_;
  57. });
  58. var anyNonEmpty = false;
  59. observers.forEach(function (observer) {
  60. var queue = observer.takeRecords();
  61. removeTransientObserversFor(observer);
  62. if (queue.length) {
  63. observer.callback_(queue, observer);
  64. anyNonEmpty = true;
  65. }
  66. });
  67. if (anyNonEmpty) dispatchCallbacks();
  68. }
  69. function removeTransientObserversFor(observer) {
  70. observer.nodes_.forEach(function (node) {
  71. var registrations = registrationsTable.get(node);
  72. if (!registrations) return;
  73. registrations.forEach(function (registration) {
  74. if (registration.observer === observer) registration.removeTransientObservers();
  75. });
  76. });
  77. }
  78. function forEachAncestorAndObserverEnqueueRecord(target, callback) {
  79. for (var node = target; node; node = node.parentNode) {
  80. var registrations = registrationsTable.get(node);
  81. if (registrations) {
  82. for (var j = 0; j < registrations.length; j++) {
  83. var registration = registrations[j];
  84. var options = registration.options;
  85. if (node !== target && !options.subtree) continue;
  86. var record = callback(options);
  87. if (record) registration.enqueue(record);
  88. }
  89. }
  90. }
  91. }
  92. var uidCounter = 0;
  93. function JsMutationObserver(callback) {
  94. this.callback_ = callback;
  95. this.nodes_ = [];
  96. this.records_ = [];
  97. this.uid_ = ++uidCounter;
  98. }
  99. JsMutationObserver.prototype = {
  100. observe: function observe(target, options) {
  101. target = wrapIfNeeded(target);
  102. if (!options.childList && !options.attributes && !options.characterData || options.attributeOldValue && !options.attributes || options.attributeFilter && options.attributeFilter.length && !options.attributes || options.characterDataOldValue && !options.characterData) {
  103. throw new SyntaxError();
  104. }
  105. var registrations = registrationsTable.get(target);
  106. if (!registrations) registrationsTable.set(target, registrations = []);
  107. var registration;
  108. for (var i = 0; i < registrations.length; i++) {
  109. if (registrations[i].observer === this) {
  110. registration = registrations[i];
  111. registration.removeListeners();
  112. registration.options = options;
  113. break;
  114. }
  115. }
  116. if (!registration) {
  117. registration = new Registration(this, target, options);
  118. registrations.push(registration);
  119. this.nodes_.push(target);
  120. }
  121. registration.addListeners();
  122. },
  123. disconnect: function disconnect() {
  124. this.nodes_.forEach(function (node) {
  125. var registrations = registrationsTable.get(node);
  126. for (var i = 0; i < registrations.length; i++) {
  127. var registration = registrations[i];
  128. if (registration.observer === this) {
  129. registration.removeListeners();
  130. registrations.splice(i, 1);
  131. break;
  132. }
  133. }
  134. }, this);
  135. this.records_ = [];
  136. },
  137. takeRecords: function takeRecords() {
  138. var copyOfRecords = this.records_;
  139. this.records_ = [];
  140. return copyOfRecords;
  141. }
  142. };
  143. function MutationRecord(type, target) {
  144. this.type = type;
  145. this.target = target;
  146. this.addedNodes = [];
  147. this.removedNodes = [];
  148. this.previousSibling = null;
  149. this.nextSibling = null;
  150. this.attributeName = null;
  151. this.attributeNamespace = null;
  152. this.oldValue = null;
  153. }
  154. function copyMutationRecord(original) {
  155. var record = new MutationRecord(original.type, original.target);
  156. record.addedNodes = original.addedNodes.slice();
  157. record.removedNodes = original.removedNodes.slice();
  158. record.previousSibling = original.previousSibling;
  159. record.nextSibling = original.nextSibling;
  160. record.attributeName = original.attributeName;
  161. record.attributeNamespace = original.attributeNamespace;
  162. record.oldValue = original.oldValue;
  163. return record;
  164. }
  165. var currentRecord, recordWithOldValue;
  166. function getRecord(type, target) {
  167. return currentRecord = new MutationRecord(type, target);
  168. }
  169. function getRecordWithOldValue(oldValue) {
  170. if (recordWithOldValue) return recordWithOldValue;
  171. recordWithOldValue = copyMutationRecord(currentRecord);
  172. recordWithOldValue.oldValue = oldValue;
  173. return recordWithOldValue;
  174. }
  175. function clearRecords() {
  176. currentRecord = recordWithOldValue = undefined;
  177. }
  178. function recordRepresentsCurrentMutation(record) {
  179. return record === recordWithOldValue || record === currentRecord;
  180. }
  181. function selectRecord(lastRecord, newRecord) {
  182. if (lastRecord === newRecord) return lastRecord;
  183. if (recordWithOldValue && recordRepresentsCurrentMutation(lastRecord)) return recordWithOldValue;
  184. return null;
  185. }
  186. function Registration(observer, target, options) {
  187. this.observer = observer;
  188. this.target = target;
  189. this.options = options;
  190. this.transientObservedNodes = [];
  191. }
  192. Registration.prototype = {
  193. enqueue: function enqueue(record) {
  194. var records = this.observer.records_;
  195. var length = records.length;
  196. if (records.length > 0) {
  197. var lastRecord = records[length - 1];
  198. var recordToReplaceLast = selectRecord(lastRecord, record);
  199. if (recordToReplaceLast) {
  200. records[length - 1] = recordToReplaceLast;
  201. return;
  202. }
  203. } else {
  204. scheduleCallback(this.observer);
  205. }
  206. records[length] = record;
  207. },
  208. addListeners: function addListeners() {
  209. this.addListeners_(this.target);
  210. },
  211. addListeners_: function addListeners_(node) {
  212. var options = this.options;
  213. if (options.attributes) node.addEventListener("DOMAttrModified", this, true);
  214. if (options.characterData) node.addEventListener("DOMCharacterDataModified", this, true);
  215. if (options.childList) node.addEventListener("DOMNodeInserted", this, true);
  216. if (options.childList || options.subtree) node.addEventListener("DOMNodeRemoved", this, true);
  217. },
  218. removeListeners: function removeListeners() {
  219. this.removeListeners_(this.target);
  220. },
  221. removeListeners_: function removeListeners_(node) {
  222. var options = this.options;
  223. if (options.attributes) node.removeEventListener("DOMAttrModified", this, true);
  224. if (options.characterData) node.removeEventListener("DOMCharacterDataModified", this, true);
  225. if (options.childList) node.removeEventListener("DOMNodeInserted", this, true);
  226. if (options.childList || options.subtree) node.removeEventListener("DOMNodeRemoved", this, true);
  227. },
  228. addTransientObserver: function addTransientObserver(node) {
  229. if (node === this.target) return;
  230. this.addListeners_(node);
  231. this.transientObservedNodes.push(node);
  232. var registrations = registrationsTable.get(node);
  233. if (!registrations) registrationsTable.set(node, registrations = []);
  234. registrations.push(this);
  235. },
  236. removeTransientObservers: function removeTransientObservers() {
  237. var transientObservedNodes = this.transientObservedNodes;
  238. this.transientObservedNodes = [];
  239. transientObservedNodes.forEach(function (node) {
  240. this.removeListeners_(node);
  241. var registrations = registrationsTable.get(node);
  242. for (var i = 0; i < registrations.length; i++) {
  243. if (registrations[i] === this) {
  244. registrations.splice(i, 1);
  245. break;
  246. }
  247. }
  248. }, this);
  249. },
  250. handleEvent: function handleEvent(e) {
  251. e.stopImmediatePropagation();
  252. switch (e.type) {
  253. case "DOMAttrModified":
  254. var name = e.attrName;
  255. var namespace = e.relatedNode.namespaceURI;
  256. var target = e.target;
  257. var record = new getRecord("attributes", target);
  258. record.attributeName = name;
  259. record.attributeNamespace = namespace;
  260. var oldValue = e.attrChange === MutationEvent.ADDITION ? null : e.prevValue;
  261. forEachAncestorAndObserverEnqueueRecord(target, function (options) {
  262. if (!options.attributes) return;
  263. if (options.attributeFilter && options.attributeFilter.length && options.attributeFilter.indexOf(name) === -1 && options.attributeFilter.indexOf(namespace) === -1) {
  264. return;
  265. }
  266. if (options.attributeOldValue) return getRecordWithOldValue(oldValue);
  267. return record;
  268. });
  269. break;
  270. case "DOMCharacterDataModified":
  271. var target = e.target;
  272. var record = getRecord("characterData", target);
  273. var oldValue = e.prevValue;
  274. forEachAncestorAndObserverEnqueueRecord(target, function (options) {
  275. if (!options.characterData) return;
  276. if (options.characterDataOldValue) return getRecordWithOldValue(oldValue);
  277. return record;
  278. });
  279. break;
  280. case "DOMNodeRemoved":
  281. this.addTransientObserver(e.target);
  282. case "DOMNodeInserted":
  283. var changedNode = e.target;
  284. var addedNodes, removedNodes;
  285. if (e.type === "DOMNodeInserted") {
  286. addedNodes = [changedNode];
  287. removedNodes = [];
  288. } else {
  289. addedNodes = [];
  290. removedNodes = [changedNode];
  291. }
  292. var previousSibling = changedNode.previousSibling;
  293. var nextSibling = changedNode.nextSibling;
  294. var record = getRecord("childList", e.target.parentNode);
  295. record.addedNodes = addedNodes;
  296. record.removedNodes = removedNodes;
  297. record.previousSibling = previousSibling;
  298. record.nextSibling = nextSibling;
  299. forEachAncestorAndObserverEnqueueRecord(e.relatedNode, function (options) {
  300. if (!options.childList) return;
  301. return record;
  302. });
  303. }
  304. clearRecords();
  305. }
  306. };
  307. global.JsMutationObserver = JsMutationObserver;
  308. if (!global.MutationObserver) {
  309. global.MutationObserver = JsMutationObserver;
  310. JsMutationObserver._isPolyfilled = true;
  311. }
  312. })(self);