2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
const {JSDOM} = require("jsdom");
|
|
|
|
|
2021-03-10 06:10:32 +01:00
|
|
|
const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
|
2021-03-11 05:43:45 +01:00
|
|
|
const jquery = require("../zjsunit/real_jquery");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-03-25 22:35:45 +01:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
const {window} = new JSDOM("<!DOCTYPE html><p>Hello world</p>");
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2021-06-15 14:54:53 +02:00
|
|
|
const {document} = window;
|
2021-03-11 05:43:45 +01:00
|
|
|
const $ = jquery(window);
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2023-02-22 23:03:47 +01:00
|
|
|
const compose_ui = mock_esm("../../web/src/compose_ui");
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("document", document);
|
2017-11-23 21:17:08 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const copy_and_paste = zrequire("copy_and_paste");
|
2017-11-23 21:17:08 +01:00
|
|
|
|
2019-01-19 18:15:17 +01:00
|
|
|
// Super stripped down version of the code in the drag-mock library
|
|
|
|
// https://github.com/andywer/drag-mock/blob/6d46c7c0ffd6a4d685e6612a90cd58cda80f30fc/src/DataTransfer.js
|
2022-04-19 02:45:44 +02:00
|
|
|
class DataTransfer {
|
|
|
|
dataByFormat = {};
|
|
|
|
getData(dataFormat) {
|
|
|
|
return this.dataByFormat[dataFormat];
|
|
|
|
}
|
|
|
|
setData(dataFormat, data) {
|
|
|
|
this.dataByFormat[dataFormat] = data;
|
|
|
|
}
|
|
|
|
}
|
2019-01-19 18:15:17 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const createPasteEvent = function () {
|
|
|
|
const clipboardData = new DataTransfer();
|
2020-07-15 01:29:15 +02:00
|
|
|
const pasteEvent = new window.Event("paste");
|
2019-01-19 18:15:17 +01:00
|
|
|
pasteEvent.clipboardData = clipboardData;
|
2020-12-11 04:26:23 +01:00
|
|
|
return new $.Event(pasteEvent);
|
2019-01-19 18:15:17 +01:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("paste_handler", () => {
|
2021-04-03 19:07:13 +02:00
|
|
|
page_params.development_environment = true;
|
2020-07-15 00:34:28 +02:00
|
|
|
let input =
|
|
|
|
'<meta http-equiv="content-type" content="text/html; charset=utf-8"><span style="color: hsl(0, 0%, 13%); font-family: arial, sans-serif; font-size: 12.8px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 100%); text-decoration-style: initial; text-decoration-color: initial;"><span> </span>love the<span> </span><b>Zulip</b><b> </b></span><b style="color: hsl(0, 0%, 13%); font-family: arial, sans-serif; font-size: 12.8px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 100%); text-decoration-style: initial; text-decoration-color: initial;">Organization</b><span style="color: hsl(0, 0%, 13%); font-family: arial, sans-serif; font-size: 12.8px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 100%); text-decoration-style: initial; text-decoration-color: initial;">.</span>';
|
|
|
|
assert.equal(
|
|
|
|
copy_and_paste.paste_handler_converter(input),
|
|
|
|
" love the **Zulip** **Organization**.",
|
|
|
|
);
|
|
|
|
|
|
|
|
input =
|
|
|
|
'<meta http-equiv="content-type" content="text/html; charset=utf-8"><span style="color: hsl(210, 12%, 16%); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 100%); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">The<span> </span></span><code style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 13.6px; padding: 0.2em 0.4em; margin: 0px; background-color: hsla(210, 13%, 12%, 0.05); border-radius: 3px; color: hsl(210, 12%, 16%); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">JSDOM</code><span style="color: hsl(210, 12%, 16%); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 100%); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"><span> </span>constructor</span>';
|
|
|
|
assert.equal(copy_and_paste.paste_handler_converter(input), "The `JSDOM` constructor");
|
|
|
|
|
|
|
|
input =
|
2021-02-23 06:58:00 +01:00
|
|
|
'<meta http-equiv="content-type" content="text/html; charset=utf-8"><a href="https://zulip.readthedocs.io/en/latest/subsystems/logging.html" target="_blank" title="https://zulip.readthedocs.io/en/latest/subsystems/logging.html" style="color: hsl(200, 100%, 40%); text-decoration: none; cursor: pointer; font-family: "Source Sans 3", "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 100%);">https://zulip.readthedocs.io/en/latest/subsystems/logging.html</a>';
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
copy_and_paste.paste_handler_converter(input),
|
|
|
|
"https://zulip.readthedocs.io/en/latest/subsystems/logging.html",
|
|
|
|
);
|
|
|
|
|
|
|
|
input =
|
2022-08-25 20:24:50 +02:00
|
|
|
'<meta http-equiv="content-type" content="text/html; charset=utf-8"><a class="reference external" href="https://zulip.readthedocs.io/en/latest/contributing/contributing.html" style="box-sizing: border-box; color: hsl(283, 39%, 53%); text-decoration: none; cursor: pointer; outline: 0px; font-family: Lato, proxima-nova, "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 99%);">Contributing guide</a>';
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
copy_and_paste.paste_handler_converter(input),
|
2022-08-25 20:24:50 +02:00
|
|
|
"[Contributing guide](https://zulip.readthedocs.io/en/latest/contributing/contributing.html)",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
input =
|
|
|
|
'<meta http-equiv="content-type" content="text/html; charset=utf-8"><span style="color: hsl(0, 0%, 0%); font-family: "Helvetica Neue", "Segoe UI", Helvetica, Arial, sans-serif; font-size: 13px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 100%); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">1. text</span>';
|
|
|
|
assert.equal(copy_and_paste.paste_handler_converter(input), "1. text");
|
|
|
|
|
|
|
|
input =
|
|
|
|
'<meta http-equiv="content-type" content="text/html; charset=utf-8"><h1 style="box-sizing: border-box; font-size: 2em; margin-top: 0px !important; margin-right: 0px; margin-bottom: 16px; margin-left: 0px; font-weight: 600; line-height: 1.25; padding-bottom: 0.3em; border-bottom: 1px solid hsl(216, 14%, 93%); color: hsl(210, 12%, 16%); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Zulip overview</h1>';
|
|
|
|
assert.equal(copy_and_paste.paste_handler_converter(input), "Zulip overview");
|
|
|
|
|
|
|
|
input =
|
|
|
|
'<meta http-equiv="content-type" content="text/html; charset=utf-8"><i style="box-sizing: inherit; color: hsl(0, 0%, 0%); font-family: Verdana, sans-serif; font-size: 15px; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: hsl(0, 0%, 100%); text-decoration-style: initial; text-decoration-color: initial;">This text is italic</i>';
|
|
|
|
assert.equal(copy_and_paste.paste_handler_converter(input), "*This text is italic*");
|
|
|
|
|
|
|
|
input =
|
2021-05-10 07:02:14 +02:00
|
|
|
'<div class="preview-content"><div class="comment"><div class="comment-body markdown-body js-preview-body" style="min-height: 131px;"><p>Test list:</p><ul><li>Item 1</li><li>Item 2</li></ul></div></div></div>';
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
copy_and_paste.paste_handler_converter(input),
|
2021-05-10 07:02:14 +02:00
|
|
|
"Test list:\n* Item 1\n* Item 2",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
input =
|
2021-05-10 07:02:14 +02:00
|
|
|
'<div class="ace-line gutter-author-d-iz88z86z86za0dz67zz78zz78zz74zz68zjz80zz71z9iz90za3z66zs0z65zz65zq8z75zlaz81zcz66zj6g2mz78zz76zmz66z22z75zfcz69zz66z ace-ltr focused-line" dir="auto" id="editor-3-ace-line-41"><span>Test list:</span></div><div class="ace-line gutter-author-d-iz88z86z86za0dz67zz78zz78zz74zz68zjz80zz71z9iz90za3z66zs0z65zz65zq8z75zlaz81zcz66zj6g2mz78zz76zmz66z22z75zfcz69zz66z line-list-type-bullet ace-ltr" dir="auto" id="editor-3-ace-line-42"><ul class="listtype-bullet listindent1 list-bullet1"><li><span class="ace-line-pocket-zws" data-faketext="" data-contentcollector-ignore-space-at="end"></span><span class="ace-line-pocket" data-faketext="" contenteditable="false"></span><span class="ace-line-pocket-zws" data-faketext="" data-contentcollector-ignore-space-at="start"></span><span>Item 1</span></li></ul></div><div class="ace-line gutter-author-d-iz88z86z86za0dz67zz78zz78zz74zz68zjz80zz71z9iz90za3z66zs0z65zz65zq8z75zlaz81zcz66zj6g2mz78zz76zmz66z22z75zfcz69zz66z line-list-type-bullet ace-ltr" dir="auto" id="editor-3-ace-line-43"><ul class="listtype-bullet listindent1 list-bullet1"><li><span class="ace-line-pocket-zws" data-faketext="" data-contentcollector-ignore-space-at="end"></span><span class="ace-line-pocket" data-faketext="" contenteditable="false"></span><span class="ace-line-pocket-zws" data-faketext="" data-contentcollector-ignore-space-at="start"></span><span>Item 2</span></li></ul></div>';
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
copy_and_paste.paste_handler_converter(input),
|
2021-05-10 07:02:14 +02:00
|
|
|
"Test list:\n* Item 1\n* Item 2",
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2019-01-19 18:15:17 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let data = "<p>text</p>";
|
2019-11-02 00:06:25 +01:00
|
|
|
let event = createPasteEvent();
|
2020-07-15 01:29:15 +02:00
|
|
|
event.originalEvent.clipboardData.setData("text/html", data);
|
2019-11-02 00:06:25 +01:00
|
|
|
let insert_syntax_and_focus_called = false;
|
2019-01-19 18:15:17 +01:00
|
|
|
compose_ui.insert_syntax_and_focus = function () {
|
|
|
|
insert_syntax_and_focus_called = true;
|
|
|
|
};
|
|
|
|
copy_and_paste.paste_handler(event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(insert_syntax_and_focus_called);
|
2019-01-19 18:15:17 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
data =
|
|
|
|
'<meta http-equiv="content-type" content="text/html; charset=utf-8"><img src="http://localhost:9991/thumbnail?url=user_uploads%2F1%2Fe2%2FHPMCcGWOG9rS2M4ybHN8sEzh%2Fpasted_image.png&size=full"/>';
|
2019-01-19 18:15:17 +01:00
|
|
|
event = createPasteEvent();
|
2020-07-15 01:29:15 +02:00
|
|
|
event.originalEvent.clipboardData.setData("text/html", data);
|
2019-01-19 18:15:17 +01:00
|
|
|
insert_syntax_and_focus_called = false;
|
|
|
|
copy_and_paste.paste_handler(event);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!insert_syntax_and_focus_called);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|