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: DOM text reinterpreted as HTML #160

Merged
merged 1 commit into from
Apr 21, 2024
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
61 changes: 39 additions & 22 deletions js/bootstrap5-toggle.ecmas.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@
* @see https://github.com/palcarazm/bootstrap5-toggle/blob/master/LICENSE
*/


"use strict";
function sanitize(text) {
if (!text) return text; // handle null or undefined
var map = {
"&": "&",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#39;",
"/": "&#x2F;",
};
return text.replace(/[&<>"'/]/g, function (m) {
return map[m];
});
}

(function () {
/**
Expand Down Expand Up @@ -55,75 +68,77 @@
// B: Set options
this.options = {
onlabel:
this.element.getAttribute("data-onlabel") ||
sanitize(this.element.getAttribute("data-onlabel")) ||
options.onlabel ||
DEPRECATION.value ||
DEFAULTS.onlabel,
onstyle:
this.element.getAttribute("data-onstyle") ||
sanitize(this.element.getAttribute("data-onstyle")) ||
options.onstyle ||
DEFAULTS.onstyle,
onvalue:
this.element.getAttribute("value") ||
this.element.getAttribute("data-onvalue") ||
sanitize(this.element.getAttribute("value")) ||
sanitize(this.element.getAttribute("data-onvalue")) ||
options.onvalue ||
DEFAULTS.onvalue,
ontitle:
this.element.getAttribute("data-ontitle") ||
sanitize(this.element.getAttribute("data-ontitle")) ||
options.ontitle ||
this.element.getAttribute("title") ||
sanitize(this.element.getAttribute("title")) ||
DEFAULTS.ontitle,
offlabel:
this.element.getAttribute("data-offlabel") ||
sanitize(this.element.getAttribute("data-offlabel")) ||
options.offlabel ||
DEPRECATION.value ||
DEFAULTS.offlabel,
offstyle:
this.element.getAttribute("data-offstyle") ||
sanitize(this.element.getAttribute("data-offstyle")) ||
options.offstyle ||
DEFAULTS.offstyle,
offvalue:
this.element.getAttribute("data-offvalue") ||
sanitize(this.element.getAttribute("data-offvalue")) ||
options.offvalue ||
DEFAULTS.offvalue,
offtitle:
this.element.getAttribute("data-offtitle") ||
sanitize(this.element.getAttribute("data-offtitle")) ||
options.offtitle ||
this.element.getAttribute("title") ||
sanitize(this.element.getAttribute("title")) ||
DEFAULTS.offtitle,
size:
this.element.getAttribute("data-size") ||
sanitize(this.element.getAttribute("data-size")) ||
options.size ||
DEFAULTS.size,
style:
this.element.getAttribute("data-style") ||
sanitize(this.element.getAttribute("data-style")) ||
options.style ||
DEFAULTS.style,
width:
this.element.getAttribute("data-width") ||
sanitize(this.element.getAttribute("data-width")) ||
options.width ||
DEFAULTS.width,
height:
this.element.getAttribute("data-height") ||
sanitize(this.element.getAttribute("data-height")) ||
options.height ||
DEFAULTS.height,
tabindex:
this.element.getAttribute("tabindex") ||
sanitize(this.element.getAttribute("tabindex")) ||
options.tabindex ||
DEFAULTS.tabindex,
tristate:
this.element.hasAttribute("tristate") ||
options.tristate ||
DEFAULTS.tristate,
name:
this.element.getAttribute("name") || options.name || DEFAULTS.name,
sanitize(this.element.getAttribute("name")) ||
options.name ||
DEFAULTS.name,
};

// C: Check deprecations
if (this.options.onlabel === DEPRECATION.value) {
if (this.element.getAttribute("data-on")) {
if (sanitize(this.element.getAttribute("data-on"))) {
DEPRECATION.log(DEPRECATION.ATTRIBUTE, "data-on", "data-onlabel");
this.options.onlabel = this.element.getAttribute("data-on");
this.options.onlabel = sanitize(this.element.getAttribute("data-on"));
} else if (options.on) {
DEPRECATION.log(DEPRECATION.OPTION, "on", "onlabel");
this.options.onlabel = options.on;
Expand All @@ -132,9 +147,11 @@
}
}
if (this.options.offlabel === DEPRECATION.value) {
if (this.element.getAttribute("data-off")) {
if (sanitize(this.element.getAttribute("data-off"))) {
DEPRECATION.log(DEPRECATION.ATTRIBUTE, "data-off", "data-offlabel");
this.options.offlabel = this.element.getAttribute("data-off");
this.options.offlabel = sanitize(
this.element.getAttribute("data-off")
);
} else if (options.off) {
DEPRECATION.log(DEPRECATION.OPTION, "off", "offlabel");
this.options.offlabel = options.off;
Expand Down
68 changes: 45 additions & 23 deletions js/bootstrap5-toggle.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,23 @@
* @see https://github.com/palcarazm/bootstrap5-toggle/blob/master/LICENSE
*/

"use strict";
function sanitize(text) {
if (!text) return text; // handle null or undefined
var map = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#39;",
"/": "&#x2F;",
};
return text.replace(/[&<>"'/]/g, function (m) {
return map[m];
});
}

+(function ($) {
"use strict";

// TOGGLE PUBLIC CLASS DEFINITION
// ==============================

Expand All @@ -26,13 +39,13 @@

// C: Check deprecations
if (this.options.onlabel === Toggle.DEPRECATION.value) {
if (this.$element.attr("data-on")) {
if (sanitize(this.$element.attr("data-on"))) {
Toggle.DEPRECATION.log(
Toggle.DEPRECATION.ATTRIBUTE,
"data-on",
"data-onlabel"
);
this.options.onlabel = this.$element.attr("data-on");
this.options.onlabel = sanitize(this.$element.attr("data-on"));
} else if (options.on) {
Toggle.DEPRECATION.log(Toggle.DEPRECATION.OPTION, "on", "onlabel");
this.options.onlabel = options.on;
Expand All @@ -41,13 +54,13 @@
}
}
if (this.options.offlabel === Toggle.DEPRECATION.value) {
if (this.$element.attr("data-off")) {
if (sanitize(this.$element.attr("data-off"))) {
Toggle.DEPRECATION.log(
Toggle.DEPRECATION.ATTRIBUTE,
"data-off",
"data-offlabel"
);
this.options.offlabel = this.$element.attr("data-off");
this.options.offlabel = sanitize(this.$element.attr("data-off"));
} else if (options.off) {
Toggle.DEPRECATION.log(Toggle.DEPRECATION.OPTION, "off", "offlabel");
this.options.offlabel = options.off;
Expand Down Expand Up @@ -93,35 +106,44 @@
Toggle.prototype.defaults = function () {
return {
onlabel:
this.$element.attr("data-onlabel") ||
sanitize(this.$element.attr("data-onlabel")) ||
Toggle.DEPRECATION.value ||
Toggle.DEFAULTS.onlabel,
offlabel:
this.$element.attr("data-offlabel") ||
sanitize(this.$element.attr("data-offlabel")) ||
Toggle.DEPRECATION.value ||
Toggle.DEFAULTS.offlabel,
onstyle: this.$element.attr("data-onstyle") || Toggle.DEFAULTS.onstyle,
offstyle: this.$element.attr("data-offstyle") || Toggle.DEFAULTS.offstyle,
onstyle:
sanitize(this.$element.attr("data-onstyle")) || Toggle.DEFAULTS.onstyle,
offstyle:
sanitize(this.$element.attr("data-offstyle")) ||
Toggle.DEFAULTS.offstyle,
onvalue:
this.$element.attr("value") ||
this.$element.attr("data-onvalue") ||
sanitize(this.$element.attr("value")) ||
sanitize(this.$element.attr("data-onvalue")) ||
Toggle.DEFAULTS.onvalue,
offvalue: this.$element.attr("data-offvalue") || Toggle.DEFAULTS.offvalue,
offvalue:
sanitize(this.$element.attr("data-offvalue")) ||
Toggle.DEFAULTS.offvalue,
ontitle:
this.$element.attr("data-ontitle") ||
this.$element.attr("title") ||
sanitize(this.$element.attr("data-ontitle")) ||
sanitize(this.$element.attr("title")) ||
Toggle.DEFAULTS.ontitle,
offtitle:
this.$element.attr("data-offtitle") ||
this.$element.attr("title") ||
sanitize(this.$element.attr("data-offtitle")) ||
sanitize(this.$element.attr("title")) ||
Toggle.DEFAULTS.offtitle,
size: this.$element.attr("data-size") || Toggle.DEFAULTS.size,
style: this.$element.attr("data-style") || Toggle.DEFAULTS.style,
width: this.$element.attr("data-width") || Toggle.DEFAULTS.width,
height: this.$element.attr("data-height") || Toggle.DEFAULTS.height,
tabindex: this.$element.attr("tabindex") || Toggle.DEFAULTS.tabindex,
size: sanitize(this.$element.attr("data-size")) || Toggle.DEFAULTS.size,
style:
sanitize(this.$element.attr("data-style")) || Toggle.DEFAULTS.style,
width:
sanitize(this.$element.attr("data-width")) || Toggle.DEFAULTS.width,
height:
sanitize(this.$element.attr("data-height")) || Toggle.DEFAULTS.height,
tabindex:
sanitize(this.$element.attr("tabindex")) || Toggle.DEFAULTS.tabindex,
tristate: this.$element.is("[tristate]") || Toggle.DEFAULTS.tristate,
name: this.$element.attr("name") || Toggle.DEFAULTS.name,
name: sanitize(this.$element.attr("name")) || Toggle.DEFAULTS.name,
};
};

Expand Down
Loading