Commit e33027cb authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Add static `trimmedRange` to `TextRange`

parent aea38355
......@@ -452,5 +452,21 @@ describe('annotator/anchoring/text-range', () => {
assert.equal(textRange.end.offset, 10);
});
});
describe('trimmedRange', () => {
it('adjusts Range start and end positions to remove whitespace', () => {
const el = document.createElement('div');
el.textContent = ' one two three ';
const range = new Range();
range.selectNodeContents(el);
const textRange = TextRange.fromRange(range).toRange();
const trimmedRange = TextRange.trimmedRange(range);
assert.equal(trimmedRange.startOffset, textRange.startOffset + 1);
assert.equal(trimmedRange.endOffset, textRange.endOffset - 1);
});
});
});
});
import { trimRange } from './trim-range';
/**
* Return the combined length of text nodes contained in `node`.
*/
......@@ -314,4 +316,12 @@ export class TextRange {
new TextPosition(root, end)
);
}
/**
* Return a new Range representing `range` trimmed of any leading or trailing
* whitespace
*/
static trimmedRange(range: Range): Range {
return trimRange(TextRange.fromRange(range).toRange());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment