diff options
Diffstat (limited to 'plugins/base/base-test-utils')
-rw-r--r-- | plugins/base/base-test-utils/api/base-test-utils.api | 3 | ||||
-rw-r--r-- | plugins/base/base-test-utils/src/main/kotlin/renderers/JsoupUtils.kt | 36 |
2 files changed, 32 insertions, 7 deletions
diff --git a/plugins/base/base-test-utils/api/base-test-utils.api b/plugins/base/base-test-utils/api/base-test-utils.api index b5a9ef2f..844a1703 100644 --- a/plugins/base/base-test-utils/api/base-test-utils.api +++ b/plugins/base/base-test-utils/api/base-test-utils.api @@ -113,7 +113,8 @@ public final class utils/I : utils/Tag { } public final class utils/JsoupUtilsKt { - public static final fun match (Lorg/jsoup/nodes/Element;[Ljava/lang/Object;)V + public static final fun match (Lorg/jsoup/nodes/Element;[Ljava/lang/Object;Z)V + public static synthetic fun match$default (Lorg/jsoup/nodes/Element;[Ljava/lang/Object;ZILjava/lang/Object;)V } public final class utils/P : utils/Tag { diff --git a/plugins/base/base-test-utils/src/main/kotlin/renderers/JsoupUtils.kt b/plugins/base/base-test-utils/src/main/kotlin/renderers/JsoupUtils.kt index ea2d13a7..9e38df10 100644 --- a/plugins/base/base-test-utils/src/main/kotlin/renderers/JsoupUtils.kt +++ b/plugins/base/base-test-utils/src/main/kotlin/renderers/JsoupUtils.kt @@ -4,12 +4,22 @@ import org.jsoup.nodes.Element import org.jsoup.nodes.Node import org.jsoup.nodes.TextNode -fun Element.match(vararg matchers: Any): Unit = +fun Element.match(vararg matchers: Any, ignoreSpanWithTokenStyle:Boolean = false): Unit = childNodes() - .filter { it !is TextNode || it.text().isNotBlank() } + .let { list -> + if(ignoreSpanWithTokenStyle) { + list + .filterNot { it is Element && it.tagName() == "span" && it.attr("class").startsWith("token ") && it.childNodeSize() == 0} + .map { if(it is Element && it.tagName() == "span" + && it.attr("class").startsWith("token ") + && it.childNodeSize() == 1) it.childNode(0) else it } + .uniteConsecutiveTextNodes() + } else list + } + .filter { (it !is TextNode || it.text().isNotBlank())} .let { it.drop(it.size - matchers.size) } .zip(matchers) - .forEach { (n, m) -> m.accepts(n) } + .forEach { (n, m) -> m.accepts(n, ignoreSpan = ignoreSpanWithTokenStyle) } open class Tag(val name: String, vararg val matchers: Any) class Div(vararg matchers: Any) : Tag("div", *matchers) @@ -20,13 +30,27 @@ class B(vararg matchers: Any) : Tag("b", *matchers) class I(vararg matchers: Any) : Tag("i", *matchers) class STRIKE(vararg matchers: Any) : Tag("strike", *matchers) object Wbr : Tag("wbr") -private fun Any.accepts(n: Node) { +private fun Any.accepts(n: Node, ignoreSpan:Boolean = true) { when (this) { is String -> assert(n is TextNode && n.text().trim() == this.trim()) { "\"$this\" expected but found: $n" } is Tag -> { assert(n is Element && n.tagName() == name) { "Tag $name expected but found: $n" } - if (n is Element && matchers.isNotEmpty()) n.match(*matchers) + if (n is Element && matchers.isNotEmpty()) n.match(*matchers, ignoreSpanWithTokenStyle = ignoreSpan) } else -> throw IllegalArgumentException("$this is not proper matcher") } -}
\ No newline at end of file +} +private fun List<Node>.uniteConsecutiveTextNodes(): MutableList<Node> { + val resList = mutableListOf<Node>() + var acc = StringBuilder() + forEachIndexed { index, item -> + if (item is TextNode) { + acc.append(item.text()) + if (!(index + 1 < size && this[index + 1] is TextNode)) { + resList.add(TextNode(acc.toString())) + acc = StringBuilder() + } + } else resList.add(item) + } + return resList + }
\ No newline at end of file |