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.

postprocess: Move list-processing to the top.

Karl Stolley716333c6be60
diff --git a/web/src/postprocess_content.ts b/web/src/postprocess_content.ts
index 4b8c752b6a3..2fc52f9f1fc 100644
--- a/web/src/postprocess_content.ts
+++ b/web/src/postprocess_content.ts
@@ -12,6 +12,20 @@ export function postprocess_content(html: string): string {
const template = inertDocument.createElement("template");
template.innerHTML = html;
+ for (const ol of template.content.querySelectorAll("ol")) {
+ const list_start = Number(ol.getAttribute("start") ?? 1);
+ // We don't count the first item in the list, as it
+ // will be identical to the start value
+ const list_length = ol.children.length - 1;
+ const max_list_counter = list_start + list_length;
+ // We count the characters in the longest list counter,
+ // and use that to offset the list accordingly in CSS
+ const max_list_counter_string_length = max_list_counter.toString().length;
+ ol.classList.add(`counter-length-${max_list_counter_string_length}`);
+ // We subtract 1 from list_start, as `count 0` displays 1.
+ ol.style.setProperty("counter-reset", `count ${list_start - 1}`);
+ }
+
for (const elt of template.content.querySelectorAll("a")) {
// Ensure that all external links have target="_blank"
// rel="opener noreferrer". This ensures that external links
@@ -211,20 +225,6 @@ export function postprocess_content(html: string): string {
}
}
- for (const ol of template.content.querySelectorAll("ol")) {
- const list_start = Number(ol.getAttribute("start") ?? 1);
- // We don't count the first item in the list, as it
- // will be identical to the start value
- const list_length = ol.children.length - 1;
- const max_list_counter = list_start + list_length;
- // We count the characters in the longest list counter,
- // and use that to offset the list accordingly in CSS
- const max_list_counter_string_length = max_list_counter.toString().length;
- ol.classList.add(`counter-length-${max_list_counter_string_length}`);
- // We subtract 1 from list_start, as `count 0` displays 1.
- ol.style.setProperty("counter-reset", `count ${list_start - 1}`);
- }
-
for (const inline_img of template.content.querySelectorAll<HTMLImageElement>(
".message-media-preview-image img",
)) {
diff --git a/web/tests/postprocess_content.test.cjs b/web/tests/postprocess_content.test.cjs
index 1f1bcadda6b..8adab165d73 100644
--- a/web/tests/postprocess_content.test.cjs
+++ b/web/tests/postprocess_content.test.cjs
@@ -13,6 +13,13 @@ const {initialize_user_settings} = zrequire("user_settings");
const user_settings = {web_font_size_px: 16};
initialize_user_settings({user_settings});
+run_test("ordered_lists", () => {
+ assert.equal(
+ postprocess_content('<ol start="9"><li>Nine</li><li>Ten</li></ol>'),
+ '<ol start="9" class="counter-length-2" style="counter-reset: count 8;"><li>Nine</li><li>Ten</li></ol>',
+ );
+});
+
// Care should be taken to present real-world cases here and
// throughout, rather than contrived examples that serve
// only to satisfy 100% test coverage.
@@ -82,13 +89,6 @@ run_test("postprocess_media_and_embeds", () => {
);
});
-run_test("ordered_lists", () => {
- assert.equal(
- postprocess_content('<ol start="9"><li>Nine</li><li>Ten</li></ol>'),
- '<ol start="9" class="counter-length-2" style="counter-reset: count 8;"><li>Nine</li><li>Ten</li></ol>',
- );
-});
-
run_test("inline_image_galleries", ({override}) => {
const thumbnail_formats = [
{