From 7a61b28fb6497c4771b3cb42781527cff3729d52 Mon Sep 17 00:00:00 2001 From: yaolongfei <2991205548@qq.com> Date: Mon, 28 Oct 2024 14:53:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=A7=A3=E6=9E=90=20f?= =?UTF-8?q?loat=20style?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 添加 getStyleValue 函数 * 从 style 中过滤 float 属性 --- src/module/parse-elem-html.ts | 16 +++++++++------- src/utils/dom.ts | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/module/parse-elem-html.ts b/src/module/parse-elem-html.ts index 885d361..60adb12 100644 --- a/src/module/parse-elem-html.ts +++ b/src/module/parse-elem-html.ts @@ -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 } diff --git a/src/utils/dom.ts b/src/utils/dom.ts index 86840df..0f54d9c 100644 --- a/src/utils/dom.ts +++ b/src/utils/dom.ts @@ -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