@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.
- package/.github/dependabot.yml +30 -0
- package/.node-version +1 -1
- package/.semaphore/semaphore.yml +9 -6
- package/lib/fonts.css +1 -1
- package/lib/src/components/actionable-empty-state/actionable-empty-state.js +8 -8
- package/lib/src/components/actionable-empty-state/actionable-empty-state.js.map +1 -1
- package/lib/src/components/auto-complete/auto-complete-match-renderer.component.d.ts +1 -1
- package/lib/src/components/auto-complete/auto-complete-match-renderer.component.js +14 -14
- package/lib/src/components/auto-complete/auto-complete-match-renderer.component.js.map +1 -1
- package/lib/src/components/auto-complete/auto-complete.component.js +12 -12
- package/lib/src/components/auto-complete/auto-complete.component.js.map +1 -1
- package/lib/src/components/auto-focus/auto-focus.service.js +3 -3
- package/lib/src/components/auto-focus/auto-focus.service.js.map +1 -1
- package/lib/src/components/color-utils/color-utils.service.js +2 -2
- package/lib/src/components/color-utils/color-utils.service.js.map +1 -1
- package/lib/src/components/date-time-zone-city-picker/date-time-zone-city-picker.js +9 -9
- package/lib/src/components/date-time-zone-city-picker/date-time-zone-city-picker.js.map +1 -1
- package/lib/src/components/date-time-zone-city-picker/time-zone-city-picker.js +6 -6
- package/lib/src/components/date-time-zone-city-picker/time-zone-city-picker.js.map +1 -1
- package/lib/src/components/date-time-zone-picker/datepicker.js +1 -1
- package/lib/src/components/date-time-zone-picker/datepicker.js.map +1 -1
- package/lib/src/components/dropdown/dropdown.d.ts +3 -3
- package/lib/src/components/dropdown/dropdown.js +11 -11
- package/lib/src/components/dropdown/dropdown.js.map +1 -1
- package/lib/src/components/dropdown-menu/dropdown-menu-action.component.d.ts +1 -0
- package/lib/src/components/dropdown-menu/dropdown-menu-action.component.js +9 -9
- package/lib/src/components/dropdown-menu/dropdown-menu-action.component.js.map +1 -1
- package/lib/src/components/dropdown-menu/dropdown-menu-contents.js +6 -6
- package/lib/src/components/dropdown-menu/dropdown-menu-contents.js.map +1 -1
- package/lib/src/components/dropdown-menu/dropdown-menu.js +9 -9
- package/lib/src/components/dropdown-menu/dropdown-menu.js.map +1 -1
- package/lib/src/components/feedback/clipboard.service.js +5 -5
- package/lib/src/components/feedback/clipboard.service.js.map +1 -1
- package/lib/src/components/feedback/feedback.component.d.ts +1 -1
- package/lib/src/components/feedback/feedback.component.js +6 -6
- package/lib/src/components/feedback/feedback.component.js.map +1 -1
- package/lib/src/components/feedback/feedback.service.js +3 -3
- package/lib/src/components/feedback/feedback.service.js.map +1 -1
- package/lib/src/components/first-time-motivator/first-time-motivator.js +8 -8
- package/lib/src/components/first-time-motivator/first-time-motivator.js.map +1 -1
- package/lib/src/components/first-time-motivator/ftm-target-element.js +1 -1
- package/lib/src/components/first-time-motivator/ftm-target-element.js.map +1 -1
- package/lib/src/components/font-selector/font-selector.js +13 -13
- package/lib/src/components/font-selector/font-selector.js.map +1 -1
- package/lib/src/components/font-selector/google-webfonts-api.service.js +4 -4
- package/lib/src/components/font-selector/google-webfonts-api.service.js.map +1 -1
- package/lib/src/components/font-selector/webfont-loader.service.js +1 -1
- package/lib/src/components/font-selector/webfont-loader.service.js.map +1 -1
- package/lib/src/components/font-weight-selector/font-weight-selector.js +11 -11
- package/lib/src/components/font-weight-selector/font-weight-selector.js.map +1 -1
- package/lib/src/components/full-screen/full-screen.service.d.ts +1 -1
- package/lib/src/components/full-screen/full-screen.service.js +1 -1
- package/lib/src/components/full-screen/full-screen.service.js.map +1 -1
- package/lib/src/components/hover-tooltip/hover-tooltip.js +1 -1
- package/lib/src/components/hover-tooltip/hover-tooltip.js.map +1 -1
- package/lib/src/components/icon/icon.service.js +1 -1
- package/lib/src/components/icon/icon.service.js.map +1 -1
- package/lib/src/components/inline-text-input/inline-text-input.component.js +2 -2
- package/lib/src/components/inline-text-input/inline-text-input.component.js.map +1 -1
- package/lib/src/components/loading-indicator/loading-indicator.component.js +2 -2
- package/lib/src/components/loading-indicator/loading-indicator.component.js.map +1 -1
- package/lib/src/components/metadata-form/metadata-form.component.js +11 -11
- package/lib/src/components/metadata-form/metadata-form.component.js.map +1 -1
- package/lib/src/components/modal/modal.component.js +14 -14
- package/lib/src/components/modal/modal.component.js.map +1 -1
- package/lib/src/components/modal/modal.service.d.ts +2 -1
- package/lib/src/components/modal/modal.service.js +6 -6
- package/lib/src/components/modal/modal.service.js.map +1 -1
- package/lib/src/components/progress-bar/progress-bar.component.js +2 -2
- package/lib/src/components/progress-bar/progress-bar.component.js.map +1 -1
- package/lib/src/components/progress-circle/progress-circle.js +2 -2
- package/lib/src/components/progress-circle/progress-circle.js.map +1 -1
- package/lib/src/components/response-handlers/api-success-handlers.js +1 -1
- package/lib/src/components/response-handlers/api-success-handlers.js.map +1 -1
- package/lib/src/components/response-handlers/error-response.service.js +2 -2
- package/lib/src/components/response-handlers/error-response.service.js.map +1 -1
- package/lib/src/components/response-handlers/feedback-error-handler.js +4 -4
- package/lib/src/components/response-handlers/feedback-error-handler.js.map +1 -1
- package/lib/src/components/response-handlers/log-error-handler.js +2 -2
- package/lib/src/components/response-handlers/log-error-handler.js.map +1 -1
- package/lib/src/components/rich-text/rich-text.component.d.ts +8 -0
- package/lib/src/components/rich-text/rich-text.component.js +57 -19
- package/lib/src/components/rich-text/rich-text.component.js.map +1 -1
- package/lib/src/components/rich-text/rich-text.service.d.ts +36 -0
- package/lib/src/components/rich-text/rich-text.service.js +136 -2
- package/lib/src/components/rich-text/rich-text.service.js.map +1 -1
- package/lib/src/components/site-header/site-header-navbar-item.component.js +8 -8
- package/lib/src/components/site-header/site-header-navbar-item.component.js.map +1 -1
- package/lib/src/components/site-header/site-header.component.js +8 -8
- package/lib/src/components/site-header/site-header.component.js.map +1 -1
- package/lib/src/components/tabs/tab-group.js +3 -3
- package/lib/src/components/tabs/tab-group.js.map +1 -1
- package/lib/src/components/tabs/tab-html-title.js +2 -2
- package/lib/src/components/tabs/tab-html-title.js.map +1 -1
- package/lib/src/components/tabs/tab.js +8 -8
- package/lib/src/components/tabs/tab.js.map +1 -1
- package/lib/src/components/tag-input/tag-input.component.d.ts +1 -1
- package/lib/src/components/tag-input/tag-input.component.js +12 -12
- package/lib/src/components/tag-input/tag-input.component.js.map +1 -1
- package/lib/src/components/theme/theme.service.js +1 -1
- package/lib/src/components/theme/theme.service.js.map +1 -1
- package/lib/src/components/theme/theme.typings.d.ts +1 -1
- package/lib/src/components/type-string/type-string.component.js +3 -3
- package/lib/src/components/type-string/type-string.component.js.map +1 -1
- package/lib/src/components/url-modification/url-modification.service.js +1 -1
- package/lib/src/components/url-modification/url-modification.service.js.map +1 -1
- package/lib/styles.css +1 -1
- package/lib/tsconfig.deploy.tsbuildinfo +1 -6298
- package/package.json +60 -52
- package/src/components/rich-text/rich-text.component.less +10 -0
- package/src/components/rich-text/rich-text.component.ts +40 -1
- package/src/components/rich-text/rich-text.service.ts +510 -336
- package/src/index.spec.js +41 -40
- package/tsconfig.tsbuildinfo +1 -10494
- package/webpack.config.js +1 -1
|
@@ -1,336 +1,510 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
// Replace
|
|
291
|
-
html = html.replace(
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
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: "Open Sans"; 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: "${faceMatch[1]}";`)
|
|
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(//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
|
+
}
|