@mediafly/js-ui 53.0.1-0 → 53.1.0-0

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