diff --git a/platform/platform-api/src/com/intellij/ui/dsl/builder/utils.kt b/platform/platform-api/src/com/intellij/ui/dsl/builder/utils.kt index 83ed5644742d..58c336fc42d8 100644 --- a/platform/platform-api/src/com/intellij/ui/dsl/builder/utils.kt +++ b/platform/platform-api/src/com/intellij/ui/dsl/builder/utils.kt @@ -4,6 +4,7 @@ package com.intellij.ui.dsl.builder import com.intellij.ide.BrowserUtil import com.intellij.ide.ui.UINumericRange import com.intellij.ui.SimpleListCellRenderer +import org.jetbrains.annotations.Nls import javax.swing.JComponent import javax.swing.JLabel import javax.swing.JList @@ -117,3 +118,19 @@ fun listCellRenderer(renderer: SimpleListCellRenderer.(T) -> Unit): Simpl } } } + +/** + * Kotlin UI DSL doesn't allow to use some tags like so all resource strings should be cleared up manually. Sometimes strings are + * received from outside, in such cases this method can be useful + * + * Example: `cleanupHtml("Some string")` returns `"Some string"` + */ +fun cleanupHtml(@Nls s: String): @Nls String { + val regex = Regex("\\s*(?.*)\\s*", RegexOption.IGNORE_CASE) + val result = regex.matchEntire(s) + if (result == null) { + return s + } + @Suppress("HardCodedStringLiteral") + return result.groups["body"]!!.value +} diff --git a/platform/platform-impl/src/com/intellij/diagnostic/PrivacyNotice.kt b/platform/platform-impl/src/com/intellij/diagnostic/PrivacyNotice.kt index f0a9f5ee09f3..e3a742d15df8 100644 --- a/platform/platform-impl/src/com/intellij/diagnostic/PrivacyNotice.kt +++ b/platform/platform-impl/src/com/intellij/diagnostic/PrivacyNotice.kt @@ -3,7 +3,9 @@ package com.intellij.diagnostic import com.intellij.openapi.util.NlsContexts import com.intellij.ui.dsl.builder.CollapsibleRow +import com.intellij.ui.dsl.builder.cleanupHtml import com.intellij.ui.dsl.builder.panel +import org.jetbrains.annotations.Nls import javax.swing.JEditorPane class PrivacyNotice(@NlsContexts.Label label: String, @NlsContexts.Label privacyPolicy: String) { @@ -18,7 +20,10 @@ class PrivacyNotice(@NlsContexts.Label label: String, @NlsContexts.Label privacy } var expanded: Boolean by collapsibleRow::expanded - var privacyPolicy: String by privacyPolicyPane::text + + fun setPrivacyPolicy(@Nls text: String) { + privacyPolicyPane.text = cleanupHtml(text) + } private lateinit var collapsibleRow: CollapsibleRow private lateinit var privacyPolicyPane: JEditorPane diff --git a/platform/platform-tests/testSrc/com/intellij/ui/dsl/builder/UtilsTest.kt b/platform/platform-tests/testSrc/com/intellij/ui/dsl/builder/UtilsTest.kt new file mode 100644 index 000000000000..f7de24e4937a --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/ui/dsl/builder/UtilsTest.kt @@ -0,0 +1,23 @@ +// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +package com.intellij.ui.dsl.builder + +import org.junit.Test +import kotlin.test.assertEquals + +class UtilsTest { + + @Test + fun testCleanupHtml() { + val testData = mapOf( + "Hello" to "Hello", + " Hello " to " Hello ", + "Hello" to "Hello", + " Hello " to " Hello ", + "Hello" to "Hello", + ) + + for ((original, expected) in testData) { + assertEquals(expected, cleanupHtml(original)) + } + } +}