Skip to content

Commit

Permalink
Merge pull request #13 from cycleccc/master
Browse files Browse the repository at this point in the history
fix: 修复解析 float style
  • Loading branch information
hqwlkj authored Nov 4, 2024
2 parents 6476947 + 7a61b28 commit 26fea62
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/module/parse-elem-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
* @author Yanghc
*/

import { DOMElement } from '../utils/dom'
import $, { DOMElement, getStyleValue } from '../utils/dom'
import { IDomEditor, SlateDescendant } from '@wangeditor/editor'
import { ImageElement } from './custom-types'


function parseHtml(elem: DOMElement, children: SlateDescendant[], editor: IDomEditor): ImageElement {
let href = elem.getAttribute('data-href') || ''
const $elem = $(elem)
let href = $elem.attr('data-href') || ''

href = decodeURIComponent(href) // 兼容 V4

return {
type: 'image',
src: elem.getAttribute('src') || '',
alt: elem.getAttribute('alt') || '',
src: $elem.attr('src') || '',
alt: $elem.attr('alt') || '',
href,
style: {
width: elem.getAttribute('width') || '',
height: elem.getAttribute('height') || '',
float: elem.getAttribute('float') || '',
width: getStyleValue($elem, 'width'),
height: getStyleValue($elem, 'height'),
float: getStyleValue($elem, 'float') || '',
},
children: [{ text: '' }], // void node 有一个空白 text
}
Expand Down
22 changes: 22 additions & 0 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ if (height) $.fn.height = height
if (filter) $.fn.filter = filter
if (empty) $.fn.empty = empty

export function getStyleValue($elem: Dom7Array, styleKey: string): string {
let res = ''

const styleStr = $elem.attr('style') || ''
const styleArr = styleStr.split(';')
const length = styleArr.length

for (let i = 0; i < length; i += 1) {
const styleItemStr = styleArr[i]

if (styleItemStr) {
const arr = styleItemStr.split(':')

if (arr[0].trim() === styleKey) {
res = arr[1].trim()
}
}
}

return res
}

export default $

// COMPAT: This is required to prevent TypeScript aliases from doing some very
Expand Down

0 comments on commit 26fea62

Please sign in to comment.