Skip to content

Commit 27ba974

Browse files
committed
remove post#sectionsContainedBy(range)
1 parent 3327408 commit 27ba974

File tree

6 files changed

+31
-194
lines changed

6 files changed

+31
-194
lines changed

src/js/editor/post.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class PostEditor {
110110
if (headSection === tailSection) {
111111
nextPosition = this.cutSection(headSection, headSectionOffset, tailSectionOffset);
112112
} else {
113-
let removedSections = post.sectionsContainedBy(range);
113+
let removedSections = [];
114114
let appendSection = headSection;
115115

116116
post.walkLeafSections(range, section => {
@@ -143,9 +143,7 @@ class PostEditor {
143143
}
144144
break;
145145
default:
146-
if (removedSections.indexOf(section) === -1) {
147-
removedSections.push(section);
148-
}
146+
removedSections.push(section);
149147
}
150148
});
151149
if (headSection !== appendSection) {

src/js/models/post.js

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -185,40 +185,6 @@ class Post {
185185
});
186186
}
187187

188-
/**
189-
* @param {Range} range
190-
* @return {Section[]} All top-level sections (direct children of `post`) wholly
191-
* contained by {range}. Sections that are partially contained by the range
192-
* are not returned.
193-
* @private
194-
*/
195-
sectionsContainedBy(range) {
196-
const {head, tail} = range;
197-
let containedSections = [];
198-
199-
const findParent = (child, conditionFn) => {
200-
while (child) {
201-
if (conditionFn(child)) { return child; }
202-
child = child.parent;
203-
}
204-
};
205-
206-
const headTopLevelSection = findParent(head.section, s => s.parent === s.post);
207-
const tailTopLevelSection = findParent(tail.section, s => s.parent === s.post);
208-
209-
if (headTopLevelSection === tailTopLevelSection) {
210-
return containedSections;
211-
}
212-
213-
let currentSection = headTopLevelSection.next;
214-
while (currentSection && currentSection !== tailTopLevelSection) {
215-
containedSections.push(currentSection);
216-
currentSection = currentSection.next;
217-
}
218-
219-
return containedSections;
220-
}
221-
222188
_nextMarkerableSection(section) {
223189
let nextSection = this._nextLeafSection(section);
224190

tests/helpers/post-abstract.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ function parseTextIntoMarkers(text, builder) {
8282
}
8383
});
8484
} else if (text.indexOf('*') === -1) {
85-
markers.push(builder.marker(text));
85+
if (text.length) {
86+
markers.push(builder.marker(text));
87+
}
8688
} else {
8789
let markup = builder.markup('b');
8890

tests/unit/editor/post-delete-at-position-test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ test('across section boundary (backward)', (assert) => {
131131
[['abc','|def'], 'abc|def', 'markup sections'],
132132
[['*abc*','|def'], '*abc*|def', 'markup sections with markup'],
133133
[['[abc]','|def'], ['[abc]|','def'], 'prev section is card'],
134-
[['abc','|[def]'], ['abc|','[def]'], 'cur section is card']
134+
[['abc','|[def]'], ['abc|','[def]'], 'cur section is card'],
135+
[['', '|abc'], ['|abc'], 'prev section is blank']
135136
];
136137

137138
examples.forEach(([before, after, msg]) => {
@@ -152,7 +153,8 @@ test('across section boundary (forward)', (assert) => {
152153
[['abc|','def'], 'abc|def', 'markup sections'],
153154
[['*abc*|','def'], '*abc*|def', 'markup sections with markup'],
154155
[['[abc]|','def'], ['[abc]|','def'], 'cur section is card'],
155-
[['abc|','[def]'], ['abc|','[def]'], 'next section is card']
156+
[['abc|','[def]'], ['abc|','[def]'], 'next section is card'],
157+
[['abc|', ''], ['abc|'], 'next section is blank']
156158
];
157159

158160
examples.forEach(([before, after, msg]) => {
@@ -172,6 +174,7 @@ test('across list item boundary (backward)', (assert) => {
172174
let examples = [
173175
[['* abc','* |def'], ['* abc', '|def'], 'start of list item'],
174176
[['* abc','|def'], ['* abc|def'], 'into list item'],
177+
[['', '* |abc'], ['', '|abc'], 'prev blank section'],
175178
];
176179

177180
examples.forEach(([before, after, msg]) => {

tests/unit/editor/post-delete-range-test.js

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,23 @@ test('#deleteRange within a section with markup (single section)', (assert) => {
115115
});
116116
});
117117

118-
test('#deleteRange entire section (single section)', (assert) => {
119-
let text = '<abc>';
120-
let { post, range } = Helpers.postAbstract.buildFromText(text);
121-
let position = run(post, postEditor => postEditor.deleteRange(range));
122-
123-
assert.ok(post.sections.length === 1 && post.sections.head.isBlank, 'post has single blank section after deleteRange');
124-
assert.ok(position.section === post.sections.head, 'position#section is correct');
125-
assert.equal(position.offset, 0, 'position#offset is correct');
126-
});
118+
test('#deleteRange entire post', (assert) => {
119+
let examples = [
120+
[['<abc>'], 'single section'],
121+
[['<[some-card]>'], 'single card'],
122+
[['<abc','def','ghi>'], 'multiple sections'],
123+
[['<>'], 'single blank section'],
124+
[['<','','>'], 'multiple blank sections']
125+
];
127126

128-
test('#deleteRange entire section (multiple sections)', (assert) => {
129-
let text = ['<abc','def','ghi>'];
130-
let { post, range } = Helpers.postAbstract.buildFromText(text);
131-
let position = run(post, postEditor => postEditor.deleteRange(range));
127+
examples.forEach(([text, msg]) => {
128+
let { post, range } = Helpers.postAbstract.buildFromText(text);
129+
let position = run(post, postEditor => postEditor.deleteRange(range));
132130

133-
assert.ok(post.sections.length === 1 && post.sections.head.isBlank, 'post has single blank section after deleteRange');
134-
assert.ok(position.section === post.sections.head, 'position#section is correct');
135-
assert.equal(position.offset, 0, 'position#offset is correct');
131+
assert.ok(post.sections.length === 1 && post.sections.head.isBlank, `post has single blank section after deleteRange (${msg})`);
132+
assert.ok(position.section === post.sections.head, `position#section is correct (${msg})`);
133+
assert.equal(position.offset, 0, `position#offset is correct (${msg})`);
134+
});
136135
});
137136

138137
test('#deleteRange across markup section boundaries', (assert) => {
@@ -204,7 +203,12 @@ test('#deleteRange across markup/non-markup section boundaries', (assert) => {
204203
[['[some-card]<','>abc'], ['[some-card]|', 'abc'], 'card->markup'],
205204
[['abc<','>[some-card]'], ['abc|', '[some-card]'], 'markup->card'],
206205

207-
[['abc<', '[some-card]', '>def'], ['abc|def'], 'containing card, boundaries in outer sections']
206+
[['abc<', '[some-card]', '>def'], ['abc|def'], 'containing card, boundaries in outer sections'],
207+
208+
[['abc', '<[some-card]>', 'def'], ['abc', '|', 'def'], 'containing card, boundaries in card section'],
209+
210+
// Ideally this would delete the blank section altogether and position cursor at the start of the card
211+
[['<', '>[some-card]'], ['|', '[some-card]'], 'blank section into card']
208212
];
209213

210214
examples.forEach(([before, after, msg]) => {
@@ -227,25 +231,6 @@ test('#deleteRange across markup/non-markup section boundaries', (assert) => {
227231
});
228232
});
229233

230-
test('#deleteRange surrounding card section', (assert) => {
231-
let { post, range } = Helpers.postAbstract.buildFromText(['abc', '<[some-card]>', 'def']);
232-
let expectedPost = Helpers.postAbstract.build(({post, markupSection, marker}) => {
233-
return post([
234-
markupSection('p', [marker('abc')]),
235-
markupSection('p'),
236-
markupSection('p', [marker('def')])
237-
]);
238-
});
239-
240-
let position = run(post, postEditor => postEditor.deleteRange(range));
241-
renderedRange = new Range(position);
242-
243-
let expectedRange = Range.create(post.sections.objectAt(1), 0);
244-
245-
assert.postIsSimilar(post, expectedPost);
246-
assert.rangeIsEqual(renderedRange, expectedRange);
247-
});
248-
249234
test('#deleteRange across list items', (assert) => {
250235
let examples = [
251236
[['* item 1<', '* >item 2'], ['* item 1|item 2'], 'at boundary'],

tests/unit/models/post-test.js

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -357,123 +357,6 @@ test('#isBlank is true when there are no sections', (assert) => {
357357
assert.ok(!_post.isBlank);
358358
});
359359

360-
// see https://github.com/bustlelabs/mobiledoc-kit/issues/134
361-
test('#sectionsContainedBy when range covers two list items', (assert) => {
362-
const post = Helpers.postAbstract.build(
363-
({post, markupSection, marker, listSection, listItem}) => {
364-
return post([
365-
listSection('ul', [listItem([marker('abc')]), listItem()]),
366-
markupSection('p', [marker('123')])
367-
]);
368-
});
369-
const li1 = post.sections.head.items.head,
370-
li2 = post.sections.head.items.tail;
371-
const section = post.sections.tail;
372-
assert.equal(li1.text, 'abc', 'precond - li1 text');
373-
assert.equal(li2.text, '', 'precond - li2 text');
374-
assert.equal(section.text, '123', 'precond - section text');
375-
376-
const range = Range.create(li1, 0, li2, li2.length);
377-
const containedSections = post.sectionsContainedBy(range);
378-
assert.equal(containedSections.length, 0, 'no sections are contained');
379-
});
380-
381-
test('#sectionsContainedBy when range contains no sections', (assert) => {
382-
const post = Helpers.postAbstract.build(
383-
({post, markupSection, marker}) => {
384-
return post([
385-
markupSection('p', [marker('abc')]),
386-
markupSection('p', [marker('123')])
387-
]);
388-
});
389-
const s1 = post.sections.head,
390-
s2 = post.sections.tail;
391-
assert.equal(s1.text, 'abc', 'precond - s1 text');
392-
assert.equal(s2.text, '123', 'precond - s2 text');
393-
394-
const range = Range.create(s1, 0, s2, s2.length);
395-
const containedSections = post.sectionsContainedBy(range);
396-
assert.equal(containedSections.length, 0, 'no sections are contained');
397-
});
398-
399-
test('#sectionsContainedBy when range contains sections', (assert) => {
400-
const post = Helpers.postAbstract.build(
401-
({post, markupSection, marker}) => {
402-
return post([
403-
markupSection('p', [marker('abc')]),
404-
markupSection('p', [marker('inner')]),
405-
markupSection('p', [marker('123')])
406-
]);
407-
});
408-
const s1 = post.sections.head,
409-
sInner = post.sections.objectAt(1),
410-
s2 = post.sections.tail;
411-
assert.equal(s1.text, 'abc', 'precond - s1 text');
412-
assert.equal(sInner.text, 'inner', 'precond - sInner text');
413-
assert.equal(s2.text, '123', 'precond - s2 text');
414-
415-
const range = Range.create(s1, 0, s2, s2.length);
416-
const containedSections = post.sectionsContainedBy(range);
417-
assert.equal(containedSections.length, 1, '1 sections are contained');
418-
assert.ok(containedSections[0] === sInner, 'inner section is contained');
419-
});
420-
421-
test('#sectionsContainedBy when range contains non-markerable sections', (assert) => {
422-
const post = Helpers.postAbstract.build(
423-
({post, markupSection, marker, cardSection, listSection, listItem}) => {
424-
return post([
425-
markupSection('p', [marker('abc')]),
426-
cardSection('test-card'),
427-
listSection('ul', [listItem([marker('li')])]),
428-
markupSection('p', [marker('123')])
429-
]);
430-
});
431-
const s1 = post.sections.head,
432-
card = post.sections.objectAt(1),
433-
list = post.sections.objectAt(2),
434-
s2 = post.sections.tail;
435-
436-
assert.equal(s1.text, 'abc', 'precond - s1 text');
437-
assert.equal(s2.text, '123', 'precond - s2 text');
438-
const range = Range.create(s1, 0, s2, s2.length);
439-
const containedSections = post.sectionsContainedBy(range);
440-
assert.equal(containedSections.length, 2, '2 sections are contained');
441-
assert.ok(containedSections.indexOf(card) !== -1, 'contains card');
442-
assert.ok(containedSections.indexOf(list) !== -1, 'contains list');
443-
});
444-
445-
test('#sectionsContainedBy when range starts/ends in list item', (assert) => {
446-
const post = Helpers.postAbstract.build(
447-
({post, markupSection, marker, cardSection, listSection, listItem}) => {
448-
return post([
449-
listSection('ul', [
450-
listItem([marker('ul1 li1')]),
451-
listItem([marker('ul1 li2')])
452-
]),
453-
markupSection('p', [marker('abc')]),
454-
cardSection('test-card'),
455-
listSection('ul', [
456-
listItem([marker('ul2 li1')]),
457-
listItem([marker('ul2 li2')])
458-
])
459-
]);
460-
});
461-
const li1 = post.sections.head.items.head,
462-
li2 = post.sections.tail.items.tail,
463-
s1 = post.sections.objectAt(1),
464-
card = post.sections.objectAt(2);
465-
466-
assert.equal(li1.text, 'ul1 li1', 'precond - li1 text');
467-
assert.equal(li2.text, 'ul2 li2', 'precond - li2 text');
468-
assert.equal(s1.text, 'abc', 'precond - s1 text');
469-
470-
const range = Range.create(li1, li1.length, li2, li2.length);
471-
const containedSections = post.sectionsContainedBy(range);
472-
assert.equal(containedSections.length, 2, '2 sections are contained');
473-
assert.ok(containedSections.indexOf(card) !== -1, 'contains card');
474-
assert.ok(containedSections.indexOf(s1) !== -1, 'contains section');
475-
});
476-
477360
test('#trimTo creates a post from the given range', (assert) => {
478361
let post = Helpers.postAbstract.build(
479362
({post, markupSection, marker}) => {

0 commit comments

Comments
 (0)