can-dom-mutate
Advanced tools
Comparing version 1.3.5 to 1.3.6
'use strict'; | ||
var domMutate = require('can-dom-mutate'); | ||
var namespace = require('can-namespace'); | ||
var domMutateEvents = require('./events/events'); | ||
function makeMutationEvent (defaultEventType, subscription, bubbles) { | ||
var elementSubscriptions = new Map(); | ||
return { | ||
_subscriptions: elementSubscriptions, | ||
defaultEventType: defaultEventType, | ||
addEventListener: function (target, eventType, handler) { | ||
var dispatch = this.dispatch; | ||
var data = elementSubscriptions.get(target); | ||
if (!data) { | ||
data = { | ||
removeListener: null, | ||
listeners: new Set() | ||
}; | ||
elementSubscriptions.set(target, data); | ||
} | ||
if (data.listeners.size === 0) { | ||
data.removeListener = subscription(target, function (mutation) { | ||
var eventData = {type: eventType}; | ||
for (var key in mutation) { | ||
eventData[key] = mutation[key]; | ||
} | ||
dispatch(target, eventData, bubbles !== false); | ||
}); | ||
} | ||
data.listeners.add(handler); | ||
target.addEventListener(eventType, handler); | ||
}, | ||
removeEventListener: function (target, eventType, handler) { | ||
target.removeEventListener(eventType, handler); | ||
var data = elementSubscriptions.get(target); | ||
if (data) { | ||
data.listeners['delete'](handler); | ||
if (data.listeners.size === 0) { | ||
data.removeListener(); | ||
elementSubscriptions['delete'](target); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
module.exports = namespace.domMutateDomEvents = { | ||
attributes: makeMutationEvent('attributes', domMutate.onNodeAttributeChange), | ||
inserted: makeMutationEvent('inserted', domMutate.onNodeInsertion, false), | ||
removed: makeMutationEvent('removed', domMutate.onNodeRemoval) | ||
}; | ||
// backwards compatibility | ||
module.exports = namespace.domMutateDomEvents = domMutateEvents; |
187
node.js
'use strict'; | ||
var globals = require('can-globals'); | ||
var namespace = require('can-namespace'); | ||
var domMutate = require('./can-dom-mutate'); | ||
var util = require('./-util'); | ||
var node = require('./node/node'); | ||
var isInDocument = util.isInDocument; | ||
var getParents = util.getParents; | ||
var synthetic = { | ||
dispatchNodeInsertion: function (container, node) { | ||
if (isInDocument(node)) { | ||
domMutate.dispatchNodeInsertion(node); | ||
} | ||
}, | ||
dispatchNodeRemoval: function (container, node) { | ||
if (isInDocument(container) && !isInDocument(node)) { | ||
domMutate.dispatchNodeRemoval(node); | ||
} | ||
} | ||
}; | ||
var compat = { | ||
replaceChild: function (newChild, oldChild) { | ||
var newChildren = getParents(newChild); | ||
var result = this.replaceChild(newChild, oldChild); | ||
synthetic.dispatchNodeRemoval(this, oldChild); | ||
for (var i = 0; i < newChildren.length; i++) { | ||
synthetic.dispatchNodeInsertion(this, newChildren[i]); | ||
} | ||
return result; | ||
}, | ||
setAttribute: function (name, value) { | ||
var oldAttributeValue = this.getAttribute(name); | ||
var result = this.setAttribute(name, value); | ||
var newAttributeValue = this.getAttribute(name); | ||
if (oldAttributeValue !== newAttributeValue) { | ||
domMutate.dispatchNodeAttributeChange(this, name, oldAttributeValue); | ||
} | ||
return result; | ||
}, | ||
removeAttribute: function (name) { | ||
var oldAttributeValue = this.getAttribute(name); | ||
var result = this.removeAttribute(name); | ||
if (oldAttributeValue) { | ||
domMutate.dispatchNodeAttributeChange(this, name, oldAttributeValue); | ||
} | ||
return result; | ||
} | ||
}; | ||
var compatData = [ | ||
['appendChild', 'Insertion'], | ||
['insertBefore', 'Insertion'], | ||
['removeChild', 'Removal'] | ||
]; | ||
compatData.forEach(function (pair) { | ||
var nodeMethod = pair[0]; | ||
var dispatchMethod = 'dispatchNode' + pair[1]; | ||
compat[nodeMethod] = function (node) { | ||
var nodes = getParents(node); | ||
var result = this[nodeMethod].apply(this, arguments); | ||
for (var i = 0; i < nodes.length; i++) { | ||
synthetic[dispatchMethod](this, nodes[i]); | ||
} | ||
return result; | ||
}; | ||
}); | ||
var normal = {}; | ||
var nodeMethods = ['appendChild', 'insertBefore', 'removeChild', 'replaceChild', 'setAttribute', 'removeAttribute']; | ||
nodeMethods.forEach(function (methodName) { | ||
normal[methodName] = function () { | ||
return this[methodName].apply(this, arguments); | ||
}; | ||
}); | ||
/** | ||
* @module {{}} can-dom-mutate/node node | ||
* @parent can-dom-mutate/modules | ||
* | ||
* Append, insert, and remove DOM nodes. Also, change node attributes. | ||
* This allows mutations to be dispatched in environments where MutationObserver is not supported. | ||
* @signature `mutateNode` | ||
* | ||
* Exports an `Object` with methods that shouhld be used to mutate HTML. | ||
* | ||
* ```js | ||
* var mutateNode = require('can-dom-mutate/node'); | ||
* var el = document.createElement('div'); | ||
* | ||
* mutateNode.appendChild.call(document.body, el); | ||
* | ||
* ``` | ||
*/ | ||
var mutate = {}; | ||
/** | ||
* @function can-dom-mutate/node.appendChild appendChild | ||
* @parent can-dom-mutate/node | ||
* | ||
* Append a node to an element, effectively `Node.prototype.appendChild`. | ||
* | ||
* @signature `mutate.appendChild.call(parent, child)` | ||
* | ||
* @param {Node} parent The parent into which the child is inserted. | ||
* @param {Node} child The child which will be inserted into the parent. | ||
* @return {Node} The appended child. | ||
*/ | ||
/** | ||
* @function can-dom-mutate/node.insertBefore insertBefore | ||
* @parent can-dom-mutate/node | ||
* | ||
* Insert a node before a given reference node in an element, effectively `Node.prototype.insertBefore`. | ||
* | ||
* @signature `mutate.insertBefore.call(parent, child, reference)` | ||
* @param {Node} parent The parent into which the child is inserted. | ||
* @param {Node} child The child which will be inserted into the parent. | ||
* @param {Node} reference The reference which the child will be placed before. | ||
* @return {Node} The inserted child. | ||
*/ | ||
/** | ||
* @function can-dom-mutate/node.removeChild removeChild | ||
* @parent can-dom-mutate/node | ||
* | ||
* Remove a node from an element, effectively `Node.prototype.removeChild`. | ||
* | ||
* @signature `mutate.removeChild.call(parent, child)` | ||
* | ||
* @param {Node} parent The parent from which the child is removed. | ||
* @param {Node} child The child which will be removed from the parent. | ||
* @return {Node} The removed child. | ||
*/ | ||
/** | ||
* @function can-dom-mutate/node.replaceChild replaceChild | ||
* @parent can-dom-mutate/node | ||
* | ||
* Insert a node before a given reference node in an element, effectively `Node.prototype.replaceChild`. | ||
* | ||
* @signature `mutate.replaceChild.call(parent, newChild, oldChild)` | ||
* | ||
* @param {Node} parent The parent into which the newChild is inserted. | ||
* @param {Node} newChild The child which is inserted into the parent. | ||
* @param {Node} oldChild The child which is removed from the parent. | ||
* @return {Node} The replaced child. | ||
*/ | ||
/** | ||
* @function can-dom-mutate/node.setAttribute setAttribute | ||
* @parent can-dom-mutate/node | ||
* | ||
* Set an attribute value on an element, effectively `Element.prototype.setAttribute`. | ||
* | ||
* @signature `mutate.setAttribute.call(element, name, value)` | ||
* | ||
* @param {Element} element The element on which to set the attribute. | ||
* @param {String} name The name of the attribute to set. | ||
* @param {String} value The value to set on the attribute. | ||
*/ | ||
/** | ||
* @function can-dom-mutate/node.removeAttribute removeAttribute | ||
* @parent can-dom-mutate/node | ||
* | ||
* Removes an attribute from an element, effectively `Element.prototype.removeAttribute`. | ||
* | ||
* @signature `mutate.removeAttribute.call(element, name, value)` | ||
* | ||
* @param {Element} element The element from which to remove the attribute. | ||
* @param {String} name The name of the attribute to remove. | ||
*/ | ||
function setMutateStrategy(observer) { | ||
var strategy = observer ? normal : compat; | ||
for (var key in strategy) { | ||
mutate[key] = strategy[key]; | ||
} | ||
} | ||
var mutationObserverKey = 'MutationObserver'; | ||
setMutateStrategy(globals.getKeyValue(mutationObserverKey)); | ||
globals.onKeyValue(mutationObserverKey, setMutateStrategy); | ||
module.exports = namespace.domMutateNode = domMutate.node = mutate; | ||
// backwards compatibility | ||
module.exports = namespace.node = node; |
{ | ||
"name": "can-dom-mutate", | ||
"description": "Dispatch and listen for DOM mutations", | ||
"version": "1.3.5", | ||
"version": "1.3.6", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "DoneJS Team", |
import './test/can-dom-mutate-test'; | ||
import './test/dom-events-test'; | ||
import './test/node-test'; | ||
import './events/events-test'; | ||
import './node/node-test'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61155
20
1399