Skip to content

Commit

Permalink
🐛 Fixed range#expandByMarker not expanding to beginning/end of section (
Browse files Browse the repository at this point in the history
#677)

Fixed range#expandByMarker not expanding to beginning/end of section

closes #676
- after failing to find a non-matching marker, perform a check for the first/last markers in the range's section matching the supplied detection callback. If it does match we can assume that we checked all markers in that direction so we expand to the beginning/end of the section
- improves behaviour to match the documentation: "expand a range to all markers matching a given check"
  • Loading branch information
kevinansfield authored and mixonic committed May 7, 2019
1 parent 9e5f064 commit 0000d1d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/js/utils/cursor/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,20 @@ class Range {
return !detectMarker(i);
};

let headMarker = head.section.markers.detect(firstNotMatchingDetect, head.marker, true);
headMarker = (headMarker && headMarker.next) || head.marker;
let headMarker = headSection.markers.detect(firstNotMatchingDetect, head.marker, true);
if (!headMarker && detectMarker(headSection.markers.head)) {
headMarker = headSection.markers.head;
} else {
headMarker = headMarker.next || head.marker;
}
let headPosition = new Position(headSection, headSection.offsetOfMarker(headMarker));

let tailMarker = tail.section.markers.detect(firstNotMatchingDetect, tail.marker);
tailMarker = (tailMarker && tailMarker.prev) || tail.marker;
if (!tailMarker && detectMarker(headSection.markers.tail)) {
tailMarker = headSection.markers.tail;
} else {
tailMarker = tailMarker.prev || tail.marker;
}
let tailPosition = new Position(tail.section, tail.section.offsetOfMarker(tailMarker) + tailMarker.length);

return headPosition.toRange(tailPosition, direction);
Expand Down
65 changes: 65 additions & 0 deletions tests/unit/utils/cursor-range-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,68 @@ test('#expandByMarker processed markers in a callback and continues as long as t
'range tail did not change'
);
});

// https://github.com/bustle/mobiledoc-kit/issues/676
test('#expandByMarker can expand to beginning of section with matching markups', (assert) => {
let post = Helpers.postAbstract.build(({post, markupSection, marker, markup}) => {
let bold = markup('b');
let italic = markup('i');
return post([
markupSection('p', [
marker('aiya', [bold]),
marker('biya', [bold, italic]),
marker('ciya', [bold]),
marker('diya', [bold]),
])
]);
});

let section = post.sections.head;
let head = section.toPosition(14); // i in 4th hiya
let tail = section.toPosition(14); // i in 4th hiya
let range = head.toRange(tail);
let expandedRange = range.expandByMarker(marker => {
return !!(detect(marker.markups, markup => markup.tagName === 'b'));
});

assert.positionIsEqual(
expandedRange.head, section.toPosition(0),
'range head is start of first marker'
);
assert.positionIsEqual(
expandedRange.tail, section.toPosition(16),
'range tail is at end of last marker'
);
});

test('#expandByMarker can expand to end of section with matching markups', (assert) => {
let post = Helpers.postAbstract.build(({post, markupSection, marker, markup}) => {
let bold = markup('b');
let italic = markup('i');
return post([
markupSection('p', [
marker('aiya', [bold]),
marker('biya', [bold, italic]),
marker('ciya', [bold]),
marker('diya', [bold]),
])
]);
});

let section = post.sections.head;
let head = section.toPosition(2); // i in 4th hiya
let tail = section.toPosition(2); // i in 4th hiya
let range = head.toRange(tail);
let expandedRange = range.expandByMarker(marker => {
return !!(detect(marker.markups, markup => markup.tagName === 'b'));
});

assert.positionIsEqual(
expandedRange.head, section.toPosition(0),
'range head is start of first marker'
);
assert.positionIsEqual(
expandedRange.tail, section.toPosition(16),
'range tail is at end of last marker'
);
});

0 comments on commit 0000d1d

Please sign in to comment.