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

LPD-30242 Add IllegalHTMLTagsCheck #2693

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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,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<String> 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";

}
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
20 changes: 20 additions & 0 deletions modules/util/source-formatter/src/main/resources/sourcechecks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@
<category name="Styling" />
<description name="Finds missing and unnecessary whitespace in `.ftl` files." />
</check>
<check name="IllegalHTMLTagsCheck">
<category name="Bug Prevention" />
<description name="Finds cases of incorrect use of HTML tags." />
<property name="enabled" value="false" />
</check>
<check name="IllegalTaglibsCheck">
<category name="Bug Prevention" />
<description name="Finds cases of incorrect use of certain deprecated taglibs in modules." />
Expand Down Expand Up @@ -374,6 +379,11 @@
<category name="Styling" />
<description name="Finds missing and unnecessary whitespace in `.html` files." />
</check>
<check name="IllegalHTMLTagsCheck">
<category name="Bug Prevention" />
<description name="Finds cases of incorrect use of HTML tags." />
<property name="enabled" value="false" />
</check>
<check name="XMLTagAttributesCheck">
<category name="Bug Prevention" />
<description name="Performs several checks on tag attributes." />
Expand Down Expand Up @@ -1226,6 +1236,11 @@
<category name="Styling" />
<description name="Validates `copyright` header." />
</check>
<check name="IllegalHTMLTagsCheck">
<category name="Bug Prevention" />
<description name="Finds cases of incorrect use of HTML tags." />
<property name="enabled" value="false" />
</check>
<check name="JSPCoreTaglibCheck">
<category name="Styling" />
<description name="Finds cases where a `c:choose` or `c:if` tag can be used instead of an if-statement." />
Expand Down Expand Up @@ -1323,6 +1338,11 @@

<!-- Liferay Source Checks -->

<check name="IllegalHTMLTagsCheck">
<category name="Bug Prevention" />
<description name="Finds cases of incorrect use of HTML tags." />
<property name="enabled" value="false" />
</check>
<check name="LanguageKeysCheck">
<category name="Bug Prevention" />
<description name="Finds missing language keys in `Language.properties`." />
Expand Down
7 changes: 7 additions & 0 deletions source-formatter.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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,\
Expand Down