@mediafly/js-ui 53.0.1 → 53.1.0-1

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 (115) hide show
  1. package/.github/dependabot.yml +30 -0
  2. package/.node-version +1 -1
  3. package/.semaphore/semaphore.yml +9 -6
  4. package/lib/fonts.css +1 -1
  5. package/lib/src/components/actionable-empty-state/actionable-empty-state.js +8 -8
  6. package/lib/src/components/actionable-empty-state/actionable-empty-state.js.map +1 -1
  7. package/lib/src/components/auto-complete/auto-complete-match-renderer.component.d.ts +1 -1
  8. package/lib/src/components/auto-complete/auto-complete-match-renderer.component.js +14 -14
  9. package/lib/src/components/auto-complete/auto-complete-match-renderer.component.js.map +1 -1
  10. package/lib/src/components/auto-complete/auto-complete.component.js +12 -12
  11. package/lib/src/components/auto-complete/auto-complete.component.js.map +1 -1
  12. package/lib/src/components/auto-focus/auto-focus.service.js +3 -3
  13. package/lib/src/components/auto-focus/auto-focus.service.js.map +1 -1
  14. package/lib/src/components/color-utils/color-utils.service.js +2 -2
  15. package/lib/src/components/color-utils/color-utils.service.js.map +1 -1
  16. package/lib/src/components/date-time-zone-city-picker/date-time-zone-city-picker.js +9 -9
  17. package/lib/src/components/date-time-zone-city-picker/date-time-zone-city-picker.js.map +1 -1
  18. package/lib/src/components/date-time-zone-city-picker/time-zone-city-picker.js +6 -6
  19. package/lib/src/components/date-time-zone-city-picker/time-zone-city-picker.js.map +1 -1
  20. package/lib/src/components/date-time-zone-picker/datepicker.js +1 -1
  21. package/lib/src/components/date-time-zone-picker/datepicker.js.map +1 -1
  22. package/lib/src/components/dropdown/dropdown.d.ts +3 -3
  23. package/lib/src/components/dropdown/dropdown.js +11 -11
  24. package/lib/src/components/dropdown/dropdown.js.map +1 -1
  25. package/lib/src/components/dropdown-menu/dropdown-menu-action.component.d.ts +1 -0
  26. package/lib/src/components/dropdown-menu/dropdown-menu-action.component.js +9 -9
  27. package/lib/src/components/dropdown-menu/dropdown-menu-action.component.js.map +1 -1
  28. package/lib/src/components/dropdown-menu/dropdown-menu-contents.js +6 -6
  29. package/lib/src/components/dropdown-menu/dropdown-menu-contents.js.map +1 -1
  30. package/lib/src/components/dropdown-menu/dropdown-menu.js +9 -9
  31. package/lib/src/components/dropdown-menu/dropdown-menu.js.map +1 -1
  32. package/lib/src/components/feedback/clipboard.service.js +5 -5
  33. package/lib/src/components/feedback/clipboard.service.js.map +1 -1
  34. package/lib/src/components/feedback/feedback.component.d.ts +1 -1
  35. package/lib/src/components/feedback/feedback.component.js +6 -6
  36. package/lib/src/components/feedback/feedback.component.js.map +1 -1
  37. package/lib/src/components/feedback/feedback.service.js +3 -3
  38. package/lib/src/components/feedback/feedback.service.js.map +1 -1
  39. package/lib/src/components/first-time-motivator/first-time-motivator.js +8 -8
  40. package/lib/src/components/first-time-motivator/first-time-motivator.js.map +1 -1
  41. package/lib/src/components/first-time-motivator/ftm-target-element.js +1 -1
  42. package/lib/src/components/first-time-motivator/ftm-target-element.js.map +1 -1
  43. package/lib/src/components/font-selector/font-selector.js +13 -13
  44. package/lib/src/components/font-selector/font-selector.js.map +1 -1
  45. package/lib/src/components/font-selector/google-webfonts-api.service.js +4 -4
  46. package/lib/src/components/font-selector/google-webfonts-api.service.js.map +1 -1
  47. package/lib/src/components/font-selector/webfont-loader.service.js +1 -1
  48. package/lib/src/components/font-selector/webfont-loader.service.js.map +1 -1
  49. package/lib/src/components/font-weight-selector/font-weight-selector.js +11 -11
  50. package/lib/src/components/font-weight-selector/font-weight-selector.js.map +1 -1
  51. package/lib/src/components/full-screen/full-screen.service.d.ts +1 -1
  52. package/lib/src/components/full-screen/full-screen.service.js +1 -1
  53. package/lib/src/components/full-screen/full-screen.service.js.map +1 -1
  54. package/lib/src/components/hover-tooltip/hover-tooltip.js +1 -1
  55. package/lib/src/components/hover-tooltip/hover-tooltip.js.map +1 -1
  56. package/lib/src/components/icon/icon.service.js +1 -1
  57. package/lib/src/components/icon/icon.service.js.map +1 -1
  58. package/lib/src/components/inline-text-input/inline-text-input.component.js +2 -2
  59. package/lib/src/components/inline-text-input/inline-text-input.component.js.map +1 -1
  60. package/lib/src/components/loading-indicator/loading-indicator.component.js +2 -2
  61. package/lib/src/components/loading-indicator/loading-indicator.component.js.map +1 -1
  62. package/lib/src/components/metadata-form/metadata-form.component.js +11 -11
  63. package/lib/src/components/metadata-form/metadata-form.component.js.map +1 -1
  64. package/lib/src/components/modal/modal.component.js +14 -14
  65. package/lib/src/components/modal/modal.component.js.map +1 -1
  66. package/lib/src/components/modal/modal.service.d.ts +2 -1
  67. package/lib/src/components/modal/modal.service.js +6 -6
  68. package/lib/src/components/modal/modal.service.js.map +1 -1
  69. package/lib/src/components/progress-bar/progress-bar.component.js +2 -2
  70. package/lib/src/components/progress-bar/progress-bar.component.js.map +1 -1
  71. package/lib/src/components/progress-circle/progress-circle.js +2 -2
  72. package/lib/src/components/progress-circle/progress-circle.js.map +1 -1
  73. package/lib/src/components/response-handlers/api-success-handlers.js +1 -1
  74. package/lib/src/components/response-handlers/api-success-handlers.js.map +1 -1
  75. package/lib/src/components/response-handlers/error-response.service.js +2 -2
  76. package/lib/src/components/response-handlers/error-response.service.js.map +1 -1
  77. package/lib/src/components/response-handlers/feedback-error-handler.js +4 -4
  78. package/lib/src/components/response-handlers/feedback-error-handler.js.map +1 -1
  79. package/lib/src/components/response-handlers/log-error-handler.js +2 -2
  80. package/lib/src/components/response-handlers/log-error-handler.js.map +1 -1
  81. package/lib/src/components/rich-text/rich-text.component.d.ts +8 -0
  82. package/lib/src/components/rich-text/rich-text.component.js +56 -19
  83. package/lib/src/components/rich-text/rich-text.component.js.map +1 -1
  84. package/lib/src/components/rich-text/rich-text.service.d.ts +24 -0
  85. package/lib/src/components/rich-text/rich-text.service.js +111 -2
  86. package/lib/src/components/rich-text/rich-text.service.js.map +1 -1
  87. package/lib/src/components/site-header/site-header-navbar-item.component.js +8 -8
  88. package/lib/src/components/site-header/site-header-navbar-item.component.js.map +1 -1
  89. package/lib/src/components/site-header/site-header.component.js +8 -8
  90. package/lib/src/components/site-header/site-header.component.js.map +1 -1
  91. package/lib/src/components/tabs/tab-group.js +3 -3
  92. package/lib/src/components/tabs/tab-group.js.map +1 -1
  93. package/lib/src/components/tabs/tab-html-title.js +2 -2
  94. package/lib/src/components/tabs/tab-html-title.js.map +1 -1
  95. package/lib/src/components/tabs/tab.js +8 -8
  96. package/lib/src/components/tabs/tab.js.map +1 -1
  97. package/lib/src/components/tag-input/tag-input.component.d.ts +1 -1
  98. package/lib/src/components/tag-input/tag-input.component.js +12 -12
  99. package/lib/src/components/tag-input/tag-input.component.js.map +1 -1
  100. package/lib/src/components/theme/theme.service.js +1 -1
  101. package/lib/src/components/theme/theme.service.js.map +1 -1
  102. package/lib/src/components/theme/theme.typings.d.ts +1 -1
  103. package/lib/src/components/type-string/type-string.component.js +3 -3
  104. package/lib/src/components/type-string/type-string.component.js.map +1 -1
  105. package/lib/src/components/url-modification/url-modification.service.js +1 -1
  106. package/lib/src/components/url-modification/url-modification.service.js.map +1 -1
  107. package/lib/styles.css +1 -1
  108. package/lib/tsconfig.deploy.tsbuildinfo +1 -6298
  109. package/package.json +60 -52
  110. package/src/components/rich-text/rich-text.component.less +10 -0
  111. package/src/components/rich-text/rich-text.component.ts +41 -1
  112. package/src/components/rich-text/rich-text.service.ts +471 -336
  113. package/src/index.spec.js +41 -40
  114. package/tsconfig.tsbuildinfo +1 -10494
  115. package/webpack.config.js +1 -1
@@ -1,336 +1,471 @@
1
- import angular, { sanitize } from 'angular'
2
- import { Inject, Injectable } from '../decorators'
3
-
4
- // text editor requires sanitization but also needs to allow for style attributes which are removed by default
5
- angular.module('app').config(['$sanitizeProvider', ($sanitizeProvider: ng.sanitize.ISanitizeProvider) => {
6
- $sanitizeProvider.addValidAttrs(['style'])
7
-
8
- // Quill uses data-list to distinguish bulleted lists from numbered lists (they are both provided as <ol>)
9
- $sanitizeProvider.addValidAttrs(['data-list'])
10
-
11
- // Allow target attribute on anchor tags
12
- $sanitizeProvider.addValidAttrs(['target'])
13
- }])
14
-
15
- @Injectable('richTextService')
16
- export default class RichTextService {
17
-
18
- constructor(
19
- @Inject('$sanitize') private $sanitize: sanitize.ISanitizeService,
20
- ) { }
21
-
22
- /** Older versions of Quill rendered some things differently. Patch those up. */
23
- backwardsCompatibleHtml(html: string): string {
24
-
25
- html = this.removeZeroWidthNoBreakSpaces(html)
26
- html = this.removeCursor(html)
27
- html = this.fixWrappingCursor(html)
28
- html = this.fixFontTags(html)
29
- html = this.fixBulletPoints(html)
30
- html = this.fixStrongBold(html)
31
-
32
- return html
33
- }
34
-
35
- /**
36
- * Transform <ul><li> into <ol><li data-list="bullet">,
37
- * and <ol><li> into <ol><li data-list="ordered">
38
- * accounting for any pattern of nested <ul> and <ol> tags.
39
- */
40
- fixBulletPoints(html: string): string {
41
-
42
- if (!html) {
43
- return html
44
- }
45
-
46
- let newHtml: string = ''
47
-
48
- // Regexes for these, in case someone invents a <link> tag or something
49
- const olStartTagRegex = new RegExp(`<ol(\\s|>)`)
50
- const ulStartTagRegex = new RegExp(`<ul(\\s|>)`)
51
- const liStartTagRegex = new RegExp(`<li(\\s|>)`)
52
- // Literal string matches for these
53
- const olEndTag = `</ol>`
54
- const ulEndTag = `</ul>`
55
-
56
- // Stack of <ol> and <ul> tags, representing how the lists are nested
57
- const listStack: string[] = []
58
-
59
- let parsedLength = 0
60
- let currentListType: string | undefined
61
-
62
- // Loop through the input html, looking for <ol>, <ul> and <li> tags. Maintain a stack of nested lists, so
63
- // we can transform the current <li> correctly.
64
- while (parsedLength < html.length) {
65
-
66
- // Find the start of the next <ol>, <ul> or <li> tag
67
- const olStart = this.indexOfRegex(html, olStartTagRegex, parsedLength)
68
- const ulStart = this.indexOfRegex(html, ulStartTagRegex, parsedLength)
69
- const liStart = this.indexOfRegex(html, liStartTagRegex, parsedLength)
70
-
71
- // Find what might be the end of the current list (or might be the end of a nested list)
72
- let nextListEnd: number
73
- switch (currentListType) {
74
- case 'ol':
75
- nextListEnd = html.indexOf(olEndTag, parsedLength)
76
- break
77
- case 'ul':
78
- nextListEnd = html.indexOf(ulEndTag, parsedLength)
79
- break
80
- default:
81
- nextListEnd = -1
82
- break
83
- }
84
-
85
- // Find which tag comes next
86
- const nextItemPos = Math.min(
87
- olStart === -1 ? html.length : olStart,
88
- ulStart === -1 ? html.length : ulStart,
89
- liStart === -1 ? html.length : liStart,
90
- nextListEnd === -1 ? html.length : nextListEnd)
91
-
92
- newHtml += html.substring(parsedLength, nextItemPos)
93
- parsedLength = nextItemPos
94
-
95
- switch (nextItemPos) {
96
- case olStart:
97
- case ulStart:
98
- // We've found the start of a list
99
- newHtml += `<ol>`
100
-
101
- const listTagEnd = html.indexOf('>', nextItemPos)
102
- if (listTagEnd === -1) {
103
- // Invalid HTML. Dump the lot.
104
- return ''
105
- }
106
- parsedLength = listTagEnd + 1
107
-
108
- if (nextItemPos === olStart) {
109
- listStack.push('ol')
110
- currentListType = 'ol'
111
- } else {
112
- listStack.push('ul')
113
- currentListType = 'ul'
114
- }
115
- break
116
-
117
- case liStart:
118
- // We've found a list item
119
- if (!currentListType) {
120
- // Invalid HTML. Dump the lot.
121
- return ''
122
- }
123
-
124
- const liTagEnd = html.indexOf('>', liStart + 1)
125
- if (liTagEnd === -1) {
126
- // Invalid HTML. Dump the lot.
127
- return ''
128
- }
129
- let liTag = html.substring(liStart, liTagEnd + 1)
130
-
131
- if (liTag.indexOf('data-list') === -1) {
132
- const dataAttr = ` data-list="${currentListType === 'ol' ? 'ordered' : 'bullet'}"`
133
- liTag = `<li${dataAttr}` + liTag.substring(3)
134
- }
135
-
136
- newHtml += liTag
137
- parsedLength = liTagEnd + 1
138
- break
139
-
140
- case nextListEnd:
141
- // We've reached the end of the current list. Write out the closing tag and pop the stack
142
- newHtml += olEndTag
143
- parsedLength = nextListEnd + olEndTag.length
144
- listStack.pop()
145
- currentListType = listStack[listStack.length - 1]
146
- break
147
- }
148
- }
149
-
150
- if (listStack.length > 0) {
151
- // We didn't close the last list. Invalid HTML. Dump the lot.
152
- return ''
153
- }
154
-
155
- return newHtml
156
- }
157
-
158
- /**
159
- * Transform <font> tags from the old Quill editor into <span> tags, as used by the new editor.
160
- * Quill used to set fonts using <font color="#000000" face="Open Sans">
161
- * Now it uses <span style="font-family: &#34;Open Sans&#34;; color: rgb(0, 0, 0);">
162
- * Although the former still renders correctly, it is skipped for editor.getText(), so it might report that the text editor is empty.
163
- */
164
- fixFontTags(html: string): string {
165
-
166
- if (!html) {
167
- return html
168
- }
169
-
170
- let fontTagPos = html.indexOf('<font')
171
- while (fontTagPos > -1) {
172
- const fontTagLength = html.indexOf('>', fontTagPos) - fontTagPos + 1
173
- const fontTag = html.substr(fontTagPos, fontTagLength)
174
-
175
- const faceMatch = fontTag.match(/face="([^"]+)"/)
176
- const colorMatch = fontTag.match(/color="([^"]+)"/)
177
-
178
- const styles: string[] = []
179
- if (faceMatch) {
180
- styles.push(`font-family: &#34;${faceMatch[1]}&#34;;`)
181
- }
182
- if (colorMatch) {
183
- styles.push(`color: ${colorMatch[1]};`)
184
- }
185
-
186
- html =
187
- html.substr(0, fontTagPos) +
188
- `<span style="${styles.join(' ')}">` +
189
- html.substr(fontTagPos + fontTagLength)
190
-
191
- fontTagPos = html.indexOf('<font')
192
- }
193
-
194
- html = html.replace(/<\/font>/g, '<\/span>')
195
-
196
- return html
197
- }
198
-
199
- /**
200
- * Quill adds its own editor cursor to its HTML: <span class="ql-cursor">|</span>
201
- * These days we remove that on save, but sometimes it's present in the HTML that was saved by an older version of the editor.
202
- * In rare cases, the HTML wraps that span around actual content. This function fixes that.
203
- */
204
- fixWrappingCursor(html: string): string {
205
-
206
- if (!html) {
207
- return html
208
- }
209
-
210
- // Regexes can't save us because the cursor span may contain other spans, like this:
211
- // <p><span class="ql-cursor"><span style="font-size: 24px;">Yikes!</span></span></p>
212
- // We want to remove the outer span, while preserving the inner span.
213
- const cursorStartTag = `<span class="ql-cursor">`
214
- const spanStartTag = `<span`
215
- const spanEndTag = `</span>`
216
- let cursorStart = html.indexOf(cursorStartTag)
217
- while (cursorStart > -1) {
218
-
219
- let currentPos = cursorStart
220
- let spanDepth = 1
221
- while (spanDepth > 0) {
222
- const nextSpanStart = html.indexOf(spanStartTag, currentPos + 1)
223
- const nextSpanEnd = html.indexOf(spanEndTag, currentPos + 1)
224
- if (nextSpanStart === -1 && nextSpanEnd === -1) {
225
- // Spans aren't closed properly, so we have invalid html. Dump the lot. (Should never happen.)
226
- html = ''
227
- spanDepth = 0
228
- } else if (nextSpanStart > -1 && nextSpanStart < nextSpanEnd) {
229
- // We've found another nested span
230
- spanDepth++
231
- currentPos = nextSpanStart
232
- } else {
233
- // We've found the end of our current span
234
- spanDepth--
235
- currentPos = nextSpanEnd
236
- }
237
- }
238
-
239
- if (html !== '') {
240
- const cursorEnd = currentPos + spanEndTag.length - 1
241
- const cursorOuterLength = cursorEnd - cursorStart + 1
242
- const cursorInnerLength = cursorOuterLength - cursorStartTag.length - spanEndTag.length
243
- html =
244
- html.substr(0, cursorStart) + // Everything before the cursor
245
- html.substr(cursorStart + cursorStartTag.length, cursorInnerLength) + // Everything inside the cursor
246
- html.substr(cursorEnd + 1) // Everything after the cursor
247
- }
248
-
249
- // There might be multiple cursors - even nested cursors - so keep going until there are none left
250
- cursorStart = html.indexOf(`<span class="ql-cursor">`)
251
- }
252
-
253
- return html
254
- }
255
-
256
- /**
257
- * To transition to supporting Google font weights, we switched to inline font weights rather than quill's default <strong> tags.
258
- * Replace the legacy strong's with regular span's and an inline font weight.
259
- */
260
- fixStrongBold(html: string): string {
261
-
262
- if (!html) { return html }
263
-
264
- const bold = '700'
265
-
266
- // Replace <strong> tags with <span> tags and add font-weight: 700
267
- html = html.replace(/<strong([^>]*)>/g, (match, attributes) => {
268
- // Check if there's already a style attribute
269
- const styleMatch = attributes.match(/style="([^"]*)"/)
270
-
271
- if (styleMatch) {
272
- // There's already a style attribute, add font-weight to it
273
- const existingStyles = (styleMatch[1] || '').trim()
274
-
275
- // Check if font-weight is already present
276
- if (existingStyles.includes('font-weight')) {
277
- // Ignore existing font-weight
278
- return `<span${attributes.replace(styleMatch[0], `style="${existingStyles}"`)}>`
279
- } else {
280
- // Add font-weight to styles
281
- const separator = existingStyles.endsWith(';') ? ' ' : '; '
282
- return `<span${attributes.replace(styleMatch[0], `style="${existingStyles}${separator}font-weight: ${bold};"`)}>`
283
- }
284
- } else {
285
- // No style attribute exists, add one with font-weight
286
- return `<span${attributes} style="font-weight: ${bold};">`
287
- }
288
- })
289
-
290
- // Replace closing tags
291
- html = html.replace(/<\/strong>/g, '</span>')
292
-
293
- return html
294
- }
295
-
296
- /** Helper method because regexes can't natively start searching from a given index */
297
- indexOfRegex(text: string, regex: RegExp, fromIndex: number): number {
298
-
299
- const match = text.substring(fromIndex).match(regex)
300
- if (!match || match.index === undefined || match.index === -1) {
301
- return -1
302
- }
303
- return fromIndex + match.index
304
- }
305
-
306
- /** Sometimes Quill's HTML includes its own cursor. We don't want to save that. */
307
- removeCursor(html: string): string {
308
-
309
- if (!html) {
310
- return html
311
- }
312
-
313
- return html.replace(/\<span class="ql-cursor"\>.?\<\/span\>/g, '')
314
- }
315
-
316
- /** Sometimes Quill adds Zero Width No-Break Spaces. */
317
- removeZeroWidthNoBreakSpaces(html: string): string {
318
-
319
- if (!html) {
320
- return html
321
- }
322
-
323
- return html.replace(/&#xFEFF;/g, '')
324
- }
325
-
326
- /** Clean up the HTML, ready for save */
327
- sanitizeHtml(html: string): string {
328
-
329
- html = this.removeZeroWidthNoBreakSpaces(html)
330
- html = this.removeCursor(html)
331
- html = this.$sanitize(html)
332
-
333
- return html
334
- }
335
-
336
- }
1
+ // tslint:disable: max-file-line-count
2
+ import angular, { sanitize } from 'angular'
3
+ import { Inject, Injectable } from '../decorators'
4
+
5
+ // text editor requires sanitization but also needs to allow for style attributes which are removed by default
6
+ angular.module('app').config(['$sanitizeProvider', ($sanitizeProvider: ng.sanitize.ISanitizeProvider) => {
7
+ $sanitizeProvider.addValidAttrs(['style'])
8
+
9
+ // Quill uses data-list to distinguish bulleted lists from numbered lists (they are both provided as <ol>)
10
+ $sanitizeProvider.addValidAttrs(['data-list'])
11
+
12
+ // Allow target attribute on anchor tags
13
+ $sanitizeProvider.addValidAttrs(['target'])
14
+ }])
15
+
16
+ /** The text styles a list item's bullet or number copies from the item's first character. */
17
+ export const LIST_MARKER_PROPERTIES = ['color', 'font-family', 'font-size', 'font-weight'] as const
18
+
19
+ /** In the editor, marker styles go on the <li> as CSS variables, because Quill would read real ones as content formats. */
20
+ export const LIST_MARKER_VARIABLE_PREFIX = '--mfly-list-marker-'
21
+
22
+ /**
23
+ * Styles removed from list items before editing. As well as the marker styles, this includes the shared `padding-left`
24
+ * that js-ui 53.1.0-0 saved, which would otherwise override the em-based indent.
25
+ */
26
+ const LIST_ITEM_STYLES_TO_CLEAR = [...LIST_MARKER_PROPERTIES, 'padding-left']
27
+
28
+ const LIST_ITEM_TAG_REGEX = /<li[\s>]/i
29
+
30
+ export type ListMarkerStyle = Partial<Record<typeof LIST_MARKER_PROPERTIES[number], string>>
31
+
32
+ @Injectable('richTextService')
33
+ export default class RichTextService {
34
+
35
+ constructor(
36
+ @Inject('$sanitize') private $sanitize: sanitize.ISanitizeService,
37
+ ) { }
38
+
39
+ /** Older versions of Quill rendered some things differently. Patch those up. */
40
+ backwardsCompatibleHtml(html: string): string {
41
+
42
+ html = this.removeZeroWidthNoBreakSpaces(html)
43
+ html = this.removeCursor(html)
44
+ html = this.fixWrappingCursor(html)
45
+ html = this.fixFontTags(html)
46
+ html = this.fixBulletPoints(html)
47
+ html = this.fixStrongBold(html)
48
+ html = this.removeListMarkerStyles(html)
49
+
50
+ return html
51
+ }
52
+
53
+ /**
54
+ * Transform <ul><li> into <ol><li data-list="bullet">,
55
+ * and <ol><li> into <ol><li data-list="ordered">
56
+ * accounting for any pattern of nested <ul> and <ol> tags.
57
+ */
58
+ fixBulletPoints(html: string): string {
59
+
60
+ if (!html) {
61
+ return html
62
+ }
63
+
64
+ let newHtml: string = ''
65
+
66
+ // Regexes for these, in case someone invents a <link> tag or something
67
+ const olStartTagRegex = new RegExp(`<ol(\\s|>)`)
68
+ const ulStartTagRegex = new RegExp(`<ul(\\s|>)`)
69
+ const liStartTagRegex = new RegExp(`<li(\\s|>)`)
70
+ // Literal string matches for these
71
+ const olEndTag = `</ol>`
72
+ const ulEndTag = `</ul>`
73
+
74
+ // Stack of <ol> and <ul> tags, representing how the lists are nested
75
+ const listStack: string[] = []
76
+
77
+ let parsedLength = 0
78
+ let currentListType: string | undefined
79
+
80
+ // Loop through the input html, looking for <ol>, <ul> and <li> tags. Maintain a stack of nested lists, so
81
+ // we can transform the current <li> correctly.
82
+ while (parsedLength < html.length) {
83
+
84
+ // Find the start of the next <ol>, <ul> or <li> tag
85
+ const olStart = this.indexOfRegex(html, olStartTagRegex, parsedLength)
86
+ const ulStart = this.indexOfRegex(html, ulStartTagRegex, parsedLength)
87
+ const liStart = this.indexOfRegex(html, liStartTagRegex, parsedLength)
88
+
89
+ // Find what might be the end of the current list (or might be the end of a nested list)
90
+ let nextListEnd: number
91
+ switch (currentListType) {
92
+ case 'ol':
93
+ nextListEnd = html.indexOf(olEndTag, parsedLength)
94
+ break
95
+ case 'ul':
96
+ nextListEnd = html.indexOf(ulEndTag, parsedLength)
97
+ break
98
+ default:
99
+ nextListEnd = -1
100
+ break
101
+ }
102
+
103
+ // Find which tag comes next
104
+ const nextItemPos = Math.min(
105
+ olStart === -1 ? html.length : olStart,
106
+ ulStart === -1 ? html.length : ulStart,
107
+ liStart === -1 ? html.length : liStart,
108
+ nextListEnd === -1 ? html.length : nextListEnd)
109
+
110
+ newHtml += html.substring(parsedLength, nextItemPos)
111
+ parsedLength = nextItemPos
112
+
113
+ switch (nextItemPos) {
114
+ case olStart:
115
+ case ulStart:
116
+ // We've found the start of a list
117
+ newHtml += `<ol>`
118
+
119
+ const listTagEnd = html.indexOf('>', nextItemPos)
120
+ if (listTagEnd === -1) {
121
+ // Invalid HTML. Dump the lot.
122
+ return ''
123
+ }
124
+ parsedLength = listTagEnd + 1
125
+
126
+ if (nextItemPos === olStart) {
127
+ listStack.push('ol')
128
+ currentListType = 'ol'
129
+ } else {
130
+ listStack.push('ul')
131
+ currentListType = 'ul'
132
+ }
133
+ break
134
+
135
+ case liStart:
136
+ // We've found a list item
137
+ if (!currentListType) {
138
+ // Invalid HTML. Dump the lot.
139
+ return ''
140
+ }
141
+
142
+ const liTagEnd = html.indexOf('>', liStart + 1)
143
+ if (liTagEnd === -1) {
144
+ // Invalid HTML. Dump the lot.
145
+ return ''
146
+ }
147
+ let liTag = html.substring(liStart, liTagEnd + 1)
148
+
149
+ if (liTag.indexOf('data-list') === -1) {
150
+ const dataAttr = ` data-list="${currentListType === 'ol' ? 'ordered' : 'bullet'}"`
151
+ liTag = `<li${dataAttr}` + liTag.substring(3)
152
+ }
153
+
154
+ newHtml += liTag
155
+ parsedLength = liTagEnd + 1
156
+ break
157
+
158
+ case nextListEnd:
159
+ // We've reached the end of the current list. Write out the closing tag and pop the stack
160
+ newHtml += olEndTag
161
+ parsedLength = nextListEnd + olEndTag.length
162
+ listStack.pop()
163
+ currentListType = listStack[listStack.length - 1]
164
+ break
165
+ }
166
+ }
167
+
168
+ if (listStack.length > 0) {
169
+ // We didn't close the last list. Invalid HTML. Dump the lot.
170
+ return ''
171
+ }
172
+
173
+ return newHtml
174
+ }
175
+
176
+ /**
177
+ * Transform <font> tags from the old Quill editor into <span> tags, as used by the new editor.
178
+ * Quill used to set fonts using <font color="#000000" face="Open Sans">
179
+ * Now it uses <span style="font-family: &#34;Open Sans&#34;; color: rgb(0, 0, 0);">
180
+ * Although the former still renders correctly, it is skipped for editor.getText(), so it might report that the text editor is empty.
181
+ */
182
+ fixFontTags(html: string): string {
183
+
184
+ if (!html) {
185
+ return html
186
+ }
187
+
188
+ let fontTagPos = html.indexOf('<font')
189
+ while (fontTagPos > -1) {
190
+ const fontTagLength = html.indexOf('>', fontTagPos) - fontTagPos + 1
191
+ const fontTag = html.substr(fontTagPos, fontTagLength)
192
+
193
+ const faceMatch = fontTag.match(/face="([^"]+)"/)
194
+ const colorMatch = fontTag.match(/color="([^"]+)"/)
195
+
196
+ const styles: string[] = []
197
+ if (faceMatch) {
198
+ styles.push(`font-family: &#34;${faceMatch[1]}&#34;;`)
199
+ }
200
+ if (colorMatch) {
201
+ styles.push(`color: ${colorMatch[1]};`)
202
+ }
203
+
204
+ html =
205
+ html.substr(0, fontTagPos) +
206
+ `<span style="${styles.join(' ')}">` +
207
+ html.substr(fontTagPos + fontTagLength)
208
+
209
+ fontTagPos = html.indexOf('<font')
210
+ }
211
+
212
+ html = html.replace(/<\/font>/g, '<\/span>')
213
+
214
+ return html
215
+ }
216
+
217
+ /**
218
+ * Quill adds its own editor cursor to its HTML: <span class="ql-cursor">|</span>
219
+ * These days we remove that on save, but sometimes it's present in the HTML that was saved by an older version of the editor.
220
+ * In rare cases, the HTML wraps that span around actual content. This function fixes that.
221
+ */
222
+ fixWrappingCursor(html: string): string {
223
+
224
+ if (!html) {
225
+ return html
226
+ }
227
+
228
+ // Regexes can't save us because the cursor span may contain other spans, like this:
229
+ // <p><span class="ql-cursor"><span style="font-size: 24px;">Yikes!</span></span></p>
230
+ // We want to remove the outer span, while preserving the inner span.
231
+ const cursorStartTag = `<span class="ql-cursor">`
232
+ const spanStartTag = `<span`
233
+ const spanEndTag = `</span>`
234
+ let cursorStart = html.indexOf(cursorStartTag)
235
+ while (cursorStart > -1) {
236
+
237
+ let currentPos = cursorStart
238
+ let spanDepth = 1
239
+ while (spanDepth > 0) {
240
+ const nextSpanStart = html.indexOf(spanStartTag, currentPos + 1)
241
+ const nextSpanEnd = html.indexOf(spanEndTag, currentPos + 1)
242
+ if (nextSpanStart === -1 && nextSpanEnd === -1) {
243
+ // Spans aren't closed properly, so we have invalid html. Dump the lot. (Should never happen.)
244
+ html = ''
245
+ spanDepth = 0
246
+ } else if (nextSpanStart > -1 && nextSpanStart < nextSpanEnd) {
247
+ // We've found another nested span
248
+ spanDepth++
249
+ currentPos = nextSpanStart
250
+ } else {
251
+ // We've found the end of our current span
252
+ spanDepth--
253
+ currentPos = nextSpanEnd
254
+ }
255
+ }
256
+
257
+ if (html !== '') {
258
+ const cursorEnd = currentPos + spanEndTag.length - 1
259
+ const cursorOuterLength = cursorEnd - cursorStart + 1
260
+ const cursorInnerLength = cursorOuterLength - cursorStartTag.length - spanEndTag.length
261
+ html =
262
+ html.substr(0, cursorStart) + // Everything before the cursor
263
+ html.substr(cursorStart + cursorStartTag.length, cursorInnerLength) + // Everything inside the cursor
264
+ html.substr(cursorEnd + 1) // Everything after the cursor
265
+ }
266
+
267
+ // There might be multiple cursors - even nested cursors - so keep going until there are none left
268
+ cursorStart = html.indexOf(`<span class="ql-cursor">`)
269
+ }
270
+
271
+ return html
272
+ }
273
+
274
+ /**
275
+ * To transition to supporting Google font weights, we switched to inline font weights rather than quill's default <strong> tags.
276
+ * Replace the legacy strong's with regular span's and an inline font weight.
277
+ */
278
+ fixStrongBold(html: string): string {
279
+
280
+ if (!html) { return html }
281
+
282
+ const bold = '700'
283
+
284
+ // Replace <strong> tags with <span> tags and add font-weight: 700
285
+ html = html.replace(/<strong([^>]*)>/g, (match, attributes) => {
286
+ // Check if there's already a style attribute
287
+ const styleMatch = attributes.match(/style="([^"]*)"/)
288
+
289
+ if (styleMatch) {
290
+ // There's already a style attribute, add font-weight to it
291
+ const existingStyles = (styleMatch[1] || '').trim()
292
+
293
+ // Check if font-weight is already present
294
+ if (existingStyles.includes('font-weight')) {
295
+ // Ignore existing font-weight
296
+ return `<span${attributes.replace(styleMatch[0], `style="${existingStyles}"`)}>`
297
+ } else {
298
+ // Add font-weight to styles
299
+ const separator = existingStyles.endsWith(';') ? ' ' : '; '
300
+ return `<span${attributes.replace(styleMatch[0], `style="${existingStyles}${separator}font-weight: ${bold};"`)}>`
301
+ }
302
+ } else {
303
+ // No style attribute exists, add one with font-weight
304
+ return `<span${attributes} style="font-weight: ${bold};">`
305
+ }
306
+ })
307
+
308
+ // Replace closing tags
309
+ html = html.replace(/<\/strong>/g, '</span>')
310
+
311
+ return html
312
+ }
313
+
314
+ /** Helper method because regexes can't natively start searching from a given index */
315
+ indexOfRegex(text: string, regex: RegExp, fromIndex: number): number {
316
+
317
+ const match = text.substring(fromIndex).match(regex)
318
+ if (!match || match.index === undefined || match.index === -1) {
319
+ return -1
320
+ }
321
+ return fromIndex + match.index
322
+ }
323
+
324
+ /** Sometimes Quill's HTML includes its own cursor. We don't want to save that. */
325
+ removeCursor(html: string): string {
326
+
327
+ if (!html) {
328
+ return html
329
+ }
330
+
331
+ return html.replace(/\<span class="ql-cursor"\>.?\<\/span\>/g, '')
332
+ }
333
+
334
+ /** Sometimes Quill adds Zero Width No-Break Spaces. */
335
+ removeZeroWidthNoBreakSpaces(html: string): string {
336
+
337
+ if (!html) {
338
+ return html
339
+ }
340
+
341
+ return html.replace(/&#xFEFF;/g, '')
342
+ }
343
+
344
+ /** Clean up the HTML, ready for save */
345
+ sanitizeHtml(html: string): string {
346
+
347
+ html = this.removeZeroWidthNoBreakSpaces(html)
348
+ html = this.removeCursor(html)
349
+ html = this.addListMarkerStyles(html)
350
+ html = this.$sanitize(html)
351
+
352
+ return html
353
+ }
354
+
355
+ /**
356
+ * Quill only styles the text runs inside a list item, so a bullet or number would take the container's style,
357
+ * not its text's. Put the style of each item's first character on the <li> itself, so its marker inherits it.
358
+ * The item's em-based indent then also scales with its own marker.
359
+ * These styles are derived from the text, so `removeListMarkerStyles` can drop them again before editing.
360
+ */
361
+ addListMarkerStyles(html: string): string {
362
+
363
+ if (!html || !LIST_ITEM_TAG_REGEX.test(html)) {
364
+ return html
365
+ }
366
+
367
+ const template = document.createElement('template')
368
+ template.innerHTML = html
369
+
370
+ template.content.querySelectorAll('li').forEach((item) => {
371
+ const style = this.getListItemMarkerStyle(item)
372
+ this.clearListMarkerStyles(item)
373
+
374
+ for (const property of LIST_MARKER_PROPERTIES) {
375
+ const value = style?.[property]
376
+ if (value) {
377
+ item.style.setProperty(property, value)
378
+ }
379
+ }
380
+
381
+ if (!item.getAttribute('style')) {
382
+ item.removeAttribute('style')
383
+ }
384
+ })
385
+
386
+ return template.innerHTML
387
+ }
388
+
389
+ /**
390
+ * Remove the list item styles added by `addListMarkerStyles` (and the editor's own marker variables), so Quill
391
+ * never reads font styles on a block. Only the <li> start tags are rewritten; the rest of the HTML is untouched.
392
+ */
393
+ removeListMarkerStyles(html: string): string {
394
+
395
+ if (!html || !LIST_ITEM_TAG_REGEX.test(html)) {
396
+ return html
397
+ }
398
+
399
+ return html.replace(/<li\b[^>]*\bstyle=[^>]*>/gi, (tag) => {
400
+ const template = document.createElement('template')
401
+ template.innerHTML = `<ol>${tag}</li></ol>`
402
+
403
+ const item = template.content.querySelector('li')
404
+ if (!item) {
405
+ return tag
406
+ }
407
+
408
+ this.clearListMarkerStyles(item)
409
+ if (!item.getAttribute('style')) {
410
+ item.removeAttribute('style')
411
+ }
412
+
413
+ return item.outerHTML.slice(0, -'</li>'.length)
414
+ })
415
+ }
416
+
417
+ /**
418
+ * The styles of a list item's first character, which its bullet or number should copy.
419
+ * Undefined when the item has no text yet.
420
+ */
421
+ getListItemMarkerStyle(item: Element): ListMarkerStyle | undefined {
422
+
423
+ const firstText = this.findFirstText(item)
424
+ if (!firstText) {
425
+ return undefined
426
+ }
427
+
428
+ const style: ListMarkerStyle = {}
429
+ for (const property of LIST_MARKER_PROPERTIES) {
430
+ for (let element = firstText.parentElement; element && element !== item; element = element.parentElement) {
431
+ const value = element.style?.getPropertyValue(property)
432
+ if (value) {
433
+ style[property] = value
434
+ break
435
+ }
436
+ }
437
+ }
438
+
439
+ return style
440
+ }
441
+
442
+ private findFirstText(item: Element): Text | undefined {
443
+
444
+ const walker = item.ownerDocument.createTreeWalker(item, NodeFilter.SHOW_TEXT)
445
+
446
+ for (let node = walker.nextNode(); node; node = walker.nextNode()) {
447
+ // Skip Quill's marker element, and whitespace between tags
448
+ if (node.parentElement?.closest('.ql-ui')) {
449
+ continue
450
+ }
451
+ if (node.textContent?.replace(//g, '').trim()) {
452
+ return node as Text
453
+ }
454
+ }
455
+
456
+ return undefined
457
+ }
458
+
459
+ private clearListMarkerStyles(item: HTMLElement): void {
460
+
461
+ for (const property of Array.from(item.style)) {
462
+ if (property.startsWith(LIST_MARKER_VARIABLE_PREFIX)) {
463
+ item.style.removeProperty(property)
464
+ }
465
+ }
466
+ for (const property of LIST_ITEM_STYLES_TO_CLEAR) {
467
+ item.style.removeProperty(property)
468
+ }
469
+ }
470
+
471
+ }