Skip to content

Commit

Permalink
Merge branch 'refs/heads/master' into a_attributes
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/commonMain/kotlin/generated/gen-enums.kt
  • Loading branch information
severn-everett committed May 15, 2024
2 parents daf4608 + 83e61ef commit 63d7e27
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 20 deletions.
5 changes: 3 additions & 2 deletions buildSrc/src/main/kotlin/kotlinx/html/generate/attributes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ fun Appendable.facade(repository: Repository, facade: AttributeFacade) {
fun Appendable.eventProperty(parent: String, attribute: AttributeInfo, shouldUnsafeCast: Boolean) {
val type = "(org.w3c.dom.events.Event) -> Unit"
variable(
receiver = parent, variable = Var(
receiver = parent,
variable = Var(
name = attribute.fieldName + "Function",
type = type,
mutable = true
varType = VarType.MUTABLE,
)
)
emptyLine()
Expand Down
15 changes: 13 additions & 2 deletions buildSrc/src/main/kotlin/kotlinx/html/generate/codegen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ fun Appendable.const(value: Const<*>) {
append(value.asValue)
}

enum class VarType {
MUTABLE,
IMMUTABLE,
CONST,
}

data class Var(
val name: String,
val type: String,
val mutable: Boolean = false,
val varType: VarType = VarType.IMMUTABLE,
val override: Boolean = false,
val forceOmitValVar: Boolean = false,
val defaultValue: String = "",
Expand All @@ -72,7 +78,12 @@ fun Appendable.variable(variable: Var, omitValVar: Boolean = false, receiver: St
if (variable.override) {
append("override ")
}
append(if (variable.mutable) "var " else "val ")
val typeString = when (variable.varType) {
VarType.MUTABLE -> "var "
VarType.IMMUTABLE -> "val "
VarType.CONST -> "const val "
}
append(typeString)
}

if (receiver.isNotEmpty()) {
Expand Down
30 changes: 21 additions & 9 deletions buildSrc/src/main/kotlin/kotlinx/html/generate/enums.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package kotlinx.html.generate

import java.util.*

val reservedNames = setOf("class", "val", "var", "object", "true", "false", "as", "is", "for")

fun String.replaceIfReserved() = if (this in reservedNames) "html" + this.capitalize() else this
Expand All @@ -12,18 +10,25 @@ fun List<String>.toAttributeValues() : List<AttributeEnumValue> =
fun Appendable.enumObject(attribute : AttributeInfo) {
val name = attribute.enumTypeName

appendLine("@Suppress(\"unused\")")
appendLine("@Suppress(\"unused\", \"ConstPropertyName\")")
clazz(Clazz(name, isObject = true)) {
attribute.enumValues.forEach {
append(" ")
variable(Var(it.fieldName, "String", false, defaultValue = "\"${it.realName}\""))
variable(Var(it.fieldName, "String", varType = VarType.CONST, defaultValue = "\"${it.realName}\""))
emptyLine()
}

emptyLine()
append(" ")
// append("private ")
variable(Var("values", "List<String>", defaultValue = attribute.enumValues.map {"\"${it.fieldName}\""}.joinToString(", ", "listOf(", ")")))
variable(
Var(
name = "values",
type = "List<String>",
defaultValue = attribute
.enumValues
.joinToString(", ", "listOf(", ")") { "\"${it.fieldName}\"" },
)
)
emptyLine()
}

Expand All @@ -32,9 +37,9 @@ fun Appendable.enumObject(attribute : AttributeInfo) {

fun Appendable.enum(attribute : AttributeInfo) {
val name = attribute.enumTypeName
val realValue = Var("realValue", "String", false, true)
val realValue = Var(name = "realValue", type = "String", varType = VarType.IMMUTABLE, override = true)

appendLine("@Suppress(\"unused\")")
appendLine("@Suppress(\"unused\", \"EnumEntryName\")")
append("enum ")
clazz(Clazz(name, variables = listOf(realValue), parents = listOf("AttributeEnum"))) {
attribute.enumValues.forEachIndexed { idx, it ->
Expand All @@ -53,6 +58,13 @@ fun Appendable.enum(attribute : AttributeInfo) {

emptyLine()
append("internal ")
variable(Var(name.decapitalize() + "Values", "Map<String, $name>", false, defaultValue = "$name.values().associateBy { it.realValue }"))
variable(
Var(
name = name.decapitalize() + "Values",
type = "Map<String, $name>",
varType = VarType.IMMUTABLE,
defaultValue = "$name.entries.associateBy { it.realValue }",
),
)
emptyLine()
}
6 changes: 4 additions & 2 deletions buildSrc/src/main/kotlin/kotlinx/html/generate/rules.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,10 @@ fun isEnumExcluded(name: String) = excludedEnums.any { it.containsMatchIn(name)

val contentlessTags = setOf("html", "head", "script", "style")

val deprecated = listOf(".*FormMethod#(put|patch|delete)" to "method is not allowed in browsers")
.map { it.first.toRegex(RegexOption.IGNORE_CASE) to it.second }
val deprecated = listOf(
".*FormMethod#(put|patch|delete)" to "method is not allowed in browsers",
".*TextAreaWrap#(virtual|physical|off)" to "value only supported in IE"
).map { it.first.toRegex(RegexOption.IGNORE_CASE) to it.second }

fun findEnumDeprecation(attribute: AttributeInfo, value: AttributeEnumValue): String? {
return deprecated.firstOrNull { p -> p.first.matches("""${attribute.enumTypeName}#${value.realName}""") }?.second
Expand Down
13 changes: 8 additions & 5 deletions buildSrc/src/main/kotlin/kotlinx/html/generate/tagsgen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ fun Appendable.tagClass(repository: Repository, tag: TagInfo, excludeAttributes:
Var(
name = "initialAttributes",
type = "Map<String, String>",
mutable = false,
varType = VarType.IMMUTABLE,
override = false,
forceOmitValVar = true
)
)
add(Var(name = "consumer", type = "TagConsumer<*>", mutable = false, override = true))
add(Var(name = "consumer", type = "TagConsumer<*>", varType = VarType.IMMUTABLE, override = true))
if (customizableNamespace) {
add(
Var(
name = "namespace",
type = "String?",
mutable = false,
varType = VarType.IMMUTABLE,
override = false,
forceOmitValVar = true,
defaultValue = namespace?.quote() ?: "null"
Expand Down Expand Up @@ -197,7 +197,10 @@ internal fun Appendable.tagAttributeVar(
repository.attributeDelegateRequests.add(attributeRequest)

indent(indent)
variable(Var(attribute.fieldName, attribute.typeName, true), receiver = receiver ?: "")
variable(
Var(name = attribute.fieldName, type = attribute.typeName, varType = VarType.MUTABLE),
receiver = receiver ?: "",
)
return attributeRequest
}

Expand Down Expand Up @@ -524,7 +527,7 @@ private fun tagBuilderFunctionArguments(tag: TagInfo, blockOrContent: Boolean):
Var(
name = "namespace",
type = "String?",
mutable = false,
varType = VarType.IMMUTABLE,
override = false,
forceOmitValVar = true,
defaultValue = defaultNamespace
Expand Down
3 changes: 3 additions & 0 deletions buildSrc/src/main/resources/html_5.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,9 @@
<xsd:restriction base="xsd:NMTOKEN">
<xsd:enumeration value="hard"/>
<xsd:enumeration value="soft"/>
<xsd:enumeration value="virtual"/>
<xsd:enumeration value="physical"/>
<xsd:enumeration value="off"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
Expand Down
30 changes: 30 additions & 0 deletions src/commonMain/kotlin/generated/gen-tag-unions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1108,10 +1108,40 @@ inline fun FlowOrInteractiveOrPhrasingContent.softTextArea(rows : String? = null
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
TEXTAREA(attributesMapOf("rows", rows,"cols", cols,"wrap", TextAreaWrap.soft.realValue,"class", classes), consumer).visit(block)
}
@Suppress("DEPRECATION")
@HtmlTagMarker
@OptIn(ExperimentalContracts::class)
inline fun FlowOrInteractiveOrPhrasingContent.virtualTextArea(rows : String? = null, cols : String? = null, classes : String? = null, crossinline block : TEXTAREA.() -> Unit = {}) : Unit {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
TEXTAREA(attributesMapOf("rows", rows,"cols", cols,"wrap", TextAreaWrap.virtual.realValue,"class", classes), consumer).visit(block)
}
@Suppress("DEPRECATION")
@HtmlTagMarker
@OptIn(ExperimentalContracts::class)
inline fun FlowOrInteractiveOrPhrasingContent.physicalTextArea(rows : String? = null, cols : String? = null, classes : String? = null, crossinline block : TEXTAREA.() -> Unit = {}) : Unit {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
TEXTAREA(attributesMapOf("rows", rows,"cols", cols,"wrap", TextAreaWrap.physical.realValue,"class", classes), consumer).visit(block)
}
@Suppress("DEPRECATION")
@HtmlTagMarker
@OptIn(ExperimentalContracts::class)
inline fun FlowOrInteractiveOrPhrasingContent.offTextArea(rows : String? = null, cols : String? = null, classes : String? = null, crossinline block : TEXTAREA.() -> Unit = {}) : Unit {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
TEXTAREA(attributesMapOf("rows", rows,"cols", cols,"wrap", TextAreaWrap.off.realValue,"class", classes), consumer).visit(block)
}
@HtmlTagMarker
fun FlowOrInteractiveOrPhrasingContent.hardTextArea(rows : String? = null, cols : String? = null, classes : String? = null, content : String = "") : Unit = TEXTAREA(attributesMapOf("rows", rows,"cols", cols,"wrap", TextAreaWrap.hard.realValue,"class", classes), consumer).visit({+content})
@HtmlTagMarker
fun FlowOrInteractiveOrPhrasingContent.softTextArea(rows : String? = null, cols : String? = null, classes : String? = null, content : String = "") : Unit = TEXTAREA(attributesMapOf("rows", rows,"cols", cols,"wrap", TextAreaWrap.soft.realValue,"class", classes), consumer).visit({+content})
@Suppress("DEPRECATION")
@HtmlTagMarker
fun FlowOrInteractiveOrPhrasingContent.virtualTextArea(rows : String? = null, cols : String? = null, classes : String? = null, content : String = "") : Unit = TEXTAREA(attributesMapOf("rows", rows,"cols", cols,"wrap", TextAreaWrap.virtual.realValue,"class", classes), consumer).visit({+content})
@Suppress("DEPRECATION")
@HtmlTagMarker
fun FlowOrInteractiveOrPhrasingContent.physicalTextArea(rows : String? = null, cols : String? = null, classes : String? = null, content : String = "") : Unit = TEXTAREA(attributesMapOf("rows", rows,"cols", cols,"wrap", TextAreaWrap.physical.realValue,"class", classes), consumer).visit({+content})
@Suppress("DEPRECATION")
@HtmlTagMarker
fun FlowOrInteractiveOrPhrasingContent.offTextArea(rows : String? = null, cols : String? = null, classes : String? = null, content : String = "") : Unit = TEXTAREA(attributesMapOf("rows", rows,"cols", cols,"wrap", TextAreaWrap.off.realValue,"class", classes), consumer).visit({+content})

/**
* Video player
Expand Down

0 comments on commit 63d7e27

Please sign in to comment.