Skip to content

Commit

Permalink
fix: 对于innerHTML生成的小程序, img=>image时,如果有width和height,把这2个值写入style属性内
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengyanan1 committed Nov 10, 2024
1 parent cfb4a44 commit b81a8f9
Showing 1 changed file with 15 additions and 1 deletion.
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 @@ export interface Element extends Node {
attributes: string[]
}

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

Expand Down Expand Up @@ -127,6 +127,20 @@ function format (
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

0 comments on commit b81a8f9

Please sign in to comment.