Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw better exception when duplicate keys are found #433

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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