From c1abebc8c90e6cce24df77f2c9ed9dd330180549 Mon Sep 17 00:00:00 2001 From: matthias Date: Wed, 8 Nov 2023 16:26:28 +0100 Subject: [PATCH] WT-12006: More robustly place the JS member functions When placing a JS member on a widget, its name and value are remembered. This is so that if the value is cleared or replaced, we do so for the same member function. There, however, exists a special case for member function where if a single character is assigned to it, it is replaced by an empty value. This serves to function such that setting the function to `0` for example, will result in it being empty. When we appended the semicolon to it, this caused that last piece of logic to be ignored, and would generate incorrect JS, which called a function `0;`. --- src/Wt/WWebWidget.C | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Wt/WWebWidget.C b/src/Wt/WWebWidget.C index 5639ad08a..81fbb846b 100644 --- a/src/Wt/WWebWidget.C +++ b/src/Wt/WWebWidget.C @@ -902,7 +902,16 @@ void WWebWidget::setJavaScriptMember(const std::string& name, std::vector& members = *otherImpl_->jsMembers_; int index = indexOfJavaScriptMember(name); - if (index != -1 && (members[index].value == value)) + // Bug #12006: For safety always append semicolon + std::string terminatedValue = value; + // Do not escape the value if it is a "default" placeholder. + // A value of "0" (or any value of length 1) is converted to an empty + // string in setImplementLayoutSizeAware. + if (!terminatedValue.empty() && terminatedValue.back() != ';' && terminatedValue != "0") { + terminatedValue += ";"; + } + + if (index != -1 && (members[index].value == terminatedValue)) return; if (value.empty()) { @@ -914,14 +923,10 @@ void WWebWidget::setJavaScriptMember(const std::string& name, if (index == -1) { OtherImpl::Member m; m.name = name; - m.value = value; - // Bug #12006: For safety always append semicolon - if (value.back() != ';') { - m.value += ";"; - } + m.value = terminatedValue; members.push_back(m); } else { - members[index].value = value; + members[index].value = terminatedValue; } }