Fix handling of selected text in VBS console

This commit is contained in:
~lucidiot 2022-09-22 22:13:47 +02:00
parent bc2629a8fc
commit f76123a721
1 changed files with 30 additions and 24 deletions

View File

@ -48,37 +48,43 @@ Sub Clear
output.innerHTML = ""
End Sub
Private Function RemoveCr (text)
Private Function RemoveCr (ByVal text)
RemoveCr = Replace(text, vbCrLf, vbLf)
End Function
Private Function GetSelectedText (element)
Dim normalizedValue, range, inputRange, endRange
Dim range
Set range = document.selection.createRange()
If Not IsNull(range) And range.parentElement() = element Then
normalizedValue = RemoveCr(element.value)
Set inputRange = element.createTextRange()
inputRange.moveToBookmark range.getBookmark()
Set endRange = element.createTextRange()
endRange.collapse False
'Selection does not begin at the end of the input
If inputRange.compareEndPoints("StartToEnd", endRange) < 0 Then
Dim selectionStart, selectionEnd
selectionStart = UBound(Split(Right(normalizedValue, Len(normalizedValue) - selectionStart), vbLf)) - inputRange.moveStart("character", -Len(element.value))
selectionEnd = Len(element.value)
If inputRange.compareEndPoints("EndToEnd", endRange) < 0 Then
selectionEnd = UBound(Split(Right(normalizedValue, Len(normalizedValue) - selectionEnd), vbCrLf)) - inputRange.moveEnd("character", -Len(element.value))
End If
GetSelectedText = Left(Right(element.value, Len(element.value) - selectionStart), selectionEnd - selectionStart)
Else
GetSelectedText = ""
End If
Else
If IsNull(range) Or range.parentElement() <> element Then
GetSelectedText = ""
Exit Function
End If
Dim normalizedValue, inputRange, endRange
normalizedValue = RemoveCr(element.value)
Set inputRange = element.createTextRange()
inputRange.moveToBookmark range.getBookmark()
Set endRange = element.createTextRange()
endRange.collapse False
'Selection begins at the end of the input, so it's empty
If inputRange.compareEndPoints("StartToEnd", endRange) > -1 Then
GetSelectedText = ""
Exit Function
End If
Dim selectionStart, selectionEnd
selectionStart = -inputRange.moveStart("character", -Len(element.value))
selectionStart = selectionStart + UBound(Split(Left(normalizedValue, selectionStart), vbLf)) + 1
If selectionStart <= 0 Then selectionStart = 1
selectionEnd = Len(normalizedValue)
If inputRange.compareEndPoints("EndToEnd", endRange) < 0 Then
selectionEnd = -inputRange.moveEnd("character", -Len(element.value))
selectionEnd = selectionEnd + UBound(Split(Left(normalizedValue, selectionEnd), vbLf))
End If
GetSelectedText = Mid(normalizedValue, selectionStart, selectionEnd - selectionStart + 1)
End Function
Private Sub Run