Skip to content

Commit f83ba83

Browse files
author
Wout Van Den Driessche
committed
Detect non-width spaces
1 parent 2d03516 commit f83ba83

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed

dist/tribute.esm.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,10 @@ class TributeRange {
717717
}
718718
}
719719

720+
convertNonWidthSpacesToRealOnes(text) {
721+
return text.replace(/[\u200B-\u200D\uFEFF]/g, ' ')
722+
}
723+
720724
getTextPrecedingCurrentSelection() {
721725
let context = this.tribute.current,
722726
text = '';
@@ -734,7 +738,7 @@ class TributeRange {
734738
let selectedElem = this.getWindowSelection().anchorNode;
735739

736740
if (selectedElem != null) {
737-
let workingNodeContent = selectedElem.textContent;
741+
let workingNodeContent = this.convertNonWidthSpacesToRealOnes(selectedElem.textContent);
738742
let selectStartOffset = this.getWindowSelection().getRangeAt(0).startOffset;
739743

740744
if (workingNodeContent && selectStartOffset >= 0) {

dist/tribute.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
(function(l, r) { if (l.getElementById('livereloadscript')) return; r = l.createElement('script'); r.async = 1; r.src = '//' + (window.location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1'; r.id = 'livereloadscript'; l.head.appendChild(r) })(window.document);
13
(function (global, factory) {
24
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
35
typeof define === 'function' && define.amd ? define(factory) :
@@ -792,6 +794,11 @@
792794
};
793795
}
794796
}
797+
}, {
798+
key: "convertNonWidthSpacesToRealOnes",
799+
value: function convertNonWidthSpacesToRealOnes(text) {
800+
return text.replace(/[\u200B-\u200D\uFEFF]/g, ' ');
801+
}
795802
}, {
796803
key: "getTextPrecedingCurrentSelection",
797804
value: function getTextPrecedingCurrentSelection() {
@@ -812,7 +819,7 @@
812819
var selectedElem = this.getWindowSelection().anchorNode;
813820

814821
if (selectedElem != null) {
815-
var workingNodeContent = selectedElem.textContent;
822+
var workingNodeContent = this.convertNonWidthSpacesToRealOnes(selectedElem.textContent);
816823
var selectStartOffset = this.getWindowSelection().getRangeAt(0).startOffset;
817824

818825
if (workingNodeContent && selectStartOffset >= 0) {

dist/tribute.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/tribute.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/tribute.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
(function(l, r) { if (l.getElementById('livereloadscript')) return; r = l.createElement('script'); r.async = 1; r.src = '//' + (window.location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1'; r.id = 'livereloadscript'; l.head.appendChild(r) })(window.document);
13
(function (global, factory) {
24
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
35
typeof define === 'function' && define.amd ? define(factory) :
@@ -792,6 +794,11 @@
792794
};
793795
}
794796
}
797+
}, {
798+
key: "convertNonWidthSpacesToRealOnes",
799+
value: function convertNonWidthSpacesToRealOnes(text) {
800+
return text.replace(/[\u200B-\u200D\uFEFF]/g, ' ');
801+
}
795802
}, {
796803
key: "getTextPrecedingCurrentSelection",
797804
value: function getTextPrecedingCurrentSelection() {
@@ -812,7 +819,7 @@
812819
var selectedElem = this.getWindowSelection().anchorNode;
813820

814821
if (selectedElem != null) {
815-
var workingNodeContent = selectedElem.textContent;
822+
var workingNodeContent = this.convertNonWidthSpacesToRealOnes(selectedElem.textContent);
816823
var selectStartOffset = this.getWindowSelection().getRangeAt(0).startOffset;
817824

818825
if (workingNodeContent && selectStartOffset >= 0) {

src/TributeRange.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ class TributeRange {
243243
}
244244
}
245245

246+
convertNonWidthSpacesToRealOnes(text) {
247+
return text.replace(/[\u200B-\u200D\uFEFF]/g, ' ')
248+
}
249+
246250
getTextPrecedingCurrentSelection() {
247251
let context = this.tribute.current,
248252
text = ''
@@ -260,7 +264,7 @@ class TributeRange {
260264
let selectedElem = this.getWindowSelection().anchorNode
261265

262266
if (selectedElem != null) {
263-
let workingNodeContent = selectedElem.textContent
267+
let workingNodeContent = this.convertNonWidthSpacesToRealOnes(selectedElem.textContent)
264268
let selectStartOffset = this.getWindowSelection().getRangeAt(0).startOffset
265269

266270
if (workingNodeContent && selectStartOffset >= 0) {

0 commit comments

Comments
 (0)