String (@NotNull CharSequence text, int afterTagOffset) { if (afterTagOffset > text.length()) { return null; } int endOffset = afterTagOffset - 1; // Check empty element like <p/> if (endOffset > 0 && text.charAt(endOffset - 1) == SLASH) { return null; } for (int i = endOffset - 1; i >= 0; i--) { char c = text.charAt(i); switch (c) { case '\n' -> { return null; } case CLOSE_TAG_SYMBOL -> { return null; } case START_TAG_SYMBOL -> { if (text.charAt(i + 1) == SLASH) { // Handle situation like <tag></tag>[offset]. return null; } return text.subSequence(i + 1, endOffset).toString(); } // There is a possible case that opening tag has attributes, e.g. <a href='bla-bla-bla'>[offset]. We want to extract // only tag name then. case ' ', '\t' -> endOffset = i; } } return null; }