@ckeditor/ckeditor5-html-support 0.0.0-internal-20241017.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 (312) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +26 -0
  4. package/build/html-support.js +5 -0
  5. package/build/translations/ar.js +1 -0
  6. package/build/translations/bg.js +1 -0
  7. package/build/translations/bn.js +1 -0
  8. package/build/translations/ca.js +1 -0
  9. package/build/translations/cs.js +1 -0
  10. package/build/translations/da.js +1 -0
  11. package/build/translations/de.js +1 -0
  12. package/build/translations/el.js +1 -0
  13. package/build/translations/en-au.js +1 -0
  14. package/build/translations/es.js +1 -0
  15. package/build/translations/et.js +1 -0
  16. package/build/translations/fi.js +1 -0
  17. package/build/translations/fr.js +1 -0
  18. package/build/translations/gl.js +1 -0
  19. package/build/translations/he.js +1 -0
  20. package/build/translations/hi.js +1 -0
  21. package/build/translations/hr.js +1 -0
  22. package/build/translations/hu.js +1 -0
  23. package/build/translations/id.js +1 -0
  24. package/build/translations/it.js +1 -0
  25. package/build/translations/ja.js +1 -0
  26. package/build/translations/jv.js +1 -0
  27. package/build/translations/ko.js +1 -0
  28. package/build/translations/lt.js +1 -0
  29. package/build/translations/lv.js +1 -0
  30. package/build/translations/ms.js +1 -0
  31. package/build/translations/nl.js +1 -0
  32. package/build/translations/no.js +1 -0
  33. package/build/translations/pl.js +1 -0
  34. package/build/translations/pt-br.js +1 -0
  35. package/build/translations/pt.js +1 -0
  36. package/build/translations/ro.js +1 -0
  37. package/build/translations/ru.js +1 -0
  38. package/build/translations/sk.js +1 -0
  39. package/build/translations/sr-latn.js +1 -0
  40. package/build/translations/sr.js +1 -0
  41. package/build/translations/sv.js +1 -0
  42. package/build/translations/th.js +1 -0
  43. package/build/translations/tr.js +1 -0
  44. package/build/translations/ug.js +1 -0
  45. package/build/translations/uk.js +1 -0
  46. package/build/translations/ur.js +1 -0
  47. package/build/translations/vi.js +1 -0
  48. package/build/translations/zh-cn.js +1 -0
  49. package/build/translations/zh.js +1 -0
  50. package/ckeditor5-metadata.json +60 -0
  51. package/dist/augmentation.d.ts +37 -0
  52. package/dist/converters.d.ts +64 -0
  53. package/dist/datafilter.d.ts +312 -0
  54. package/dist/dataschema.d.ts +191 -0
  55. package/dist/fullpage.d.ts +29 -0
  56. package/dist/generalhtmlsupport.d.ts +106 -0
  57. package/dist/generalhtmlsupportconfig.d.ts +81 -0
  58. package/dist/htmlcomment.d.ts +79 -0
  59. package/dist/htmlpagedataprocessor.d.ts +26 -0
  60. package/dist/index-content.css +4 -0
  61. package/dist/index-editor.css +51 -0
  62. package/dist/index.css +63 -0
  63. package/dist/index.css.map +1 -0
  64. package/dist/index.d.ts +29 -0
  65. package/dist/index.js +4040 -0
  66. package/dist/index.js.map +1 -0
  67. package/dist/integrations/codeblock.d.ts +31 -0
  68. package/dist/integrations/customelement.d.ts +35 -0
  69. package/dist/integrations/dualcontent.d.ts +53 -0
  70. package/dist/integrations/heading.d.ts +39 -0
  71. package/dist/integrations/image.d.ts +34 -0
  72. package/dist/integrations/integrationutils.d.ts +19 -0
  73. package/dist/integrations/list.d.ts +35 -0
  74. package/dist/integrations/mediaembed.d.ts +34 -0
  75. package/dist/integrations/script.d.ts +34 -0
  76. package/dist/integrations/style.d.ts +34 -0
  77. package/dist/integrations/table.d.ts +31 -0
  78. package/dist/schemadefinitions.d.ts +17 -0
  79. package/dist/translations/ar.d.ts +8 -0
  80. package/dist/translations/ar.js +5 -0
  81. package/dist/translations/ar.umd.js +11 -0
  82. package/dist/translations/bg.d.ts +8 -0
  83. package/dist/translations/bg.js +5 -0
  84. package/dist/translations/bg.umd.js +11 -0
  85. package/dist/translations/bn.d.ts +8 -0
  86. package/dist/translations/bn.js +5 -0
  87. package/dist/translations/bn.umd.js +11 -0
  88. package/dist/translations/ca.d.ts +8 -0
  89. package/dist/translations/ca.js +5 -0
  90. package/dist/translations/ca.umd.js +11 -0
  91. package/dist/translations/cs.d.ts +8 -0
  92. package/dist/translations/cs.js +5 -0
  93. package/dist/translations/cs.umd.js +11 -0
  94. package/dist/translations/da.d.ts +8 -0
  95. package/dist/translations/da.js +5 -0
  96. package/dist/translations/da.umd.js +11 -0
  97. package/dist/translations/de.d.ts +8 -0
  98. package/dist/translations/de.js +5 -0
  99. package/dist/translations/de.umd.js +11 -0
  100. package/dist/translations/el.d.ts +8 -0
  101. package/dist/translations/el.js +5 -0
  102. package/dist/translations/el.umd.js +11 -0
  103. package/dist/translations/en-au.d.ts +8 -0
  104. package/dist/translations/en-au.js +5 -0
  105. package/dist/translations/en-au.umd.js +11 -0
  106. package/dist/translations/en.d.ts +8 -0
  107. package/dist/translations/en.js +5 -0
  108. package/dist/translations/en.umd.js +11 -0
  109. package/dist/translations/es.d.ts +8 -0
  110. package/dist/translations/es.js +5 -0
  111. package/dist/translations/es.umd.js +11 -0
  112. package/dist/translations/et.d.ts +8 -0
  113. package/dist/translations/et.js +5 -0
  114. package/dist/translations/et.umd.js +11 -0
  115. package/dist/translations/fi.d.ts +8 -0
  116. package/dist/translations/fi.js +5 -0
  117. package/dist/translations/fi.umd.js +11 -0
  118. package/dist/translations/fr.d.ts +8 -0
  119. package/dist/translations/fr.js +5 -0
  120. package/dist/translations/fr.umd.js +11 -0
  121. package/dist/translations/gl.d.ts +8 -0
  122. package/dist/translations/gl.js +5 -0
  123. package/dist/translations/gl.umd.js +11 -0
  124. package/dist/translations/he.d.ts +8 -0
  125. package/dist/translations/he.js +5 -0
  126. package/dist/translations/he.umd.js +11 -0
  127. package/dist/translations/hi.d.ts +8 -0
  128. package/dist/translations/hi.js +5 -0
  129. package/dist/translations/hi.umd.js +11 -0
  130. package/dist/translations/hr.d.ts +8 -0
  131. package/dist/translations/hr.js +5 -0
  132. package/dist/translations/hr.umd.js +11 -0
  133. package/dist/translations/hu.d.ts +8 -0
  134. package/dist/translations/hu.js +5 -0
  135. package/dist/translations/hu.umd.js +11 -0
  136. package/dist/translations/id.d.ts +8 -0
  137. package/dist/translations/id.js +5 -0
  138. package/dist/translations/id.umd.js +11 -0
  139. package/dist/translations/it.d.ts +8 -0
  140. package/dist/translations/it.js +5 -0
  141. package/dist/translations/it.umd.js +11 -0
  142. package/dist/translations/ja.d.ts +8 -0
  143. package/dist/translations/ja.js +5 -0
  144. package/dist/translations/ja.umd.js +11 -0
  145. package/dist/translations/jv.d.ts +8 -0
  146. package/dist/translations/jv.js +5 -0
  147. package/dist/translations/jv.umd.js +11 -0
  148. package/dist/translations/ko.d.ts +8 -0
  149. package/dist/translations/ko.js +5 -0
  150. package/dist/translations/ko.umd.js +11 -0
  151. package/dist/translations/lt.d.ts +8 -0
  152. package/dist/translations/lt.js +5 -0
  153. package/dist/translations/lt.umd.js +11 -0
  154. package/dist/translations/lv.d.ts +8 -0
  155. package/dist/translations/lv.js +5 -0
  156. package/dist/translations/lv.umd.js +11 -0
  157. package/dist/translations/ms.d.ts +8 -0
  158. package/dist/translations/ms.js +5 -0
  159. package/dist/translations/ms.umd.js +11 -0
  160. package/dist/translations/nl.d.ts +8 -0
  161. package/dist/translations/nl.js +5 -0
  162. package/dist/translations/nl.umd.js +11 -0
  163. package/dist/translations/no.d.ts +8 -0
  164. package/dist/translations/no.js +5 -0
  165. package/dist/translations/no.umd.js +11 -0
  166. package/dist/translations/pl.d.ts +8 -0
  167. package/dist/translations/pl.js +5 -0
  168. package/dist/translations/pl.umd.js +11 -0
  169. package/dist/translations/pt-br.d.ts +8 -0
  170. package/dist/translations/pt-br.js +5 -0
  171. package/dist/translations/pt-br.umd.js +11 -0
  172. package/dist/translations/pt.d.ts +8 -0
  173. package/dist/translations/pt.js +5 -0
  174. package/dist/translations/pt.umd.js +11 -0
  175. package/dist/translations/ro.d.ts +8 -0
  176. package/dist/translations/ro.js +5 -0
  177. package/dist/translations/ro.umd.js +11 -0
  178. package/dist/translations/ru.d.ts +8 -0
  179. package/dist/translations/ru.js +5 -0
  180. package/dist/translations/ru.umd.js +11 -0
  181. package/dist/translations/sk.d.ts +8 -0
  182. package/dist/translations/sk.js +5 -0
  183. package/dist/translations/sk.umd.js +11 -0
  184. package/dist/translations/sr-latn.d.ts +8 -0
  185. package/dist/translations/sr-latn.js +5 -0
  186. package/dist/translations/sr-latn.umd.js +11 -0
  187. package/dist/translations/sr.d.ts +8 -0
  188. package/dist/translations/sr.js +5 -0
  189. package/dist/translations/sr.umd.js +11 -0
  190. package/dist/translations/sv.d.ts +8 -0
  191. package/dist/translations/sv.js +5 -0
  192. package/dist/translations/sv.umd.js +11 -0
  193. package/dist/translations/th.d.ts +8 -0
  194. package/dist/translations/th.js +5 -0
  195. package/dist/translations/th.umd.js +11 -0
  196. package/dist/translations/tr.d.ts +8 -0
  197. package/dist/translations/tr.js +5 -0
  198. package/dist/translations/tr.umd.js +11 -0
  199. package/dist/translations/ug.d.ts +8 -0
  200. package/dist/translations/ug.js +5 -0
  201. package/dist/translations/ug.umd.js +11 -0
  202. package/dist/translations/uk.d.ts +8 -0
  203. package/dist/translations/uk.js +5 -0
  204. package/dist/translations/uk.umd.js +11 -0
  205. package/dist/translations/ur.d.ts +8 -0
  206. package/dist/translations/ur.js +5 -0
  207. package/dist/translations/ur.umd.js +11 -0
  208. package/dist/translations/vi.d.ts +8 -0
  209. package/dist/translations/vi.js +5 -0
  210. package/dist/translations/vi.umd.js +11 -0
  211. package/dist/translations/zh-cn.d.ts +8 -0
  212. package/dist/translations/zh-cn.js +5 -0
  213. package/dist/translations/zh-cn.umd.js +11 -0
  214. package/dist/translations/zh.d.ts +8 -0
  215. package/dist/translations/zh.js +5 -0
  216. package/dist/translations/zh.umd.js +11 -0
  217. package/dist/utils.d.ts +76 -0
  218. package/lang/contexts.json +3 -0
  219. package/lang/translations/ar.po +22 -0
  220. package/lang/translations/bg.po +22 -0
  221. package/lang/translations/bn.po +22 -0
  222. package/lang/translations/ca.po +22 -0
  223. package/lang/translations/cs.po +22 -0
  224. package/lang/translations/da.po +22 -0
  225. package/lang/translations/de.po +22 -0
  226. package/lang/translations/el.po +22 -0
  227. package/lang/translations/en-au.po +22 -0
  228. package/lang/translations/en.po +22 -0
  229. package/lang/translations/es.po +22 -0
  230. package/lang/translations/et.po +22 -0
  231. package/lang/translations/fi.po +22 -0
  232. package/lang/translations/fr.po +22 -0
  233. package/lang/translations/gl.po +22 -0
  234. package/lang/translations/he.po +22 -0
  235. package/lang/translations/hi.po +22 -0
  236. package/lang/translations/hr.po +22 -0
  237. package/lang/translations/hu.po +22 -0
  238. package/lang/translations/id.po +22 -0
  239. package/lang/translations/it.po +22 -0
  240. package/lang/translations/ja.po +22 -0
  241. package/lang/translations/jv.po +22 -0
  242. package/lang/translations/ko.po +22 -0
  243. package/lang/translations/lt.po +22 -0
  244. package/lang/translations/lv.po +22 -0
  245. package/lang/translations/ms.po +22 -0
  246. package/lang/translations/nl.po +22 -0
  247. package/lang/translations/no.po +22 -0
  248. package/lang/translations/pl.po +22 -0
  249. package/lang/translations/pt-br.po +22 -0
  250. package/lang/translations/pt.po +22 -0
  251. package/lang/translations/ro.po +22 -0
  252. package/lang/translations/ru.po +22 -0
  253. package/lang/translations/sk.po +22 -0
  254. package/lang/translations/sr-latn.po +22 -0
  255. package/lang/translations/sr.po +22 -0
  256. package/lang/translations/sv.po +22 -0
  257. package/lang/translations/th.po +22 -0
  258. package/lang/translations/tr.po +22 -0
  259. package/lang/translations/ug.po +22 -0
  260. package/lang/translations/uk.po +22 -0
  261. package/lang/translations/ur.po +22 -0
  262. package/lang/translations/vi.po +22 -0
  263. package/lang/translations/zh-cn.po +22 -0
  264. package/lang/translations/zh.po +22 -0
  265. package/package.json +48 -0
  266. package/src/augmentation.d.ts +33 -0
  267. package/src/augmentation.js +5 -0
  268. package/src/converters.d.ts +60 -0
  269. package/src/converters.js +180 -0
  270. package/src/datafilter.d.ts +308 -0
  271. package/src/datafilter.js +755 -0
  272. package/src/dataschema.d.ts +187 -0
  273. package/src/dataschema.js +202 -0
  274. package/src/fullpage.d.ts +25 -0
  275. package/src/fullpage.js +86 -0
  276. package/src/generalhtmlsupport.d.ts +102 -0
  277. package/src/generalhtmlsupport.js +246 -0
  278. package/src/generalhtmlsupportconfig.d.ts +77 -0
  279. package/src/generalhtmlsupportconfig.js +5 -0
  280. package/src/htmlcomment.d.ts +75 -0
  281. package/src/htmlcomment.js +224 -0
  282. package/src/htmlpagedataprocessor.d.ts +22 -0
  283. package/src/htmlpagedataprocessor.js +67 -0
  284. package/src/index.d.ts +25 -0
  285. package/src/index.js +14 -0
  286. package/src/integrations/codeblock.d.ts +27 -0
  287. package/src/integrations/codeblock.js +107 -0
  288. package/src/integrations/customelement.d.ts +31 -0
  289. package/src/integrations/customelement.js +162 -0
  290. package/src/integrations/dualcontent.d.ts +49 -0
  291. package/src/integrations/dualcontent.js +125 -0
  292. package/src/integrations/heading.d.ts +35 -0
  293. package/src/integrations/heading.js +72 -0
  294. package/src/integrations/image.d.ts +30 -0
  295. package/src/integrations/image.js +195 -0
  296. package/src/integrations/integrationutils.d.ts +15 -0
  297. package/src/integrations/integrationutils.js +21 -0
  298. package/src/integrations/list.d.ts +31 -0
  299. package/src/integrations/list.js +185 -0
  300. package/src/integrations/mediaembed.d.ts +30 -0
  301. package/src/integrations/mediaembed.js +125 -0
  302. package/src/integrations/script.d.ts +30 -0
  303. package/src/integrations/script.js +65 -0
  304. package/src/integrations/style.d.ts +30 -0
  305. package/src/integrations/style.js +65 -0
  306. package/src/integrations/table.d.ts +27 -0
  307. package/src/integrations/table.js +169 -0
  308. package/src/schemadefinitions.d.ts +13 -0
  309. package/src/schemadefinitions.js +964 -0
  310. package/src/utils.d.ts +72 -0
  311. package/src/utils.js +139 -0
  312. package/theme/datafilter.css +56 -0
@@ -0,0 +1,755 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module html-support/datafilter
7
+ */
8
+ import { Plugin } from 'ckeditor5/src/core.js';
9
+ import { Matcher, StylesMap } from 'ckeditor5/src/engine.js';
10
+ import { CKEditorError, priorities, isValidAttributeName } from 'ckeditor5/src/utils.js';
11
+ import { Widget } from 'ckeditor5/src/widget.js';
12
+ import { viewToModelObjectConverter, toObjectWidgetConverter, createObjectView, viewToAttributeInlineConverter, attributeToViewInlineConverter, emptyInlineModelElementToViewConverter, viewToModelBlockAttributeConverter, modelToViewBlockAttributeConverter } from './converters.js';
13
+ import { default as DataSchema } from './dataschema.js';
14
+ import { getHtmlAttributeName } from './utils.js';
15
+ import { isPlainObject } from 'lodash-es';
16
+ import '../theme/datafilter.css';
17
+ /**
18
+ * Allows to validate elements and element attributes registered by {@link module:html-support/dataschema~DataSchema}.
19
+ *
20
+ * To enable registered element in the editor, use {@link module:html-support/datafilter~DataFilter#allowElement} method:
21
+ *
22
+ * ```ts
23
+ * dataFilter.allowElement( 'section' );
24
+ * ```
25
+ *
26
+ * You can also allow or disallow specific element attributes:
27
+ *
28
+ * ```ts
29
+ * // Allow `data-foo` attribute on `section` element.
30
+ * dataFilter.allowAttributes( {
31
+ * name: 'section',
32
+ * attributes: {
33
+ * 'data-foo': true
34
+ * }
35
+ * } );
36
+ *
37
+ * // Disallow `color` style attribute on 'section' element.
38
+ * dataFilter.disallowAttributes( {
39
+ * name: 'section',
40
+ * styles: {
41
+ * color: /[\s\S]+/
42
+ * }
43
+ * } );
44
+ * ```
45
+ *
46
+ * To apply the information about allowed and disallowed attributes in custom integration plugin,
47
+ * use the {@link module:html-support/datafilter~DataFilter#processViewAttributes `processViewAttributes()`} method.
48
+ */
49
+ export default class DataFilter extends Plugin {
50
+ constructor(editor) {
51
+ super(editor);
52
+ this._dataSchema = editor.plugins.get('DataSchema');
53
+ this._allowedAttributes = new Matcher();
54
+ this._disallowedAttributes = new Matcher();
55
+ this._allowedElements = new Set();
56
+ this._disallowedElements = new Set();
57
+ this._dataInitialized = false;
58
+ this._coupledAttributes = null;
59
+ this._registerElementsAfterInit();
60
+ this._registerElementHandlers();
61
+ this._registerCoupledAttributesPostFixer();
62
+ this._registerAssociatedHtmlAttributesPostFixer();
63
+ }
64
+ /**
65
+ * @inheritDoc
66
+ */
67
+ static get pluginName() {
68
+ return 'DataFilter';
69
+ }
70
+ /**
71
+ * @inheritDoc
72
+ */
73
+ static get isOfficialPlugin() {
74
+ return true;
75
+ }
76
+ /**
77
+ * @inheritDoc
78
+ */
79
+ static get requires() {
80
+ return [DataSchema, Widget];
81
+ }
82
+ /**
83
+ * Load a configuration of one or many elements, where their attributes should be allowed.
84
+ *
85
+ * **Note**: Rules will be applied just before next data pipeline data init or set.
86
+ *
87
+ * @param config Configuration of elements that should have their attributes accepted in the editor.
88
+ */
89
+ loadAllowedConfig(config) {
90
+ for (const pattern of config) {
91
+ // MatcherPattern allows omitting `name` to widen the search of elements.
92
+ // Let's keep it consistent and match every element if a `name` has not been provided.
93
+ const elementName = pattern.name || /[\s\S]+/;
94
+ const rules = splitRules(pattern);
95
+ this.allowElement(elementName);
96
+ rules.forEach(pattern => this.allowAttributes(pattern));
97
+ }
98
+ }
99
+ /**
100
+ * Load a configuration of one or many elements, where their attributes should be disallowed.
101
+ *
102
+ * **Note**: Rules will be applied just before next data pipeline data init or set.
103
+ *
104
+ * @param config Configuration of elements that should have their attributes rejected from the editor.
105
+ */
106
+ loadDisallowedConfig(config) {
107
+ for (const pattern of config) {
108
+ // MatcherPattern allows omitting `name` to widen the search of elements.
109
+ // Let's keep it consistent and match every element if a `name` has not been provided.
110
+ const elementName = pattern.name || /[\s\S]+/;
111
+ const rules = splitRules(pattern);
112
+ // Disallow element itself if there is no other rules.
113
+ if (rules.length == 0) {
114
+ this.disallowElement(elementName);
115
+ }
116
+ else {
117
+ rules.forEach(pattern => this.disallowAttributes(pattern));
118
+ }
119
+ }
120
+ }
121
+ /**
122
+ * Load a configuration of one or many elements, where when empty should be allowed.
123
+ *
124
+ * **Note**: It modifies DataSchema so must be loaded before registering filtering rules.
125
+ *
126
+ * @param config Configuration of elements that should be preserved even if empty.
127
+ */
128
+ loadAllowedEmptyElementsConfig(config) {
129
+ for (const elementName of config) {
130
+ this.allowEmptyElement(elementName);
131
+ }
132
+ }
133
+ /**
134
+ * Allow the given element in the editor context.
135
+ *
136
+ * This method will only allow elements described by the {@link module:html-support/dataschema~DataSchema} used
137
+ * to create data filter.
138
+ *
139
+ * **Note**: Rules will be applied just before next data pipeline data init or set.
140
+ *
141
+ * @param viewName String or regular expression matching view name.
142
+ */
143
+ allowElement(viewName) {
144
+ for (const definition of this._dataSchema.getDefinitionsForView(viewName, true)) {
145
+ this._addAllowedElement(definition);
146
+ // Reset cached map to recalculate it on the next usage.
147
+ this._coupledAttributes = null;
148
+ }
149
+ }
150
+ /**
151
+ * Disallow the given element in the editor context.
152
+ *
153
+ * This method will only disallow elements described by the {@link module:html-support/dataschema~DataSchema} used
154
+ * to create data filter.
155
+ *
156
+ * @param viewName String or regular expression matching view name.
157
+ */
158
+ disallowElement(viewName) {
159
+ for (const definition of this._dataSchema.getDefinitionsForView(viewName, false)) {
160
+ this._disallowedElements.add(definition.view);
161
+ }
162
+ }
163
+ /**
164
+ * Allow the given empty element in the editor context.
165
+ *
166
+ * This method will only allow elements described by the {@link module:html-support/dataschema~DataSchema} used
167
+ * to create data filter.
168
+ *
169
+ * **Note**: It modifies DataSchema so must be called before registering filtering rules.
170
+ *
171
+ * @param viewName String or regular expression matching view name.
172
+ */
173
+ allowEmptyElement(viewName) {
174
+ for (const definition of this._dataSchema.getDefinitionsForView(viewName, true)) {
175
+ if (definition.isInline) {
176
+ this._dataSchema.extendInlineElement({ ...definition, allowEmpty: true });
177
+ }
178
+ }
179
+ }
180
+ /**
181
+ * Allow the given attributes for view element allowed by {@link #allowElement} method.
182
+ *
183
+ * @param config Pattern matching all attributes which should be allowed.
184
+ */
185
+ allowAttributes(config) {
186
+ this._allowedAttributes.add(config);
187
+ }
188
+ /**
189
+ * Disallow the given attributes for view element allowed by {@link #allowElement} method.
190
+ *
191
+ * @param config Pattern matching all attributes which should be disallowed.
192
+ */
193
+ disallowAttributes(config) {
194
+ this._disallowedAttributes.add(config);
195
+ }
196
+ /**
197
+ * Processes all allowed and disallowed attributes on the view element by consuming them and returning the allowed ones.
198
+ *
199
+ * This method applies the configuration set up by {@link #allowAttributes `allowAttributes()`}
200
+ * and {@link #disallowAttributes `disallowAttributes()`} over the given view element by consuming relevant attributes.
201
+ * It returns the allowed attributes that were found on the given view element for further processing by integration code.
202
+ *
203
+ * ```ts
204
+ * dispatcher.on( 'element:myElement', ( evt, data, conversionApi ) => {
205
+ * // Get rid of disallowed and extract all allowed attributes from a viewElement.
206
+ * const viewAttributes = dataFilter.processViewAttributes( data.viewItem, conversionApi );
207
+ * // Do something with them, i.e. store inside a model as a dictionary.
208
+ * if ( viewAttributes ) {
209
+ * conversionApi.writer.setAttribute( 'htmlAttributesOfMyElement', viewAttributes, data.modelRange );
210
+ * }
211
+ * } );
212
+ * ```
213
+ *
214
+ * @see module:engine/conversion/viewconsumable~ViewConsumable#consume
215
+ *
216
+ * @returns Object with following properties:
217
+ * - attributes Set with matched attribute names.
218
+ * - styles Set with matched style names.
219
+ * - classes Set with matched class names.
220
+ */
221
+ processViewAttributes(viewElement, conversionApi) {
222
+ const { consumable } = conversionApi;
223
+ // Make sure that the disabled attributes are handled before the allowed attributes are called.
224
+ // For example, for block images the <figure> converter triggers conversion for <img> first and then for other elements, i.e. <a>.
225
+ matchAndConsumeAttributes(viewElement, this._disallowedAttributes, consumable);
226
+ return prepareGHSAttribute(viewElement, matchAndConsumeAttributes(viewElement, this._allowedAttributes, consumable));
227
+ }
228
+ /**
229
+ * Adds allowed element definition and fires registration event.
230
+ */
231
+ _addAllowedElement(definition) {
232
+ if (this._allowedElements.has(definition)) {
233
+ return;
234
+ }
235
+ this._allowedElements.add(definition);
236
+ // For attribute based integrations (table figure, document lists, etc.) register related element definitions.
237
+ if ('appliesToBlock' in definition && typeof definition.appliesToBlock == 'string') {
238
+ for (const relatedDefinition of this._dataSchema.getDefinitionsForModel(definition.appliesToBlock)) {
239
+ if (relatedDefinition.isBlock) {
240
+ this._addAllowedElement(relatedDefinition);
241
+ }
242
+ }
243
+ }
244
+ // We need to wait for all features to be initialized before we can register
245
+ // element, so we can access existing features model schemas.
246
+ // If the data has not been initialized yet, _registerElementsAfterInit() method will take care of
247
+ // registering elements.
248
+ if (this._dataInitialized) {
249
+ // Defer registration to the next data pipeline data set so any disallow rules could be applied
250
+ // even if added after allow rule (disallowElement).
251
+ this.editor.data.once('set', () => {
252
+ this._fireRegisterEvent(definition);
253
+ }, {
254
+ // With the highest priority listener we are able to register elements right before
255
+ // running data conversion.
256
+ priority: priorities.highest + 1
257
+ });
258
+ }
259
+ }
260
+ /**
261
+ * Registers elements allowed by {@link module:html-support/datafilter~DataFilter#allowElement} method
262
+ * once {@link module:engine/controller/datacontroller~DataController editor's data controller} is initialized.
263
+ */
264
+ _registerElementsAfterInit() {
265
+ this.editor.data.on('init', () => {
266
+ this._dataInitialized = true;
267
+ for (const definition of this._allowedElements) {
268
+ this._fireRegisterEvent(definition);
269
+ }
270
+ }, {
271
+ // With highest priority listener we are able to register elements right before
272
+ // running data conversion. Also:
273
+ // * Make sure that priority is higher than the one used by `RealTimeCollaborationClient`,
274
+ // as RTC is stopping event propagation.
275
+ // * Make sure no other features hook into this event before GHS because otherwise the
276
+ // downcast conversion (for these features) could run before GHS registered its converters
277
+ // (https://github.com/ckeditor/ckeditor5/issues/11356).
278
+ priority: priorities.highest + 1
279
+ });
280
+ }
281
+ /**
282
+ * Registers default element handlers.
283
+ */
284
+ _registerElementHandlers() {
285
+ this.on('register', (evt, definition) => {
286
+ const schema = this.editor.model.schema;
287
+ // Object element should be only registered for new features.
288
+ // If the model schema is already registered, it should be handled by
289
+ // #_registerBlockElement() or #_registerObjectElement() attribute handlers.
290
+ if (definition.isObject && !schema.isRegistered(definition.model)) {
291
+ this._registerObjectElement(definition);
292
+ }
293
+ else if (definition.isBlock) {
294
+ this._registerBlockElement(definition);
295
+ }
296
+ else if (definition.isInline) {
297
+ this._registerInlineElement(definition);
298
+ }
299
+ else {
300
+ /**
301
+ * The definition cannot be handled by the data filter.
302
+ *
303
+ * Make sure that the registered definition is correct.
304
+ *
305
+ * @error data-filter-invalid-definition
306
+ */
307
+ throw new CKEditorError('data-filter-invalid-definition', null, definition);
308
+ }
309
+ evt.stop();
310
+ }, { priority: 'lowest' });
311
+ }
312
+ /**
313
+ * Registers a model post-fixer that is removing coupled GHS attributes of inline elements. Those attributes
314
+ * are removed if a coupled feature attribute is removed.
315
+ *
316
+ * For example, consider following HTML:
317
+ *
318
+ * ```html
319
+ * <a href="foo.html" id="myId">bar</a>
320
+ * ```
321
+ *
322
+ * Which would be upcasted to following text node in the model:
323
+ *
324
+ * ```html
325
+ * <$text linkHref="foo.html" htmlA="{ attributes: { id: 'myId' } }">bar</$text>
326
+ * ```
327
+ *
328
+ * When the user removes the link from that text (using UI), only `linkHref` attribute would be removed:
329
+ *
330
+ * ```html
331
+ * <$text htmlA="{ attributes: { id: 'myId' } }">bar</$text>
332
+ * ```
333
+ *
334
+ * The `htmlA` attribute would stay in the model and would cause GHS to generate an `<a>` element.
335
+ * This is incorrect from UX point of view, as the user wanted to remove the whole link (not only `href`).
336
+ */
337
+ _registerCoupledAttributesPostFixer() {
338
+ const model = this.editor.model;
339
+ const selection = model.document.selection;
340
+ model.document.registerPostFixer(writer => {
341
+ const changes = model.document.differ.getChanges();
342
+ let changed = false;
343
+ const coupledAttributes = this._getCoupledAttributesMap();
344
+ for (const change of changes) {
345
+ // Handle only attribute removals.
346
+ if (change.type != 'attribute' || change.attributeNewValue !== null) {
347
+ continue;
348
+ }
349
+ // Find a list of coupled GHS attributes.
350
+ const attributeKeys = coupledAttributes.get(change.attributeKey);
351
+ if (!attributeKeys) {
352
+ continue;
353
+ }
354
+ // Remove the coupled GHS attributes on the same range as the feature attribute was removed.
355
+ for (const { item } of change.range.getWalker()) {
356
+ for (const attributeKey of attributeKeys) {
357
+ if (item.hasAttribute(attributeKey)) {
358
+ writer.removeAttribute(attributeKey, item);
359
+ changed = true;
360
+ }
361
+ }
362
+ }
363
+ }
364
+ return changed;
365
+ });
366
+ this.listenTo(selection, 'change:attribute', (evt, { attributeKeys }) => {
367
+ const removeAttributes = new Set();
368
+ const coupledAttributes = this._getCoupledAttributesMap();
369
+ for (const attributeKey of attributeKeys) {
370
+ // Handle only attribute removals.
371
+ if (selection.hasAttribute(attributeKey)) {
372
+ continue;
373
+ }
374
+ // Find a list of coupled GHS attributes.
375
+ const coupledAttributeKeys = coupledAttributes.get(attributeKey);
376
+ if (!coupledAttributeKeys) {
377
+ continue;
378
+ }
379
+ for (const coupledAttributeKey of coupledAttributeKeys) {
380
+ if (selection.hasAttribute(coupledAttributeKey)) {
381
+ removeAttributes.add(coupledAttributeKey);
382
+ }
383
+ }
384
+ }
385
+ if (removeAttributes.size == 0) {
386
+ return;
387
+ }
388
+ model.change(writer => {
389
+ for (const attributeKey of removeAttributes) {
390
+ writer.removeSelectionAttribute(attributeKey);
391
+ }
392
+ });
393
+ });
394
+ }
395
+ /**
396
+ * Removes `html*Attributes` attributes from incompatible elements.
397
+ *
398
+ * For example, consider the following HTML:
399
+ *
400
+ * ```html
401
+ * <heading2 htmlH2Attributes="...">foobar[]</heading2>
402
+ * ```
403
+ *
404
+ * Pressing `enter` creates a new `paragraph` element that inherits
405
+ * the `htmlH2Attributes` attribute from `heading2`.
406
+ *
407
+ * ```html
408
+ * <heading2 htmlH2Attributes="...">foobar</heading2>
409
+ * <paragraph htmlH2Attributes="...">[]</paragraph>
410
+ * ```
411
+ *
412
+ * This postfixer ensures that this doesn't happen, and that elements can
413
+ * only have `html*Attributes` associated with them,
414
+ * e.g.: `htmlPAttributes` for `<p>`, `htmlDivAttributes` for `<div>`, etc.
415
+ *
416
+ * With it enabled, pressing `enter` at the end of `<heading2>` will create
417
+ * a new paragraph without the `htmlH2Attributes` attribute.
418
+ *
419
+ * ```html
420
+ * <heading2 htmlH2Attributes="...">foobar</heading2>
421
+ * <paragraph>[]</paragraph>
422
+ * ```
423
+ */
424
+ _registerAssociatedHtmlAttributesPostFixer() {
425
+ const model = this.editor.model;
426
+ model.document.registerPostFixer(writer => {
427
+ const changes = model.document.differ.getChanges();
428
+ let changed = false;
429
+ for (const change of changes) {
430
+ if (change.type !== 'insert' || change.name === '$text') {
431
+ continue;
432
+ }
433
+ for (const attr of change.attributes.keys()) {
434
+ if (!attr.startsWith('html') || !attr.endsWith('Attributes')) {
435
+ continue;
436
+ }
437
+ if (!model.schema.checkAttribute(change.name, attr)) {
438
+ writer.removeAttribute(attr, change.position.nodeAfter);
439
+ changed = true;
440
+ }
441
+ }
442
+ }
443
+ return changed;
444
+ });
445
+ }
446
+ /**
447
+ * Collects the map of coupled attributes. The returned map is keyed by the feature attribute name
448
+ * and coupled GHS attribute names are stored in the value array.
449
+ */
450
+ _getCoupledAttributesMap() {
451
+ if (this._coupledAttributes) {
452
+ return this._coupledAttributes;
453
+ }
454
+ this._coupledAttributes = new Map();
455
+ for (const definition of this._allowedElements) {
456
+ if (definition.coupledAttribute && definition.model) {
457
+ const attributeNames = this._coupledAttributes.get(definition.coupledAttribute);
458
+ if (attributeNames) {
459
+ attributeNames.push(definition.model);
460
+ }
461
+ else {
462
+ this._coupledAttributes.set(definition.coupledAttribute, [definition.model]);
463
+ }
464
+ }
465
+ }
466
+ return this._coupledAttributes;
467
+ }
468
+ /**
469
+ * Fires `register` event for the given element definition.
470
+ */
471
+ _fireRegisterEvent(definition) {
472
+ if (definition.view && this._disallowedElements.has(definition.view)) {
473
+ return;
474
+ }
475
+ this.fire(definition.view ? `register:${definition.view}` : 'register', definition);
476
+ }
477
+ /**
478
+ * Registers object element and attribute converters for the given data schema definition.
479
+ */
480
+ _registerObjectElement(definition) {
481
+ const editor = this.editor;
482
+ const schema = editor.model.schema;
483
+ const conversion = editor.conversion;
484
+ const { view: viewName, model: modelName } = definition;
485
+ schema.register(modelName, definition.modelSchema);
486
+ /* istanbul ignore next: paranoid check -- @preserve */
487
+ if (!viewName) {
488
+ return;
489
+ }
490
+ schema.extend(definition.model, {
491
+ allowAttributes: [getHtmlAttributeName(viewName), 'htmlContent']
492
+ });
493
+ // Store element content in special `$rawContent` custom property to
494
+ // avoid editor's data filtering mechanism.
495
+ editor.data.registerRawContentMatcher({
496
+ name: viewName
497
+ });
498
+ conversion.for('upcast').elementToElement({
499
+ view: viewName,
500
+ model: viewToModelObjectConverter(definition),
501
+ // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
502
+ // this listener is called before it. If not, some elements will be transformed into a paragraph.
503
+ // `+ 2` is used to take priority over `_addDefaultH1Conversion` in the Heading plugin.
504
+ converterPriority: priorities.low + 2
505
+ });
506
+ conversion.for('upcast').add(viewToModelBlockAttributeConverter(definition, this));
507
+ conversion.for('editingDowncast').elementToStructure({
508
+ model: {
509
+ name: modelName,
510
+ attributes: [getHtmlAttributeName(viewName)]
511
+ },
512
+ view: toObjectWidgetConverter(editor, definition)
513
+ });
514
+ conversion.for('dataDowncast').elementToElement({
515
+ model: modelName,
516
+ view: (modelElement, { writer }) => {
517
+ return createObjectView(viewName, modelElement, writer);
518
+ }
519
+ });
520
+ conversion.for('dataDowncast').add(modelToViewBlockAttributeConverter(definition));
521
+ }
522
+ /**
523
+ * Registers block element and attribute converters for the given data schema definition.
524
+ */
525
+ _registerBlockElement(definition) {
526
+ const editor = this.editor;
527
+ const schema = editor.model.schema;
528
+ const conversion = editor.conversion;
529
+ const { view: viewName, model: modelName } = definition;
530
+ if (!schema.isRegistered(definition.model)) {
531
+ schema.register(definition.model, definition.modelSchema);
532
+ if (!viewName) {
533
+ return;
534
+ }
535
+ conversion.for('upcast').elementToElement({
536
+ model: modelName,
537
+ view: viewName,
538
+ // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
539
+ // this listener is called before it. If not, some elements will be transformed into a paragraph.
540
+ // `+ 2` is used to take priority over `_addDefaultH1Conversion` in the Heading plugin.
541
+ converterPriority: priorities.low + 2
542
+ });
543
+ conversion.for('downcast').elementToElement({
544
+ model: modelName,
545
+ view: viewName
546
+ });
547
+ }
548
+ if (!viewName) {
549
+ return;
550
+ }
551
+ schema.extend(definition.model, {
552
+ allowAttributes: getHtmlAttributeName(viewName)
553
+ });
554
+ conversion.for('upcast').add(viewToModelBlockAttributeConverter(definition, this));
555
+ conversion.for('downcast').add(modelToViewBlockAttributeConverter(definition));
556
+ }
557
+ /**
558
+ * Registers inline element and attribute converters for the given data schema definition.
559
+ *
560
+ * Extends `$text` model schema to allow the given definition model attribute and its properties.
561
+ */
562
+ _registerInlineElement(definition) {
563
+ const editor = this.editor;
564
+ const schema = editor.model.schema;
565
+ const conversion = editor.conversion;
566
+ const attributeKey = definition.model;
567
+ // This element is stored in the model as an attribute on a block element, for example DocumentLists.
568
+ if (definition.appliesToBlock) {
569
+ return;
570
+ }
571
+ schema.extend('$text', {
572
+ allowAttributes: attributeKey
573
+ });
574
+ if (definition.attributeProperties) {
575
+ schema.setAttributeProperties(attributeKey, definition.attributeProperties);
576
+ }
577
+ conversion.for('upcast').add(viewToAttributeInlineConverter(definition, this));
578
+ conversion.for('downcast').attributeToElement({
579
+ model: attributeKey,
580
+ view: attributeToViewInlineConverter(definition)
581
+ });
582
+ if (!definition.allowEmpty) {
583
+ return;
584
+ }
585
+ schema.setAttributeProperties(attributeKey, { copyFromObject: false });
586
+ if (!schema.isRegistered('htmlEmptyElement')) {
587
+ schema.register('htmlEmptyElement', {
588
+ inheritAllFrom: '$inlineObject'
589
+ });
590
+ }
591
+ editor.data.htmlProcessor.domConverter.registerInlineObjectMatcher(element => {
592
+ // Element must be empty and have any attribute.
593
+ if (element.name == definition.view &&
594
+ element.isEmpty &&
595
+ Array.from(element.getAttributeKeys()).length) {
596
+ return {
597
+ name: true
598
+ };
599
+ }
600
+ return null;
601
+ });
602
+ conversion.for('editingDowncast')
603
+ .elementToElement({
604
+ model: 'htmlEmptyElement',
605
+ view: emptyInlineModelElementToViewConverter(definition, true)
606
+ });
607
+ conversion.for('dataDowncast')
608
+ .elementToElement({
609
+ model: 'htmlEmptyElement',
610
+ view: emptyInlineModelElementToViewConverter(definition)
611
+ });
612
+ }
613
+ }
614
+ /**
615
+ * Matches and consumes matched attributes.
616
+ *
617
+ * @returns Object with following properties:
618
+ * - attributes Array with matched attribute names.
619
+ * - classes Array with matched class names.
620
+ * - styles Array with matched style names.
621
+ */
622
+ function matchAndConsumeAttributes(viewElement, matcher, consumable) {
623
+ const matches = matcher.matchAll(viewElement) || [];
624
+ const stylesProcessor = viewElement.document.stylesProcessor;
625
+ return matches.reduce((result, { match }) => {
626
+ // Verify and consume styles.
627
+ for (const style of match.styles || []) {
628
+ // Check longer forms of the same style as those could be matched
629
+ // but not present in the element directly.
630
+ // Consider only longhand (or longer than current notation) so that
631
+ // we do not include all sides of the box if only one side is allowed.
632
+ const sortedRelatedStyles = stylesProcessor.getRelatedStyles(style)
633
+ .filter(relatedStyle => relatedStyle.split('-').length > style.split('-').length)
634
+ .sort((a, b) => b.split('-').length - a.split('-').length);
635
+ for (const relatedStyle of sortedRelatedStyles) {
636
+ if (consumable.consume(viewElement, { styles: [relatedStyle] })) {
637
+ result.styles.push(relatedStyle);
638
+ }
639
+ }
640
+ // Verify and consume style as specified in the matcher.
641
+ if (consumable.consume(viewElement, { styles: [style] })) {
642
+ result.styles.push(style);
643
+ }
644
+ }
645
+ // Verify and consume class names.
646
+ for (const className of match.classes || []) {
647
+ if (consumable.consume(viewElement, { classes: [className] })) {
648
+ result.classes.push(className);
649
+ }
650
+ }
651
+ // Verify and consume other attributes.
652
+ for (const attributeName of match.attributes || []) {
653
+ if (consumable.consume(viewElement, { attributes: [attributeName] })) {
654
+ result.attributes.push(attributeName);
655
+ }
656
+ }
657
+ return result;
658
+ }, {
659
+ attributes: [],
660
+ classes: [],
661
+ styles: []
662
+ });
663
+ }
664
+ /**
665
+ * Prepares the GHS attribute value as an object with element attributes' values.
666
+ */
667
+ function prepareGHSAttribute(viewElement, { attributes, classes, styles }) {
668
+ if (!attributes.length && !classes.length && !styles.length) {
669
+ return null;
670
+ }
671
+ return {
672
+ ...(attributes.length && {
673
+ attributes: getAttributes(viewElement, attributes)
674
+ }),
675
+ ...(styles.length && {
676
+ styles: getReducedStyles(viewElement, styles)
677
+ }),
678
+ ...(classes.length && {
679
+ classes
680
+ })
681
+ };
682
+ }
683
+ /**
684
+ * Returns attributes as an object with names and values.
685
+ */
686
+ function getAttributes(viewElement, attributes) {
687
+ const attributesObject = {};
688
+ for (const key of attributes) {
689
+ const value = viewElement.getAttribute(key);
690
+ if (value !== undefined && isValidAttributeName(key)) {
691
+ attributesObject[key] = value;
692
+ }
693
+ }
694
+ return attributesObject;
695
+ }
696
+ /**
697
+ * Returns styles as an object reduced to shorthand notation without redundant entries.
698
+ */
699
+ function getReducedStyles(viewElement, styles) {
700
+ // Use StyleMap to reduce style value to the minimal form (without shorthand and long-hand notation and duplication).
701
+ const stylesMap = new StylesMap(viewElement.document.stylesProcessor);
702
+ for (const key of styles) {
703
+ const styleValue = viewElement.getStyle(key);
704
+ if (styleValue !== undefined) {
705
+ stylesMap.set(key, styleValue);
706
+ }
707
+ }
708
+ return Object.fromEntries(stylesMap.getStylesEntries());
709
+ }
710
+ /**
711
+ * Matcher by default has to match **all** patterns to count it as an actual match. Splitting the pattern
712
+ * into separate patterns means that any matched pattern will be count as a match.
713
+ *
714
+ * @param pattern Pattern to split.
715
+ * @param attributeName Name of the attribute to split (e.g. 'attributes', 'classes', 'styles').
716
+ */
717
+ function splitPattern(pattern, attributeName) {
718
+ const { name } = pattern;
719
+ const attributeValue = pattern[attributeName];
720
+ if (isPlainObject(attributeValue)) {
721
+ return Object.entries(attributeValue)
722
+ .map(([key, value]) => ({
723
+ name,
724
+ [attributeName]: {
725
+ [key]: value
726
+ }
727
+ }));
728
+ }
729
+ if (Array.isArray(attributeValue)) {
730
+ return attributeValue
731
+ .map(value => ({
732
+ name,
733
+ [attributeName]: [value]
734
+ }));
735
+ }
736
+ return [pattern];
737
+ }
738
+ /**
739
+ * Rules are matched in conjunction (AND operation), but we want to have a match if *any* of the rules is matched (OR operation).
740
+ * By splitting the rules we force the latter effect.
741
+ */
742
+ function splitRules(rules) {
743
+ const { name, attributes, classes, styles } = rules;
744
+ const splitRules = [];
745
+ if (attributes) {
746
+ splitRules.push(...splitPattern({ name, attributes }, 'attributes'));
747
+ }
748
+ if (classes) {
749
+ splitRules.push(...splitPattern({ name, classes }, 'classes'));
750
+ }
751
+ if (styles) {
752
+ splitRules.push(...splitPattern({ name, styles }, 'styles'));
753
+ }
754
+ return splitRules;
755
+ }