Skip to content

Commit

Permalink
MWPW-159291 Fix fragment recursive check algo (#2997)
Browse files Browse the repository at this point in the history
* MWPW-159291 Fix fragment recursive check algo

* Update to have original recursive behavior again
  • Loading branch information
chrischrischris authored Oct 21, 2024
1 parent f72bd60 commit cc8f804
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
32 changes: 25 additions & 7 deletions libs/blocks/fragment/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,32 @@ const removeHash = (url) => {
const isCircularRef = (href) => [...Object.values(fragMap)]
.some((tree) => {
const node = tree.find(href);
return node ? !(node.isLeaf) : false;
return node?.isRecursive;
});

const updateFragMap = (fragment, a, href) => {
const fragLinks = [...fragment.querySelectorAll('a')]
.filter((link) => localizeLink(link.href).includes('/fragments/'));
if (!fragLinks.length) return;

if (document.body.contains(a)) { // is fragment on page (not nested)
if (document.body.contains(a) && !a.parentElement?.closest('.fragment')) {
// eslint-disable-next-line no-use-before-define
fragMap[href] = new Tree(href);
fragLinks.forEach((link) => fragMap[href].insert(href, localizeLink(removeHash(link.href))));
} else {
Object.values(fragMap).forEach((tree) => {
if (tree.find(href)) {
fragLinks.forEach((link) => tree.insert(href, localizeLink(removeHash(link.href))));
}
const hrefNode = tree.find(href);
if (!hrefNode) return;

fragLinks.forEach((link) => {
const localizedHref = localizeLink(removeHash(link.href));
const parentNodeSameHref = hrefNode.findParent(localizedHref);
if (parentNodeSameHref) {
parentNodeSameHref.isRecursive = true;
} else {
hrefNode.addChild(localizedHref);
}
});
});
}
};
Expand Down Expand Up @@ -143,10 +152,19 @@ class Node {
this.value = value;
this.parent = parent;
this.children = [];
this.isRecursive = false;
}

addChild(key, value = key) {
const alreadyHasChild = this.children.some((n) => n.key === key);
if (!alreadyHasChild) {
this.children.push(new Node(key, value, this));
}
}

get isLeaf() {
return this.children.length === 0;
findParent(key) {
if (this.parent?.key === key) return this.parent;
return this.parent?.findParent(key);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/blocks/fragment/mocks/fragments/frag-b.plain.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div>
<h5>Frag B, Loads Frag A</h5>
<a href="/test/blocks/fragment/mocks/fragments/frag-a">Frag A link</a>
<a href="/test/blocks/fragment/mocks/fragments/frag-c">Frag C link</a>
</div>
4 changes: 4 additions & 0 deletions test/blocks/fragment/mocks/fragments/frag-c.plain.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
<h5>Frag B, Loads Frag A</h5>
<a href="/test/blocks/fragment/mocks/fragments/frag-a">Frag A link</a>
</div>
26 changes: 24 additions & 2 deletions test/blocks/fragment/tree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,30 @@ describe('Tree Data Struct', () => {
t.remove('first');
expect(t.find('first')).to.be.undefined;
expect(t.find('second')).to.be.equal(t.root.children[0]);
});

it('Will return false if it cant insert or remove a node', () => {
const t = new Tree('root');
t.insert('root', 'first');
t.insert('root', 'second');
const isInserted = t.insert('doesnt-exist', 'third');
expect(isInserted).to.be.false;

const isRemoved = t.remove('doesnt-exist');
expect(isRemoved).to.be.false;
});

it('Can add child directly from a Node', () => {
const t = new Tree('root');
t.insert('root', 'first');
t.insert('root', 'second');
const firstNode = t.find('first');
firstNode.addChild('third');
expect(firstNode.children.length).to.be.equal(1);
expect(firstNode.children[0].key).to.be.equal('third');

expect(t.find('second').isLeaf).to.be.true;
expect(t.root.isLeaf).to.be.false;
// adding a duplicate key should not add a new child
firstNode.addChild('third');
expect(firstNode.children.length).to.be.equal(1);
});
});

0 comments on commit cc8f804

Please sign in to comment.