Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setElementContents for outerHTML #4182

Merged
Show file tree
Hide file tree
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
20 changes: 13 additions & 7 deletions themes/bootstrap3/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ var VuFind = (function VuFind() {
*
* @param {Element} elm Target element
* @param {string} html HTML
* @param {Object} attrs Any additional attributes
* @param {Object} attrs Any additional attributes (does not work with outerHtml as property)
* @param {string} property Target property ('innerHTML', 'outerHTML' or '' for no HTML update)
*/
function setElementContents(elm, html, attrs = {}, property = 'innerHTML') {
Expand All @@ -349,14 +349,21 @@ var VuFind = (function VuFind() {
}
});

let scriptElement = elm;

if (property === 'innerHTML') {
elm.replaceChildren(...tmpDiv.childNodes);
} else if (property === 'outerHTML') {
scriptElement = elm.parentNode;
elm.replaceWith(...tmpDiv.childNodes);
}

// Set any attributes (N.B. has to be done before scripts in case they rely on the attributes):
Object.entries(attrs).forEach(([attr, value]) => elm.setAttribute(attr, value));
if (property !== 'outerHTML') {
// Set any attributes (N.B. has to be done before scripts in case they rely on the attributes):
Object.entries(attrs).forEach(([attr, value]) => elm.setAttribute(attr, value));
} else if (Object.keys(attrs).length > 0) {
console.error("Incompatible parameter 'attrs' " + JSON.stringify(attrs) + " passed to setElementContents() while 'property' is 'outerHTML'.");
}
Copy link
Member

Choose a reason for hiding this comment

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

Should we perhaps add something like:

Suggested change
}
} else {
console.error("Incompatible parameters passed to setElementContents().");
}

...just so there's a bit of feedback if somebody attempts something illegal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but only when the 'attrs' is actually not empty. I added it.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, good point -- thanks!


// Append any scripts:
scripts.forEach(script => {
Expand All @@ -366,7 +373,7 @@ var VuFind = (function VuFind() {
newScript.src = script.src;
}
newScript.setAttribute('nonce', getCspNonce());
elm.appendChild(newScript);
scriptElement.appendChild(newScript);
});
}

Expand All @@ -386,10 +393,9 @@ var VuFind = (function VuFind() {
*
* @param {Element} elm Target element
* @param {string} html HTML
* @param {Object} attrs Any additional attributes
*/
function setOuterHtml(elm, html, attrs = {}) {
setElementContents(elm, html, attrs, 'outerHTML');
function setOuterHtml(elm, html) {
setElementContents(elm, html, {}, 'outerHTML');
}

var loadHtml = function loadHtml(_element, url, data, success) {
Expand Down
20 changes: 13 additions & 7 deletions themes/bootstrap5/js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ var VuFind = (function VuFind() {
*
* @param {Element} elm Target element
* @param {string} html HTML
* @param {Object} attrs Any additional attributes
* @param {Object} attrs Any additional attributes (does not work with outerHtml as property)
* @param {string} property Target property ('innerHTML', 'outerHTML' or '' for no HTML update)
*/
function setElementContents(elm, html, attrs = {}, property = 'innerHTML') {
Expand All @@ -349,14 +349,21 @@ var VuFind = (function VuFind() {
}
});

let scriptElement = elm;

if (property === 'innerHTML') {
elm.replaceChildren(...tmpDiv.childNodes);
} else if (property === 'outerHTML') {
scriptElement = elm.parentNode;
elm.replaceWith(...tmpDiv.childNodes);
}

// Set any attributes (N.B. has to be done before scripts in case they rely on the attributes):
Object.entries(attrs).forEach(([attr, value]) => elm.setAttribute(attr, value));
if (property !== 'outerHTML') {
// Set any attributes (N.B. has to be done before scripts in case they rely on the attributes):
Object.entries(attrs).forEach(([attr, value]) => elm.setAttribute(attr, value));
} else if (Object.keys(attrs).length > 0) {
console.error("Incompatible parameter 'attrs' " + JSON.stringify(attrs) + " passed to setElementContents() while 'property' is 'outerHTML'.");
}

// Append any scripts:
scripts.forEach(script => {
Expand All @@ -366,7 +373,7 @@ var VuFind = (function VuFind() {
newScript.src = script.src;
}
newScript.setAttribute('nonce', getCspNonce());
elm.appendChild(newScript);
scriptElement.appendChild(newScript);
});
}

Expand All @@ -386,10 +393,9 @@ var VuFind = (function VuFind() {
*
* @param {Element} elm Target element
* @param {string} html HTML
* @param {Object} attrs Any additional attributes
*/
function setOuterHtml(elm, html, attrs = {}) {
setElementContents(elm, html, attrs, 'outerHTML');
function setOuterHtml(elm, html) {
setElementContents(elm, html, {}, 'outerHTML');
}

var loadHtml = function loadHtml(_element, url, data, success) {
Expand Down
Loading