From 724f9fa491638ae7fcfed07eefa376bacd34d29f Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Thu, 30 Apr 2020 15:48:01 -0300 Subject: [wallet] accept but strip markup in ToS markdown headings --- .../java/net/taler/wallet/withdraw/TosSection.kt | 26 +++++++++++++++------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'wallet/src') diff --git a/wallet/src/main/java/net/taler/wallet/withdraw/TosSection.kt b/wallet/src/main/java/net/taler/wallet/withdraw/TosSection.kt index 72a9e34..b27de42 100644 --- a/wallet/src/main/java/net/taler/wallet/withdraw/TosSection.kt +++ b/wallet/src/main/java/net/taler/wallet/withdraw/TosSection.kt @@ -17,6 +17,7 @@ package net.taler.wallet.withdraw import io.noties.markwon.Markwon +import org.commonmark.node.Code import org.commonmark.node.Document import org.commonmark.node.Heading import org.commonmark.node.Node @@ -44,14 +45,9 @@ internal fun parseTos(markwon: Markwon, text: String): List { sections.add(TosSection(lastHeading, section)) section = Document() } - // check that this is a plain heading - if (node.firstChild !is Text || node.firstChild.next != null) { - throw ParseException( - "Primary heading includes more than just text", sections.size - ) - } - // start new section - lastHeading = (node.firstChild as Text).literal + // start new section with new heading (stripped of markup) + lastHeading = getNodeText(node) + if (lastHeading.isBlank()) throw ParseException("Empty heading", 0) } else if (lastHeading == null) { throw ParseException("Found text before first primary heading", 0) } else { @@ -63,3 +59,17 @@ internal fun parseTos(markwon: Markwon, text: String): List { sections.add(TosSection(lastHeading, section)) return sections } + +private fun getNodeText(rootNode: Node): String { + var node: Node? = rootNode.firstChild + var text = "" + while (node != null) { + text += when (node) { + is Text -> node.literal + is Code -> node.literal + else -> getNodeText(node) + } + node = node.next + } + return text +} -- cgit v1.2.3