TextRange (final XmlText xmlText) { final int offset = xmlText.getStartOffsetInParent(); final String value = xmlText.getValue(); final String trimmed = value.trim(); final int i = value.indexOf(trimmed); final int start = xmlText.displayToPhysical(i) + offset; return trimmed.isEmpty() ? new TextRange(start, start) : new TextRange(start, xmlText.displayToPhysical(i + trimmed.length() - 1) + offset + 1); }