Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 1282fee

Browse files
patricksmmskylef
authored andcommitted
fix(json): pick correct root element in serialize
Pick the correct root element for references
1 parent 3311d74 commit 1282fee

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

packages/json-serializer/lib/serializeJSON.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
function collectElementByIDs(element) {
22
const dataStructures = {};
3-
43
const { parents } = element;
5-
if (parents) {
6-
const rootElement = parents.get(0);
74

8-
if (rootElement) {
9-
rootElement.recursiveChildren.forEach((element) => {
10-
if (element.id) {
11-
dataStructures[element.id.toValue()] = element;
12-
}
13-
});
14-
}
5+
if (!parents || parents.isEmpty) {
6+
return dataStructures;
7+
}
8+
9+
const rootElement = parents.get(parents.length - 1);
10+
11+
if (rootElement) {
12+
rootElement.recursiveChildren.forEach((element) => {
13+
if (element.id) {
14+
dataStructures[element.id.toValue()] = element;
15+
}
16+
});
1517
}
1618

1719
return dataStructures;

packages/json-serializer/test/serializeJSON-test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ describe('#serializeJSON', () => {
2626
new namespace.elements.Category([
2727
new namespace.elements.String('hello world', { id: 'message' }),
2828
], { classes: ['dataStructures'] }),
29-
element,
29+
new namespace.elements.Category([
30+
element,
31+
]),
3032
]).freeze();
3133

3234
expect(serializeJSON(element)).to.equal('"hello world"');

0 commit comments

Comments
 (0)