Skip to content

Commit

Permalink
throw better exception when duplicate keys are found
Browse files Browse the repository at this point in the history
  • Loading branch information
refractalize committed May 5, 2017
1 parent 947ecf9 commit 6061a24
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions test/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,17 @@ test('mixed keys move from i>0 to i<length-1', function (assert) {
assert.end()
})

test('duplicate keys throws appropriate error', function (assert) {
var start = nodesFromArray(['1', '2', '3'])
var end = nodesFromArray(['1', '2', '2', '3'])

assert.throws(function () {
diff(start, end)
}, 'duplicate vdom key')

assert.end()
})

/*
keyTest(
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42],
Expand Down
7 changes: 7 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var TextNode = require("../vnode/vtext")
var version = require("../vnode/version")
var assertEqualDom = require("./lib/assert-equal-dom.js")
var patchCount = require("./lib/patch-count.js")
var selector = require("../vnode/selector")



Expand Down Expand Up @@ -1027,6 +1028,12 @@ test("Different namespaces creates a patch", function (assert) {
assert.end()
})

test('can pretty print node as selector', function (assert) {
var node = h("div", {id: 'an-id', className: " class-1 class-2"})
assert.equal(selector(node), "div#an-id.class-1.class-2")
assert.end()
})

// Safely translates style values using the DOM in the browser
function style(name, value) {
var node = render(h())
Expand Down
5 changes: 5 additions & 0 deletions vnode/selector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function selector (node) {
return node.tagName.toLowerCase()
+ (node.properties.id ? '#' + node.properties.id : '')
+ (node.properties.className ? '.' + node.properties.className.trim().replace(/ +/g, '.') : '')
}
7 changes: 6 additions & 1 deletion vtree/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var isVText = require("../vnode/is-vtext")
var isWidget = require("../vnode/is-widget")
var isThunk = require("../vnode/is-thunk")
var handleThunk = require("../vnode/handle-thunk")
var selector = require("../vnode/selector")

var diffProps = require("./diff-props")

Expand Down Expand Up @@ -400,7 +401,11 @@ function keyIndex(children) {
var child = children[i]

if (child.key) {
keys[child.key] = i
if (keys[child.key]) {
throw new Error('duplicate vdom key: ' + child.key + ', vdom: ' + selector(child))
} else {
keys[child.key] = i
}
} else {
free.push(i)
}
Expand Down

0 comments on commit 6061a24

Please sign in to comment.