Skip to content

Commit 7fd8a70

Browse files
Breaking: Remove generatesKeys option
Remove this option that generates a random if "key" or "id" are not defined. Radom keys can negatively impact performance and may cause issues with component state.
1 parent 75ed1cf commit 7fd8a70

File tree

4 files changed

+4
-46
lines changed

4 files changed

+4
-46
lines changed

src/config.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ export interface DOMConfig {
6464
export interface Config {
6565
dom?: DOMConfig
6666
overrides?: SelectorsToElement,
67-
generatesKeys?: boolean,
6867
useFragment?: boolean,
6968
}
7069

@@ -86,7 +85,6 @@ const defaultConfig: Config = {
8685
dom: {
8786
ADD_ATTR: ['key'],
8887
},
89-
generatesKeys: false,
9088
useFragment: false,
9189
}
9290

src/react.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const reactAttributesMap: { [keyof: string]: string } = {
5454
usemap: 'useMap',
5555
}
5656

57-
function transformAttributes(attributesMap: NamedNodeMap, options: Config): Attributes {
57+
function transformAttributes(attributesMap: NamedNodeMap): Attributes {
5858
const attributes = getAttributes(attributesMap)
5959
const transformedAttributes: Attributes = {}
6060
Object.keys(attributes).forEach((key) => {
@@ -64,9 +64,6 @@ function transformAttributes(attributesMap: NamedNodeMap, options: Config): Attr
6464
transformedAttributes[key] = attributes[key]
6565
}
6666
})
67-
if (options.generatesKeys) {
68-
transformedAttributes.key = attributes.key || attributes.id || Math.random().toString(36).substr(2, 5)
69-
}
7067
return transformedAttributes
7168
}
7269

@@ -75,7 +72,7 @@ function renderTextNode(node: Node & ChildNode) {
7572
}
7673

7774
function renderElementNode(node: Node & ChildNode, options: Config) {
78-
const element = transform(node as EnrichedElement, options)
75+
const element = transform(node as EnrichedElement)
7976

8077
if (element.override) {
8178
return React.cloneElement(element.override(element.attributes, node.textContent))
@@ -87,9 +84,9 @@ function renderElementNode(node: Node & ChildNode, options: Config) {
8784
return React.createElement(element.nodeName, element.attributes)
8885
}
8986

90-
function transform(element: EnrichedElement, options: Config) {
87+
function transform(element: EnrichedElement) {
9188
return {
92-
attributes: transformAttributes(element.attributes, options),
89+
attributes: transformAttributes(element.attributes),
9390
childNodes: element.childNodes,
9491
nodeName: element.nodeName.toLowerCase(),
9592
nodeType: element.nodeType,

tests/config.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ describe('Options', () => {
1111
RETURN_DOM_FRAGMENT: true,
1212
RETURN_DOM_IMPORT: false,
1313
},
14-
generatesKeys: false,
1514
useFragment: false,
1615
})
1716
})
@@ -30,7 +29,6 @@ describe('Options', () => {
3029
RETURN_DOM_FRAGMENT: true,
3130
RETURN_DOM_IMPORT: false,
3231
},
33-
generatesKeys: false,
3432
useFragment: false,
3533
})
3634
})
@@ -48,7 +46,6 @@ describe('Options', () => {
4846
RETURN_DOM_FRAGMENT: true,
4947
RETURN_DOM_IMPORT: false,
5048
},
51-
generatesKeys: false,
5249
useFragment: false,
5350
})
5451
})

tests/react.spec.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -76,39 +76,5 @@ describe('React', () => {
7676
expect(wrapper.find('a').text()).toEqual('Link')
7777
})
7878
})
79-
80-
describe('#generatesKeys', () => {
81-
it('should use key if provided', () => {
82-
const div = document.createElement('div')
83-
const link = document.createElement('a')
84-
link.textContent = 'Link'
85-
link.setAttribute('key', 'link')
86-
div.appendChild(link)
87-
88-
const wrapper = shallowWrapper(render(div.childNodes, { generatesKeys: true }))
89-
expect(wrapper.find('a').key()).toEqual('link')
90-
})
91-
92-
it('should use id if provided and key is not here', () => {
93-
const div = document.createElement('div')
94-
const link = document.createElement('a')
95-
link.textContent = 'Link'
96-
link.setAttribute('id', 'link')
97-
div.appendChild(link)
98-
99-
const wrapper = shallowWrapper(render(div.childNodes, { generatesKeys: true }))
100-
expect(wrapper.find('a').key()).toEqual('link')
101-
})
102-
103-
it('should use a random key if key nor id are specified', () => {
104-
const div = document.createElement('div')
105-
const link = document.createElement('a')
106-
link.textContent = 'Link'
107-
div.appendChild(link)
108-
109-
const wrapper = shallowWrapper(render(div.childNodes, { generatesKeys: true }))
110-
expect(wrapper.find('a').key()).toHaveLength(5)
111-
})
112-
})
11379
})
11480
})

0 commit comments

Comments
 (0)