This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathhtmldataprocessor.js
100 lines (87 loc) · 2.8 KB
/
htmldataprocessor.js
1
2
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
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module engine/dataprocessor/htmldataprocessor
*/
/* globals document, DOMParser */
import BasicHtmlWriter from './basichtmlwriter';
import DomConverter from '../view/domconverter';
import { NBSP_FILLER } from '../view/filler';
/**
* The HTML data processor class.
* This data processor implementation uses HTML as input and output data.
*
* @implements module:engine/dataprocessor/dataprocessor~DataProcessor
*/
export default class HtmlDataProcessor {
/**
* Creates a new instance of the HTML data processor class.
*/
constructor() {
/**
* A DOM parser instance used to parse an HTML string to an HTML document.
*
* @private
* @member {DOMParser}
*/
this._domParser = new DOMParser();
/**
* A DOM converter used to convert DOM elements to view elements.
*
* @private
* @member {module:engine/view/domconverter~DomConverter}
*/
this._domConverter = new DomConverter( { blockFiller: NBSP_FILLER } );
/**
* A basic HTML writer instance used to convert DOM elements to an HTML string.
*
* @private
* @member {module:engine/dataprocessor/basichtmlwriter~BasicHtmlWriter}
*/
this._htmlWriter = new BasicHtmlWriter();
}
/**
* Converts a provided {@link module:engine/view/documentfragment~DocumentFragment document fragment}
* to data format — in this case to an HTML string.
*
* @param {module:engine/view/documentfragment~DocumentFragment} viewFragment
* @returns {String} HTML string.
*/
toData( viewFragment ) {
// Convert view DocumentFragment to DOM DocumentFragment.
const domFragment = this._domConverter.viewToDom( viewFragment, document );
// Convert DOM DocumentFragment to HTML output.
return this._htmlWriter.getHtml( domFragment );
}
/**
* Converts the provided HTML string to a view tree.
*
* @param {String} data An HTML string.
* @returns {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment|null} A converted view element.
*/
toView( data ) {
// Convert input HTML data to DOM DocumentFragment.
const domFragment = this._toDom( data );
// Convert DOM DocumentFragment to view DocumentFragment.
return this._domConverter.domToView( domFragment );
}
/**
* Converts an HTML string to its DOM representation. Returns a document fragment containing nodes parsed from
* the provided data.
*
* @private
* @param {String} data
* @returns {DocumentFragment}
*/
_toDom( data ) {
const document = this._domParser.parseFromString( data, 'text/html' );
const fragment = document.createDocumentFragment();
const nodes = document.body.childNodes;
while ( nodes.length > 0 ) {
fragment.appendChild( nodes[ 0 ] );
}
return fragment;
}
}