-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection-demo.html
More file actions
155 lines (130 loc) · 4.05 KB
/
selection-demo.html
File metadata and controls
155 lines (130 loc) · 4.05 KB
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
<!DOCTYPE html>
<meta charset=utf8>
<style>
div#editor {
border: 4px solid lightblue;
padding: 1em;
}
small {
color: hsl(0 0 40%);
font-style: italic;
}
pre {
border: 2px solid lightgray;
padding: 1em;
}
mark#position-raw {
border: 1px solid red;
}
mark#position-html {
border: 1px solid green;
}
</style>
<p>Make selections and change the content to see the difference between text mappings and HTML mappings.
View the source to see one further complication, that parsed DOM does not match HTML strings!
(<small>Hint: the browser gives no access to the original HTML producing the page</small>)</p>
<p>
<div id=editor contenteditable>
My <em data-count=1>bonny</em> lies over the <span>ocean 🌊</span>.<br>
My <em data-count=2>bonny</em> lies over the <span>sea 🌊</span>.<br>
My <em data-count=3>bonny</em> lies over the <span>ocean 🌊</span>.<br>
So <strong>bring back</strong> my <em data-count=4>bonny</em> to <del>you</del><ins>me</ins>.
</div>
<button>Set as innerHTML</button>
<p>
<strong>Offsets in the raw text of the <em>serialized HTML output</em>.</strong>
<pre inert id=raw-output></pre>
<strong>Offsets mapped into the text nodes of the <em>serialized HTML output</em>.</strong>
<pre inert id=html-output></pre>
<strong>Void meta tags inserted as marker positions.</strong>
<pre inert id=meta-output></pre>
<script>
const b = document.querySelector( 'button' );
const e = document.querySelector( '#editor' );
const h = document.querySelector( '#html-output' );
const m = document.querySelector( '#meta-output' );
const r = document.querySelector( '#raw-output' );
const charsUntil = ( node, offset ) => {
const w = document.createTreeWalker( e, NodeFilter.SHOW_TEXT );
let c = 0;
while ( w.nextNode() ) {
if ( w.currentNode === node ) {
return c + offset;
} else {
c += w.currentNode.data.length;
}
}
return c;
};
const update = event => {
r.innerText = e.innerHTML;
h.innerText = e.innerHTML;
m.innerText = e.innerHTML;
const p = window.getSelection().rangeCount > 0
? window.getSelection().getRangeAt(0)
: null;
if ( null === p ) {
return;
}
const startOffset = charsUntil( p.startContainer, p.startOffset );
const endOffset = charsUntil( p.endContainer, p.endOffset );
const node = document.createElement( 'pre' );
node.innerText = e.innerHTML.slice( 0, startOffset );
let prefix = node.innerHTML;
node.innerText = e.innerHTML.slice( startOffset, endOffset );
let infix = node.innerHTML;
node.innerText = e.innerHTML.slice( endOffset );
let suffix = node.innerHTML;
r.innerHTML = (
prefix +
'<mark id=position-raw>' +
infix +
'</mark>' +
suffix
);
const html = e.cloneNode(true);
const w = document.createTreeWalker( html, NodeFilter.SHOW_TEXT );
let c = 0;
while ( w.nextNode() ) {
const n = w.currentNode;
const l = n.data.length;
let added = 0;
if ( c <= startOffset && ( c + l ) > startOffset ) {
n.data = n.data.slice( 0, startOffset - c ) + '\u{e000}' + n.data.slice( startOffset - c );
added++;
}
if ( c <= endOffset && ( c + l ) > endOffset ) {
n.data = n.data.slice( 0, endOffset - c + added ) + '\u{e001}' + n.data.slice( endOffset - c + added );
break;
}
c += l;
}
const hStart = html.innerHTML.indexOf( '\u{e000}' );
const hEnd = html.innerHTML.indexOf( '\u{e001}' );
node.innerText = html.innerHTML.slice( 0, hStart );
prefix = node.innerHTML;
node.innerText = html.innerHTML.slice( hStart, hEnd ).replace( '\u{e000}', '' );
infix = node.innerHTML;
node.innerText = html.innerHTML.slice( hEnd ).replace( '\u{e001}', '' );
suffix = node.innerHTML;
h.innerHTML = (
prefix +
'<mark id=position-html>' +
infix +
'</mark>' +
suffix
);
m.innerHTML = (
prefix +
'<meta name="cursor-start" content="user-hash">' +
infix +
'<meta name="cursor-end" content="user-hash">' +
suffix
);
console.log( `Selection from ${startOffset} to ${endOffset}` );
};
e.addEventListener( 'input', update );
document.addEventListener( 'selectionchange', update );
b.addEventListener( 'click', () => { e.innerHTML = e.innerText; update(); } );
update();
</script>