*/
private PointRange<Node> hijackFragment(String xmlInRange, String annotations,
PointRange<Node> origRange) {
Point<Node> origStart = origRange.getFirst();
Point<Node> origEnd = origRange.getSecond();
SpanElement spanForXml = Document.get().createSpanElement();
spanForXml.setAttribute(WAVE_XML_ATTRIBUTE, xmlInRange);
spanForXml.setAttribute(WAVE_ANNOTATIONS_ATTRIBUTE, annotations);
spanForXml.setClassName(MAGIC_CLASSNAME);
LOG.trace().log("original point: " + origStart);
// NOTE(user): An extra span is required at the end for Safari, otherwise
// the span with the metadata may get discarded.
SpanElement trailingSpan = Document.get().createSpanElement();
trailingSpan.setInnerHTML(" ");
if (origEnd.isInTextNode()) {
Text t = (Text) origEnd.getContainer();
t.setData(t.getData().substring(0, origEnd.getTextOffset()));
origEnd.getContainer().getParentElement().insertAfter(spanForXml, t);
origEnd.getContainer().getParentElement().insertAfter(trailingSpan, spanForXml);
} else {
origEnd.getContainer().insertAfter(spanForXml, origEnd.getNodeAfter());
origEnd.getContainer().insertAfter(trailingSpan, spanForXml);
}
Point<Node> newEnd =
Point.<Node> inElement(spanForXml.getParentElement(), trailingSpan.getNextSibling());
LOG.trace().log("new point: " + newEnd);
LOG.trace().logPlainText("parent: " + spanForXml.getParentElement().getInnerHTML());
assert newEnd.getNodeAfter() == null
|| newEnd.getNodeAfter().getParentElement() == newEnd.getContainer() : "inconsistent point";
return new PointRange<Node>(origStart, newEnd);