Skip to content

Commit

Permalink
Throw error on missing selector
Browse files Browse the repository at this point in the history
  • Loading branch information
burnoo committed Sep 4, 2024
1 parent e74092e commit 7f40b95
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
19 changes: 8 additions & 11 deletions kspoon/src/commonMain/kotlin/decoder/HtmlTag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ internal sealed class HtmlTag {
data class Index(val index: Int) : HtmlTag()
}

internal fun createSelectorHtmlTag(
selector: String,
selectorAnnotation: Selector?,
) = HtmlTag.Selector(
selector = selector,
textMode = when (selectorAnnotation?.textMode) {
internal fun Selector.toHtmlTag() = HtmlTag.Selector(
selector = value,
textMode = when (textMode) {
SelectorHtmlTextMode.Text -> HtmlTextMode.Text
SelectorHtmlTextMode.InnerHtml -> HtmlTextMode.InnerHtml
SelectorHtmlTextMode.OuterHtml -> HtmlTextMode.OuterHtml
SelectorHtmlTextMode.Data -> HtmlTextMode.Data
SelectorHtmlTextMode.Default, null -> null
SelectorHtmlTextMode.Default -> null
},
attribute = selectorAnnotation?.attr?.handleNullability(),
index = selectorAnnotation?.index ?: 0,
defaultValue = selectorAnnotation?.defValue?.handleNullability(),
regex = selectorAnnotation?.regex?.handleNullability()?.toRegex(),
attribute = attr.handleNullability(),
index = index,
defaultValue = defValue.handleNullability(),
regex = regex.handleNullability()?.toRegex(),
)
27 changes: 19 additions & 8 deletions kspoon/src/commonMain/kotlin/decoder/HtmlTreeDecoder.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:OptIn(ExperimentalSerializationApi::class, InternalSerializationApi::class)

package dev.burnoo.ksoup.decoder

import com.fleeksoft.ksoup.nodes.Comment
Expand All @@ -16,6 +14,7 @@ import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.descriptors.StructureKind
import kotlinx.serialization.descriptors.capturedKClass
import kotlinx.serialization.descriptors.elementNames
import kotlinx.serialization.encoding.CompositeDecoder
import kotlinx.serialization.internal.TaggedDecoder
Expand All @@ -26,6 +25,7 @@ import kotlinx.serialization.modules.overwriteWith
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract

@OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class)
internal class HtmlTreeDecoder internal constructor(
private val elements: Elements,
private val configuration: KspoonConfiguration,
Expand All @@ -43,11 +43,17 @@ internal class HtmlTreeDecoder internal constructor(
private var elementIndex = 0

override fun SerialDescriptor.getTag(index: Int): HtmlTag {
val selectorAnnotation = getSelectorAnnotations(index)
val selector = selectorAnnotation?.value ?: getElementName(index)
return when (val newIndex = selector.toIntOrNull()) {
null -> createSelectorHtmlTag(selector, selectorAnnotation)
else -> HtmlTag.Index(newIndex)
val selectorAnnotation = getElementSelectorAnnotation(index)
val newIndex = if (selectorAnnotation == null) {
getElementName(index).toIntOrNull()
} else {
null
}
return when {
selectorAnnotation != null -> selectorAnnotation.toHtmlTag()
newIndex != null -> HtmlTag.Index(newIndex)
isElementADocument(index) -> Selector(":root").toHtmlTag()
else -> error("Selector annotation not added for ${getElementDescriptor(index).serialName}")
}
}

Expand Down Expand Up @@ -161,10 +167,15 @@ internal class HtmlTreeDecoder internal constructor(
return if (matchResult.groupValues.size > 1) matchResult.groupValues[1] else matchResult.value
}

private fun SerialDescriptor.getSelectorAnnotations(index: Int): Selector? {
private fun SerialDescriptor.getElementSelectorAnnotation(index: Int): Selector? {
return getElementAnnotations(index).filterIsInstance<Selector>().firstOrNull()
}

private fun SerialDescriptor.isElementADocument(index: Int): Boolean {
val elementDescriptor = getElementDescriptor(index)
return elementDescriptor.serialName == "com.fleeksoft.ksoup.nodes.Document" || elementDescriptor.capturedKClass == Document::class
}

private fun Elements.getAtAsElements(index: Int) = getOrNull(index)?.let(::Elements) ?: Elements()

inner class SerializerDecoder {
Expand Down

0 comments on commit 7f40b95

Please sign in to comment.