[Pkg-javascript-commits] [ltx] 409/469: fix equality with serializable objects
Jonas Smedegaard
dr at jones.dk
Wed Aug 31 13:03:32 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository ltx.
commit b2d3c72a30772fbfecff18d12ee6a90b8691a69c
Author: Sonny Piers <sonny at fastmail.net>
Date: Wed Nov 4 10:55:48 2015 +0100
fix equality with serializable objects
---
lib/Element.js | 11 ++++++++---
test/equality-test.js | 18 ++++++++++++++++++
2 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/lib/Element.js b/lib/Element.js
index d90845f..1559f86 100644
--- a/lib/Element.js
+++ b/lib/Element.js
@@ -361,12 +361,12 @@ Element.prototype.write = function (writer) {
writer(this.name)
for (var k in this.attrs) {
var v = this.attrs[k]
- if (v || (v === '') || (v === 0)) {
+ if (v != null) { // === null || undefined
writer(' ')
writer(k)
writer('="')
if (typeof v !== 'string') {
- v = v.toString(10)
+ v = v.toString()
}
writer(escapeXML(v))
writer('"')
@@ -389,7 +389,12 @@ Element.prototype.attrsEquals = function (el) {
var length = keys.length
if (length !== Object.keys(el.attrs).length) return false
for (var i = 0, l = length; i < l; i++) {
- if (attrs[keys[i]] !== el.attrs[keys[i]]) return false
+ var key = keys[i]
+ var value = attrs[key]
+ if (value == null || el.attrs[key] == null) { // === null || undefined
+ if (value !== el.attrs[key]) return false
+ }
+ else if (value.toString() !== el.attrs[key].toString()) return false
}
return true
}
diff --git a/test/equality-test.js b/test/equality-test.js
index 5f7a1a8..7abe67b 100644
--- a/test/equality-test.js
+++ b/test/equality-test.js
@@ -39,6 +39,20 @@ vows.describe('equality').addBatch({
var d = new Element('foo', {b: 'c', a: 'b'})
assert.equal(c.attrsEquals(d), true)
},
+ 'it returns true if elements attributes are serialized equaly': function () {
+ var a = new Element('foo', {foo: 'false'})
+ var b = new Element('foo', {foo: false})
+ assert.equal(a.attrsEquals(b), true)
+
+ var c = new Element('foo', {foo: '0'})
+ var d = new Element('foo', {foo: 0})
+ assert.equal(c.attrsEquals(d), true)
+
+ var foo = {toString: function () { return 'hello' }}
+ var e = new Element('foo', {foo: foo})
+ var f = new Element('foo', {foo: 'hello'})
+ assert.equal(e.attrsEquals(f), true)
+ },
'it returns false if elements attributes differ': function () {
var a = new Element('foo', {a: 'b'})
var b = new Element('foo')
@@ -51,6 +65,10 @@ vows.describe('equality').addBatch({
var e = new Element('foo', {b: 'a'})
var f = new Element('foo', {a: 'b'})
assert.equal(e.attrsEquals(f), false)
+
+ var g = new Element('foo', {foo: 'bar'})
+ var h = new Element('foo', {bar: 'bar'})
+ assert.equal(g.attrsEquals(h), false)
}
},
'childrenEquals': {
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git
More information about the Pkg-javascript-commits
mailing list