-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: optimizing regex pettern for html meta matching (#4235)
#### What type of PR is this? /kind improvement /area core /milestone 2.8.x #### What this PR does / why we need it: 优化去除 Html Meta 重复标签的正则表达式 see #4234 for more details. #### Which issue(s) this PR fixes: Fixes #4234 #### Does this PR introduce a user-facing change? ```release-note 优化去除 Html Meta 重复标签的正则表达式 ```
- Loading branch information
Showing
3 changed files
with
84 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
application/src/test/java/run/halo/app/theme/dialect/DuplicateMetaTagProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package run.halo.app.theme.dialect; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.regex.Matcher; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Tests for {@link DuplicateMetaTagProcessor}. | ||
* | ||
* @author guqing | ||
* @since 2.8.0 | ||
*/ | ||
class DuplicateMetaTagProcessorTest { | ||
|
||
@Test | ||
void extractMetaTag() { | ||
// normal | ||
String text = "<meta name=\"description\" content=\"a description\"/>"; | ||
Matcher matcher = DuplicateMetaTagProcessor.META_PATTERN.matcher(text); | ||
assertThat(matcher.find()).isTrue(); | ||
assertThat(matcher.group(1)).isEqualTo("description"); | ||
|
||
// name and content are not in the general order | ||
text = "<meta content=\"K1,K2\" name=\"keywords\"/>"; | ||
matcher = DuplicateMetaTagProcessor.META_PATTERN.matcher(text); | ||
assertThat(matcher.find()).isTrue(); | ||
assertThat(matcher.group(1)).isEqualTo("keywords"); | ||
|
||
// no closing slash | ||
text = "<meta content=\"K1,K2\" name=\"keywords\">"; | ||
matcher = DuplicateMetaTagProcessor.META_PATTERN.matcher(text); | ||
assertThat(matcher.find()).isTrue(); | ||
assertThat(matcher.group(1)).isEqualTo("keywords"); | ||
|
||
// multiple line breaks and other stuff | ||
text = """ | ||
<meta content="全局 Head description" name="description" /> | ||
<style> | ||
.moment .momemt-content pre.notranslate { | ||
background: #f3f3f3; | ||
color: #444; | ||
} | ||
</style> | ||
"""; | ||
matcher = DuplicateMetaTagProcessor.META_PATTERN.matcher(text); | ||
assertThat(matcher.find()).isTrue(); | ||
assertThat(matcher.group(1)).isEqualTo("description"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters