From b1b412722b478a00d7de91ae6a81f45dacace28e Mon Sep 17 00:00:00 2001 From: zhengyanan1 <2821084381@qq.com> Date: Wed, 13 Nov 2024 14:27:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DinnerHTML=E7=94=9F?= =?UTF-8?q?=E6=88=90=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=97=B6=E7=9A=84=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E9=80=BB=E8=BE=91.=20=E5=AF=B9=E4=BA=8Ea=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=EF=BC=8C=E9=80=BB=E8=BE=91=E4=BF=9D=E6=8C=81=E8=B7=9F?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5a=E6=A0=87=E7=AD=BE=E5=9F=BA=E6=9C=AC?= =?UTF-8?q?=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/dom-external/inner-html/parser.ts | 32 ++++++++++++++++--- .../src/dom-external/inner-html/tags.ts | 26 +++++++++++++-- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/packages/taro-runtime/src/dom-external/inner-html/parser.ts b/packages/taro-runtime/src/dom-external/inner-html/parser.ts index 7c4e934cedc1..b2b28ce5c9f7 100644 --- a/packages/taro-runtime/src/dom-external/inner-html/parser.ts +++ b/packages/taro-runtime/src/dom-external/inner-html/parser.ts @@ -3,7 +3,7 @@ import { isFunction } from '@tarojs/shared' import { options } from '../../options' import { Scaner, Token } from './scaner' import StyleTagParser from './style' -import { isBlockElements, isInlineElements, isMiniElements, specialMiniElements } from './tags' +import { getSpecialElementMapping, isBlockElements, isInlineElements, isMiniElements, isSpecialElements, specialMiniElements } from './tags' import { unquote } from './utils' import type { TaroDocument } from '../../dom/document' @@ -47,7 +47,7 @@ export interface Element extends Node { attributes: string[] } -export interface ParsedTaroElement extends TaroElement{ +export interface ParsedTaroElement extends TaroElement { h5tagName?: string } @@ -71,7 +71,23 @@ function hasTerminalParent (tagName: string, stack: Element[]) { return false } -function getTagName (tag: string) { +/** + * 将属性数组转换为属性对象 + * @param attributes 字符串数组,包含属性信息 + * @returns 属性对象,键为属性名,值为属性值或true + */ +function attributesArray2Props (attributes: string[]): Record { + const props: Record = {} + for (let i = 0; i < attributes.length; i++) { + const attr = attributes[i] + const [key, value] = splitEqual(attr) + props[key] = value == null ? true : unquote(value) + } + + return props +} + +function getTagName (tag: string, attributes: string[]) { if (options.html!.renderHTMLTag) { return tag } @@ -84,6 +100,14 @@ function getTagName (tag: string) { return 'view' } else if (isInlineElements(tag)) { return 'text' + } else if (isSpecialElements(tag)) { + // if it's special tag, the real tag is determined by the config mapping + const mapping = getSpecialElementMapping(tag) + const props = attributesArray2Props(attributes) + + if (mapping) { + return mapping.mapName(props) + } } return 'view' @@ -146,7 +170,7 @@ function format ( child.attributes.push(`style=${styleText.replace(/['"]/g, '')}`) } - const el: ParsedTaroElement = document.createElement(getTagName(child.tagName)) + const el: ParsedTaroElement = document.createElement(getTagName(child.tagName, child.attributes)) el.h5tagName = child.tagName parent?.appendChild(el) diff --git a/packages/taro-runtime/src/dom-external/inner-html/tags.ts b/packages/taro-runtime/src/dom-external/inner-html/tags.ts index 62886d506ec7..cca961b94f85 100644 --- a/packages/taro-runtime/src/dom-external/inner-html/tags.ts +++ b/packages/taro-runtime/src/dom-external/inner-html/tags.ts @@ -1,4 +1,4 @@ -import { internalComponents } from '@tarojs/shared' +import { internalComponents, isString } from '@tarojs/shared' export function makeMap ( str: string, @@ -17,6 +17,25 @@ export const specialMiniElements = { iframe: 'web-view' } +interface SpecialMap { + mapName: (props: Record) => string +} + +const specialElements = new Map([ + ['a', { + mapName (props) { + if (props.as && isString(props.as)) return props.as.toLowerCase() + return !props.href || isString(props.href) && (/^javascript/.test(props.href)) ? 'view' : 'navigator' + } + }], +]) + +export const getSpecialElementMapping = (tag: string, expectsLowerCase:boolean = true) => { + tag = expectsLowerCase ? tag.toLowerCase() : tag + return specialElements.get(tag) +} + + const internalCompsList = Object.keys(internalComponents) .map(i => i.toLowerCase()) .join(',') @@ -25,7 +44,10 @@ const internalCompsList = Object.keys(internalComponents) export const isMiniElements = makeMap(internalCompsList, true) // https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements -export const isInlineElements = makeMap('a,i,abbr,iframe,select,acronym,slot,small,span,bdi,kbd,strong,big,map,sub,sup,br,mark,mark,meter,template,canvas,textarea,cite,object,time,code,output,u,data,picture,tt,datalist,var,dfn,del,q,em,s,embed,samp,b', true) +export const isInlineElements = makeMap('i,abbr,iframe,select,acronym,slot,small,span,bdi,kbd,strong,big,map,sub,sup,br,mark,mark,meter,template,canvas,textarea,cite,object,time,code,output,u,data,picture,tt,datalist,var,dfn,del,q,em,s,embed,samp,b', true) // https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements export const isBlockElements = makeMap('address,fieldset,li,article,figcaption,main,aside,figure,nav,blockquote,footer,ol,details,form,p,dialog,h1,h2,h3,h4,h5,h6,pre,dd,header,section,div,hgroup,table,dl,hr,ul,dt', true) + +// specialElements +export const isSpecialElements = makeMap('a', true) \ No newline at end of file