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

fix: 使用innerHTML赋值后转小程序时,a标签转view标签+img转image标签时,把width和height写入style属性【latest】 #16781

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion packages/taro-runtime/src/dom-external/inner-html/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
attributes: string[]
}

export interface ParsedTaroElement extends TaroElement{
export interface ParsedTaroElement extends TaroElement {
h5tagName?: string
}

Expand Down Expand Up @@ -127,6 +127,20 @@
parent?.appendChild(text)
return text
}
// img标签,把width和height写入style
if (child.tagName === 'img') {
let styleText = ''
for (let i = 0; i < child.attributes.length; i++) {
const attr = child.attributes[i]
const [key, value] = splitEqual(attr)

Check warning on line 135 in packages/taro-runtime/src/dom-external/inner-html/parser.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-runtime/src/dom-external/inner-html/parser.ts#L132-L135

Added lines #L132 - L135 were not covered by tests
if (key === 'width' || key === 'height') {
styleText += `${key}:${value};`

Check warning on line 137 in packages/taro-runtime/src/dom-external/inner-html/parser.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-runtime/src/dom-external/inner-html/parser.ts#L137

Added line #L137 was not covered by tests
} else if (key === 'style') {
styleText = `${styleText}${value};`

Check warning on line 139 in packages/taro-runtime/src/dom-external/inner-html/parser.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-runtime/src/dom-external/inner-html/parser.ts#L139

Added line #L139 was not covered by tests
}
}
child.attributes.push(`style=${styleText.replace(/['"]/g, '')}`)

Check warning on line 142 in packages/taro-runtime/src/dom-external/inner-html/parser.ts

View check run for this annotation

Codecov / codecov/patch

packages/taro-runtime/src/dom-external/inner-html/parser.ts#L142

Added line #L142 was not covered by tests
}

const el: ParsedTaroElement = document.createElement(getTagName(child.tagName))
el.h5tagName = child.tagName
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-runtime/src/dom-external/inner-html/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ 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)
export const isBlockElements = makeMap('a,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)
Loading