/
tab replacer img.html
484 lines (441 loc) · 12.1 KB
/
tab replacer img.html
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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>4 tab replacer img</title>
<style>
body
{
font-size: 80%;
}
#highlighted, #highlighted2, #highlighted3, .tabspaceviewer, #fontWidthMeasurer
{
border: solid 1px #ddd;
-webkit-user-modify: read-write;
white-space: pre;
font-family: monaco;
position: relative;
z-index: 4;
}
img
{
content: 'hello';
display: inline;
width: 24px;
padding: 4px;
overflow: visible;
}
.fakeTab0
{
econtent: 'BLAH';
emargin-left: -24px;
emargin-right: -24px;
efont-size: 0;
edisplay: none;
efont-size: 0;
epadding-left: 1em;
evisibility: hidden;
eposition: fixed;
border: solid 1px rgba(255, 0, 0, 0.5);
eborder-right: solid 30px lime;
efloat: left;
eposition: fixed;
eposition: relative;
eleft: -60px;
}
/*
#highlighted div
{
line-height: 1.4em;
eheight: 1em;
}
*/
.fakeTab
{
background-color: rgba(0, 255, 0, 0.5);
/*
edisplay: inline-block;
ewidth: 24px;
eoverflow: hidden;
edisplay: inline-table;
edisplay: table-cell;
emax-width: 24px;
font-size: 0px;
letter-spacing: -1px;
epadding-right: 30px;
epadding-right: 7px;
epadding-left: 7px;
epadding-top: 7px;
margin-top: 7px;
line-height: 7pt;
emargin-bottom: 7px;
*/
/*
font-size: 0px;
letter-spacing: -1px;
epadding-left: 7px;
padding-right: 7px;
epadding-bottom: 7px;
*/
/*
white-space: pre;
white-space: pre-line;
epadding-right: 24px;
emargin-right: 24px;
display: inline-block;
width: 24px;
eheight: 4px;
econtent: ('-');
font-size: 40px;
margin-bottom: -10px;
vertical-align: bottom;
*/
/*
eletter-spacing: 10px;
etext-indent: 10px;
edisplay: inline-block;
white-space: normal;
efont-size: 0;
letter-spacing: 35px;
epadding-right: 16px;
eposition: relative;
*/
ewhite-space: normal;
efont-size: 0;
eletter-spacing: -6px;
epadding-right: 24px;
eposition: relative;
/*
eborder: solid 1px rgba(0, 255, 0, 0.3);
emargin-right: 24px;
efloat: left;
eoverflow: hidden;
position: absolute;
visibility: hidden;
edisplay: none;
-webkit-user-modify: read-only;
*/
}
.fakeTabHighlighter
{
emargin-left: -30px;
epadding: 4px;
position: relative;
background-color: rgba(255, 0, 0, 0.3);
}
.fakeTabHighlighter:after
{
econtent: '*';
}
#highlighted3 button
{
display: inline;
}
/*
.tabspaceviewer
{
white-space: pre;
font-family: monaco;
font-size: 7pt;
}
*/
.visualtab
{
background-color: red;
position: absolute;
display: inline-block;
border-left: solid 1px #ddd;
height: 800px;
}
.newlineNormal
{
white-space: normal;
}
</style>
</head>
<body>
<div id='fontWidthMeasurer'><span>*</span></div>
<div class='tabspaceviewer'><span class='visualtab'></span>1234<span class='visualtab'></span>1234<span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> <span class='visualtab'></span> </div>
<div id='highlighted'>
1***2
1___2___3___4 a
1 2 3 4
1* 2* 3* 4
1** 2** 3** 4
1*** 2*** 3*** 4
1**** 2**** 3**** 4
1***** 2***** 3***** 4
1****** 2****** 3****** 4
1 2** 3* 4***5 6
- +
1
a Hello test =
a Hello, real tab follows to check height test trailing tab->
more trailing tabs ->
lone tab follows
a
lone tab ends
<-one tabs start
<-two tabs start
<-three tabs start
= test obst obst objzae st ob st ob st ob st obect
lone line follows
lone line ended
test fakst ob st ob st obect klzejrezlkjr st ob eklzrjzelkj st ob jdslk st obe span
1 2 3 4fin st ob kldfjzelkjrlzek st obezkrj st ob st obst ob st obezrkjezlkrj st ob
1 2 3 4fin
1234
test fake tab
</div>
<div id='highlighted2'>
Hello test =
Hello, real tab follows to check height test
= test obst obst objzae st ob st ob st ob st obect
test fakst ob st ob st obect klzejrezlkjr st ob eklzrjzelkj st ob jdslk st obe span
1 2 3 4fin st ob kldfjzelkjrlzek st obezkrj st ob st obst ob st obezrkjezlkrj st ob
1 2 3 4fin
1234
test fake tab
</div>
<div id='highlighted3'>
aerlkm rmelzk rmelzkr zemlk rez lr iezpo mlkremlzkrze
Hello <button>test</button> world !
Hello <button disabled='true'>test</button> world !
Hello <input type='submit'>test</input> world !
Hello <select>test<option>TEST</option><option>TEST123</option></select> world !
Hello <label style='background-color: red; width: 200px'>test</label> world !
Hello <form>test</form> world !
aerlkm rmelzk rmelzkr zemlk rez lr iezpo mlkremlzkrze
</div>
<pre>
<div id='dump'></div>
<div id='dump2'></div>
<script>
var fontWidthMeasurer = document.getElementById('fontWidthMeasurer')
var charWidth = fontWidthMeasurer.firstChild.offsetWidth
function dumpHash(o)
{
var str = ''
for (var i in o) str += i + '=' + (o[i]) + '\n'
return str
}
function htmlEncode(str)
{
// return str
return str.replace(/</g, '<')
return str.replace(/</g, '<')
return str.replace(/</g, '<')
}
function getTickCount()
{
return (new Date).getTime()
}
function replaceTabInLine(line, html)
{
var tabLengths = []
function countTabs(str, idx)
{
var length = (idx+deltaTabPosition) % tabLength
length = tabLength - length
// Add spaces to position (minus one to 'remove' the tab)
deltaTabPosition += length-1
tabLengths.push(length)
}
var tabIndex = 0
function replaceTabs(str, idx)
{
return '<span class="fakeTab" style="letter-spacing: -' + charWidth + 'px; padding-right: ' + tabLengths[tabIndex++]*charWidth + 'px"> </span>'
// return "<img class='fakeTab' src='' style='width: " + tabLengths[tabIndex++]*charWidth + "px'>"
}
var tabLength = 4
var deltaTabPosition = 0
line.replace(/\t/g, countTabs)
html = html.replace(/\t/g, replaceTabs)
return html
}
function transformText(n)
{
var lines = n.innerText.split('\n')
var t = ''
n.innerHTML = ''
for (var i=0; i<lines.length; i++)
{
var line = lines[i]
// line = line.replace(/\t/g, '<span class="fakeTab"> </span>')
var isLastLineAndIsEmpty = i == lines.length-1 && line.match(/^$/)
// Don't add a new line on the last line if it's empty
if (!isLastLineAndIsEmpty)
{
if (line.match(/^$/)) line = line + '<span class="newline">\n</span>'
else line = line + '<span class="newlineNormal">\n</span>'
}
line = replaceTabInLine(line, line)
var n2 = document.createElement('DIV')
n2.className = 'line'
n2.innerHTML = line
n.appendChild(n2)
}
}
var highlighted = document.getElementById('highlighted')
var highlighted2 = document.getElementById('highlighted2')
var highlighted3 = document.getElementById('highlighted3')
transformText(highlighted)
transformText(highlighted2)
// alert(highlighted3.innerText)
function focusMe()
{
highlighted.focus()
getSelection().collapseToStart()
}
setTimeout(focusMe, 10)
function keydown()
{
setTimeout(dumpSelection, 0)
setTimeout(postKeydown, 10)
}
function postKeydown()
{
var sel = getSelection()
if (sel.type != 'Caret') return
var str = getTickCount()
str += '\nanchorNode=' + sel.anchorNode
var a = sel.anchorNode
var s = selectionAsLineAndOffset()
if (s.line == 0)
{
// alert('*' + a.nodeValue + '$\nlineNumber=' + lineNumberFromNode(a) + '\nparent=' + a.parentNode.parentNode.outerHTML)
}
// alert(dumpHash(s))
transformText(highlighted)
setSelectionAsLineAndOffset(s.line, s.offset)
str += '\nline=' + s.line + ' offset=' + s.offset
document.getElementById('dump2').innerHTML = str
}
function lineNodeFromNode(node)
{
while (node)
{
if (node.nodeType == 1 && node.className.match(/\bline\b/))
return node
node = node.parentNode
}
}
function lineNumberFromNode(node)
{
node = lineNodeFromNode(node)
if (!node) return
// return parseFloat(node.id.match(/\d+/))
var i = 0
node = node.previousSibling
while (node)
{
i++
node = node.previousSibling
}
return i
}
function nodeFromLineNumber(i)
{
return highlighted.childNodes[i]
}
function rawSelection()
{
var sel = getSelection()
return sel.getRangeAt(0)
}
function setRawSelection(r)
{
var sel = getSelection()
sel.removeAllRanges()
sel.addRange(r)
// sel.setBaseAndExtent(r.anchorNode, r.anchorOffset, r.extentNode, r.extentOffset)
}
function selectionAsLineAndOffset()
{
var sel = getSelection()
var raw = rawSelection()
sel.modify('extend', 'left', 'paragraphboundary')
var selectionText = sel.toString()
var length = selectionText.length
var range = sel.getRangeAt(0)
// If there are no real tabs (\t) in the line, try checking for fake ones (img class='fakeTab')
var h = { type : 'Caret', line : lineNumberFromNode(sel.anchorNode), offset : length }
setRawSelection(raw)
return h
}
function setSelectionAsLineAndOffset(line, offset)
{
var line = nodeFromLineNumber(line)
var sel = getSelection()
sel.setPosition(line, 0)
for (var i=0; i<offset; i++)
sel.modify('move', 'right', 'character')
}
function mousedown()
{
var n = event.target
if (n.nodeType != 1) n = n.parentNode
if (n && n.className == 'fakeTab')
{
// Direct scrollLeft to select either left or right portion of the tab
// Only works the second time
if (event.offsetX > n.offsetWidth/2) n.scrollLeft = n.clientWidth
else n.scrollLeft = 0
}
setTimeout(dumpSelection, 0)
}
function dumpSelection()
{
var sel = getSelection()
var str = getTickCount()
// alert(dumpHash(sel))
// alert(n)
try
{
str += '\nsel=' + sel.toString() + '!'
var n = sel.anchorNode
str += '\nanchorNode=<' + (n.nodeValue || htmlEncode(n.outerHTML)) + '>'
str += '\nanchorOffset=' + sel.anchorOffset
if (n.nodeValue)
str += '\nanchor=<' + n.nodeValue.substr(0, sel.anchorOffset) + '<span style="color: red">*</span>' + n.nodeValue.substr(sel.anchorOffset) + '>'
var n = n.parentNode
str += '\nanchorNode.parentNode=<' + (n.nodeValue || htmlEncode(n.outerHTML)) + '>'
var fakeTab
if (n && n.nodeType == 1 && n.className == 'fakeTab')
{
str += '\n<span style="color: red">fakeTab scrollLeft=' + n.scrollLeft + '</span>'
// alert(n.scrollLeft)
// n.scrollLeft = 30000
// n.scrollLeft = n.offsetWidth
// str += '\nscrollLeft=clientWidth=' + n.clientWidth
str += '\nclientWidth=' + n.clientWidth
fakeTab = n
}
while (n && n.tagName != 'DIV') n = n.parentNode
if (n)
{
str += '\n<b>line=</b>' + n.innerText
str += '\nline.offsetHeight=' + n.offsetHeight
}
}
catch (e)
{
str += '\nselection fail'
}
document.getElementById('dump').innerHTML = str
}
highlighted.onkeydown = keydown
highlighted.onmousedown = mousedown
// alert(fontWidthMeasurer.firstChild.offsetWidth)
</script>
<pre>
can IMG be treated as a standard html node containing text ?
^NO
move up, down, go to start/end of line fails with inline-block TABs
use letter-spacing to remove tab size.
BUG when line ends with a TAB : go to line start/line end
TAB prim in lign plant
adding \n at the end bugs selection.
^but not if \n has white-space: normal !
</body>
</html>