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
|
// JavaScript
// Editor for HTML page
"use strict";
class HTMLPageEditor {
constructor (xRootNode=document.rootElement, bCheckSignature=false) {
this.xRootNode = xRootNode;
this.lNode = [];
this.bCheckSignature = bCheckSignature;
this._lParsableNodes = ["P", "LI"];
this._lRootNodes = ["DIV", "UL", "OL"];
}
* _getParsableNodes (xRootNode) {
// recursive function
try {
for (let xNode of this.xRootNode.childNodes) {
if (xNode.className !== "moz-cite-prefix" && xNode.tagName !== "BLOCKQUOTE"
&& (xNode.nodeType == Node.TEXT_NODE || (xNode.nodeType == Node.ELEMENT_NODE && !xNode.textContent.startsWith(">")))
&& xNode.textContent !== "") {
if (xNode.tagName === undefined) {
|
>
>
>
|
>
|
|
|
|
<
|
|
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
|
// JavaScript
// Editor for HTML page
"use strict";
/*
Editor for HTML page (Thunderbird or Iframe)
*/
class HTMLPageEditor {
constructor (xDocument, bCheckSignature=false) {
this.xDocument = xDocument;
this.xRootNode = xDocument.rootElement;
this.lNode = [];
this.bCheckSignature = bCheckSignature;
this._lParsableNodes = ["P", "LI", "H1", "H2", "H3", "H4", "H5"];
this._lRootNodes = ["DIV", "UL", "OL"];
}
* _getParsableNodes () {
// recursive function
try {
for (let xNode of this.xRootNode.childNodes) {
if (xNode.className !== "moz-cite-prefix" && xNode.tagName !== "BLOCKQUOTE"
&& (xNode.nodeType == Node.TEXT_NODE || (xNode.nodeType == Node.ELEMENT_NODE && !xNode.textContent.startsWith(">")))
&& xNode.textContent !== "") {
if (xNode.tagName === undefined) {
|
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
}
}
catch (e) {
showError(e);
}
}
getPageText () {
try {
let sPageText = "";
for (let [i, sLine] of this.getParagraphs()) {
sPageText += sLine + "\n";
}
return sPageText;
}
|
|
|
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
}
}
catch (e) {
showError(e);
}
}
getText () {
try {
let sPageText = "";
for (let [i, sLine] of this.getParagraphs()) {
sPageText += sLine + "\n";
}
return sPageText;
}
|
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
return this.lNode[iPara].textContent;
}
catch (e) {
showError(e);
}
}
writeParagraph (iPara, sText) {
try {
return this.lNode[iPara].textContent = sText;
}
catch (e) {
showError(e);
}
}
changeParagraph (iPara, sModif, iStart, iEnd) {
let sText = this.getParagraph(iPara);
this.writeParagraph(iPara, sText.slice(0, iStart) + sModif + sText.slice(iEnd));
}
}
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
return this.lNode[iPara].textContent;
}
catch (e) {
showError(e);
}
}
setParagraph (iPara, sText) {
try {
return this.lNode[iPara].textContent = sText;
}
catch (e) {
showError(e);
}
}
changeParagraph (iPara, sModif, iStart, iEnd) {
let sText = this.getParagraph(iPara);
this.writeParagraph(iPara, sText.slice(0, iStart) + sModif + sText.slice(iEnd));
}
clear () {
this.xDocument = null;
this.xRootNode = null;
this.lNode.length = 0;
}
}
/*
Editor for TextNode (Textarea or editable node)
*/
class TextNodeEditor {
constructor (what, xResultNode=null) {
this.xNode = null;
this.dParagraph = new Map();
this.bTextArea = false;
this.bIframe = false;
this.bResultInEvent = false; // if true, the node content is not modified, but an event is dispatched on the node with the modified text
this.xResultNode = null; // only useful for text analysed without node
if (xResultNode instanceof HTMLElement) {
this.xResultNode = xResultNode;
this.bResultInEvent = true;
}
if (typeof(what) == "string") {
// SIMPLE TEXT
if (!this.xResultNode) {
oGrammalecte.oGCPanel.addMessageToGCPanel("⛔ Aucun champ textuel défini. Les changements ne seront pas répercutés sur la zone d’où le texte a été extrait.");
}
this.loadText(sText);
}
else if (what.nodeType && what.nodeType === 1) {
// NODE
this.xNode = what;
this.bResultInEvent = Boolean(this.xNode.dataset.grammalecte_result_via_event && this.xNode.dataset.grammalecte_result_via_event == "true");
this.bTextArea = (this.xNode.tagName == "TEXTAREA" || this.xNode.tagName == "INPUT");
this.bIframe = (this.xNode.tagName == "IFRAME");
if (this.bTextArea) {
this.xNode.disabled = true;
this.loadText(this.xNode.value);
}
else if (this.bIframe) {
// iframe
if (!this.bResultInEvent) {
oGrammalecte.oGCPanel.addMessageToGCPanel("⛔ La zone analysée est un cadre contenant une autre page web (“iframe”). Les changements faits ne peuvent être pas répercutés dans cette zone.");
}
this.loadText(this.xNode.contentWindow.document.body.innerText);
}
else {
// editable node
oGrammalecte.oGCPanel.addMessageToGCPanel("❗ La zone de texte analysée est un champ textuel enrichi susceptible de contenir des éléments non textuels qui seront effacés lors de la correction.");
this.loadText(this.xNode.innerText);
}
}
}
loadText (sText) {
// function also used by the text formatter
if (typeof(sText) === "string") {
this.dParagraph.clear();
let i = 0;
let iStart = 0;
let iEnd = 0;
sText = sText.replace(/\r\n/g, "\n").replace(/\r/g, "\n").normalize("NFC");
while ((iEnd = sText.indexOf("\n", iStart)) !== -1) {
this.dParagraph.set(i, sText.slice(iStart, iEnd));
i++;
iStart = iEnd+1;
}
this.dParagraph.set(i, sText.slice(iStart));
//console.log("Paragraphs number: " + (i+1));
}
this.write();
}
clear () {
if (this.xNode !== null) {
this.xNode.disabled = false;
this.bTextArea = false;
this.bIframe = false;
this.bResultInEvent = false;
this.xNode = null;
this.xResultNode = null;
}
this.dParagraph.clear();
}
getText () {
return [...this.dParagraph.values()].join("\n").normalize("NFC");
}
setParagraph (iParagraph, sText) {
this.dParagraph.set(iParagraph, sText);
this.write();
}
getParagraph (iParaNum) {
return this.dParagraph.get(iParaNum);
}
_eraseNodeContent () {
while (this.xNode.firstChild) {
this.xNode.removeChild(this.xNode.firstChild);
}
}
write () {
if (this.xNode !== null) {
if (this.bResultInEvent) {
const xEvent = new CustomEvent("GrammalecteResult", { detail: JSON.stringify({ sType: "text", sText: this.getText() }) });
this.xNode.dispatchEvent(xEvent);
//console.log("[Grammalecte debug] Text sent to xNode via event:", xEvent.detail);
}
else if (this.bTextArea) {
this.xNode.value = this.getText();
//console.log("[Grammalecte debug] text written in textarea:", this.getText());
}
else if (this.bIframe) {
//console.log(this.getText());
}
else {
this._eraseNodeContent();
this.dParagraph.forEach((val, key) => {
this.xNode.appendChild(document.createTextNode(val.normalize("NFC")));
this.xNode.appendChild(document.createElement("br"));
});
//console.log("[Grammalecte debug] text written in editable node:", this.getText());
}
}
else if (this.xResultNode !== null) {
const xEvent = new CustomEvent("GrammalecteResult", { detail: JSON.stringify({ sType: "text", sText: this.getText() }) });
this.xResultNode.dispatchEvent(xEvent);
//console.log("[Grammalecte debug] Text sent to xResultNode via event:", xEvent.detail);
}
}
}
|