@contentful/field-editor-rich-text 3.4.9 → 3.4.11

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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,18 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [3.4.11](https://github.com/contentful/field-editors/compare/@contentful/field-editor-rich-text@3.4.10...@contentful/field-editor-rich-text@3.4.11) (2023-03-09)
7
+
8
+ ### Bug Fixes
9
+
10
+ - **rich-text:** pasting hyperlinks with blocks ([#1364](https://github.com/contentful/field-editors/issues/1364)) ([32bfe1d](https://github.com/contentful/field-editors/commit/32bfe1dfaf6d976c69690a99b69b5beff570eba6))
11
+
12
+ ## [3.4.10](https://github.com/contentful/field-editors/compare/@contentful/field-editor-rich-text@3.4.9...@contentful/field-editor-rich-text@3.4.10) (2023-03-09)
13
+
14
+ ### Bug Fixes
15
+
16
+ - **RTE:** remove whitespaces only in tables and lists [TOL-104] ([#1362](https://github.com/contentful/field-editors/issues/1362)) ([8b106c8](https://github.com/contentful/field-editors/commit/8b106c8eb08d79cb6c5ae8353d5a1fcf7afab1cf))
17
+
6
18
  ## [3.4.9](https://github.com/contentful/field-editors/compare/@contentful/field-editor-rich-text@3.4.8...@contentful/field-editor-rich-text@3.4.9) (2023-03-03)
7
19
 
8
20
  ### Bug Fixes
@@ -6096,6 +6096,62 @@ var createParagraphPlugin = function createParagraphPlugin() {
6096
6096
  return plateParagraph.createParagraphPlugin(config);
6097
6097
  };
6098
6098
 
6099
+ var wrapSpaceAround = function wrapSpaceAround(el) {
6100
+ var spacer = new Text(' ');
6101
+ var parent = el.parentNode;
6102
+
6103
+ if (!parent) {
6104
+ return;
6105
+ }
6106
+
6107
+ if (el.previousSibling) {
6108
+ parent.insertBefore(spacer, el);
6109
+ }
6110
+
6111
+ if (el.nextSibling) {
6112
+ parent.insertBefore(spacer, el.nextSibling);
6113
+ }
6114
+ };
6115
+
6116
+ var unwrap = function unwrap(el) {
6117
+ // add a spacer to avoid the content being cramped together with
6118
+ // the element siblings after it's unwrapped. It may not always
6119
+ // be desired but it should be easy to adjust by the end user.
6120
+ wrapSpaceAround(el);
6121
+ el.replaceWith.apply(el, Array.from(el.childNodes));
6122
+ };
6123
+ /**
6124
+ * In HTML it's a valid structure to have a block element inside a link
6125
+ *
6126
+ * e.g: <a href="..."><h1> My link </h1><h2>is fancy</h2></a>
6127
+ *
6128
+ * However, that's not supported in Slate. There are generally two ways to
6129
+ * handle this:
6130
+ *
6131
+ * a) Unwrap inner blocks while preserving the content.
6132
+ * e.g. <a href="...">My link is fancy</a>
6133
+ *
6134
+ * b) Break the link into multiple elements with having each block
6135
+ * element as a wrapper for an anchor with the same URL
6136
+ * e.g. <h1><a href="...">My link</a></h1>
6137
+ * and <h2><a href="...">is fancy</a></h2>
6138
+ *
6139
+ * We take approach (a) in here.
6140
+ */
6141
+
6142
+
6143
+ var sanitizeAnchors = function sanitizeAnchors(doc) {
6144
+ var unsupportedTagSelector = "a :not(" + [// Bold
6145
+ 'b', 'strong', // Code
6146
+ 'code', 'pre', // Italic
6147
+ 'em', 'i', // Super/subscript
6148
+ 'sub', 'sup', // Underline
6149
+ 'u', // Other
6150
+ 'span'].join(',') + ")";
6151
+ doc.querySelectorAll(unsupportedTagSelector).forEach(unwrap);
6152
+ return doc;
6153
+ };
6154
+
6099
6155
  /**
6100
6156
  * Remove empty spreadsheets columns/rows for performance reasons.
6101
6157
  */
@@ -6174,15 +6230,43 @@ var stripStyleTags = function stripStyleTags(doc) {
6174
6230
  });
6175
6231
  return doc;
6176
6232
  };
6233
+ /**
6234
+ * Remove all <meta /> tags
6235
+ */
6236
+
6237
+
6238
+ var stripMetaTags = function stripMetaTags(doc) {
6239
+ doc.querySelectorAll('meta').forEach(function (el) {
6240
+ return el.remove();
6241
+ });
6242
+ return doc;
6243
+ }; // Attention: Order is important
6244
+
6177
6245
 
6178
- var transformers = [stripStyleTags, sanitizeSheets];
6246
+ var transformers = [stripStyleTags, sanitizeSheets, stripMetaTags, sanitizeAnchors];
6179
6247
  var sanitizeHTML = function sanitizeHTML(html) {
6180
6248
  // Parse the HTML string and pipe it through our transformers
6181
6249
  var doc = transformers.reduce(function (value, cb) {
6182
6250
  return cb(value);
6183
6251
  }, new DOMParser().parseFromString(html, 'text/html'));
6184
- return doc.body.innerHTML.replace(/>\s+</g, '><') // Remove whitespace between tags
6185
- .replace(/(.*)<div.*>(<table.*<\/table>)<\/div>(.*)/g, '$1$2$3'); // remove div containers from tables
6252
+ var previous;
6253
+
6254
+ do {
6255
+ // save previous first before doing modifications
6256
+ previous = doc.body.innerHTML; // Update the body with the cleaned up content
6257
+
6258
+ doc.body.innerHTML = doc.body.innerHTML // remove div container from tables
6259
+ // capture groups explained:
6260
+ // 1. and 3. every content/linebreaks before and after the div container
6261
+ // 2. the table inside the container, including content and linebreaks
6262
+ // The div container including attributes and possible linebreaks inside wil be removed
6263
+ .replace(/(.*\s)?<div.*>\s*(<table(?:.|\s)*<\/table>)\s*<\/div>(.*\s)?/g, '$1$2$3') // remove whitespaces between some tags, as this can lead to unwanted behaviour:
6264
+ // - table -> empty table cells
6265
+ // - list -> leading whitespaces
6266
+ .replace(/<(\/)?(table|thead|tbody|tr|td|th|caption|col|colgroup|ol|ul|li)(.*)>\s+</g, '<$1$2$3><');
6267
+ } while (doc.body.innerHTML !== previous);
6268
+
6269
+ return doc.body.innerHTML;
6186
6270
  };
6187
6271
 
6188
6272
  /**