decidim-core 0.31.3 → 0.31.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/app/cells/decidim/content_blocks/html_cell.rb +1 -1
  3. data/app/cells/decidim/content_blocks/static_page/section_cell.rb +1 -1
  4. data/app/cells/decidim/content_blocks/static_page/summary_cell.rb +1 -1
  5. data/app/cells/decidim/content_blocks/static_page/two_pane_section_cell.rb +2 -2
  6. data/app/cells/decidim/data_consent/category.erb +5 -5
  7. data/app/cells/decidim/nav_links/show.erb +3 -3
  8. data/app/cells/decidim/participatory_space_private_user/show.erb +6 -6
  9. data/app/cells/decidim/participatory_space_private_user_cell.rb +0 -4
  10. data/app/cells/decidim/upload_modal_cell.rb +5 -0
  11. data/app/controllers/decidim/download_your_data_controller.rb +1 -1
  12. data/app/controllers/decidim/notifications_subscriptions_controller.rb +8 -0
  13. data/app/controllers/decidim/private_downloads_controller.rb +29 -0
  14. data/app/helpers/decidim/mailer_helper.rb +36 -0
  15. data/app/helpers/decidim/menu_helper.rb +2 -1
  16. data/app/helpers/decidim/newsletters_helper.rb +4 -22
  17. data/app/mailers/decidim/application_mailer.rb +4 -0
  18. data/app/models/decidim/attachment.rb +20 -2
  19. data/app/models/decidim/authorization.rb +7 -0
  20. data/app/models/decidim/private_download.rb +61 -0
  21. data/app/models/decidim/private_export.rb +6 -0
  22. data/app/packs/src/decidim/controllers/accordion/accordion.test.js +118 -0
  23. data/app/packs/src/decidim/controllers/accordion/controller.js +24 -0
  24. data/app/packs/src/decidim/controllers/dropdown/controller.js +26 -0
  25. data/app/packs/src/decidim/controllers/dropdown/dropdown.test.js +187 -0
  26. data/app/packs/src/decidim/controllers/form_validator/form_validator.js +3 -2
  27. data/app/packs/src/decidim/controllers/form_validator/form_validator.test.js +5 -0
  28. data/app/packs/src/decidim/editor/extensions/image/index.js +49 -11
  29. data/app/packs/src/decidim/editor/extensions/image/node_view.js +9 -1
  30. data/app/packs/src/decidim/editor/extensions/link/bubble_menu.js +34 -6
  31. data/app/packs/src/decidim/editor/extensions/link/index.js +45 -12
  32. data/app/packs/src/decidim/editor/test/extensions/image_links.test.js +161 -0
  33. data/app/packs/src/decidim/sw/push-permissions.js +48 -13
  34. data/app/packs/stylesheets/decidim/_rich_text.scss +17 -0
  35. data/app/packs/stylesheets/decidim/editor.scss +10 -0
  36. data/app/presenters/decidim/menu_item_presenter.rb +7 -1
  37. data/app/services/decidim/notifications_subscriptions_persistor.rb +6 -0
  38. data/app/services/decidim/push_subscription_endpoint_validator.rb +34 -0
  39. data/app/services/decidim/send_push_notification.rb +5 -1
  40. data/app/views/decidim/devise/registrations/new.html.erb +1 -0
  41. data/app/views/decidim/devise/shared/_tos_fields.html.erb +3 -3
  42. data/app/views/decidim/notification_mailer/event_received.html.erb +3 -3
  43. data/app/views/decidim/notifications_settings/show.html.erb +5 -5
  44. data/app/views/decidim/pages/_tabbed.html.erb +3 -3
  45. data/app/views/decidim/shared/_filters.html.erb +5 -5
  46. data/app/views/decidim/shared/filters/_check_boxes_tree.html.erb +1 -1
  47. data/app/views/decidim/shared/filters/_collection.html.erb +1 -1
  48. data/config/locales/ca-IT.yml +1 -0
  49. data/config/locales/ca.yml +1 -0
  50. data/config/locales/cs.yml +2 -0
  51. data/config/locales/de.yml +27 -0
  52. data/config/locales/en.yml +1 -0
  53. data/config/locales/es-MX.yml +1 -0
  54. data/config/locales/es-PY.yml +1 -0
  55. data/config/locales/es.yml +1 -0
  56. data/config/locales/eu.yml +4 -0
  57. data/config/locales/fi-plain.yml +5 -0
  58. data/config/locales/fi.yml +7 -2
  59. data/config/locales/fr-CA.yml +1 -0
  60. data/config/locales/fr.yml +1 -0
  61. data/config/locales/it.yml +10 -0
  62. data/config/locales/pt-BR.yml +1 -1
  63. data/config/locales/sk.yml +1417 -0
  64. data/config/locales/sv.yml +1 -0
  65. data/config/routes.rb +1 -0
  66. data/lib/decidim/content_parsers/blob_parser.rb +3 -3
  67. data/lib/decidim/content_renderers/blob_renderer.rb +2 -2
  68. data/lib/decidim/core/test/shared_examples/participatory_space_members_shared_examples.rb +121 -0
  69. data/lib/decidim/core/version.rb +1 -1
  70. data/lib/decidim/participatory_space_user.rb +1 -1
  71. metadata +14 -6
@@ -0,0 +1,187 @@
1
+ /* global jest */
2
+ /* eslint max-lines: ["error", 400] */
3
+
4
+ import DropdownController from "src/decidim/controllers/dropdown/controller";
5
+
6
+ jest.mock("a11y-dropdown-component", () => ({
7
+ render: jest.fn(),
8
+ destroy: jest.fn()
9
+ }));
10
+
11
+ describe("DropdownController", () => {
12
+ let element = null;
13
+ let dropdownMenuEl = null;
14
+ let controller = null;
15
+
16
+ const createController = (controllerElement) => {
17
+ const ControllerClass = DropdownController;
18
+ const instance = Object.create(ControllerClass.prototype);
19
+ Reflect.defineProperty(instance, "element", {
20
+ get: () => controllerElement,
21
+ configurable: true
22
+ });
23
+ return instance;
24
+ };
25
+
26
+ beforeEach(() => {
27
+ window.matchMedia = jest.fn().mockImplementation((query) => ({
28
+ matches: false,
29
+ media: query,
30
+ addListener: jest.fn(),
31
+ removeListener: jest.fn()
32
+ }));
33
+
34
+ document.body.innerHTML = `
35
+ <button
36
+ id="dropdown-trigger"
37
+ data-controller="dropdown"
38
+ data-target="dropdown-menu"
39
+ data-open-md="true"
40
+ data-auto-close="true"
41
+ >
42
+ Dropdown Trigger
43
+ </button>
44
+ <ul id="dropdown-menu" class="dropdown-menu">
45
+ <li><a href="/link1">Link 1</a></li>
46
+ <li><a href="/link2">Link 2</a></li>
47
+ <li><a href="/link3">Link 3</a></li>
48
+ </ul>
49
+ `;
50
+
51
+ element = document.getElementById("dropdown-trigger");
52
+ dropdownMenuEl = document.getElementById("dropdown-menu");
53
+ controller = createController(element);
54
+ });
55
+
56
+ afterEach(() => {
57
+ document.body.innerHTML = "";
58
+ Reflect.deleteProperty(window, "matchMedia");
59
+ });
60
+
61
+ describe("removeAriaRoles", () => {
62
+ it("removes role attribute from dropdown menu", () => {
63
+ dropdownMenuEl.setAttribute("role", "menu");
64
+
65
+ controller.removeAriaRoles();
66
+
67
+ expect(dropdownMenuEl.getAttribute("role")).toBeNull();
68
+ });
69
+
70
+ it("removes aria-labelledby attribute from dropdown menu", () => {
71
+ dropdownMenuEl.setAttribute("aria-labelledby", "trigger");
72
+
73
+ controller.removeAriaRoles();
74
+
75
+ expect(dropdownMenuEl.getAttribute("aria-labelledby")).toBeNull();
76
+ });
77
+
78
+ it("removes tabindex attribute from dropdown menu", () => {
79
+ dropdownMenuEl.setAttribute("tabindex", "-1");
80
+
81
+ controller.removeAriaRoles();
82
+
83
+ expect(dropdownMenuEl.getAttribute("tabindex")).toBeNull();
84
+ });
85
+
86
+ it("removes role from li elements", () => {
87
+ const li = dropdownMenuEl.querySelector("li");
88
+ li.setAttribute("role", "none");
89
+
90
+ controller.removeAriaRoles();
91
+
92
+ expect(li.getAttribute("role")).toBeNull();
93
+ });
94
+
95
+ it("removes role from all li elements", () => {
96
+ const listItems = dropdownMenuEl.querySelectorAll("li");
97
+ listItems.forEach((li) => {
98
+ li.setAttribute("role", "none");
99
+ });
100
+
101
+ controller.removeAriaRoles();
102
+
103
+ listItems.forEach((li) => {
104
+ expect(li.getAttribute("role")).toBeNull();
105
+ });
106
+ });
107
+
108
+ it("removes role from anchor elements", () => {
109
+ const anchor = dropdownMenuEl.querySelector("a");
110
+ anchor.setAttribute("role", "menuitem");
111
+
112
+ controller.removeAriaRoles();
113
+
114
+ expect(anchor.getAttribute("role")).toBeNull();
115
+ });
116
+
117
+ it("removes tabindex from anchor elements", () => {
118
+ const anchor = dropdownMenuEl.querySelector("a");
119
+ anchor.setAttribute("tabindex", "-1");
120
+
121
+ controller.removeAriaRoles();
122
+
123
+ expect(anchor.getAttribute("tabindex")).toBeNull();
124
+ });
125
+
126
+ it("handles missing dropdown menu gracefully", () => {
127
+ const mockElement = document.createElement("button");
128
+ mockElement.dataset.target = "nonexistent-menu";
129
+ const mockController = createController(mockElement);
130
+
131
+ expect(() => {
132
+ mockController.removeAriaRoles();
133
+ }).not.toThrow();
134
+ });
135
+
136
+ it("handles elements without the attributes gracefully", () => {
137
+ expect(() => {
138
+ controller.removeAriaRoles();
139
+ }).not.toThrow();
140
+
141
+ expect(dropdownMenuEl.getAttribute("role")).toBeNull();
142
+ });
143
+ });
144
+
145
+ describe("data-add-aria-roles option", () => {
146
+ it("keeps role menu when data-add-aria-roles is true", () => {
147
+ element.setAttribute("data-add-aria-roles", "true");
148
+ dropdownMenuEl.setAttribute("role", "menu");
149
+ dropdownMenuEl.querySelector("li").setAttribute("role", "none");
150
+ dropdownMenuEl.querySelector("a").setAttribute("role", "menuitem");
151
+ const testController = createController(element);
152
+
153
+ testController.connect();
154
+
155
+ expect(dropdownMenuEl.getAttribute("role")).toBe("menu");
156
+ expect(dropdownMenuEl.querySelector("li").getAttribute("role")).toBe("none");
157
+ expect(dropdownMenuEl.querySelector("a").getAttribute("role")).toBe("menuitem");
158
+ });
159
+
160
+ it("keeps role menu when data-add-aria-roles is not set (default)", () => {
161
+ dropdownMenuEl.setAttribute("role", "menu");
162
+ dropdownMenuEl.querySelector("li").setAttribute("role", "none");
163
+ dropdownMenuEl.querySelector("a").setAttribute("role", "menuitem");
164
+ const testController = createController(element);
165
+
166
+ testController.connect();
167
+
168
+ expect(dropdownMenuEl.getAttribute("role")).toBe("menu");
169
+ expect(dropdownMenuEl.querySelector("li").getAttribute("role")).toBe("none");
170
+ expect(dropdownMenuEl.querySelector("a").getAttribute("role")).toBe("menuitem");
171
+ });
172
+
173
+ it("removes role menu when data-add-aria-roles is false", () => {
174
+ element.setAttribute("data-add-aria-roles", "false");
175
+ dropdownMenuEl.setAttribute("role", "menu");
176
+ dropdownMenuEl.querySelector("li").setAttribute("role", "none");
177
+ dropdownMenuEl.querySelector("a").setAttribute("role", "menuitem");
178
+ const testController = createController(element);
179
+
180
+ testController.connect();
181
+
182
+ expect(dropdownMenuEl.getAttribute("role")).toBeNull();
183
+ expect(dropdownMenuEl.querySelector("li").getAttribute("role")).toBeNull();
184
+ expect(dropdownMenuEl.querySelector("a").getAttribute("role")).toBeNull();
185
+ });
186
+ });
187
+ });
@@ -104,7 +104,7 @@ class FormValidator {
104
104
  let announceElement = this.element.querySelector(".sr-announce");
105
105
 
106
106
  if (announceElement) {
107
- announceElement.remove();
107
+ return;
108
108
  }
109
109
 
110
110
  announceElement = document.createElement("div");
@@ -113,7 +113,7 @@ class FormValidator {
113
113
  this.element.prepend(announceElement);
114
114
 
115
115
  setTimeout(() => {
116
- announceElement.textContent = getDictionary("forms.correct_errors");
116
+ announceElement.textContent = getDictionary("forms").correct_errors;
117
117
  }, 100);
118
118
  }
119
119
 
@@ -259,6 +259,7 @@ class FormValidator {
259
259
  this.removeInputErrorClasses(inputElement);
260
260
  } else {
261
261
  this.addInputErrorClasses(inputElement, failedValidatorNames);
262
+ this.announceFormErrorForScreenReader();
262
263
  }
263
264
  }
264
265
 
@@ -522,6 +522,11 @@ describe("FormValidator", () => {
522
522
  validatorInstance.validateSingleInput(textInput);
523
523
  expect(textInput.classList.contains("is-invalid-input")).toBe(true);
524
524
 
525
+ const announceElement = formElement.querySelector(".sr-announce");
526
+ expect(announceElement).toBeTruthy();
527
+ expect(announceElement.getAttribute("aria-live")).toBe("assertive");
528
+ expect(announceElement.classList.contains("sr-only")).toBe(true);
529
+
525
530
  // Reset validation
526
531
  validatorInstance.resetFormValidation();
527
532
 
@@ -80,7 +80,9 @@ export default Image.extend({
80
80
  addAttributes() {
81
81
  return {
82
82
  ...this.parent?.(),
83
- width: { default: null }
83
+ width: { default: null },
84
+ href: { default: null },
85
+ target: { default: null }
84
86
  };
85
87
  },
86
88
 
@@ -92,13 +94,16 @@ export default Image.extend({
92
94
  ...this.parent?.(),
93
95
  imageDialog: () => async ({ dispatch }) => {
94
96
  if (dispatch) {
95
- let { src, alt, width } = this.editor.getAttributes("image");
97
+ let { src, alt, width, href, target } = this.editor.getAttributes("image");
96
98
 
97
99
  this.editor.commands.toggleDialog(true);
98
- const dialogState = await uploadDialog.toggle({ src, alt }, {
99
- inputLabel: i18n.altLabel,
100
- uploadHandler: async (file) => uploadImage(file, this.options.uploadImagesPath)
101
- });
100
+ const dialogState = await uploadDialog.toggle(
101
+ { src, alt },
102
+ {
103
+ inputLabel: i18n.altLabel,
104
+ uploadHandler: async (file) => uploadImage(file, this.options.uploadImagesPath)
105
+ }
106
+ );
102
107
  this.editor.commands.toggleDialog(false);
103
108
 
104
109
  if (dialogState !== "save") {
@@ -113,8 +118,7 @@ export default Image.extend({
113
118
 
114
119
  src = uploadDialog.getValue("src");
115
120
  alt = uploadDialog.getValue("alt");
116
-
117
- return this.editor.chain().setImage({ src, alt, width }).focus(null, { scrollIntoView: false }).run();
121
+ return this.editor.chain().setImage({ src, alt, width, href, target }).focus(null, { scrollIntoView: false }).run();
118
122
  }
119
123
 
120
124
  return true;
@@ -127,18 +131,52 @@ export default Image.extend({
127
131
  },
128
132
 
129
133
  parseHTML() {
130
- return [{ tag: "div[data-image] img[src]:not([src^='data:'])" }];
134
+ return [
135
+ {
136
+ tag: "div[data-image] img[src]:not([src^='data:'])",
137
+ getAttrs: (dom) => {
138
+ // Check if the image's parent div is wrapped in a link
139
+ const imageDiv = dom.closest("div[data-image]");
140
+ const link = imageDiv?.parentElement;
141
+ if (link && link.tagName === "A" && link.hasAttribute("href")) {
142
+ const attrs = { href: link.getAttribute("href") };
143
+ if (link.hasAttribute("target")) {
144
+ attrs.target = link.getAttribute("target");
145
+ }
146
+ return attrs;
147
+ }
148
+ return {};
149
+ }
150
+ }
151
+ ];
131
152
  },
132
153
 
133
154
  renderHTML({ HTMLAttributes }) {
134
- return [
155
+ const { href, target, ...imgAttributes } = HTMLAttributes;
156
+ const imageContent = [
135
157
  "div",
136
158
  { "class": "editor-content-image", "data-image": "" },
137
159
  [
138
160
  "img",
139
- mergeAttributes(this.options.HTMLAttributes, HTMLAttributes)
161
+ mergeAttributes(this.options.HTMLAttributes, imgAttributes)
140
162
  ]
141
163
  ];
164
+
165
+ if (href) {
166
+ const anchorAttributes = { href };
167
+ const anchorTarget = target;
168
+ if (anchorTarget) {
169
+ anchorAttributes.target = anchorTarget;
170
+ anchorAttributes.rel = "noopener noreferrer";
171
+ }
172
+ return [
173
+ "a",
174
+ anchorAttributes,
175
+ imageContent
176
+ ];
177
+ }
178
+
179
+ return imageContent;
142
180
  },
143
181
 
144
182
  addProseMirrorPlugins() {
@@ -62,7 +62,9 @@ export default (self) => {
62
62
  const img = contentDOM.querySelector("img");
63
63
  let activeResizeControl = null,
64
64
  currentHeight = null,
65
+ currentHref = node.attrs.href,
65
66
  currentSrc = node.attrs.src,
67
+ currentTarget = node.attrs.target,
66
68
  currentWidth = null,
67
69
  naturalHeight = img.naturalHeight,
68
70
  naturalWidth = img.naturalWidth,
@@ -185,7 +187,13 @@ export default (self) => {
185
187
  return false;
186
188
  }
187
189
 
188
- const { alt, src, title, width } = updatedNode.attrs;
190
+ const { alt, src, title, width, href, target } = updatedNode.attrs;
191
+
192
+ // If the href or target changed, we need to recreate the node because the
193
+ // structure changes (wrapped in <a> vs not wrapped)
194
+ if (href !== currentHref || target !== currentTarget) {
195
+ return false;
196
+ }
189
197
 
190
198
  // We set the value through an attribute change here because otherwise
191
199
  // we would trigger a mutation in the DOM which causes the update method
@@ -1,25 +1,53 @@
1
- import { PluginKey } from "prosemirror-state";
1
+ import { NodeSelection, PluginKey } from "prosemirror-state";
2
2
 
3
3
  import { getDictionary } from "src/decidim/refactor/moved/i18n";
4
4
  import BubbleMenu from "src/decidim/editor/common/bubble_menu";
5
5
 
6
6
  class LinkBubbleMenu extends BubbleMenu {
7
- shouldDisplay() {
8
- return this.editor.isActive("link");
7
+ shouldDisplay(view) {
8
+ if (this.editor.isActive("link")) {
9
+ return true;
10
+ }
11
+
12
+ const selection = view.state.selection;
13
+ return this.isImage(selection) && Boolean(selection.node.attrs.href);
9
14
  }
10
15
 
11
- display() {
12
- const { href } = this.editor.getAttributes("link");
13
- this.element.querySelector("[data-linkbubble-value]").textContent = href;
16
+ display(view) {
17
+ if (this.editor.isActive("link")) {
18
+ const { href } = this.editor.getAttributes("link");
19
+ this.updateHref(href);
20
+ return;
21
+ }
22
+
23
+ const selection = view.state.selection;
24
+ if (this.isImage(selection)) {
25
+ this.bubble.style.zIndex = "10";
26
+ this.updateHref(selection.node.attrs.href);
27
+ }
14
28
  }
15
29
 
16
30
  handleAction(action) {
17
31
  if (action === "remove") {
32
+ const { selection } = this.editor.state;
33
+ if (this.isImage(selection)) {
34
+ this.editor.chain().focus(null, { scrollIntoView: false }).updateAttributes("image", { href: null, target: null }).run();
35
+ return;
36
+ }
37
+
18
38
  this.editor.chain().focus(null, { scrollIntoView: false }).unsetLink().run();
19
39
  } else {
20
40
  this.editor.commands.linkDialog();
21
41
  }
22
42
  }
43
+
44
+ updateHref(href) {
45
+ this.element.querySelector("[data-linkbubble-value]").textContent = href;
46
+ }
47
+
48
+ isImage(selection) {
49
+ return selection instanceof NodeSelection && selection.node.type.name === "image";
50
+ }
23
51
  }
24
52
 
25
53
  const createElement = () => {
@@ -1,5 +1,5 @@
1
1
  import Link from "@tiptap/extension-link";
2
- import { Plugin } from "prosemirror-state";
2
+ import { Plugin, NodeSelection } from "prosemirror-state";
3
3
 
4
4
  import { getDictionary } from "src/decidim/refactor/moved/i18n";
5
5
  import InputDialog from "src/decidim/editor/common/input_dialog";
@@ -40,31 +40,55 @@ export default Link.extend({
40
40
  ...this.parent?.(),
41
41
 
42
42
  toggleLinkBubble: () => ({ dispatch }) => {
43
+ const { selection } = this.editor.state;
44
+ const isImageSelection = selection instanceof NodeSelection && selection.node.type.name === "image";
45
+ const imageHasLink = isImageSelection && Boolean(selection.node.attrs.href);
46
+
43
47
  if (dispatch) {
44
- if (this.editor.isActive("link")) {
45
- this.storage.bubbleMenu.show();
48
+ if (this.editor.isActive("link") || (isImageSelection && imageHasLink)) {
49
+ this.storage.bubbleMenu.handleSelectionChange(this.editor.view);
46
50
  return true;
47
51
  }
48
52
 
49
53
  this.storage.bubbleMenu.hide();
50
54
  return false;
51
55
  }
56
+
57
+ if (isImageSelection) {
58
+ return imageHasLink;
59
+ }
60
+
52
61
  return this.editor.isActive("link");
53
62
  },
54
63
 
55
64
  linkDialog: () => async ({ dispatch, commands }) => {
56
65
  if (dispatch) {
57
- // If the cursor is within the link but the link is not selected, the
58
- // link would not be correctly updated. Also if only a part of the
59
- // link is selected, the link would be split to separate links, only
60
- // the current selection getting the updated link URL.
61
- commands.extendMarkRange("link");
66
+ const { state } = this.editor;
67
+ const { selection } = state;
68
+ const isImageSelection = selection instanceof NodeSelection && selection.node.type.name === "image";
69
+ const nodeType = (() => {
70
+ if (isImageSelection) {
71
+ return "image";
72
+ }
73
+ return "link";
74
+ })();
75
+
76
+ if (!isImageSelection) {
77
+ // If the cursor is within the link but the link is not selected, the
78
+ // link would not be correctly updated. Also if only a part of the
79
+ // link is selected, the link would be split to separate links, only
80
+ // the current selection getting the updated link URL.
81
+ commands.extendMarkRange("link");
82
+ }
62
83
 
63
84
  this.storage.bubbleMenu.hide();
64
85
 
65
86
  const { allowTargetControl } = this.options;
66
87
 
67
- let { href, target } = this.editor.getAttributes("link");
88
+ let href = null;
89
+ let target = null;
90
+
91
+ ({ href, target } = this.editor.getAttributes(nodeType));
68
92
 
69
93
  const inputs = { href: { type: "text", label: i18n.hrefLabel } };
70
94
  if (allowTargetControl) {
@@ -88,16 +112,25 @@ export default Link.extend({
88
112
  target = null;
89
113
  }
90
114
 
115
+ const buildChain = () => this.editor.chain().focus(null, { scrollIntoView: false });
116
+
91
117
  if (dialogState !== "save") {
92
- this.editor.chain().focus(null, { scrollIntoView: false }).toggleLinkBubble().run();
118
+ buildChain().toggleLinkBubble().run();
93
119
  return false;
94
120
  }
95
121
 
96
122
  if (!href || href.trim().length < 1) {
97
- return this.editor.chain().focus(null, { scrollIntoView: false }).unsetLink().run();
123
+ if (isImageSelection) {
124
+ return buildChain().updateAttributes("image", { href: null, target: null }).run();
125
+ }
126
+ return buildChain().unsetLink().run();
127
+ }
128
+
129
+ if (isImageSelection) {
130
+ return buildChain().updateAttributes("image", { href: href, target }).toggleLinkBubble().run();
98
131
  }
99
132
 
100
- return this.editor.chain().focus(null, { scrollIntoView: false }).setLink({ href, target }).toggleLinkBubble().run();
133
+ return buildChain().setLink({ href: href, target }).toggleLinkBubble().run();
101
134
  }
102
135
 
103
136
  return true;
@@ -0,0 +1,161 @@
1
+ /* global jest, global */
2
+
3
+ import { createBasicEditor, updateContent, sleep } from "src/decidim/editor/test/helpers";
4
+
5
+ import Dialog from "src/decidim/editor/extensions/dialog";
6
+ import Image from "src/decidim/editor/extensions/image";
7
+ import uploadTemplates from "src/decidim/editor/test/fixtures/upload_templates";
8
+
9
+ class DummyDialog {
10
+ constructor(element) { this.element = element; }
11
+
12
+ open() { this.element.dataset.dialogOpen = true; }
13
+
14
+ close() { this.element.dataset.dialogOpen = null; }
15
+ }
16
+
17
+ // Not implemented in Jest
18
+ global.Touch = class Touch {
19
+ constructor(options) {
20
+ this.pageX = options.pageX;
21
+ this.pageY = options.pageY;
22
+ }
23
+ }
24
+
25
+ describe("Image (links)", () => {
26
+ let editor = null;
27
+ let editorElement = null;
28
+ let uploadDialogElement = null;
29
+
30
+ beforeEach(() => {
31
+ document.body.innerHTML = "";
32
+
33
+ const dialogWrapper = document.createElement("div");
34
+ dialogWrapper.innerHTML = uploadTemplates.redesign;
35
+ uploadDialogElement = dialogWrapper.firstElementChild;
36
+ uploadDialogElement.dataset.dialog = "testDialog";
37
+ uploadDialogElement.dialog = new DummyDialog(uploadDialogElement);
38
+ window.Decidim.currentDialogs = { testDialog: uploadDialogElement.dialog };
39
+ document.body.append(uploadDialogElement);
40
+
41
+ editor = createBasicEditor({
42
+ extensions: [Dialog, Image.configure({ uploadDialogSelector: "#upload_dialog", uploadImagesPath: "/editor_images", contentTypes: ["image/png"] })]
43
+ });
44
+ editorElement = editor.view.dom;
45
+
46
+ const csrf = document.createElement("meta");
47
+ csrf.setAttribute("name", "csrf-token");
48
+ csrf.setAttribute("content", "abcdef0123456789");
49
+ document.head.append(csrf);
50
+
51
+ global.fetch = jest.fn(() => Promise.resolve({ ok: true, json: () => Promise.resolve({ url: "/path/to/image.jpg" }) }));
52
+ });
53
+
54
+ afterEach(() => {
55
+ jest.restoreAllMocks();
56
+ });
57
+
58
+ it("allows setting an image with href and target attributes", async () => {
59
+ editorElement.focus();
60
+ editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image", href: "https://example.com", target: "_blank" });
61
+ await sleep(0);
62
+
63
+ expect(editor.getHTML()).toMatchHtml(`
64
+ <a href="https://example.com" target="_blank" rel="noopener noreferrer">
65
+ <div class="editor-content-image" data-image="">
66
+ <img src="/path/to/image.jpg" alt="Test image">
67
+ </div>
68
+ </a>
69
+ `);
70
+ });
71
+
72
+ it("allows setting an image with href but no target", async () => {
73
+ editorElement.focus();
74
+ editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image", href: "https://example.com" });
75
+ await sleep(0);
76
+
77
+ expect(editor.getHTML()).toMatchHtml(`
78
+ <a href="https://example.com">
79
+ <div class="editor-content-image" data-image="">
80
+ <img src="/path/to/image.jpg" alt="Test image">
81
+ </div>
82
+ </a>
83
+ `);
84
+ });
85
+
86
+ it("parses existing HTML with linked images", async () => {
87
+ editorElement.focus();
88
+ await updateContent(editorElement,
89
+ '<a href="https://example.com" target="_blank"><div class="editor-content-image" data-image=""><img src="/path/to/image.jpg" alt="Test image"></div></a>',
90
+ editor
91
+ );
92
+
93
+ expect(editor.getHTML()).toMatchHtml(`
94
+ <a href="https://example.com" target="_blank" rel="noopener noreferrer">
95
+ <div class="editor-content-image" data-image="">
96
+ <img src="/path/to/image.jpg" alt="Test image">
97
+ </div>
98
+ </a>
99
+ `);
100
+ });
101
+
102
+ it("parses existing HTML with linked images without target", async () => {
103
+ editorElement.focus();
104
+ await updateContent(editorElement,
105
+ '<a href="https://example.com"><div class="editor-content-image" data-image=""><img src="/path/to/image.jpg" alt="Test image"></div></a>',
106
+ editor
107
+ );
108
+
109
+ expect(editor.getHTML()).toMatchHtml(`
110
+ <a href="https://example.com">
111
+ <div class="editor-content-image" data-image="">
112
+ <img src="/path/to/image.jpg" alt="Test image">
113
+ </div>
114
+ </a>
115
+ `);
116
+ });
117
+
118
+ it("allows updating image link attributes", async () => {
119
+ editorElement.focus();
120
+ editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image", href: "https://example.com", target: "_blank" });
121
+ await sleep(0);
122
+
123
+ editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image", href: "https://docs.example.com" });
124
+ await sleep(0);
125
+
126
+ expect(editor.getHTML()).toMatchHtml(`
127
+ <a href="https://docs.example.com">
128
+ <div class="editor-content-image" data-image="">
129
+ <img src="/path/to/image.jpg" alt="Test image">
130
+ </div>
131
+ </a>
132
+ `);
133
+ });
134
+
135
+ it("allows removing link from image", async () => {
136
+ editorElement.focus();
137
+ editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image", href: "https://example.com", target: "_blank" });
138
+ await sleep(0);
139
+
140
+ editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image", href: null, target: null });
141
+ await sleep(0);
142
+
143
+ expect(editor.getHTML()).toMatchHtml(`
144
+ <div class="editor-content-image" data-image="">
145
+ <img src="/path/to/image.jpg" alt="Test image">
146
+ </div>
147
+ `);
148
+ });
149
+
150
+ it("renders images without links correctly", async () => {
151
+ editorElement.focus();
152
+ editor.commands.setImage({ src: "/path/to/image.jpg", alt: "Test image" });
153
+ await sleep(0);
154
+
155
+ expect(editor.getHTML()).toMatchHtml(`
156
+ <div class="editor-content-image" data-image="">
157
+ <img src="/path/to/image.jpg" alt="Test image">
158
+ </div>
159
+ `);
160
+ });
161
+ });