Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strips HTML tags from code completion list #3877

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.client.ui.syntax;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class HTMLTagRemover {
/* The following expression relies on the non-greediness of the replaceAll function */
private static final String HTML_TAG_REGEX = "<[^>]*>";

private static final ConcurrentMap<String, String> htmlCache = new ConcurrentHashMap<>();

String remove(String htmlString) {
if (htmlString == null) {
return null;
}
return htmlCache.computeIfAbsent(htmlString, s -> s.replaceAll(HTML_TAG_REGEX, ""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class MapToolScriptAutoComplete {
private static final String I18N_SHORT_DESCRIPTION = ".description";
private static final String I18N_SUMMARY = ".summary";
private static final String I18N_PLACEHOLDER_TEXT = "TBD";
private static final HTMLTagRemover htmlTagRemover = new HTMLTagRemover();

DefaultCompletionProvider provider =
new DefaultCompletionProvider() {
Expand All @@ -50,7 +51,11 @@ protected boolean isValidChar(char ch) {
public MapToolScriptAutoComplete() {
for (String macro : MapTool.getParser().listAllMacroFunctions().keySet())
provider.addCompletion(
new BasicCompletion(provider, macro, getShortDescription(macro), getSummary(macro)));
new BasicCompletion(
provider,
macro,
htmlTagRemover.remove(getShortDescription(macro)),
getSummary(macro)));

// Add UDFs
UserDefinedMacroFunctions udfManager = UserDefinedMacroFunctions.getInstance();
Expand All @@ -62,27 +67,36 @@ public MapToolScriptAutoComplete() {
new BasicCompletion(
provider,
udf,
udfManager.getFunctionDescription(udf),
htmlTagRemover.remove(udfManager.getFunctionDescription(udf)),
(StringUtils.isBlank(udfSummary)) ? null : udfSummary));
}

// Add "Special Variables" as Data Type
for (String dataType : MapToolScriptSyntax.DATA_TYPES)
provider.addCompletion(
new BasicCompletion(
provider, dataType, getShortDescription(dataType), getSummary(dataType)));
provider,
dataType,
htmlTagRemover.remove(getShortDescription(dataType)),
getSummary(dataType)));

// Add "Roll Options" as Reserved word
for (String reservedWord : MapToolScriptSyntax.RESERVED_WORDS)
provider.addCompletion(
new BasicCompletion(
provider, reservedWord, getShortDescription(reservedWord), getSummary(reservedWord)));
provider,
reservedWord,
htmlTagRemover.remove(getShortDescription(reservedWord)),
getSummary(reservedWord)));

// Add "Events" as Reserved Word 2
for (String reservedWord : MapToolScriptSyntax.RESERVED_WORDS_2)
provider.addCompletion(
new BasicCompletion(
provider, reservedWord, getShortDescription(reservedWord), getSummary(reservedWord)));
provider,
reservedWord,
htmlTagRemover.remove(getShortDescription(reservedWord)),
getSummary(reservedWord)));

for (Function function : MapToolExpressionParser.getMacroFunctions()) {
if (function instanceof DefinesSpecialVariables) {
Expand All @@ -91,7 +105,7 @@ public MapToolScriptAutoComplete() {
new BasicCompletion(
provider,
specialVariable,
getShortDescription(specialVariable),
htmlTagRemover.remove(getShortDescription(specialVariable)),
getSummary(specialVariable)));
}
}
Expand Down Expand Up @@ -162,10 +176,8 @@ private String getShortDescription(String macro) {
// if there is no shortDesc try if one of the functions has one
if (shortDesc == null) {
for (Function function : MapToolExpressionParser.getMacroFunctions()) {
if (function instanceof AdditionalFunctionDescription) {
final AdditionalFunctionDescription functionExtended =
(AdditionalFunctionDescription) function;
// try if the function has a summary for this macro
if (function instanceof AdditionalFunctionDescription functionExtended) {
// try if the function has a shortDesc for this macro
final String shortDescExtended = functionExtended.getFunctionDescription(macro);
if (shortDescExtended != null) {
return shortDescExtended;
Expand All @@ -185,9 +197,7 @@ private String getSummary(String macro) {
// if there is no summary try if one of the functions has one
if (summary == null) {
for (final Function function : MapToolExpressionParser.getMacroFunctions()) {
if (function instanceof AdditionalFunctionDescription) {
final AdditionalFunctionDescription functionExtended =
(AdditionalFunctionDescription) function;
if (function instanceof AdditionalFunctionDescription functionExtended) {
// try if the function has a summary for this macro
final String summaryExtended = functionExtended.getFunctionSummary(macro);
if (summaryExtended != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.client.ui.syntax;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class HTMLTagRemoverTest {

private final HTMLTagRemover remover = new HTMLTagRemover();

@Test
void nullRemainsNull() {
assertEquals(null, remover.remove(null));
}

@Test
void emptyStringRemainsEmpty() {
assertEquals("", remover.remove(""));
}

@Test
void openTagsAreRemoved() {
assertEquals("text some text", remover.remove("text <tag>some text"));
}

@Test
void closeTagsAreRemoved() {
assertEquals("text some text", remover.remove("text </tag>some text"));
}

@Test
void unfinishedTagsAreNotRemoved() {
assertEquals("text <tag some text", remover.remove("text <tag some text"));
}

@Test
void multipleTagsAreRemoved() {
assertEquals(
"text more text even more text",
remover.remove("text <tag>more text</tag> even more text"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.maptool.client.ui.syntax;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;
import org.fife.ui.autocomplete.BasicCompletion;
import org.fife.ui.autocomplete.Completion;
import org.fife.ui.autocomplete.DefaultCompletionProvider;
import org.junit.jupiter.api.Test;

class MapToolScriptAutoCompleteTest {

@Test
void tagsAreStrippedFromAShortDesc() {
MapToolScriptAutoComplete mapToolScriptAutoComplete = new MapToolScriptAutoComplete();
DefaultCompletionProvider completionProvider =
(DefaultCompletionProvider) mapToolScriptAutoComplete.get();

List<Completion> completionList = completionProvider.getCompletionByInputText("json.length");

assertEquals(1, completionList.size());
BasicCompletion completion = (BasicCompletion) completionList.get(0);
assertEquals(
"Returns the number of fields in a json object or number of elements in a json array .",
completion.getShortDescription());
}
}