@contentful/field-editor-rich-text 3.4.14 → 3.4.16

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.
@@ -6242,23 +6242,36 @@ var sanitizeHTML = function sanitizeHTML(html) {
6242
6242
  var doc = transformers.reduce(function (value, cb) {
6243
6243
  return cb(value);
6244
6244
  }, new DOMParser().parseFromString(html, 'text/html'));
6245
+ var replacers = [// remove whitespaces between some tags, as this can lead to unwanted behaviour:
6246
+ // - table -> empty table cells
6247
+ // - list -> leading whitespaces
6248
+ function (innerHtml) {
6249
+ return innerHtml.replace(/<(\/)?(table|thead|tbody|tr|td|th|caption|col|colgroup|ol|ul|li)(.*)>\s+<(\/)?(table|thead|tbody|tr|td|th|caption|col|colgroup|ol|ul|li)/g, '<$1$2$3><$4$5');
6250
+ }, // remove empty elements before the ending block element tag
6251
+ function (innerHtml) {
6252
+ return innerHtml.replace(/(?:<[^>^/]*>)\s*(?:<\/[^>]*>)<\/(div|p|table|thead|tbody|tr|td|th|caption|col|colgroup|ol|ul|li)/g, '</$1');
6253
+ }, // remove whitespaces before the ending block element tag
6254
+ function (innerHTML) {
6255
+ return innerHTML.replace(/\s*<\/(div|p|table|thead|tbody|tr|td|th|caption|col|colgroup|ol|ul|li)/g, '</$1');
6256
+ }]; // Call this replacer only if there are tables inside, as it can have a larger performance impact
6257
+
6258
+ if (doc.querySelector('table')) {
6259
+ // remove div container from tables
6260
+ // The div container including attributes and possible linebreaks inside wil be removed
6261
+ replacers.unshift(function (innerHtml) {
6262
+ var result = innerHtml.replace(/<div[^>]*>\s*(<table(?:.|\s)*<\/table>)\s*<\/div>/g, '$1');
6263
+ return result;
6264
+ });
6265
+ }
6266
+
6245
6267
  var previous;
6246
6268
 
6247
6269
  do {
6248
6270
  // save previous first before doing modifications
6249
- previous = doc.body.innerHTML; // Update the body with the cleaned up content
6250
-
6251
- doc.body.innerHTML = doc.body.innerHTML // remove div container from tables
6252
- // capture groups explained:
6253
- // 1. and 3. every content/linebreaks before and after the div container
6254
- // 2. the table inside the container, including content and linebreaks
6255
- // The div container including attributes and possible linebreaks inside wil be removed
6256
- .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:
6257
- // - table -> empty table cells
6258
- // - list -> leading whitespaces
6259
- .replace(/<(\/)?(table|thead|tbody|tr|td|th|caption|col|colgroup|ol|ul|li)(.*)>\s+<(\/)?(table|thead|tbody|tr|td|th|caption|col|colgroup|ol|ul|li)/g, '<$1$2$3><$4$5') // Removes empty elements before the closing tag of the capture group
6260
- // <p><span> </span></p> -> <p></p>
6261
- .replace(/(?:<[^>^/]*>)?\s*(?:<\/[^>]*>)?<\/(div|p|table|thead|tbody|tr|td|th|caption|col|colgroup|ol|ul|li)/g, '</$1');
6271
+ previous = doc.body.innerHTML;
6272
+ doc.body.innerHTML = replacers.reduce(function (innerHTML, replacer) {
6273
+ return replacer(innerHTML);
6274
+ }, doc.body.innerHTML);
6262
6275
  } while (doc.body.innerHTML !== previous);
6263
6276
 
6264
6277
  return doc.body.innerHTML;