-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathencodeHTML.js
64 lines (55 loc) · 1.77 KB
/
encodeHTML.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
// https://akelpad.sourceforge.net/forum/viewtopic.php?p=4271#p4271
// https://infocatcher.ucoz.net/js/akelpad_scripts/encodeHTML.js
// https://github.com/Infocatcher/AkelPad_scripts/blob/master/encodeHTML.js
// (c) Infocatcher 2009-2010
// Version: 0.1.5.2 - 2010-07-08
// Author: Infocatcher
//// Encode HTML entities
// & => &
// < => <
// > => >
// " => "
//var AkelPad = new ActiveXObject("AkelPad.document");
var hMainWnd = AkelPad.GetMainWnd();
var hWndEdit = AkelPad.GetEditWnd();
var oFunction = AkelPad.SystemFunction();
if(hMainWnd && !AkelPad.GetEditReadOnly(hWndEdit)) {
var lpPoint = AkelPad.MemAlloc(8 /*sizeof(POINT)*/);
if(lpPoint) {
toggleRedraw(hWndEdit, false);
var selParams = getSelParams();
AkelPad.SendMessage(hWndEdit, 1245 /*EM_GETSCROLLPOS*/, 0, lpPoint);
selParams = encodeHTML(selParams);
restoreSelParams(selParams);
AkelPad.SendMessage(hWndEdit, 1246 /*EM_SETSCROLLPOS*/, 0, lpPoint);
toggleRedraw(hWndEdit, true);
AkelPad.MemFree(lpPoint);
}
}
function encodeHTML(selParams) {
// Get selection or all text
var txt = AkelPad.GetSelText() || AkelPad.SetSel(0, -1) || AkelPad.GetSelText();
var selStart = AkelPad.GetSelStart();
txt = txt
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """);
AkelPad.ReplaceSel(txt);
return [selStart, selStart + txt.length];
}
function toggleRedraw(hWnd, bRedraw) {
AkelPad.SendMessage(hWnd, 11 /*WM_SETREDRAW*/, bRedraw, 0);
if(!bRedraw)
return;
oFunction.AddParameter(hWnd);
oFunction.AddParameter(0);
oFunction.AddParameter(true);
oFunction.Call("user32::InvalidateRect");
}
function getSelParams() {
return [AkelPad.GetSelStart(), AkelPad.GetSelEnd()];
}
function restoreSelParams(selParams) {
AkelPad.SetSel(selParams[0], selParams[1]);
}