// off the first part of the span-node, and apply the style to it
int intersectionLength = selectionSpan.end - textSpan.start;
TextSpan node1 = spanNode.getRange(0, intersectionLength);
styleApplicator.apply(node1);
Node node2 = spanNode.getRange(intersectionLength, characterCount - intersectionLength);
Element parent = spanNode.getParent();
int index = parent.remove(spanNode);
parent.insert(node1, index);
parent.insert(node2, index + 1);
} else if (selectionSpan.end >= textSpan.end) {
// if the selection covers the last part of the span-node, split off
// the last part of the span-node, and apply the style to it
int intersectionStart = selectionSpan.start - textSpan.start;
TextSpan part1 = spanNode.getRange(0, intersectionStart);
TextSpan part2 = spanNode.getRange(intersectionStart,
characterCount - intersectionStart);
styleApplicator.apply(part2);
Element parent = spanNode.getParent();
int index = parent.remove(spanNode);
parent.insert(part1, index);
parent.insert(part2, index + 1);
} else {
// if the selection covers an internal part of the span-node, split
// the
// span-node into 3 parts, and apply the style to the second part
int part2Start = selectionSpan.start - textSpan.start;
int part2End = selectionSpan.end - textSpan.start;
TextSpan part1 = spanNode.getRange(0, part2Start);
TextSpan part2 = spanNode.getRange(part2Start, part2End
- part2Start);
TextSpan part3 = spanNode.getRange(part2End, characterCount
- part2End);
styleApplicator.apply(part2);
Element parent = spanNode.getParent();
int index = parent.remove(spanNode);
parent.insert(part1, index);
parent.insert(part2, index + 1);
parent.insert(part3, index + 2);
}
}