diff --git a/modules/util/source-formatter/src/main/java/com/liferay/source/formatter/check/IllegalHTMLTagsCheck.java b/modules/util/source-formatter/src/main/java/com/liferay/source/formatter/check/IllegalHTMLTagsCheck.java new file mode 100644 index 00000000000000..c0dba69b41a897 --- /dev/null +++ b/modules/util/source-formatter/src/main/java/com/liferay/source/formatter/check/IllegalHTMLTagsCheck.java @@ -0,0 +1,66 @@ +/** + * SPDX-FileCopyrightText: (c) 2024 Liferay, Inc. https://liferay.com + * SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06 + */ + +package com.liferay.source.formatter.check; + +import com.liferay.petra.string.StringBundler; +import com.liferay.portal.kernel.util.StringUtil; +import com.liferay.portal.tools.ToolsUtil; + +import java.util.List; + +/** + * @author Alan Huang + */ +public class IllegalHTMLTagsCheck extends BaseFileCheck { + + @Override + public boolean isLiferaySourceCheck() { + return true; + } + + @Override + protected String doProcess( + String fileName, String absolutePath, String content) { + + List avoidHtmlHeaderNames = getAttributeValues( + _AVOID_HTML_HEADER_NAMES_KEY, absolutePath); + + for (String avoidHtmlHeaderName : avoidHtmlHeaderNames) { + int x = -1; + + while (true) { + x = StringUtil.indexOfAny( + content, + new String[] { + "<" + avoidHtmlHeaderName + " ", + "<" + avoidHtmlHeaderName + ">" + }, + x + 1); + + if (x == -1) { + break; + } + + if (ToolsUtil.isInsideQuotes(content, x)) { + continue; + } + + addMessage( + fileName, + StringBundler.concat( + "Do not use '", avoidHtmlHeaderName, + "', use 'div' instead"), + getLineNumber(content, x)); + } + } + + return content; + } + + private static final String _AVOID_HTML_HEADER_NAMES_KEY = + "avoidHtmlHeaderNames"; + +} \ No newline at end of file diff --git a/modules/util/source-formatter/src/main/resources/documentation/all_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/all_checks.markdown index a85cff28e35c28..33ace39c777374 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/all_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/all_checks.markdown @@ -118,6 +118,7 @@ GroovyImportsCheck | [Styling](styling_checks.markdown#styling-checks) | .groovy HTMLEmptyLinesCheck | [Styling](styling_checks.markdown#styling-checks) | .html or .path | Finds missing and unnecessary empty lines. | HTMLWhitespaceCheck | [Styling](styling_checks.markdown#styling-checks) | .html or .path | Finds missing and unnecessary whitespace in `.html` files. | [IfStatementCheck](check/if_statement_check.markdown#ifstatementcheck) | [Styling](styling_checks.markdown#styling-checks) | .java, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds empty if-statements and consecutive if-statements with identical bodies. | +IllegalHTMLTagsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | .ftl, .html, .js, .jsp, .jspf, .jspx, .jsx, .path, .tag, .tpl or .vm | Finds cases of incorrect use of HTML tags. | IllegalImportsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | .java, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds cases of incorrect use of certain classes. | IllegalTaglibsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | .ftl, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds cases of incorrect use of certain deprecated taglibs in modules. | [IncorrectFileLocationCheck](check/incorrect_file_location_check.markdown#incorrectfilelocationcheck) | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | | Checks that `/src/*/java/` only contains `.java` files. | diff --git a/modules/util/source-formatter/src/main/resources/documentation/bug_prevention_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/bug_prevention_checks.markdown index f31d6ec48a4f00..81d38086b3d2e0 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/bug_prevention_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/bug_prevention_checks.markdown @@ -54,6 +54,7 @@ GradleProvidedDependenciesCheck | .gradle | Validates the scope of dependencies GradleRestClientDependenciesCheck | .gradle | Validates the project dependencies `.*-rest-client` can only be used for `testIntegrationImplementation`. | GradleTestDependencyVersionCheck | .gradle | Checks the version for dependencies in gradle build files. | GradleTestUtilDeployDirCheck | .gradle | Checks for incorrect use of `deployDir`. | +IllegalHTMLTagsCheck | .ftl, .html, .js, .jsp, .jspf, .jspx, .jsx, .path, .tag, .tpl or .vm | Finds cases of incorrect use of HTML tags. | IllegalImportsCheck | .java, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds cases of incorrect use of certain classes. | IllegalTaglibsCheck | .ftl, .jsp, .jspf, .jspx, .tag, .tpl or .vm | Finds cases of incorrect use of certain deprecated taglibs in modules. | [IncorrectFileLocationCheck](check/incorrect_file_location_check.markdown#incorrectfilelocationcheck) | | Checks that `/src/*/java/` only contains `.java` files. | diff --git a/modules/util/source-formatter/src/main/resources/documentation/ftl_source_processor_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/ftl_source_processor_checks.markdown index 262a740b3913d3..4b81a84e523a27 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/ftl_source_processor_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/ftl_source_processor_checks.markdown @@ -11,4 +11,5 @@ FTLStylingCheck | [Styling](styling_checks.markdown#styling-checks) | Applies ru FTLTagAttributesCheck | [Styling](styling_checks.markdown#styling-checks) | Sorts and formats attributes values in tags. | FTLTagCheck | [Styling](styling_checks.markdown#styling-checks) | Finds cases where consecutive `#assign` can be combined. | FTLWhitespaceCheck | [Styling](styling_checks.markdown#styling-checks) | Finds missing and unnecessary whitespace in `.ftl` files. | +IllegalHTMLTagsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of HTML tags. | IllegalTaglibsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of certain deprecated taglibs in modules. | \ No newline at end of file diff --git a/modules/util/source-formatter/src/main/resources/documentation/html_source_processor_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/html_source_processor_checks.markdown index 616108aca8a80b..fb6005cefa96b8 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/html_source_processor_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/html_source_processor_checks.markdown @@ -4,4 +4,5 @@ Check | Category | Description ----- | -------- | ----------- HTMLEmptyLinesCheck | [Styling](styling_checks.markdown#styling-checks) | Finds missing and unnecessary empty lines. | HTMLWhitespaceCheck | [Styling](styling_checks.markdown#styling-checks) | Finds missing and unnecessary whitespace in `.html` files. | +IllegalHTMLTagsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of HTML tags. | XMLTagAttributesCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Performs several checks on tag attributes. | \ No newline at end of file diff --git a/modules/util/source-formatter/src/main/resources/documentation/js_source_processor_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/js_source_processor_checks.markdown index cb84f2f6fb414d..5e3bc34de61f3c 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/js_source_processor_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/js_source_processor_checks.markdown @@ -2,6 +2,7 @@ Check | Category | Description ----- | -------- | ----------- +IllegalHTMLTagsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of HTML tags. | [JSLodashDependencyCheck](check/js_lodash_dependency_check.markdown#jslodashdependencycheck) | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds incorrect use of `AUI._`. | JSStylingCheck | [Styling](styling_checks.markdown#styling-checks) | Applies rules to enforce consistency in code style. | JSWhitespaceCheck | [Styling](styling_checks.markdown#styling-checks) | Finds missing and unnecessary whitespace in `.js` files. | diff --git a/modules/util/source-formatter/src/main/resources/documentation/jsp_source_processor_checks.markdown b/modules/util/source-formatter/src/main/resources/documentation/jsp_source_processor_checks.markdown index 34432b5f68d2d2..776f2abe2b2c16 100644 --- a/modules/util/source-formatter/src/main/resources/documentation/jsp_source_processor_checks.markdown +++ b/modules/util/source-formatter/src/main/resources/documentation/jsp_source_processor_checks.markdown @@ -21,6 +21,7 @@ FactoryCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-ch [GenericTypeCheck](check/generic_type_check.markdown#generictypecheck) | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Checks that generics are always specified to provide compile-time checking and removing the risk of `ClassCastException` during runtime. | [GetterUtilCheck](check/getter_util_check.markdown#getterutilcheck) | [Styling](styling_checks.markdown#styling-checks) | Finds cases where the default value is passed to `GetterUtil.get*` or `ParamUtil.get*`. | [IfStatementCheck](check/if_statement_check.markdown#ifstatementcheck) | [Styling](styling_checks.markdown#styling-checks) | Finds empty if-statements and consecutive if-statements with identical bodies. | +IllegalHTMLTagsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of HTML tags. | IllegalImportsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of certain classes. | IllegalTaglibsCheck | [Bug Prevention](bug_prevention_checks.markdown#bug-prevention-checks) | Finds cases of incorrect use of certain deprecated taglibs in modules. | InstanceofOrderCheck | [Styling](styling_checks.markdown#styling-checks) | Check the order of `instanceof` calls. | diff --git a/modules/util/source-formatter/src/main/resources/sourcechecks.xml b/modules/util/source-formatter/src/main/resources/sourcechecks.xml index 0000e9a87b6efe..bbbb7a9973434f 100644 --- a/modules/util/source-formatter/src/main/resources/sourcechecks.xml +++ b/modules/util/source-formatter/src/main/resources/sourcechecks.xml @@ -241,6 +241,11 @@ + + + + + @@ -374,6 +379,11 @@ + + + + + @@ -1226,6 +1236,11 @@ + + + + + @@ -1323,6 +1338,11 @@ + + + + + diff --git a/source-formatter.properties b/source-formatter.properties index f4abae261ba3df..a5bc62a9cb31c4 100644 --- a/source-formatter.properties +++ b/source-formatter.properties @@ -747,6 +747,13 @@ source.check.IfStatementCheck.enabled=true + source.check.IllegalHTMLTagsCheck.avoidHtmlHeaderNames=\ + h4,\ + h5,\ + h6 + + source.check.IllegalHTMLTagsCheck.enabled=true + source.check.IllegalImportsCheck.allowedOptionalFileNames=\ modules/apps/portal-vulcan/portal-vulcan-impl/src/main/java/com/liferay/portal/vulcan/internal/resource/OpenAPIResourceImpl.java,\ modules/apps/push-notifications/push-notifications-sender/push-notifications-sender-apple/src/main/java/com/liferay/push/notifications/sender/apple/internal/ApplePushNotificationsSender.java,\