bugfix:第一次递归就进行自闭合check导致自闭合DOM全部跑到最外层

This commit is contained in:
Searchstars
2024-09-24 22:03:34 +08:00
parent bf304a8a25
commit 5775541348
2 changed files with 28 additions and 24 deletions
+22 -20
View File
@@ -34,39 +34,41 @@ function stripHtmlTags(html: string): string {
const selfClosingTags = new Set(['img', 'br', 'hr', 'input', 'meta', 'link', 'area', 'base', 'col', 'command', 'embed', 'keygen', 'param', 'source', 'track', 'wbr']);
// 更新 parseContentHtml 函数
export function parseContentHtml(html: string): HtmlDocument[] {
export function parseContentHtml(html: string, processSelfClosingTags: boolean = true): HtmlDocument[] {
const tagRegex = /<(\w+)([^>]*)>([\s\S]*?)<\/\1>/g; // 匹配 HTML 标签及其内容
const selfClosingTagRegex = /<(\w+)([^>]*)\/>/g; // 匹配以 /> 结尾的自闭合标签
let elements: HtmlDocument[] = [];
let lastIndex = 0; // 上一次匹配结束的位置
// 处理以 /> 结尾的自闭合标签
html = html.replace(selfClosingTagRegex, (match, tagName, attributes) => {
const element: HtmlDocument = {
type: tagName,
attributes: parseAttributes(attributes),
style: parseStyle(attributes),
children: [],
};
elements.push(element);
return ''; // 移除匹配的自闭合标签
});
// 处理固有自闭合标签(不以 /> 结尾)
selfClosingTags.forEach(tag => {
const regex = new RegExp(`<${tag}([^>]*)>`, 'g');
html = html.replace(regex, (match, attributes) => {
if(processSelfClosingTags){
// 处理以 /> 结尾的自闭合标签
html = html.replace(selfClosingTagRegex, (match, tagName, attributes) => {
const element: HtmlDocument = {
type: tag,
type: tagName,
attributes: parseAttributes(attributes),
style: parseStyle(attributes),
children: [],
};
elements.push(element);
return ''; // 移除匹配的标签
return ''; // 移除匹配的自闭合标签
});
});
// 处理固有自闭合标签(不以 /> 结尾)
selfClosingTags.forEach(tag => {
const regex = new RegExp(`<${tag}([^>]*)>`, 'g');
html = html.replace(regex, (match, attributes) => {
const element: HtmlDocument = {
type: tag,
attributes: parseAttributes(attributes),
style: parseStyle(attributes),
children: [],
};
elements.push(element);
return ''; // 移除匹配的标签
});
});
}
let match;
while ((match = tagRegex.exec(html)) !== null) {
+6 -4
View File
@@ -20,7 +20,7 @@
</div>
</div>
<div style="margin-top: 22px; width: 80%; justify-content: center">
<html-renderer doms="{{article_content[current_page_num]}}" @open-image="OpenImage"></html-renderer>
<html-renderer doms="{{article_content}}" @open-image="OpenImage"></html-renderer>
</div>
</div>
<text style="margin-top: 25px"></text>
@@ -78,13 +78,15 @@ export default {
const article = this.$app.$def.articletools.parseArticlePageHtml(article_html)
this.article.readInfo = article.readInfo // 原地更新 减少开辟内存空间
let article_full = this.$app.$def.htmlparser.parseContentHtml(article.readInfo.content)
let article_full = this.$app.$def.htmlparser.parseContentHtml(article.readInfo.content, false)
this.$app.$def.articletools.PatchArticleContent(article_full)
this.article_content = this.SplitArticle(article_full, this.$app.$def.settings.SETTINGS.article_split_dom_count)
//this.article_content = this.SplitArticle(article_full, this.$app.$def.settings.SETTINGS.article_split_dom_count)
this.article_content = article_full
this.max_page_num = this.article_content.length
console.log(this.article_content)
console.log(article.readInfo.content)
console.log(JSON.stringify(this.article_content))
this.anims.loading.stop()
this.show_loading = false