Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

components: Generate properly escaped HTML.

Anders Kaseorgf8fbae4d8ed1
diff --git a/frontend_tests/node_tests/components.js b/frontend_tests/node_tests/components.js
index 5563fa07f76..48bfe8856f6 100644
--- a/frontend_tests/node_tests/components.js
+++ b/frontend_tests/node_tests/components.js
@@ -118,7 +118,7 @@ run_test("basics", () => {
return self;
})();
- set_global("$", (sel) => {
+ set_global("$", (sel, attributes) => {
if (sel.stub) {
// The component often redundantly re-wraps objects.
return sel;
@@ -129,12 +129,45 @@ run_test("basics", () => {
return switcher;
case "<div class='tab-switcher stream_sorter_toggle'></div>":
return switcher;
- case "<div class='ind-tab' data-tab-key='keyboard-shortcuts' data-tab-id='0' tabindex='0'>translated: Keyboard shortcuts</div>":
- return make_tab(0);
- case "<div class='ind-tab' data-tab-key='message-formatting' data-tab-id='1' tabindex='0'>translated: Message formatting</div>":
- return make_tab(1);
- case "<div class='ind-tab' data-tab-key='search-operators' data-tab-id='2' tabindex='0'>translated: Search operators</div>":
- return make_tab(2);
+ case "<div>": {
+ const tab_id = attributes["data-tab-id"];
+ assert.deepEqual(
+ attributes,
+ [
+ {
+ class: "ind-tab",
+ "data-tab-key": "keyboard-shortcuts",
+ "data-tab-id": 0,
+ tabindex: 0,
+ },
+ {
+ class: "ind-tab",
+ "data-tab-key": "message-formatting",
+ "data-tab-id": 1,
+ tabindex: 0,
+ },
+ {
+ class: "ind-tab",
+ "data-tab-key": "search-operators",
+ "data-tab-id": 2,
+ tabindex: 0,
+ },
+ ][tab_id],
+ );
+ return {
+ text: (text) => {
+ assert.equal(
+ text,
+ [
+ "translated: Keyboard shortcuts",
+ "translated: Message formatting",
+ "translated: Search operators",
+ ][tab_id],
+ );
+ return make_tab(tab_id);
+ },
+ };
+ }
default:
throw new Error("unknown selector: " + sel);
}
diff --git a/static/js/components.js b/static/js/components.js
index 07058d92bcb..88dbd375b21 100644
--- a/static/js/components.js
+++ b/static/js/components.js
@@ -23,15 +23,12 @@ exports.toggle = function (opts) {
for (const [i, value] of opts.values.entries()) {
// create a tab with a tab-id so they don't have to be referenced
// by text value which can be inconsistent.
- const tab = $(
- "<div class='ind-tab' data-tab-key='" +
- value.key +
- "' data-tab-id='" +
- i +
- "' tabindex='0'>" +
- value.label +
- "</div>",
- );
+ const tab = $("<div>", {
+ class: "ind-tab",
+ "data-tab-key": value.key,
+ "data-tab-id": i,
+ tabindex: 0,
+ }).text(value.label);
// add proper classes for styling in CSS.
if (i === 0) {