mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-27 08:50:15 +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
|
||||
}
|
||||
|
||||
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 {
|
||||
//<editor-fold defaultstate="collapsed" desc="Generated Code">
|
||||
|
@ -22,4 +22,16 @@ internal class HtmlEscapeTest {
|
||||
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.data.*
|
||||
import net.mamoe.mirai.message.data.visitor.MessageVisitor
|
||||
import net.mamoe.mirai.utils.encodeHtmlEscape
|
||||
import net.mamoe.mirai.utils.safeCast
|
||||
|
||||
// internal runtime value, not serializable
|
||||
@ -152,9 +153,7 @@ internal data class ForwardMessageInternal(
|
||||
}
|
||||
}
|
||||
|
||||
private fun String.xmlEnc(): String {
|
||||
return this.replace("&", "&")
|
||||
}
|
||||
private fun String.xmlEnc(): String = encodeHtmlEscape()
|
||||
|
||||
internal fun RichMessage.Key.forwardMessage(
|
||||
resId: String,
|
||||
|
Loading…
Reference in New Issue
Block a user