Skip to content

Commit

Permalink
Explicitly type each token to string to correctly calculate its length
Browse files Browse the repository at this point in the history
  • Loading branch information
kasnerz committed Feb 3, 2025
1 parent 33f4c34 commit e53fbf9
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions factgenie/static/js/span-annotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ class SpanAnnotator {
let currentIndex = 0;
if (this.granularity === 'words') {
const parts = text.split(/(\s+)/);

return parts.map((part, arrayIndex) => {
if (arrayIndex % 2 === 1) {
return ''; // Remove standalone whitespace spans
}

const whitespace = arrayIndex < parts.length - 1 ? parts[arrayIndex + 1] : '';
const fullContent = part + whitespace;
const fullContent = String(part) + whitespace;
const span = `<span class="annotatable"
data-index="${currentIndex}"
data-content="${part}"
Expand Down Expand Up @@ -356,7 +357,7 @@ class SpanAnnotator {
const [min, max] = [Math.min(startIdx, endIdx), Math.max(startIdx, endIdx)];

// Get the actual end position by adding length of the last token
const maxWithLength = max + $end.data('content').length - 1;
const maxWithLength = max + String($end.data('content')).length - 1;

// Check for overlap if not allowed
if (!this.overlapAllowed && this._hasExistingAnnotations(doc, min, maxWithLength)) {
Expand Down Expand Up @@ -409,7 +410,7 @@ class SpanAnnotator {
$('.whitespace', $span).removeClass('whitespace-hidden');

if (spanAnnotations.length > 0) {
const content = $span.data('content');
const content = String($span.data('content'));
const isLastInAnyAnnotation = spanAnnotations.some(ann =>
position + content.length >= ann.start + ann.text.length);
const hasMultipleAnnotations = spanAnnotations.length > 1;
Expand Down

0 comments on commit e53fbf9

Please sign in to comment.