Skip to content

Commit 8ebd99e

Browse files
committed
Restore Chrome selection misreporting workaround
FIX: Restore a workaround for a Chrome selection bug that had regressed in the previous release.
1 parent f7b4095 commit 8ebd99e

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/docview.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ export class DocView {
137137
// selection from the one it displays (issue #218). This tries
138138
// to detect that situation.
139139
let track = browser.chrome || browser.ios ? {node: observer.selectionRange.focusNode!, written: false} : undefined
140-
this.tile.sync()
141-
if (track && (track.written || observer.selectionRange.focusNode != track.node)) this.forceSelection = true
140+
this.tile.sync(track)
141+
if (track && (track.written || observer.selectionRange.focusNode != track.node || !this.tile.dom.contains(track.node)))
142+
this.forceSelection = true
142143
this.tile.dom.style.height = ""
143144
})
144145
let gaps = []

src/tile.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export abstract class Tile {
6666

6767
get domAttrs(): Attrs | null { return null }
6868

69-
sync() {
69+
sync(track?: {node: Node, written: boolean}) {
7070
this.flags |= TileFlag.Synced
7171
if (this.flags & TileFlag.AttrsDirty) {
7272
this.flags &= ~TileFlag.AttrsDirty
@@ -154,15 +154,17 @@ export abstract class CompositeTile extends Tile {
154154
child.parent = this
155155
}
156156

157-
sync() {
157+
sync(track?: {node: Node, written: boolean}) {
158158
if (this.flags & TileFlag.Synced) return
159-
super.sync()
159+
super.sync(track)
160160
let parent = this.dom, prev: Node | null = null, next
161+
let tracking = track?.node == parent ? track : null
161162
let length = 0
162163
for (let child of this.children) {
163-
child.sync()
164+
child.sync(track)
164165
length += child.length + child.breakAfter
165166
next = prev ? prev.nextSibling : parent.firstChild
167+
if (tracking && next != child.dom) tracking.written = true
166168
if (child.dom!.parentNode == parent) {
167169
while (next && next != child.dom) next = rm(next)
168170
} else {
@@ -171,6 +173,7 @@ export abstract class CompositeTile extends Tile {
171173
prev = child.dom!
172174
}
173175
next = prev ? prev.nextSibling : parent.firstChild
176+
if (tracking && next) tracking.written = true
174177
while (next) next = rm(next)
175178
this.length = length
176179
}
@@ -383,10 +386,13 @@ export class TextTile extends Tile {
383386
super(dom, text.length)
384387
}
385388

386-
sync() {
389+
sync(track?: {node: Node, written: boolean}) {
387390
if (this.flags & TileFlag.Synced) return
388-
super.sync()
389-
if (this.dom.nodeValue != this.text) this.dom.nodeValue = this.text
391+
super.sync(track)
392+
if (this.dom.nodeValue != this.text) {
393+
if (track && track.node == this.dom) track.written = true
394+
this.dom.nodeValue = this.text
395+
}
390396
}
391397

392398
isText(): this is TextTile { return true }

0 commit comments

Comments
 (0)