-
Notifications
You must be signed in to change notification settings - Fork 342
/
clean-html.ts
141 lines (119 loc) · 3.29 KB
/
clean-html.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
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
/*!
* Jodit Editor (https://xdsoft.net/jodit/)
* Released under MIT see LICENSE.txt in the project root for license information.
* Copyright (c) 2013-2024 Valeriy Chupurnov. All rights reserved. https://xdsoft.net
*/
/**
* [[include:plugins/clean-html/README.md]]
* @packageDocumentation
* @module plugins/clean-html
*/
import type { IJodit, Nullable } from 'jodit/types';
import { hook, watch } from 'jodit/core/decorators';
import { Dom } from 'jodit/core/dom/dom';
import { LazyWalker } from 'jodit/core/dom/lazy-walker';
import { pluginSystem } from 'jodit/core/global';
import { safeHTML } from 'jodit/core/helpers/html/safe-html';
import { Plugin } from 'jodit/core/plugin/plugin';
import './config';
import {
getHash,
removeFormatForCollapsedSelection,
removeFormatForSelection,
visitNodeWalker
} from './helpers';
/**
* Clean HTML after removeFormat and insertHorizontalRule command
*/
export class cleanHtml extends Plugin {
/** @override */
override buttons: Plugin['buttons'] = [
{
name: 'eraser',
group: 'font-style'
}
];
/** @override */
protected override afterInit(jodit: IJodit): void {}
private get isEditMode(): boolean {
return !(
this.j.isInDestruct ||
!this.j.isEditorMode() ||
this.j.getReadOnly()
);
}
/**
* Clean HTML code on every change
*/
@watch([':change', ':afterSetMode', ':afterInit', ':mousedown', ':keydown'])
protected onChangeCleanHTML(): void {
if (!this.isEditMode) {
return;
}
const editor = this.j;
this.walker.setWork(editor.editor);
this.currentSelectionNode = editor.s.current();
}
private currentSelectionNode: Nullable<Node> = null;
private walker: LazyWalker = new LazyWalker(this.j.async, {
timeout: this.j.o.cleanHTML.timeout
});
@hook('ready')
protected startWalker(): void {
const { jodit } = this;
const allow = getHash(this.j.o.cleanHTML.allowTags);
const deny = getHash(this.j.o.cleanHTML.denyTags);
this.walker
.on('visit', (node: Node) =>
visitNodeWalker(
jodit,
node,
allow,
deny,
this.currentSelectionNode
)
)
.on('end', (affected: boolean): void => {
this.j.e.fire(
affected
? 'internalChange finishedCleanHTMLWorker'
: 'finishedCleanHTMLWorker'
);
});
}
@watch(':beforeCommand')
protected beforeCommand(command: string): void | false {
if (command.toLowerCase() === 'removeformat') {
if (this.j.s.isCollapsed()) {
removeFormatForCollapsedSelection(this.j);
} else {
removeFormatForSelection(this.j);
}
return false;
}
}
/**
* Event handler when manually assigning a value to the HTML editor.
*/
@watch(':beforeSetNativeEditorValue')
protected onBeforeSetNativeEditorValue(data: { value: string }): boolean {
const [sandBox, iframe] = this.j.o.cleanHTML.useIframeSandbox
? this.j.createInside.sandbox()
: [this.j.createInside.div()];
sandBox.innerHTML = data.value;
this.onSafeHTML(sandBox);
data.value = sandBox.innerHTML;
safeHTML(sandBox, { safeJavaScriptLink: true, removeOnError: true });
Dom.safeRemove(iframe);
return false;
}
@watch(':safeHTML')
protected onSafeHTML(sandBox: HTMLElement): void {
safeHTML(sandBox, this.j.o.cleanHTML);
}
/** @override */
protected override beforeDestruct(): void {
this.walker.destruct();
}
}
pluginSystem.add('cleanHtml', cleanHtml);