-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
htmlpagedataprocessor.ts
91 lines (70 loc) · 2.49 KB
/
htmlpagedataprocessor.ts
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
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module html-support/htmlpagedataprocessor
*/
import { HtmlDataProcessor, UpcastWriter, type ViewDocumentFragment } from 'ckeditor5/src/engine.js';
/**
* The full page HTML data processor class.
* This data processor implementation uses HTML as input and output data.
*/
export default class HtmlPageDataProcessor extends HtmlDataProcessor {
/**
* @inheritDoc
*/
public override toView( data: string ): ViewDocumentFragment {
// Ignore content that is not a full page source.
if ( !data.match( /<(?:html|body|head|meta)(?:\s[^>]*)?>/i ) ) {
return super.toView( data );
}
// Store doctype and xml declaration in a separate properties as they can't be stringified later.
let docType = '';
let xmlDeclaration = '';
data = data.replace( /<!DOCTYPE[^>]*>/i, match => {
docType = match;
return '';
} );
data = data.replace( /<\?xml\s[^?]*\?>/i, match => {
xmlDeclaration = match;
return '';
} );
// Convert input HTML data to DOM DocumentFragment.
const domFragment = this._toDom( data );
// Convert DOM DocumentFragment to view DocumentFragment.
const viewFragment = this.domConverter.domToView(
domFragment,
{ skipComments: this.skipComments }
) as ViewDocumentFragment;
const writer = new UpcastWriter( viewFragment.document );
// Using the DOM document with body content extracted as a skeleton of the page.
writer.setCustomProperty( '$fullPageDocument', domFragment.ownerDocument.documentElement.outerHTML, viewFragment );
if ( docType ) {
writer.setCustomProperty( '$fullPageDocType', docType, viewFragment );
}
if ( xmlDeclaration ) {
writer.setCustomProperty( '$fullPageXmlDeclaration', xmlDeclaration, viewFragment );
}
return viewFragment;
}
/**
* @inheritDoc
*/
public override toData( viewFragment: ViewDocumentFragment ): string {
let data = super.toData( viewFragment );
const page = viewFragment.getCustomProperty( '$fullPageDocument' ) as string | undefined;
const docType = viewFragment.getCustomProperty( '$fullPageDocType' );
const xmlDeclaration = viewFragment.getCustomProperty( '$fullPageXmlDeclaration' );
if ( page ) {
data = page.replace( /<\/body\s*>/, data + '$&' );
if ( docType ) {
data = docType + '\n' + data;
}
if ( xmlDeclaration ) {
data = xmlDeclaration + '\n' + data;
}
}
return data;
}
}