From a644da8b921e495ce050aefcb5c3c0a79aab1948 Mon Sep 17 00:00:00 2001 From: Pavel Porvatov Date: Tue, 31 Jan 2023 16:23:28 +0100 Subject: [PATCH] Fixed exception `Invalid html: tag inserted automatically and shouldn't be used, text: I agree to ...` Stacktrace: at com.intellij.ui.dsl.builder.components.DslLabel.setText(DslLabel.kt:122) at com.intellij.diagnostic.PrivacyNotice$privacyPolicy$2.set(PrivacyNotice.kt:21) at com.intellij.diagnostic.PrivacyNotice.setPrivacyPolicy(PrivacyNotice.kt:21) GitOrigin-RevId: ebb44f6abb6234ca8ec9cb77ef05a29ee6252574 --- .../src/com/intellij/ui/dsl/builder/utils.kt | 17 ++++++++++++++ .../com/intellij/diagnostic/PrivacyNotice.kt | 7 +++++- .../com/intellij/ui/dsl/builder/UtilsTest.kt | 23 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 platform/platform-tests/testSrc/com/intellij/ui/dsl/builder/UtilsTest.kt 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)) + } + } +}