mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-28 01:10:17 +08:00
[core] Encode html entities for forward message; fix #2241
This commit is contained in:
parent
c52e99dd87
commit
c77ed79b12
@ -31,6 +31,23 @@ public fun String.decodeHtmlEscape(): String = replace(STR_TO_CHAR_PATTERN) { ma
|
|||||||
match.value
|
match.value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public fun String.encodeHtmlEscape(): String = buildString(length) {
|
||||||
|
this@encodeHtmlEscape.forEach { c ->
|
||||||
|
if (needDoHtmlEscape(c)) {
|
||||||
|
append("&#").append(c.code).append(';')
|
||||||
|
} else {
|
||||||
|
append(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun needDoHtmlEscape(c: Char): Boolean {
|
||||||
|
if (c.code < 32) return true // Ascii control codes
|
||||||
|
|
||||||
|
if (c in "#@!~$%^&*()<>/\\\"'") return true
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private val STR_TO_CHAR_MAPPINGS: Map<String, String> by lazy {
|
private val STR_TO_CHAR_MAPPINGS: Map<String, String> by lazy {
|
||||||
//<editor-fold defaultstate="collapsed" desc="Generated Code">
|
//<editor-fold defaultstate="collapsed" desc="Generated Code">
|
||||||
|
@ -22,4 +22,16 @@ internal class HtmlEscapeTest {
|
|||||||
ALL.decodeHtmlEscape()
|
ALL.decodeHtmlEscape()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testEncode() {
|
||||||
|
val str = buildString {
|
||||||
|
for (i in 1 until 2048) {
|
||||||
|
append(i.toChar())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val escaped = str.encodeHtmlEscape()
|
||||||
|
// println(escaped)
|
||||||
|
assertEquals(escaped.decodeHtmlEscape(), str)
|
||||||
|
}
|
||||||
}
|
}
|
@ -21,6 +21,7 @@ import net.mamoe.mirai.internal.network.protocol.data.proto.MsgTransmit
|
|||||||
import net.mamoe.mirai.message.MessageReceipt
|
import net.mamoe.mirai.message.MessageReceipt
|
||||||
import net.mamoe.mirai.message.data.*
|
import net.mamoe.mirai.message.data.*
|
||||||
import net.mamoe.mirai.message.data.visitor.MessageVisitor
|
import net.mamoe.mirai.message.data.visitor.MessageVisitor
|
||||||
|
import net.mamoe.mirai.utils.encodeHtmlEscape
|
||||||
import net.mamoe.mirai.utils.safeCast
|
import net.mamoe.mirai.utils.safeCast
|
||||||
|
|
||||||
// internal runtime value, not serializable
|
// internal runtime value, not serializable
|
||||||
@ -152,9 +153,7 @@ internal data class ForwardMessageInternal(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun String.xmlEnc(): String {
|
private fun String.xmlEnc(): String = encodeHtmlEscape()
|
||||||
return this.replace("&", "&")
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun RichMessage.Key.forwardMessage(
|
internal fun RichMessage.Key.forwardMessage(
|
||||||
resId: String,
|
resId: String,
|
||||||
|
Loading…
Reference in New Issue
Block a user