From 12752f0b280802c7a9334f955021df7cb17e1343 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 17 Dec 2025 09:54:27 +0000 Subject: [PATCH] Remove stale theme entries in CSSTheme.updateResources When updating theme resources from CSS, if a property is removed from the CSS, it should be removed from the generated resource file. Previously, removed properties would persist as stale entries. This change introduces a cleanup step in `updateResources` that iterates over modified UIIDs and removes all existing theme properties associated with them before applying the new properties from the CSS. A helper method `isOwnedBy` ensures that only properties strictly belonging to the UIID (and its states) are removed, preventing accidental deletion of unrelated properties even if UIIDs contain dots. --- .../com/codename1/designer/css/CSSTheme.java | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/CodenameOneDesigner/src/com/codename1/designer/css/CSSTheme.java b/CodenameOneDesigner/src/com/codename1/designer/css/CSSTheme.java index 190dfeaad9..bb43da6cbc 100644 --- a/CodenameOneDesigner/src/com/codename1/designer/css/CSSTheme.java +++ b/CodenameOneDesigner/src/com/codename1/designer/css/CSSTheme.java @@ -1667,9 +1667,42 @@ public Map calculateSelectorCacheStatus(File cachedFile) th } + private boolean isOwnedBy(String key, String id) { + if (key.length() <= id.length() + 1) { + return false; + } + if (!key.startsWith(id)) { + return false; + } + char sep = key.charAt(id.length()); + if (sep != '.' && sep != '#') { + return false; + } + // Check for dots in the suffix + return key.indexOf('.', id.length() + 1) == -1; + } + public void updateResources() { - // TODO: We need to remove stale theme entries - // https://github.com/codenameone/CodenameOne/issues/2698 + if (res != null) { + Map themeData = res.getTheme(themeName); + if (themeData != null) { + Set keys = new HashSet(themeData.keySet()); + Set modifiedIds = new HashSet(); + for (String id : elements.keySet()) { + if (isModified(id)) { + modifiedIds.add(id); + } + } + + for (String id : modifiedIds) { + for (String key : keys) { + if (isOwnedBy(key, id)) { + res.setThemeProperty(themeName, key, null); + } + } + } + } + } for (String id : elements.keySet()) { if (!isModified(id)) {