dom-serialize
Advanced tools
Comparing version 1.2.1 to 2.0.0
2.0.0 / 2015-02-03 | ||
================== | ||
* README: update for `serializeTarget` | ||
* index: emit the "serialize" event on the node that we know is in the DOM | ||
1.2.1 / 2015-02-03 | ||
@@ -3,0 +9,0 @@ ================== |
21
index.js
@@ -33,2 +33,3 @@ | ||
* @param {Function} [fn] - optional callback function to use in the "serialize" event for this call | ||
* @param {EventTarget} [eventTarget] - optional EventTarget instance to emit the "serialize" event on (defaults to `node`) | ||
* return {String} | ||
@@ -38,3 +39,3 @@ * @public | ||
function serialize (node, context, fn) { | ||
function serialize (node, context, fn, eventTarget) { | ||
if (!node) return ''; | ||
@@ -71,4 +72,7 @@ if ('function' === typeof context) { | ||
if (node.dispatchEvent(e)) { | ||
e.serializeTarget = node; | ||
var target = eventTarget || node; | ||
if (target.dispatchEvent(e)) { | ||
// `e.detail.serialize` can be set to a: | ||
@@ -78,10 +82,11 @@ // String - returned directly | ||
// Anything else - get Stringified first, and then returned directly | ||
if (e.detail.serialize != null) { | ||
if ('string' === typeof e.detail.serialize) { | ||
rtn = e.detail.serialize; | ||
} else if ('number' === typeof e.detail.serialize.nodeType) { | ||
var s = e.detail.serialize; | ||
if (s != null) { | ||
if ('string' === typeof s) { | ||
rtn = s; | ||
} else if ('number' === typeof s.nodeType) { | ||
// make it go through the serialization logic | ||
rtn = serialize(e.detail.serialize, context); | ||
rtn = serialize(s, context, null, target); | ||
} else { | ||
rtn = String(e.detail.serialize); | ||
rtn = String(s); | ||
} | ||
@@ -88,0 +93,0 @@ } else { |
{ | ||
"name": "dom-serialize", | ||
"version": "1.2.1", | ||
"version": "2.0.0", | ||
"description": "Serializes any DOM node into a String", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -24,3 +24,8 @@ dom-serialize | ||
The "serialize" event bubbles, so it could be a good idea to utilize | ||
event delegation on a known root node that will be serialized. | ||
Check the `event.serializeTarget` property to check which `Node` is | ||
currently being serialized. | ||
Installation | ||
@@ -62,6 +67,6 @@ ------------ | ||
console.log(serialize(node, function (event) { | ||
if (event.target === node.firstChild) { | ||
if (event.serializeTarget === node.firstChild) { | ||
// for the first child, output an ellipsis to summarize "content" | ||
event.detail.serialze = '…'; | ||
} else if (event.target !== node) { | ||
} else if (event.serializeTarget !== node) { | ||
// any other child | ||
@@ -68,0 +73,0 @@ event.preventDefault(); |
@@ -165,7 +165,9 @@ | ||
count++; | ||
e.detail.serialize = document.createTextNode('foo'); | ||
if (count === 1) { | ||
e.detail.serialize = document.createTextNode('foo'); | ||
} | ||
}); | ||
assert.equal(0, count); | ||
assert.equal('foo', serialize(node)); | ||
assert.equal(1, count); | ||
assert.equal(2, count); | ||
}); | ||
@@ -202,6 +204,6 @@ | ||
assert.equal('foo', e.detail.context); | ||
if (e.target === node) | ||
if (e.serializeTarget === node) | ||
return; | ||
else if (e.target === node.firstChild) | ||
e.detail.serialize = document.createTextNode('…'); | ||
else if (e.serializeTarget.nodeValue === 'foo') | ||
e.detail.serialize = '…'; | ||
else | ||
@@ -212,3 +214,3 @@ e.preventDefault(); | ||
assert.equal(0, count); | ||
assert.equal('<div>…</div>', serialize(node, 'foo')); | ||
assert.equal('<div>…</div>', serialize(node, 'foo')); | ||
assert.equal(4, count); | ||
@@ -254,4 +256,32 @@ }); | ||
it('should support one-time callback function on Nodes set in `e.detail.serialize`', function () { | ||
node = document.createElement('div'); | ||
node.appendChild(document.createTextNode('foo')); | ||
// `node` must be inside the DOM for the "serialize" event to bubble | ||
document.body.appendChild(node); | ||
var count = 0; | ||
function callback (e) { | ||
count++; | ||
if (2 === count) { | ||
assert.equal('foo', e.serializeTarget.nodeValue); | ||
var text = document.createTextNode('bar'); | ||
e.detail.serialize = text; | ||
} else if (3 === count) { | ||
assert.equal('bar', e.serializeTarget.nodeValue); | ||
var text = document.createTextNode('baz'); | ||
e.detail.serialize = text; | ||
} | ||
} | ||
assert.equal(0, count); | ||
assert.equal('<div>baz</div>', serialize(node, callback)); | ||
assert.equal(4, count); | ||
}); | ||
}); | ||
}); |
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
21208
431
82