Skip to content
Open
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
43 changes: 33 additions & 10 deletions app/javascript/controllers/content_loader_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,43 @@ export default class extends Controller {
this.load()
}

stripHtmlComments(input) {
let previous
let output = input

do {
previous = output
output = output.replace(/<!--[\s\S]*?-->/g, '')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there actually HTML comments we need to strip? I see whitespace, but that's handled by the trim without this new method.

} while (output !== previous)

return output
}
Comment on lines +10 to +20
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change Copilot is suggesting is what I originally had. It would override the existing code in this helper method, which was suggested in a prior Copilot review. 🙃


load() {
fetch(this.urlValue)
.then(response => response.text())
.then(html => {
const parentElement = this.element.parentElement;
// Replace the entire element with the fetched HTML
this.element.outerHTML = html;
// Hide primo links if libkey link is present
if (parentElement.querySelector('.libkey-link')) {
const resultGet = parentElement.closest('.result-get');
if (resultGet) {
const primoLinks = resultGet.querySelectorAll('.primo-link');
// removing instead of hiding to avoid layout issues when selecting which link to highlight
primoLinks.forEach(link => link.remove());
const parentElement = this.element.parentElement
// Strip HTML comments and trim whitespace
const cleanedHtml = this.stripHtmlComments(html).trim()
// Replace the entire element with the fetched HTML, or remove if empty
if (cleanedHtml) {
this.element.outerHTML = cleanedHtml
// Hide primo links if libkey link is present
if (parentElement.querySelector('.libkey-link')) {
const resultGet = parentElement.closest('.result-get')
if (resultGet) {
const primoLinks = resultGet.querySelectorAll('.primo-link')
// removing instead of hiding to avoid layout issues when selecting which link to highlight
primoLinks.forEach(link => link.remove())
}
}
} else {
// Remove only this loader element
this.element.remove();
// Remove result-get container if it's now empty (no fulfillment links and no other content)
if (!parentElement.textContent.trim()) {
parentElement.remove();
}
}
Comment thread
jazairi marked this conversation as resolved.
Comment thread
jazairi marked this conversation as resolved.
})
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function with high complexity (count = 7): load [qlty:function-complexity]

Expand Down