From c5d1b02d6a7c85bf3e492faabc36113adb374e54 Mon Sep 17 00:00:00 2001 From: Gael Robin Date: Tue, 8 Oct 2024 14:53:53 +0200 Subject: [PATCH 1/3] Closes #34: exclude svg use from lrc --- src/BeaconLrc.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/BeaconLrc.js b/src/BeaconLrc.js index 52fd10d..01d9a69 100644 --- a/src/BeaconLrc.js +++ b/src/BeaconLrc.js @@ -23,12 +23,23 @@ class BeaconLrc { _getLazyRenderElements() { const elements = document.querySelectorAll('[data-rocket-location-hash]'); + const svgUseTargets = this._getSvgUseTargets(); if (elements.length <= 0) { return []; } - const validElements = Array.from(elements).filter(element => !this._skipElement(element)); + const validElements = Array.from(elements).filter(element => { + if (this._skipElement(element)) { + return false; + } + if (svgUseTargets.includes(element)) { + this.logger.logColoredMessage(`Element skipped because of SVG: ${element.tagName}`, 'orange'); + return false; + } + return true; + }); + return validElements.map(element => ({ element: element, @@ -135,6 +146,21 @@ class BeaconLrc { : 'No hash detected'; } + _getSvgUseTargets() { + const useElements = document.querySelectorAll('use'); + const targets = new Set(); + + useElements.forEach(use => { + let parent = use.parentElement; + while (parent && parent !== document.body) { + targets.add(parent); + parent = parent.parentElement; + } + }); + + return Array.from(targets); + } + getResults() { return this.lazyRenderElements; } From b56057d237da822b6ab016ed583c22bf3f183d40 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 10 Oct 2024 13:49:37 +0100 Subject: [PATCH 2/3] Added test --- test/BeaconLrc.test.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/test/BeaconLrc.test.js b/test/BeaconLrc.test.js index d0724be..5d22ebd 100644 --- a/test/BeaconLrc.test.js +++ b/test/BeaconLrc.test.js @@ -132,6 +132,21 @@ describe('BeaconLrc', function() { _skipElementStub.restore(); }); + it('should skip elements with svg use', function() { + const _getElementDepthStub = sinon.stub(beaconLrc, '_getElementDepth'); + _getElementDepthStub.returns(1); + + const _svgElementStub = sinon.stub(beaconLrc, '_getSvgUseTargets'); + _svgElementStub.returns([mockElements[2]]); + + const elements = beaconLrc._getLazyRenderElements(); + const skippedElement = elements.find(el => el.hash === 'hash3'); + assert.strictEqual(skippedElement, undefined); + + _getElementDepthStub.restore(); + _svgElementStub.restore(); + }); + it('should return correct distance', () => { const distance = beaconLrc._getElementDistance(mockElements[2]); assert.strictEqual(distance, 600); From 2d1f92849a1f10002906278feb80fe3880e461f0 Mon Sep 17 00:00:00 2001 From: Michael Lee Date: Thu, 10 Oct 2024 17:16:14 +0100 Subject: [PATCH 3/3] Added test --- test/BeaconLrc.test.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/BeaconLrc.test.js b/test/BeaconLrc.test.js index 5d22ebd..562f293 100644 --- a/test/BeaconLrc.test.js +++ b/test/BeaconLrc.test.js @@ -53,7 +53,7 @@ describe('BeaconLrc', function() { // Mocking document.querySelectorAll global.document = { querySelectorAll: (selector) => { - if (selector === '[data-rocket-location-hash]') { + if (selector === '[data-rocket-location-hash]' || selector === 'use') { return mockElements; } return []; @@ -247,4 +247,14 @@ describe('BeaconLrc', function() { _getElementXPathStub.restore(); }); + + it('should return the correct SVG use target elements', function() { + mockElements[0].parentElement = { tagName: 'svg', parentElement: null }; + mockElements[1].parentElement = { tagName: 'div', parentElement: null }; + + const targets = beaconLrc._getSvgUseTargets(); + assert.strictEqual(targets.length, 2); + assert.strictEqual(targets[0].tagName, 'svg'); + assert.strictEqual(targets[1].tagName, 'div'); + }); }); \ No newline at end of file