2024-07-23 21:07:39 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2024-07-23 21:07:39 +02:00
|
|
|
|
2024-11-12 03:59:37 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace.js");
|
|
|
|
const {run_test} = require("./lib/test.js");
|
2024-07-23 21:19:01 +02:00
|
|
|
|
|
|
|
const thumbnail = mock_esm("../src/thumbnail");
|
2024-07-23 21:07:39 +02:00
|
|
|
|
|
|
|
const {postprocess_content} = zrequire("postprocess_content");
|
2024-10-09 08:44:21 +02:00
|
|
|
const {initialize_user_settings} = zrequire("user_settings");
|
|
|
|
|
|
|
|
const user_settings = {};
|
|
|
|
initialize_user_settings({user_settings});
|
2024-07-23 21:07:39 +02:00
|
|
|
|
|
|
|
run_test("postprocess_content", () => {
|
|
|
|
assert.equal(
|
|
|
|
postprocess_content(
|
|
|
|
'<a href="http://example.com">good</a> ' +
|
|
|
|
'<a href="http://zulip.zulipdev.com/user_uploads/w/ha/tever/file.png">upload</a> ' +
|
|
|
|
'<a href="http://localhost:NNNN">invalid</a> ' +
|
|
|
|
'<a href="javascript:alert(1)">unsafe</a> ' +
|
|
|
|
'<a href="/#fragment" target="_blank">fragment</a>' +
|
|
|
|
'<div class="message_inline_image">' +
|
|
|
|
'<a href="http://zulip.zulipdev.com/user_uploads/w/ha/tever/inline.png" title="inline image">upload</a> ' +
|
|
|
|
'<a role="button">button</a> ' +
|
|
|
|
"</div>",
|
|
|
|
),
|
|
|
|
'<a href="http://example.com" target="_blank" rel="noopener noreferrer" title="http://example.com/">good</a> ' +
|
|
|
|
'<a href="http://zulip.zulipdev.com/user_uploads/w/ha/tever/file.png" target="_blank" rel="noopener noreferrer" title="translated: Download file.png">upload</a> ' +
|
|
|
|
"<a>invalid</a> " +
|
|
|
|
"<a>unsafe</a> " +
|
|
|
|
'<a href="/#fragment" title="http://zulip.zulipdev.com/#fragment">fragment</a>' +
|
|
|
|
'<div class="message_inline_image">' +
|
|
|
|
'<a href="http://zulip.zulipdev.com/user_uploads/w/ha/tever/inline.png" target="_blank" rel="noopener noreferrer" aria-label="inline image">upload</a> ' +
|
|
|
|
'<a role="button">button</a> ' +
|
|
|
|
"</div>",
|
|
|
|
);
|
|
|
|
});
|
2024-07-23 21:19:01 +02:00
|
|
|
|
|
|
|
run_test("message_inline_animated_image_still", ({override}) => {
|
|
|
|
const thumbnail_formats = [
|
|
|
|
{
|
|
|
|
name: "840x560-anim.webp",
|
|
|
|
max_width: 840,
|
|
|
|
max_height: 560,
|
|
|
|
format: "webp",
|
|
|
|
animated: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "840x560.webp",
|
|
|
|
max_width: 840,
|
|
|
|
max_height: 560,
|
|
|
|
format: "webp",
|
|
|
|
animated: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "300x200-anim.webp",
|
|
|
|
max_width: 300,
|
|
|
|
max_height: 200,
|
|
|
|
format: "webp",
|
|
|
|
animated: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "300x200.webp",
|
|
|
|
max_width: 300,
|
|
|
|
max_height: 200,
|
|
|
|
format: "webp",
|
|
|
|
animated: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "300x200.jpg",
|
|
|
|
max_width: 300,
|
|
|
|
max_height: 200,
|
|
|
|
format: "jpg",
|
|
|
|
animated: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
// TODO: Initialize the real thumbnail.ts rather than mocking it.
|
|
|
|
override(thumbnail, "preferred_format", thumbnail_formats[3]);
|
|
|
|
override(thumbnail, "animated_format", thumbnail_formats[2]);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
postprocess_content(
|
|
|
|
'<div class="message_inline_image">' +
|
|
|
|
'<a href="/user_uploads/path/to/image.png" title="image.png">' +
|
|
|
|
'<img data-original-dimensions="3264x2448" src="/user_uploads/thumbnail/path/to/image.png/840x560.webp">' +
|
|
|
|
"</a>" +
|
|
|
|
"</div>",
|
|
|
|
),
|
|
|
|
'<div class="message_inline_image">' +
|
|
|
|
'<a href="/user_uploads/path/to/image.png" target="_blank" rel="noopener noreferrer" aria-label="image.png">' +
|
2024-08-02 20:47:47 +02:00
|
|
|
'<img data-original-dimensions="3264x2448" src="/user_uploads/thumbnail/path/to/image.png/300x200.webp" loading="lazy">' +
|
2024-07-23 21:19:01 +02:00
|
|
|
"</a>" +
|
|
|
|
"</div>",
|
|
|
|
);
|
|
|
|
|
|
|
|
// Now verify the behavior for animated images.
|
|
|
|
override(user_settings, "web_animate_image_previews", "always");
|
|
|
|
assert.equal(
|
|
|
|
postprocess_content(
|
|
|
|
'<div class="message_inline_image">' +
|
|
|
|
'<a href="/user_uploads/path/to/image.png" title="image.png">' +
|
|
|
|
'<img data-original-dimensions="3264x2448" src="/user_uploads/thumbnail/path/to/image.png/840x560.webp" data-animated="true">' +
|
|
|
|
"</a>" +
|
|
|
|
"</div>",
|
|
|
|
),
|
|
|
|
'<div class="message_inline_image">' +
|
|
|
|
'<a href="/user_uploads/path/to/image.png" target="_blank" rel="noopener noreferrer" aria-label="image.png">' +
|
2024-08-02 20:47:47 +02:00
|
|
|
'<img data-original-dimensions="3264x2448" src="/user_uploads/thumbnail/path/to/image.png/300x200-anim.webp" data-animated="true" loading="lazy">' +
|
2024-07-23 21:19:01 +02:00
|
|
|
"</a>" +
|
|
|
|
"</div>",
|
|
|
|
);
|
|
|
|
|
|
|
|
// And verify the different behavior for other values of the animation setting.
|
|
|
|
override(user_settings, "web_animate_image_previews", "on_hover");
|
|
|
|
assert.equal(
|
|
|
|
postprocess_content(
|
|
|
|
'<div class="message_inline_image">' +
|
|
|
|
'<a href="/user_uploads/path/to/image.png" title="image.png">' +
|
|
|
|
'<img data-original-dimensions="3264x2448" src="/user_uploads/thumbnail/path/to/image.png/840x560.webp" data-animated="true">' +
|
|
|
|
"</a>" +
|
|
|
|
"</div>",
|
|
|
|
),
|
|
|
|
'<div class="message_inline_image message_inline_animated_image_still">' +
|
|
|
|
'<a href="/user_uploads/path/to/image.png" target="_blank" rel="noopener noreferrer" aria-label="image.png">' +
|
2024-08-02 20:47:47 +02:00
|
|
|
'<img data-original-dimensions="3264x2448" src="/user_uploads/thumbnail/path/to/image.png/300x200.webp" data-animated="true" loading="lazy">' +
|
2024-07-23 21:19:01 +02:00
|
|
|
"</a>" +
|
|
|
|
"</div>",
|
|
|
|
);
|
|
|
|
|
|
|
|
override(user_settings, "web_animate_image_previews", "never");
|
|
|
|
assert.equal(
|
|
|
|
postprocess_content(
|
|
|
|
'<div class="message_inline_image">' +
|
|
|
|
'<a href="/user_uploads/path/to/image.png" title="image.png">' +
|
|
|
|
'<img data-original-dimensions="3264x2448" src="/user_uploads/thumbnail/path/to/image.png/840x560.webp" data-animated="true">' +
|
|
|
|
"</a>" +
|
|
|
|
"</div>",
|
|
|
|
),
|
|
|
|
'<div class="message_inline_image message_inline_animated_image_still">' +
|
|
|
|
'<a href="/user_uploads/path/to/image.png" target="_blank" rel="noopener noreferrer" aria-label="image.png">' +
|
2024-08-02 20:47:47 +02:00
|
|
|
'<img data-original-dimensions="3264x2448" src="/user_uploads/thumbnail/path/to/image.png/300x200.webp" data-animated="true" loading="lazy">' +
|
2024-07-23 21:19:01 +02:00
|
|
|
"</a>" +
|
|
|
|
"</div>",
|
|
|
|
);
|
|
|
|
});
|