/** * Get the spacer for the given paragraph. * Lazily creates & registers one if not present. * If there's one that the browser created, registers it as our own. * If the browser put a different one in to the one that we were already * using, replace ours with the browser's. * @param paragraph * @return The spacer */ protected BRElement getSpacer(Element paragraph) { Node last = paragraph.getLastChild(); BRElement spacer = paragraph.getPropertyJSO(BR_REF).cast(); if (spacer == null) { // Register our spacer, using one the browser put in if present spacer = isSpacer(last) ? last.<BRElement>cast() : Document.get().createBRElement(); setupSpacer(paragraph, spacer); } else if (isSpacer(last) && last != spacer) { // The browser put a different one in by itself, so let's use that one if (spacer.hasParentElement()) { spacer.removeFromParent(); } spacer = last.<BRElement>cast(); setupSpacer(paragraph, spacer); } return spacer; }
private void ensureSpacerIfDoesntEndWithText(Element paragraph) { Node last = paragraph.getLastChild(); BRElement spacer = getSpacer(paragraph); if (last == spacer) { last = last.getPreviousSibling(); } if (last == null || DomHelper.isElement(last)) { paragraph.appendChild(spacer); } else { spacer.removeFromParent(); } }
/** * @param n Node to test, or null * @return true if n is a spacer <br/>, WHETHER OR NOT we created it * or the browser's native editor created it */ protected boolean isSpacer(Node n) { if (n == null) { return false; } if (DomHelper.isTextNode(n)) { return false; } Element e = n.cast(); return e.getTagName().equalsIgnoreCase(BRElement.TAG) && !NodeManager.hasBackReference(e); }
/** * {@inheritDoc} */ @Override public void onRemovingChild(Node child, Element paragraph) { Node first = paragraph.getFirstChild(); BRElement spacer = getSpacer(paragraph); if (first == null) { appendSpacer(paragraph); } else if (first != spacer) { spacer.removeFromParent(); } }
/** * Converts the given {@code HTML} into robot compatible plaintext. * * @param html the {@code HTML} to convert. * @return a plain text version of the given {@code HTML}. */ private static String convertToPlainText(String html) { StringBuffer result = new StringBuffer(); Matcher matcher = MARKUP_PATTERN.matcher(html); while (matcher.find()) { String replacement = ""; String tag = matcher.group().substring(1, matcher.group().length() - 1).split(" ")[0]; if (ParagraphElement.TAG.equalsIgnoreCase(tag) || BRElement.TAG.equalsIgnoreCase(tag)) { replacement = "\n"; } matcher.appendReplacement(result, replacement); } matcher.appendTail(result); return result.toString(); }
/** * Converts the given {@code HTML} into robot compatible plain text. * * @param html the text to convert. * @return a plain text version of the given html text. */ private static String convertToPlainText(String html) { StringBuffer result = new StringBuffer(); Matcher matcher = MARKUP_PATTERN.matcher(html); while (matcher.find()) { String replacement = ""; String tag = matcher.group().substring(1, matcher.group().length() - 1).split(" ")[0]; if (ParagraphElement.TAG.equalsIgnoreCase(tag) || BRElement.TAG.equalsIgnoreCase(tag)) { replacement = "\n"; } matcher.appendReplacement(result, replacement); } matcher.appendTail(result); return result.toString(); }
/** * Setup the given br element as the paragraph's spacer * @param paragraph * @param spacer */ private void setupSpacer(Element paragraph, BRElement spacer) { NodeManager.setTransparency(spacer, Skip.DEEP); paragraph.setPropertyJSO(BR_REF, spacer); }