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.

zjquery: Move attribute accessors to FakeElement.

Anders Kaseorg65f3e224a238
diff --git a/web/tests/lib/zjquery.cjs b/web/tests/lib/zjquery.cjs
index 98563db8545..0437afdeeb8 100644
--- a/web/tests/lib/zjquery.cjs
+++ b/web/tests/lib/zjquery.cjs
@@ -87,7 +87,7 @@ function make_zjquery() {
// they want to be wrapped by jQuery (so they can
// in turn return stubs). The convention is that
// they provide a to_$ attribute.
- if (arg.to_$) {
+ if (typeof arg === "object" && "to_$" in arg) {
assert.equal(typeof arg.to_$, "function");
return arg.to_$();
}
diff --git a/web/tests/lib/zjquery_element.cjs b/web/tests/lib/zjquery_element.cjs
index bb1c4ebe881..d6f7a4f1cbb 100644
--- a/web/tests/lib/zjquery_element.cjs
+++ b/web/tests/lib/zjquery_element.cjs
@@ -4,8 +4,6 @@ const assert = require("node:assert/strict");
const FakeEvent = require("./zjquery_event.cjs");
-const noop = function () {};
-
function split_words(x) {
return Array.isArray(x) ? x : x.trim().split(/\s+/);
}
@@ -62,12 +60,25 @@ class FakeClassList extends RejectMissing {
}
class FakeElementState {
+ jquery_data = new Map();
selector = undefined;
shown = false;
}
const fake_element_state = new WeakMap();
+function decamel(s) {
+ return s.replaceAll(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
+}
+
+function normalize_attribute(name) {
+ return String(name).replaceAll(/[A-Z]/g, (c) => c.toLowerCase());
+}
+
+function dataset_key_to_attribute(key) {
+ return `data-${decamel(key)}`;
+}
+
class FakeElement extends RejectMissing {
_tippy = undefined;
classList = new FakeClassList();
@@ -75,10 +86,24 @@ class FakeElement extends RejectMissing {
textContent = "never-been-set";
value = undefined;
+ #attributes = new Map();
+
constructor() {
super();
fake_element_state.set(this, new FakeElementState());
}
+ hasAttribute(name) {
+ return this.#attributes.has(normalize_attribute(name));
+ }
+ getAttribute(name) {
+ return this.#attributes.get(normalize_attribute(name)) ?? null;
+ }
+ removeAttribute(name) {
+ this.#attributes.delete(normalize_attribute(name));
+ }
+ setAttribute(name, value) {
+ this.#attributes.set(normalize_attribute(name), String(value));
+ }
}
// TODO: convert this to a true class
@@ -88,7 +113,6 @@ exports.FakeJQuery = function (selector, opts) {
const find_results = new Map();
let $my_parent;
const parents_result = new Map();
- const attrs = new Map();
const event_store = make_event_store(selector);
const $self = {
@@ -110,18 +134,50 @@ exports.FakeJQuery = function (selector, opts) {
assert.notEqual(typeof arg, "string");
return this;
},
- attr(name, val) {
- if (val === undefined) {
- return attrs.get(name);
+ attr(name, ...args) {
+ assert.notEqual(name, undefined);
+ if (args.length === 0) {
+ return 0 in this ? (this[0].getAttribute(name) ?? undefined) : undefined;
+ }
+ const [value] = args;
+ for (const element of this) {
+ element.setAttribute(name, value);
}
- attrs.set(name, val);
return this;
},
- data(name, val) {
- if (val === undefined) {
- return attrs.get("data-" + name);
+ data(key, ...args) {
+ if (args.length === 0) {
+ if (!(0 in this)) {
+ return undefined;
+ }
+ const state = fake_element_state.get(this[0]);
+ if (state.jquery_data.has(key)) {
+ return state.jquery_data.get(key);
+ }
+ let value = this[0].getAttribute(dataset_key_to_attribute(key));
+
+ if (value === null) {
+ return null;
+ }
+
+ if (/^true$|^false$|^null$|^{.*}$|^\[.*]$/s.test(value)) {
+ try {
+ value = JSON.parse(value);
+ } catch {
+ // use the unparsed value
+ }
+ } else if (Number(value).toString() === value) {
+ value = Number(value);
+ }
+
+ state.jquery_data.set(key, value);
+ return value;
+ }
+
+ const [value] = args;
+ for (const element of this) {
+ fake_element_state.get(element).jquery_data.set(key, value);
}
- attrs.set("data-" + name, val);
return this;
},
each(callback) {
@@ -260,7 +316,9 @@ exports.FakeJQuery = function (selector, opts) {
return this;
},
removeAttr(name) {
- attrs.delete(name);
+ for (const element of this) {
+ element.removeAttribute(name);
+ }
return this;
},
removeClass(class_names) {
@@ -281,7 +339,16 @@ exports.FakeJQuery = function (selector, opts) {
for node testing.
`);
},
- removeData: noop,
+ removeData(keys) {
+ keys = split_words(keys);
+ for (const element of this) {
+ const state = fake_element_state.get(element);
+ for (const key of keys) {
+ state.jquery_data.delete(key);
+ }
+ }
+ return this;
+ },
set_find_results(find_selector, $jquery_object) {
assert.notEqual(
$jquery_object,
diff --git a/web/tests/zjquery.test.cjs b/web/tests/zjquery.test.cjs
index 9e07e503d33..1ec064669a2 100644
--- a/web/tests/zjquery.test.cjs
+++ b/web/tests/zjquery.test.cjs
@@ -48,15 +48,15 @@ run_test("basics", () => {
const $widget = $("#my-widget");
$widget.attr("data-employee-id", 42);
- assert.equal($widget.attr("data-employee-id"), 42);
+ assert.equal($widget.attr("data-employee-id"), "42");
assert.equal($widget.data("employee-id"), 42);
$widget.data("department-id", 77);
- assert.equal($widget.attr("data-department-id"), 77);
+ assert.equal($widget.attr("data-department-id"), undefined);
assert.equal($widget.data("department-id"), 77);
$widget.data("department-name", "hr");
- assert.equal($widget.attr("data-department-name"), "hr");
+ assert.equal($widget.attr("data-department-name"), undefined);
assert.equal($widget.data("department-name"), "hr");
$widget.html("<b>hello</b>"); // eslint-disable-line no-jquery/no-parse-html-literal