@ckeditor/ckeditor5-engine 30.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 (117) hide show
  1. package/LICENSE.md +17 -0
  2. package/README.md +30 -0
  3. package/package.json +70 -0
  4. package/src/controller/datacontroller.js +563 -0
  5. package/src/controller/editingcontroller.js +149 -0
  6. package/src/conversion/conversion.js +644 -0
  7. package/src/conversion/conversionhelpers.js +40 -0
  8. package/src/conversion/downcastdispatcher.js +914 -0
  9. package/src/conversion/downcasthelpers.js +1706 -0
  10. package/src/conversion/mapper.js +696 -0
  11. package/src/conversion/modelconsumable.js +329 -0
  12. package/src/conversion/upcastdispatcher.js +807 -0
  13. package/src/conversion/upcasthelpers.js +997 -0
  14. package/src/conversion/viewconsumable.js +623 -0
  15. package/src/dataprocessor/basichtmlwriter.js +32 -0
  16. package/src/dataprocessor/dataprocessor.jsdoc +64 -0
  17. package/src/dataprocessor/htmldataprocessor.js +159 -0
  18. package/src/dataprocessor/htmlwriter.js +22 -0
  19. package/src/dataprocessor/xmldataprocessor.js +161 -0
  20. package/src/dev-utils/model.js +482 -0
  21. package/src/dev-utils/operationreplayer.js +140 -0
  22. package/src/dev-utils/utils.js +103 -0
  23. package/src/dev-utils/view.js +1091 -0
  24. package/src/index.js +52 -0
  25. package/src/model/batch.js +82 -0
  26. package/src/model/differ.js +1282 -0
  27. package/src/model/document.js +483 -0
  28. package/src/model/documentfragment.js +390 -0
  29. package/src/model/documentselection.js +1261 -0
  30. package/src/model/element.js +438 -0
  31. package/src/model/history.js +138 -0
  32. package/src/model/item.jsdoc +14 -0
  33. package/src/model/liveposition.js +182 -0
  34. package/src/model/liverange.js +221 -0
  35. package/src/model/markercollection.js +553 -0
  36. package/src/model/model.js +934 -0
  37. package/src/model/node.js +507 -0
  38. package/src/model/nodelist.js +217 -0
  39. package/src/model/operation/attributeoperation.js +202 -0
  40. package/src/model/operation/detachoperation.js +103 -0
  41. package/src/model/operation/insertoperation.js +188 -0
  42. package/src/model/operation/markeroperation.js +154 -0
  43. package/src/model/operation/mergeoperation.js +216 -0
  44. package/src/model/operation/moveoperation.js +209 -0
  45. package/src/model/operation/nooperation.js +58 -0
  46. package/src/model/operation/operation.js +139 -0
  47. package/src/model/operation/operationfactory.js +49 -0
  48. package/src/model/operation/renameoperation.js +155 -0
  49. package/src/model/operation/rootattributeoperation.js +211 -0
  50. package/src/model/operation/splitoperation.js +254 -0
  51. package/src/model/operation/transform.js +2389 -0
  52. package/src/model/operation/utils.js +292 -0
  53. package/src/model/position.js +1164 -0
  54. package/src/model/range.js +1049 -0
  55. package/src/model/rootelement.js +111 -0
  56. package/src/model/schema.js +1851 -0
  57. package/src/model/selection.js +902 -0
  58. package/src/model/text.js +138 -0
  59. package/src/model/textproxy.js +279 -0
  60. package/src/model/treewalker.js +414 -0
  61. package/src/model/utils/autoparagraphing.js +77 -0
  62. package/src/model/utils/deletecontent.js +528 -0
  63. package/src/model/utils/getselectedcontent.js +150 -0
  64. package/src/model/utils/insertcontent.js +824 -0
  65. package/src/model/utils/modifyselection.js +229 -0
  66. package/src/model/utils/selection-post-fixer.js +297 -0
  67. package/src/model/writer.js +1574 -0
  68. package/src/view/attributeelement.js +274 -0
  69. package/src/view/containerelement.js +123 -0
  70. package/src/view/document.js +221 -0
  71. package/src/view/documentfragment.js +273 -0
  72. package/src/view/documentselection.js +387 -0
  73. package/src/view/domconverter.js +1437 -0
  74. package/src/view/downcastwriter.js +2121 -0
  75. package/src/view/editableelement.js +118 -0
  76. package/src/view/element.js +945 -0
  77. package/src/view/elementdefinition.jsdoc +59 -0
  78. package/src/view/emptyelement.js +119 -0
  79. package/src/view/filler.js +161 -0
  80. package/src/view/item.jsdoc +14 -0
  81. package/src/view/matcher.js +776 -0
  82. package/src/view/node.js +391 -0
  83. package/src/view/observer/arrowkeysobserver.js +58 -0
  84. package/src/view/observer/bubblingemittermixin.js +307 -0
  85. package/src/view/observer/bubblingeventinfo.js +71 -0
  86. package/src/view/observer/clickobserver.js +46 -0
  87. package/src/view/observer/compositionobserver.js +79 -0
  88. package/src/view/observer/domeventdata.js +82 -0
  89. package/src/view/observer/domeventobserver.js +99 -0
  90. package/src/view/observer/fakeselectionobserver.js +118 -0
  91. package/src/view/observer/focusobserver.js +106 -0
  92. package/src/view/observer/inputobserver.js +44 -0
  93. package/src/view/observer/keyobserver.js +83 -0
  94. package/src/view/observer/mouseobserver.js +56 -0
  95. package/src/view/observer/mutationobserver.js +345 -0
  96. package/src/view/observer/observer.js +118 -0
  97. package/src/view/observer/selectionobserver.js +242 -0
  98. package/src/view/placeholder.js +285 -0
  99. package/src/view/position.js +426 -0
  100. package/src/view/range.js +533 -0
  101. package/src/view/rawelement.js +148 -0
  102. package/src/view/renderer.js +1037 -0
  103. package/src/view/rooteditableelement.js +107 -0
  104. package/src/view/selection.js +718 -0
  105. package/src/view/styles/background.js +73 -0
  106. package/src/view/styles/border.js +362 -0
  107. package/src/view/styles/margin.js +41 -0
  108. package/src/view/styles/padding.js +40 -0
  109. package/src/view/styles/utils.js +277 -0
  110. package/src/view/stylesmap.js +938 -0
  111. package/src/view/text.js +147 -0
  112. package/src/view/textproxy.js +199 -0
  113. package/src/view/treewalker.js +496 -0
  114. package/src/view/uielement.js +238 -0
  115. package/src/view/upcastwriter.js +484 -0
  116. package/src/view/view.js +721 -0
  117. package/theme/placeholder.css +27 -0
@@ -0,0 +1,64 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module engine/dataprocessor/dataprocessor
8
+ */
9
+
10
+ /**
11
+ * The data processor interface. It should be implemented by actual data processors.
12
+ *
13
+ * Each data processor implements a certain format of the data. For example, {@glink features/markdown Markdown data processor}
14
+ * will convert the data (a Markdown string) to a {@link module:engine/view/documentfragment~DocumentFragment document fragment} and back.
15
+ *
16
+ * **Note:** While the CKEditor 5 architecture supports changing the data format, in most scenarios we do recommend sticking to
17
+ * the default format which is HTML (supported by the {@link module:engine/dataprocessor/htmldataprocessor~HtmlDataProcessor}).
18
+ * HTML remains [the best standard for rich-text data](https://medium.com/content-uneditable/a-standard-for-rich-text-data-4b3a507af552).
19
+ *
20
+ * And please do remember – using Markdown [does not automatically make your
21
+ * application/website secure](https://github.com/ckeditor/ckeditor5-markdown-gfm/issues/16#issuecomment-375752994).
22
+ *
23
+ * @interface DataProcessor
24
+ */
25
+
26
+ /**
27
+ * Converts a {@link module:engine/view/documentfragment~DocumentFragment document fragment} to data.
28
+ *
29
+ * @method #toData
30
+ * @param {module:engine/view/documentfragment~DocumentFragment} fragment The document fragment to be processed.
31
+ * @returns {*}
32
+ */
33
+
34
+ /**
35
+ * Converts the data to a {@link module:engine/view/documentfragment~DocumentFragment document fragment}.
36
+ *
37
+ * @method #toView
38
+ * @param {*} data The data to be processed.
39
+ * @returns {module:engine/view/documentfragment~DocumentFragment}
40
+ */
41
+
42
+ /**
43
+ * Registers a {@link module:engine/view/matcher~MatcherPattern} for view elements whose content should be treated as raw data
44
+ * and its content should be converted to a
45
+ * {@link module:engine/view/element~Element#getCustomProperty custom property of a view element} called `"$rawContent"` while
46
+ * converting {@link #toView to view}.
47
+ *
48
+ * @method #registerRawContentMatcher
49
+ * @param {module:engine/view/matcher~MatcherPattern} pattern Pattern matching all view elements whose content should
50
+ * be treated as plain text.
51
+ */
52
+
53
+ /**
54
+ * If the processor is set to use marked fillers, it will insert `&nbsp;` fillers wrapped in `<span>` elements
55
+ * (`<span data-cke-filler="true">&nbsp;</span>`) instead of regular `&nbsp;` characters.
56
+ *
57
+ * This mode allows for more precise handling of block fillers (so they do not leak into the editor content) but bloats the
58
+ * editor data with additional markup.
59
+ *
60
+ * This mode may be required by some features and will be turned on by them automatically.
61
+ *
62
+ * @method #useFillerType
63
+ * @param {'default'|'marked'} type Whether to use the default or marked `&nbsp;` block fillers.
64
+ */
@@ -0,0 +1,159 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module engine/dataprocessor/htmldataprocessor
8
+ */
9
+
10
+ /* globals document, DOMParser, Node */
11
+
12
+ import BasicHtmlWriter from './basichtmlwriter';
13
+ import DomConverter from '../view/domconverter';
14
+
15
+ /**
16
+ * The HTML data processor class.
17
+ * This data processor implementation uses HTML as input and output data.
18
+ *
19
+ * @implements module:engine/dataprocessor/dataprocessor~DataProcessor
20
+ */
21
+ export default class HtmlDataProcessor {
22
+ /**
23
+ * Creates a new instance of the HTML data processor class.
24
+ *
25
+ * @param {module:engine/view/document~Document} document The view document instance.
26
+ */
27
+ constructor( document ) {
28
+ /**
29
+ * A DOM parser instance used to parse an HTML string to an HTML document.
30
+ *
31
+ * @private
32
+ * @member {DOMParser}
33
+ */
34
+ this._domParser = new DOMParser();
35
+
36
+ /**
37
+ * A DOM converter used to convert DOM elements to view elements.
38
+ *
39
+ * @private
40
+ * @member {module:engine/view/domconverter~DomConverter}
41
+ */
42
+ this._domConverter = new DomConverter( document, { blockFillerMode: 'nbsp' } );
43
+
44
+ /**
45
+ * A basic HTML writer instance used to convert DOM elements to an HTML string.
46
+ *
47
+ * @private
48
+ * @member {module:engine/dataprocessor/basichtmlwriter~BasicHtmlWriter}
49
+ */
50
+ this._htmlWriter = new BasicHtmlWriter();
51
+ }
52
+
53
+ /**
54
+ * Converts a provided {@link module:engine/view/documentfragment~DocumentFragment document fragment}
55
+ * to data format &mdash; in this case to an HTML string.
56
+ *
57
+ * @param {module:engine/view/documentfragment~DocumentFragment} viewFragment
58
+ * @returns {String} HTML string.
59
+ */
60
+ toData( viewFragment ) {
61
+ // Convert view DocumentFragment to DOM DocumentFragment.
62
+ const domFragment = this._domConverter.viewToDom( viewFragment, document );
63
+
64
+ // Convert DOM DocumentFragment to HTML output.
65
+ return this._htmlWriter.getHtml( domFragment );
66
+ }
67
+
68
+ /**
69
+ * Converts the provided HTML string to a view tree.
70
+ *
71
+ * @param {String} data An HTML string.
72
+ * @returns {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment|null} A converted view element.
73
+ */
74
+ toView( data ) {
75
+ // Convert input HTML data to DOM DocumentFragment.
76
+ const domFragment = this._toDom( data );
77
+
78
+ // Convert DOM DocumentFragment to view DocumentFragment.
79
+ return this._domConverter.domToView( domFragment );
80
+ }
81
+
82
+ /**
83
+ * Registers a {@link module:engine/view/matcher~MatcherPattern} for view elements whose content should be treated as raw data
84
+ * and not processed during the conversion from the DOM to the view elements.
85
+ *
86
+ * The raw data can be later accessed by a
87
+ * {@link module:engine/view/element~Element#getCustomProperty custom property of a view element} called `"$rawContent"`.
88
+ *
89
+ * @param {module:engine/view/matcher~MatcherPattern} pattern Pattern matching all view elements whose content should
90
+ * be treated as raw data.
91
+ */
92
+ registerRawContentMatcher( pattern ) {
93
+ this._domConverter.registerRawContentMatcher( pattern );
94
+ }
95
+
96
+ /**
97
+ * If the processor is set to use marked fillers, it will insert `&nbsp;` fillers wrapped in `<span>` elements
98
+ * (`<span data-cke-filler="true">&nbsp;</span>`) instead of regular `&nbsp;` characters.
99
+ *
100
+ * This mode allows for a more precise handling of the block fillers (so they do not leak into the editor content) but
101
+ * bloats the editor data with additional markup.
102
+ *
103
+ * This mode may be required by some features and will be turned on by them automatically.
104
+ *
105
+ * @param {'default'|'marked'} type Whether to use the default or the marked `&nbsp;` block fillers.
106
+ */
107
+ useFillerType( type ) {
108
+ this._domConverter.blockFillerMode = type == 'marked' ? 'markedNbsp' : 'nbsp';
109
+ }
110
+
111
+ /**
112
+ * Converts an HTML string to its DOM representation. Returns a document fragment containing nodes parsed from
113
+ * the provided data.
114
+ *
115
+ * @private
116
+ * @param {String} data
117
+ * @returns {DocumentFragment}
118
+ */
119
+ _toDom( data ) {
120
+ const document = this._domParser.parseFromString( data, 'text/html' );
121
+ const fragment = document.createDocumentFragment();
122
+
123
+ // The rules for parsing an HTML string can be read on https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhtml.
124
+ //
125
+ // In short, parsing tokens in an HTML string starts with the so-called "initial" insertion mode. When a DOM parser is in this
126
+ // state and encounters a comment node, it inserts this comment node as the last child of the newly-created `HTMLDocument` object.
127
+ // The parser then proceeds to successive insertion modes during parsing subsequent tokens and appends in the `HTMLDocument` object
128
+ // other nodes (like <html>, <head>, <body>). This causes that the first leading comments from HTML string become the first nodes
129
+ // in the `HTMLDocument` object, but not in the <body> collection, because they are ultimately located before the <html> element.
130
+ //
131
+ // Therefore, so that such leading comments do not disappear, they all are moved from the `HTMLDocument` object to the document
132
+ // fragment, until the <html> element is encountered.
133
+ //
134
+ // See: https://github.com/ckeditor/ckeditor5/issues/9861.
135
+ let documentChildNode = document.firstChild;
136
+
137
+ while ( !documentChildNode.isSameNode( document.documentElement ) ) {
138
+ const node = documentChildNode;
139
+
140
+ documentChildNode = documentChildNode.nextSibling;
141
+
142
+ // It seems that `DOMParser#parseFromString()` adds only comment nodes directly to the `HTMLDocument` object, before the <html>
143
+ // node. The condition below is just to be sure we are moving only comment nodes.
144
+
145
+ /* istanbul ignore else */
146
+ if ( node.nodeType == Node.COMMENT_NODE ) {
147
+ fragment.appendChild( node );
148
+ }
149
+ }
150
+
151
+ const bodyChildNodes = document.body.childNodes;
152
+
153
+ while ( bodyChildNodes.length > 0 ) {
154
+ fragment.appendChild( bodyChildNodes[ 0 ] );
155
+ }
156
+
157
+ return fragment;
158
+ }
159
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module engine/dataprocessor/htmlwriter
8
+ */
9
+
10
+ /**
11
+ * The HTML writer interface.
12
+ *
13
+ * @interface module:engine/dataprocessor/htmlwriter~HtmlWriter
14
+ */
15
+
16
+ /**
17
+ * Returns an HTML string created from a document fragment.
18
+ *
19
+ * @method module:engine/dataprocessor/htmlwriter~HtmlWriter#getHtml
20
+ * @param {DocumentFragment} fragment
21
+ * @returns {String}
22
+ */
@@ -0,0 +1,161 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module engine/dataprocessor/xmldataprocessor
8
+ */
9
+
10
+ /* globals DOMParser, document */
11
+
12
+ import BasicHtmlWriter from './basichtmlwriter';
13
+ import DomConverter from '../view/domconverter';
14
+
15
+ /**
16
+ * The XML data processor class.
17
+ * This data processor implementation uses XML as input and output data.
18
+ * This class is needed because unlike HTML, XML allows to use any tag with any value.
19
+ * For example, `<link>Text</link>` is a valid XML but invalid HTML.
20
+ *
21
+ * @implements module:engine/dataprocessor/dataprocessor~DataProcessor
22
+ */
23
+ export default class XmlDataProcessor {
24
+ /**
25
+ * Creates a new instance of the XML data processor class.
26
+ *
27
+ * @param {module:engine/view/document~Document} document The view document instance.
28
+ * @param {Object} options Configuration options.
29
+ * @param {Array<String>} [options.namespaces=[]] A list of namespaces allowed to use in the XML input.
30
+ */
31
+ constructor( document, options = {} ) {
32
+ /**
33
+ * A list of namespaces allowed to use in the XML input.
34
+ *
35
+ * For example, registering namespaces [ 'attribute', 'container' ] allows to use `<attirbute:tagName></attribute:tagName>`
36
+ * and `<container:tagName></container:tagName>` input. It is mainly for debugging.
37
+ *
38
+ * @public
39
+ * @member {DOMParser}
40
+ */
41
+ this.namespaces = options.namespaces || [];
42
+
43
+ /**
44
+ * DOM parser instance used to parse an XML string to an XML document.
45
+ *
46
+ * @private
47
+ * @member {DOMParser}
48
+ */
49
+ this._domParser = new DOMParser();
50
+
51
+ /**
52
+ * DOM converter used to convert DOM elements to view elements.
53
+ *
54
+ * @private
55
+ * @member {module:engine/view/domconverter~DomConverter}
56
+ */
57
+ this._domConverter = new DomConverter( document, { blockFillerMode: 'nbsp' } );
58
+
59
+ /**
60
+ * A basic HTML writer instance used to convert DOM elements to an XML string.
61
+ * There is no need to use a dedicated XML writer because the basic HTML writer works well in this case.
62
+ *
63
+ * @private
64
+ * @member {module:engine/dataprocessor/basichtmlwriter~BasicHtmlWriter}
65
+ */
66
+ this._htmlWriter = new BasicHtmlWriter();
67
+ }
68
+
69
+ /**
70
+ * Converts the provided {@link module:engine/view/documentfragment~DocumentFragment document fragment}
71
+ * to data format &mdash; in this case an XML string.
72
+ *
73
+ * @param {module:engine/view/documentfragment~DocumentFragment} viewFragment
74
+ * @returns {String} An XML string.
75
+ */
76
+ toData( viewFragment ) {
77
+ // Convert view DocumentFragment to DOM DocumentFragment.
78
+ const domFragment = this._domConverter.viewToDom( viewFragment, document );
79
+
80
+ // Convert DOM DocumentFragment to XML output.
81
+ // There is no need to use dedicated for XML serializing method because BasicHtmlWriter works well in this case.
82
+ return this._htmlWriter.getHtml( domFragment );
83
+ }
84
+
85
+ /**
86
+ * Converts the provided XML string to a view tree.
87
+ *
88
+ * @param {String} data An XML string.
89
+ * @returns {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment|null} A converted view element.
90
+ */
91
+ toView( data ) {
92
+ // Convert input XML data to DOM DocumentFragment.
93
+ const domFragment = this._toDom( data );
94
+
95
+ // Convert DOM DocumentFragment to view DocumentFragment.
96
+ return this._domConverter.domToView( domFragment, { keepOriginalCase: true } );
97
+ }
98
+
99
+ /**
100
+ * Registers a {@link module:engine/view/matcher~MatcherPattern} for view elements whose content should be treated as raw data
101
+ * and not processed during the conversion from XML to view elements.
102
+ *
103
+ * The raw data can be later accessed by a
104
+ * {@link module:engine/view/element~Element#getCustomProperty custom property of a view element} called `"$rawContent"`.
105
+ *
106
+ * @param {module:engine/view/matcher~MatcherPattern} pattern Pattern matching all view elements whose content should
107
+ * be treated as raw data.
108
+ */
109
+ registerRawContentMatcher( pattern ) {
110
+ this._domConverter.registerRawContentMatcher( pattern );
111
+ }
112
+
113
+ /**
114
+ * If the processor is set to use marked fillers, it will insert `&nbsp;` fillers wrapped in `<span>` elements
115
+ * (`<span data-cke-filler="true">&nbsp;</span>`) instead of regular `&nbsp;` characters.
116
+ *
117
+ * This mode allows for a more precise handling of block fillers (so they do not leak into editor content) but
118
+ * bloats the editor data with additional markup.
119
+ *
120
+ * This mode may be required by some features and will be turned on by them automatically.
121
+ *
122
+ * @param {'default'|'marked'} type Whether to use the default or the marked `&nbsp;` block fillers.
123
+ */
124
+ useFillerType( type ) {
125
+ this._domConverter.blockFillerMode = type == 'marked' ? 'markedNbsp' : 'nbsp';
126
+ }
127
+
128
+ /**
129
+ * Converts an XML string to its DOM representation. Returns a document fragment containing nodes parsed from
130
+ * the provided data.
131
+ *
132
+ * @private
133
+ * @param {String} data
134
+ * @returns {DocumentFragment}
135
+ */
136
+ _toDom( data ) {
137
+ // Stringify namespaces.
138
+ const namespaces = this.namespaces.map( nsp => `xmlns:${ nsp }="nsp"` ).join( ' ' );
139
+
140
+ // Wrap data into root element with optional namespace definitions.
141
+ data = `<xml ${ namespaces }>${ data }</xml>`;
142
+
143
+ const parsedDocument = this._domParser.parseFromString( data, 'text/xml' );
144
+
145
+ // Parse validation.
146
+ const parserError = parsedDocument.querySelector( 'parsererror' );
147
+
148
+ if ( parserError ) {
149
+ throw new Error( 'Parse error - ' + parserError.textContent );
150
+ }
151
+
152
+ const fragment = parsedDocument.createDocumentFragment();
153
+ const nodes = parsedDocument.documentElement.childNodes;
154
+
155
+ while ( nodes.length > 0 ) {
156
+ fragment.appendChild( nodes[ 0 ] );
157
+ }
158
+
159
+ return fragment;
160
+ }
161
+ }