From 68e29ba16166f6ed83575d48c191af1f076e174e Mon Sep 17 00:00:00 2001 From: lichenabson <1114795127@qq.com> Date: Thu, 18 Feb 2021 11:15:07 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=BB=98=E8=AE=A4=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E7=A9=BA=E8=A1=8C=E7=94=B1


=E6=94=B9=E6=88=90

?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cypress/integration/e2e/txt/append.spec.ts | 2 +- src/editor/init-fns/init-dom.ts | 3 ++- src/editor/init-fns/init-selection.ts | 7 ++--- src/editor/selection.ts | 3 ++- .../code/bind-event/jump-code-block-down.ts | 3 ++- src/menus/code/create-panel-conf.ts | 3 ++- src/menus/head/index.ts | 3 ++- src/menus/quote/bind-event/index.ts | 5 ++-- src/menus/quote/index.ts | 3 ++- src/menus/split-line/index.ts | 3 ++- src/menus/table/bind-event/tooltip-event.ts | 7 ++--- src/menus/table/create-table.ts | 3 ++- src/menus/todo/bind-event/index.ts | 7 ++--- src/menus/video/create-panel-conf.ts | 3 ++- src/menus/video/upload-video.ts | 3 ++- src/text/event-hooks/del-to-keep-p.ts | 9 ++++--- src/text/event-hooks/enter-to-create-p.ts | 4 +-- src/text/event-hooks/index.ts | 2 +- src/text/index.ts | 26 ++++++++----------- src/utils/const.ts | 9 +++++++ test/unit/editor/history/decompile.test.ts | 3 ++- test/unit/menus/redo.test.ts | 2 +- test/unit/menus/table/create-table.test.ts | 3 ++- test/unit/menus/undo.test.ts | 3 ++- test/unit/menus/video/upload-video.test.ts | 5 ++-- test/unit/text/del-to-keep-p.test.ts | 15 ++++++----- test/unit/text/enter-to-create-p.test.ts | 5 ++-- test/unit/text/index.test.ts | 5 ++-- 28 files changed, 88 insertions(+), 61 deletions(-) diff --git a/cypress/integration/e2e/txt/append.spec.ts b/cypress/integration/e2e/txt/append.spec.ts index 889452b..a232a49 100644 --- a/cypress/integration/e2e/txt/append.spec.ts +++ b/cypress/integration/e2e/txt/append.spec.ts @@ -8,7 +8,7 @@ describe('test editor.txt.append()', () => { const text = 'test123' it('editor.txt.append()功能光标位置是否正常', () => { - cy.get('#div1').contains('欢迎使用 wangEditor 富文本编辑器') + cy.get('#div1').contains('富文本编辑器') cy.getEditor().then((editor: Editor) => { const menusLen = editor.menus.menuList.length + 1 diff --git a/src/editor/init-fns/init-dom.ts b/src/editor/init-fns/init-dom.ts index dfd75ef..88e3eae 100644 --- a/src/editor/init-fns/init-dom.ts +++ b/src/editor/init-fns/init-dom.ts @@ -6,6 +6,7 @@ import Editor from '../index' import $, { DomElement } from '../../utils/dom-core' import { getRandom } from '../../utils/util' +import { EMPTY_P } from '../../utils/const' const styleSettings = { border: '1px solid #c9d8db', @@ -65,7 +66,7 @@ export default function (editor: Editor): void { // 编辑器有默认值的时候隐藏placeholder $placeholder.hide() } else { - $textElem.append($('


')) // 新增一行,方便继续编辑 + $textElem.append($(EMPTY_P)) // 新增一行,方便继续编辑 } // 编辑区域加入DOM diff --git a/src/editor/init-fns/init-selection.ts b/src/editor/init-fns/init-selection.ts index 9e9f78c..6b5f4f0 100644 --- a/src/editor/init-fns/init-selection.ts +++ b/src/editor/init-fns/init-selection.ts @@ -5,6 +5,7 @@ import Editor from '../index' import $ from '../../utils/dom-core' +import { EMPTY_P } from '../../utils/const' /** * 初始化编辑器选区,将光标定位到文档末尾 @@ -16,7 +17,7 @@ function initSelection(editor: Editor, newLine?: boolean) { const $children = $textElem.children() if (!$children || !$children.length) { // 如果编辑器区域无内容,添加一个空行,重新设置选区 - $textElem.append($('


')) + $textElem.append($(EMPTY_P)) initSelection(editor) return } @@ -28,8 +29,8 @@ function initSelection(editor: Editor, newLine?: boolean) { const html = $last.html().toLowerCase() const nodeName = $last.getNodeName() if ((html !== '
' && html !== '
') || nodeName !== 'P') { - // 最后一个元素不是


,添加一个空行,重新设置选区 - $textElem.append($('


')) + // 最后一个元素不是 空标签,添加一个空行,重新设置选区 + $textElem.append($(EMPTY_P)) initSelection(editor) return } diff --git a/src/editor/selection.ts b/src/editor/selection.ts index ff04db3..ba2b2b9 100644 --- a/src/editor/selection.ts +++ b/src/editor/selection.ts @@ -5,6 +5,7 @@ import $, { DomElement } from '../utils/dom-core' import { UA } from '../utils/util' import Editor from './index' +import { EMPTY_P } from '../utils/const' class SelectionAndRange { public editor: Editor @@ -58,7 +59,7 @@ class SelectionAndRange { const $textElem = editor.$textElem if ($textElem.isContain($containerElem)) { if ($textElem.elems[0] === $containerElem.elems[0]) { - if ($textElem.html().trim() === '


') { + if ($textElem.html().trim() === EMPTY_P) { const $children = $textElem.children() const $last = $children?.last() editor.selection.createRangeByElem($last as DomElement, true, true) diff --git a/src/menus/code/bind-event/jump-code-block-down.ts b/src/menus/code/bind-event/jump-code-block-down.ts index 68f420e..c89f767 100644 --- a/src/menus/code/bind-event/jump-code-block-down.ts +++ b/src/menus/code/bind-event/jump-code-block-down.ts @@ -2,6 +2,7 @@ * @description 代码块为最后一块内容时往下跳出代码块 * @author zhengwenjian */ +import { EMPTY_P } from '../../../utils/const' import Editor from '../../../editor/index' import $ from '../../../utils/dom-core' @@ -20,7 +21,7 @@ export default function bindEventJumpCodeBlock(editor: Editor) { const $lastNode = $textElem.children()?.last() if (node?.elems[0].tagName === 'XMP' && $lastNode?.elems[0].tagName === 'PRE') { // 就是最后一块是代码块的情况插入空p标签并光标移至p - const $emptyP = $('


') + const $emptyP = $(EMPTY_P) $textElem.append($emptyP) } }) diff --git a/src/menus/code/create-panel-conf.ts b/src/menus/code/create-panel-conf.ts index bb2e9c3..74b4e4d 100644 --- a/src/menus/code/create-panel-conf.ts +++ b/src/menus/code/create-panel-conf.ts @@ -8,6 +8,7 @@ import { PanelConf } from '../menu-constructors/Panel' import { getRandom } from '../../utils/util' import $, { DomElement } from '../../utils/dom-core' import isActive from './is-active' +import { EMPTY_P } from '../../utils/const' export default function (editor: Editor, text: string, languageType: string): PanelConf { // panel 中需要用到的id @@ -35,7 +36,7 @@ export default function (editor: Editor, text: string, languageType: string): Pa // 通过dom操作添加换行标签 // @ts-ignore - $('


').insertAfter($codeElem) + $(EMPTY_P).insertAfter($codeElem) } /** diff --git a/src/menus/head/index.ts b/src/menus/head/index.ts index 035e5d8..191d9b9 100644 --- a/src/menus/head/index.ts +++ b/src/menus/head/index.ts @@ -9,6 +9,7 @@ import Editor from '../../editor/index' import { MenuActive } from '../menu-constructors/Menu' import { getRandomCode } from '../../utils/util' import { TCatalog } from '../../config/events' +import { EMPTY_P } from '../../utils/const' class Head extends DropListMenu implements MenuActive { oldCatalogs: TCatalog[] | undefined @@ -154,7 +155,7 @@ class Head extends DropListMenu implements MenuActive { let endElem = $($selection.getSelectionEndElem()) // 判断用户选中元素是否为最后一个空元素,如果是将endElem指向上一个元素 if ( - endElem.elems[0].outerHTML === $('


').elems[0].outerHTML && + endElem.elems[0].outerHTML === $(EMPTY_P).elems[0].outerHTML && !endElem.elems[0].nextSibling ) { endElem = endElem.prev()! diff --git a/src/menus/quote/bind-event/index.ts b/src/menus/quote/bind-event/index.ts index 2b60fbc..6e985a0 100644 --- a/src/menus/quote/bind-event/index.ts +++ b/src/menus/quote/bind-event/index.ts @@ -1,3 +1,4 @@ +import { EMPTY_P } from '../../../utils/const' import Editor from '../../../editor/index' import $, { DomElement } from '../../../utils/dom-core' function bindEvent(editor: Editor) { @@ -5,7 +6,7 @@ function bindEvent(editor: Editor) { const $selectElem = editor.selection.getSelectionContainerElem() as DomElement const $topSelectElem = editor.selection.getSelectionRangeTopNodes()[0] // 对quote的enter进行特殊处理 - //最后一行为


时再按会出跳出blockquote + //最后一行为空标签时再按会出跳出blockquote if ($topSelectElem?.getNodeName() === 'BLOCKQUOTE') { // firefox下点击引用按钮会选中外容器
if ($selectElem.getNodeName() === 'BLOCKQUOTE') { @@ -15,7 +16,7 @@ function bindEvent(editor: Editor) { if ($selectElem.text() === '') { e.preventDefault() $selectElem.remove() - const $newLine = $('


') + const $newLine = $(EMPTY_P) $newLine.insertAfter($topSelectElem) // 将光标移动br前面 editor.selection.moveCursor($newLine.getNode(), 0) diff --git a/src/menus/quote/index.ts b/src/menus/quote/index.ts index e5e44c0..9192605 100644 --- a/src/menus/quote/index.ts +++ b/src/menus/quote/index.ts @@ -9,6 +9,7 @@ import BtnMenu from '../menu-constructors/BtnMenu' import { MenuActive } from '../menu-constructors/Menu' import bindEvent from './bind-event' import createQuote from './create-quote-node' +import { EMPTY_P } from '../../utils/const' class Quote extends BtnMenu implements MenuActive { constructor(editor: Editor) { @@ -62,7 +63,7 @@ class Quote extends BtnMenu implements MenuActive { // 即时更新btn状态 this.tryChangeActive() // 防止最后一行无法跳出 - $(`


`).insertAfter($quote) + $(EMPTY_P).insertAfter($quote) return } diff --git a/src/menus/split-line/index.ts b/src/menus/split-line/index.ts index 7534e54..0dc0f8e 100644 --- a/src/menus/split-line/index.ts +++ b/src/menus/split-line/index.ts @@ -8,6 +8,7 @@ import Editor from '../../editor/index' import { MenuActive } from '../menu-constructors/Menu' import bindEvent from './bind-event/index' import { UA } from '../../utils/util' +import { EMPTY_P } from '../../utils/const' class splitLine extends BtnMenu implements MenuActive { constructor(editor: Editor) { const $elem = $( @@ -53,7 +54,7 @@ class splitLine extends BtnMenu implements MenuActive { */ private createSplitLine(): void { // 防止插入分割线时没有占位元素的尴尬 - let splitLineDOM: string = '


' + let splitLineDOM: string = `
${EMPTY_P}` // 火狐浏览器不需要br标签占位 if (UA.isFirefox) { splitLineDOM = '

' diff --git a/src/menus/table/bind-event/tooltip-event.ts b/src/menus/table/bind-event/tooltip-event.ts index 320f12b..08e335b 100644 --- a/src/menus/table/bind-event/tooltip-event.ts +++ b/src/menus/table/bind-event/tooltip-event.ts @@ -11,6 +11,7 @@ import Editor from '../../../editor/index' import operatingEvent from './event/operating-event' import getNode from './event/getNode' +import { EMPTY_P } from '../../../utils/const' /** * 生成 Tooltip 的显示隐藏函数 @@ -38,7 +39,7 @@ function createShowHideFn(editor: Editor) { // 选中img元素 editor.selection.createRangeByElem($node) editor.selection.restoreSelection() - editor.cmd.do('insertHTML', '


') + editor.cmd.do('insertHTML', EMPTY_P) // 返回 true,表示执行完之后,隐藏 tooltip。否则不隐藏。 return true }, @@ -104,7 +105,7 @@ function createShowHideFn(editor: Editor) { editor.selection.restoreSelection() if (trLength === 0) { - newdom = '


' + newdom = EMPTY_P } else { newdom = getnode.getTableHtml( operatingEvent.DeleteRow($(htmlStr), index).elems[0] @@ -166,7 +167,7 @@ function createShowHideFn(editor: Editor) { editor.selection.restoreSelection() if (tdLength === 1) { - newdom = '


' + newdom = EMPTY_P } else { newdom = getnode.getTableHtml( operatingEvent.DeleteCol($(htmlStr), index).elems[0] diff --git a/src/menus/table/create-table.ts b/src/menus/table/create-table.ts index 9aada2c..8d18e82 100644 --- a/src/menus/table/create-table.ts +++ b/src/menus/table/create-table.ts @@ -3,6 +3,7 @@ * @author lichunlin */ +import { EMPTY_P } from '../../utils/const' import Editor from '../../editor/index' import $ from '../../utils/dom-core' @@ -55,7 +56,7 @@ class CreateTable { const tableDom = `` + rowStr + - '


' + `${EMPTY_P}` return tableDom } } diff --git a/src/menus/todo/bind-event/index.ts b/src/menus/todo/bind-event/index.ts index e7bf9a2..2130a00 100644 --- a/src/menus/todo/bind-event/index.ts +++ b/src/menus/todo/bind-event/index.ts @@ -3,6 +3,7 @@ import $ from '../../../utils/dom-core' import { getCursorNextNode, isAllTodo } from '../util' import createTodo from '../todo' import { dealTextNode, isTodo } from '../util' +import { EMPTY_P } from '../../../utils/const' /** * todolist 内部逻辑 @@ -56,7 +57,7 @@ function bindEvent(editor: Editor) { // 回车时内容为空时,删去此行 if ($topSelectElem.text() === '') { - const $p = $(`


`) + const $p = $(EMPTY_P) $p.insertAfter($topSelectElem) selection.moveCursor($p.getNode()) $topSelectElem.remove() @@ -106,7 +107,7 @@ function bindEvent(editor: Editor) { // 处理内容为空的情况 if ($topSelectElem.text() === '') { e.preventDefault() - const $newP = $(`


`) + const $newP = $(EMPTY_P) $newP.insertAfter($topSelectElem) $topSelectElem.remove() selection.moveCursor($newP.getNode(), 0) @@ -139,7 +140,7 @@ function bindEvent(editor: Editor) { const $topSelectElem = selection.getSelectionRangeTopNodes()[0] if ($topSelectElem && isTodo($topSelectElem)) { if ($topSelectElem.text() === '') { - $(`


`).insertAfter($topSelectElem) + $(EMPTY_P).insertAfter($topSelectElem) $topSelectElem.remove() } } diff --git a/src/menus/video/create-panel-conf.ts b/src/menus/video/create-panel-conf.ts index 8e50fe6..ac380bf 100644 --- a/src/menus/video/create-panel-conf.ts +++ b/src/menus/video/create-panel-conf.ts @@ -8,6 +8,7 @@ import { PanelConf, PanelTabConf } from '../menu-constructors/Panel' import { getRandom } from '../../utils/util' import $ from '../../utils/dom-core' import UploadVideo from './upload-video' +import { EMPTY_P } from '../../utils/const' export default function (editor: Editor, video: string): PanelConf { const config = editor.config @@ -24,7 +25,7 @@ export default function (editor: Editor, video: string): PanelConf { * @param iframe html标签 */ function insertVideo(video: string): void { - editor.cmd.do('insertHTML', video + '


') + editor.cmd.do('insertHTML', video + EMPTY_P) // video添加后的回调 editor.config.onlineVideoCallback(video) diff --git a/src/menus/video/upload-video.ts b/src/menus/video/upload-video.ts index fcb4fdb..a4d1ee6 100644 --- a/src/menus/video/upload-video.ts +++ b/src/menus/video/upload-video.ts @@ -7,6 +7,7 @@ import Editor from '../../editor/index' import { arrForEach, forEach } from '../../utils/util' import post from '../../editor/upload/upload-core' import Progress from '../../editor/upload/progress' +import { EMPTY_P } from '../../utils/const' type ResData = { url: string @@ -251,7 +252,7 @@ class UploadVideo { if (!config.customInsertVideo) { editor.cmd.do( 'insertHTML', - `


` + `${EMPTY_P}` ) } else { config.customInsertVideo(url) diff --git a/src/text/event-hooks/del-to-keep-p.ts b/src/text/event-hooks/del-to-keep-p.ts index 9d6404b..67afc43 100644 --- a/src/text/event-hooks/del-to-keep-p.ts +++ b/src/text/event-hooks/del-to-keep-p.ts @@ -1,13 +1,14 @@ /** - * @description 删除时保留


+ * @description 删除时保留 EMPTY_P * @author wangfupeng */ +import { EMPTY_P } from '../../utils/const' import Editor from '../../editor/index' import $ from '../../utils/dom-core' /** - * 删除时保留


+ * 删除时保留 EMPTY_P * @param editor 编辑器实例 * @param deleteUpEvents delete 键 up 时的 hooks * @param deleteDownEvents delete 建 down 时的 hooks @@ -20,7 +21,7 @@ function deleteToKeepP(editor: Editor, deleteUpEvents: Function[], deleteDownEve // firefox 时用 txtHtml === '
' 判断,其他用 !txtHtml 判断 if (!txtHtml || txtHtml === '
') { // 内容空了 - const $p = $('


') + const $p = $(EMPTY_P) $textElem.html(' ') // 一定要先清空,否则在 firefox 下有问题 $textElem.append($p) editor.selection.createRangeByElem($p, false, true) @@ -35,7 +36,7 @@ function deleteToKeepP(editor: Editor, deleteUpEvents: Function[], deleteDownEve function downFn(e: Event) { const $textElem = editor.$textElem const txtHtml = $textElem.html().toLowerCase().trim() - if (txtHtml === '


') { + if (txtHtml === EMPTY_P) { // 最后剩下一个空行,就不再删除了 e.preventDefault() return diff --git a/src/text/event-hooks/enter-to-create-p.ts b/src/text/event-hooks/enter-to-create-p.ts index bd4d6f3..4fffbc5 100644 --- a/src/text/event-hooks/enter-to-create-p.ts +++ b/src/text/event-hooks/enter-to-create-p.ts @@ -40,8 +40,8 @@ function enterToCreateP(editor: Editor, enterUpEvents: Function[], enterDownEven } const nodeName = $selectionElem.getNodeName() - if (nodeName === 'P') { - // 当前的标签是 P ,不用做处理 + if (nodeName === 'P' && $selectionElem.attr('data-we-empty-p') === null) { + // 当前的标签是 P 且不为 editor 生成的空白占位 p 标签,不用做处理 return } diff --git a/src/text/event-hooks/index.ts b/src/text/event-hooks/index.ts index ee5bbd8..41a5f17 100644 --- a/src/text/event-hooks/index.ts +++ b/src/text/event-hooks/index.ts @@ -21,7 +21,7 @@ function initTextHooks(text: Text): void { // 回车时,保证生成的是

标签 enterToCreateP(editor, eventHooks.enterUpEvents, eventHooks.enterDownEvents) - // 删除时,保留


+ // 删除时,保留 EMPTY_P deleteToKeepP(editor, eventHooks.deleteUpEvents, eventHooks.deleteDownEvents) // tab 转换为空格 diff --git a/src/text/index.ts b/src/text/index.ts index 11a8d33..e9491ab 100644 --- a/src/text/index.ts +++ b/src/text/index.ts @@ -9,6 +9,7 @@ import initEventHooks from './event-hooks/index' import { UA, throttle } from '../utils/util' import getChildrenJSON, { NodeListType } from './getChildrenJSON' import getHtmlByNodeList from './getHtmlByNodeList' +import { EMPTY_P, EMPTY_P_LAST_REGEX, EMPTY_P_REGEX } from '../utils/const' /** 按键函数 */ type KeyBoardHandler = (event: KeyboardEvent) => unknown @@ -123,7 +124,7 @@ class Text { * 清空内容 */ public clear(): void { - this.html('


') + this.html(EMPTY_P) } /** @@ -139,12 +140,16 @@ class Text { let html = $textElem.html() // 未选中任何内容的时候点击“加粗”或者“斜体”等按钮,就得需要一个空的占位符 ​ ,这里替换掉 html = html.replace(/\u200b/gm, '') - html = html.replace(/

<\/p>/gim, '') // 去掉空行 - html = html.replace(/

<\/p>$/gim, '') // 去掉最后的


+ // 去掉空行 + html = html.replace(/

<\/p>/gim, '') + // 去掉最后的 空标签 + html = html.replace(EMPTY_P_LAST_REGEX, '') + // 为了避免用户在最后生成的EMPTY_P标签中编辑数据, 最后产生多余标签, 去除所有p标签上的data-we-empty-p属性 + html = html.replace(EMPTY_P_REGEX, '

') /** * 这里的代码为了处理火狐多余的空行标签,但是强制删除空行标签会带来其他问题 - * html()方法返回的的值,"


"中pr会被删除,只留下

,点不进去,从而产生垃圾数据 + * html()方法返回的的值,EMPTY_P中pr会被删除,只留下

,点不进去,从而产生垃圾数据 * 目前在末位有多个空行的情况下执行撤销重做操作,会产生一种不记录末尾空行的错觉 * 暂时注释, 等待进一步的兼容处理 */ @@ -174,7 +179,7 @@ class Text { // 有 val ,则是设置 html val = val.trim() if (val === '') { - val = `


` + val = EMPTY_P } if (val.indexOf('<') !== 0) { // 内容用 p 标签包裹 @@ -244,20 +249,11 @@ class Text { */ public append(html: string): void { const editor = this.editor - const $textElem = editor.$textElem - const blankLineReg = /(


<\/p>)+$/g if (html.indexOf('<') !== 0) { // 普通字符串,用

包裹 html = `

${html}

` } - if (blankLineReg.test($textElem.html().trim())) { - // 如果有多个空行替换最后一个


- const insertHtml = $textElem.html().replace(/(.*)


<\/p>/, '$1' + html) - this.html(insertHtml) - } else { - $textElem.append($(html)) - } - + this.html(this.html() + html) // 初始化选区,将光标定位到内容尾部 editor.initSelection() } diff --git a/src/utils/const.ts b/src/utils/const.ts index 80ee01d..2423d01 100644 --- a/src/utils/const.ts +++ b/src/utils/const.ts @@ -7,3 +7,12 @@ export function EMPTY_FN() {} //用于校验是否为url格式字符串 export const urlRegex = /^(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-.,@?^=%&:/~+#]*[\w\-@?^=%&/~+#])?/ + +// 编辑器为了方便继续输入/换行等原因 主动生成的空标签 +export const EMPTY_P = '


' + +// 用于校验dom中最后 由编辑器主动生成的空标签结构 +export const EMPTY_P_LAST_REGEX = /

<\/p>$/gim + +// 用于校验dom中所有 由编辑器主动生成的空标签结构 +export const EMPTY_P_REGEX = /

/gim diff --git a/test/unit/editor/history/decompile.test.ts b/test/unit/editor/history/decompile.test.ts index b7db2d1..319e68d 100644 --- a/test/unit/editor/history/decompile.test.ts +++ b/test/unit/editor/history/decompile.test.ts @@ -7,6 +7,7 @@ import createEditor from '../../../helpers/create-editor' import Editor from '../../../../src/editor' import compile from '../../../../src/editor/history/data/node/compile' import { restore, revoke } from '../../../../src/editor/history/data/node/decompilation' +import { EMPTY_P } from '../../../../src/utils/const' let editor: Editor @@ -28,7 +29,7 @@ describe('Editor history decompile', () => { revoke(compileData) - expect(editor.$textElem.html()).toEqual('


') + expect(editor.$textElem.html()).toEqual(EMPTY_P) done() }) diff --git a/test/unit/menus/redo.test.ts b/test/unit/menus/redo.test.ts index cbb3d11..88665da 100644 --- a/test/unit/menus/redo.test.ts +++ b/test/unit/menus/redo.test.ts @@ -23,7 +23,7 @@ test('重做 兼容模式', done => { const redo = getMenuInstance(editor, Redo) as Redo redo.clickHandler() // 兼容模式 - expect(editor.$textElem.html()).toEqual('


123

') + expect(editor.txt.html()).toEqual('


123

') } done() }, diff --git a/test/unit/menus/table/create-table.test.ts b/test/unit/menus/table/create-table.test.ts index 4878135..55035fc 100644 --- a/test/unit/menus/table/create-table.test.ts +++ b/test/unit/menus/table/create-table.test.ts @@ -6,6 +6,7 @@ import CreateTable from '../../../../src/menus/table/create-table' import createEditor from '../../../helpers/create-editor' import mockCommand from '../../../helpers/command-mock' import $ from '../../../../src/utils/dom-core' +import { EMPTY_P } from '../../../../src/utils/const' let editor: ReturnType let createTableInstance: CreateTable @@ -26,7 +27,7 @@ describe('Create Table Util', () => { expect(document.execCommand).toBeCalledWith( 'insertHTML', false, - `


` + `
${EMPTY_P}` ) }) diff --git a/test/unit/menus/undo.test.ts b/test/unit/menus/undo.test.ts index ae8761c..81dabff 100644 --- a/test/unit/menus/undo.test.ts +++ b/test/unit/menus/undo.test.ts @@ -6,6 +6,7 @@ import createEditor from '../../helpers/create-editor' import Undo from '../../../src/menus/undo/index' import mockCmdFn from '../../helpers/command-mock' import { getMenuInstance } from '../../helpers/menus' +import { EMPTY_P } from '../../../src/utils/const' test('撤销', done => { let count = 0 @@ -18,7 +19,7 @@ test('撤销', done => { undo.clickHandler() if (editor.isCompatibleMode) { // 兼容模式 - expect(editor.$textElem.html()).toEqual('


') + expect(editor.$textElem.html()).toEqual(EMPTY_P) } else { // 标准模式 expect(editor.history.size).toEqual([0, 1]) diff --git a/test/unit/menus/video/upload-video.test.ts b/test/unit/menus/video/upload-video.test.ts index bda80f2..07ad711 100644 --- a/test/unit/menus/video/upload-video.test.ts +++ b/test/unit/menus/video/upload-video.test.ts @@ -8,6 +8,7 @@ import mockFile from '../../../helpers/mock-file' import mockXHR from '../../../helpers/mock-xhr' import Editor from '../../../../src/editor' import UploadVideo from '../../../../src/menus/video/upload-video' +import { EMPTY_P } from '../../../../src/utils/const' let editor: Editor let id = 1 @@ -71,7 +72,7 @@ describe('upload video', () => { expect(document.execCommand).toBeCalledWith( 'insertHTML', false, - `


` + `${EMPTY_P}` ) }) @@ -89,7 +90,7 @@ describe('upload video', () => { expect(document.execCommand).toBeCalledWith( 'insertHTML', false, - `


` + `${EMPTY_P}` ) }) diff --git a/test/unit/text/del-to-keep-p.test.ts b/test/unit/text/del-to-keep-p.test.ts index 5d1ee4e..f7033a3 100644 --- a/test/unit/text/del-to-keep-p.test.ts +++ b/test/unit/text/del-to-keep-p.test.ts @@ -3,6 +3,7 @@ * @author luochao */ import delToKeepP from '../../../src/text/event-hooks/del-to-keep-p' +import { EMPTY_P } from '../../../src/utils/const' import createEditor from '../../helpers/create-editor' describe('editor.text event-hooks tab-to-space test', () => { @@ -17,7 +18,7 @@ describe('editor.text event-hooks tab-to-space test', () => { expect(downFns.length).toBe(1) }) - test('当编辑器内容为空时,执行 up 函数,则会插入


内容', () => { + test('当编辑器内容为空时,执行 up 函数,则会插入 EMPTY_P 内容', () => { const upFns: Function[] = [] const downFns: Function[] = [] const editor = createEditor(document, 'div2') @@ -30,10 +31,10 @@ describe('editor.text event-hooks tab-to-space test', () => { fn() }) - expect(editor.$textElem.elems[0].innerHTML).toEqual('


') + expect(editor.$textElem.html()).toEqual(EMPTY_P) }) - test('当编辑器内容只有
时,执行 up 函数,则会插入


内容', () => { + test('当编辑器内容只有
时,执行 up 函数,则会插入 EMPTY_P 内容', () => { const upFns: Function[] = [] const downFns: Function[] = [] const editor = createEditor(document, 'div3') @@ -46,17 +47,17 @@ describe('editor.text event-hooks tab-to-space test', () => { fn() }) - expect(editor.$textElem.elems[0].innerHTML).toEqual('


') + expect(editor.$textElem.html()).toEqual(` ${EMPTY_P}`) }) - test('当编辑器内容清空到只剩下


内容时,则不允许再删除', () => { + test('当编辑器内容清空到只剩下 EMPTY_P 内容时,则不允许再删除', () => { const upFns: Function[] = [] const downFns: Function[] = [] const editor = createEditor(document, 'div4') delToKeepP(editor, upFns, downFns) - editor.txt.html('


') + editor.txt.html(EMPTY_P) const e = new KeyboardEvent('mousedown') const mockPreventDefault = jest.fn() @@ -66,7 +67,7 @@ describe('editor.text event-hooks tab-to-space test', () => { fn(e) }) - expect(editor.$textElem.elems[0].innerHTML).toEqual('


') + expect(editor.$textElem.html()).toEqual(EMPTY_P) expect(mockPreventDefault).toBeCalled() }) }) diff --git a/test/unit/text/enter-to-create-p.test.ts b/test/unit/text/enter-to-create-p.test.ts index 8f9bd1c..c462c68 100644 --- a/test/unit/text/enter-to-create-p.test.ts +++ b/test/unit/text/enter-to-create-p.test.ts @@ -6,6 +6,7 @@ import enterToCreateP from '../../../src/text/event-hooks/enter-to-create-p' import $ from '../../../src/utils/dom-core' import createEditor from '../../helpers/create-editor' import commandMock from '../../helpers/command-mock' +import { EMPTY_P } from '../../../src/utils/const' type Editor = ReturnType @@ -38,7 +39,7 @@ describe('editor.text event-hooks tab-to-space test', () => { expect(downFns.length).toBe(1) }) - test('当编辑器选区内容父元素为
,则移除内容, 插入


', () => { + test('当编辑器选区内容父元素为
,则移除内容, 插入 EMPTY_P', () => { const upFns: Function[] = [] const downFns: Function[] = [] @@ -52,7 +53,7 @@ describe('editor.text event-hooks tab-to-space test', () => { fn() }) - expect(editor.$textElem.elems[0].innerHTML).toEqual('


') + expect(editor.$textElem.elems[0].innerHTML).toEqual(EMPTY_P) }) test('当编辑器选区内容的父元素不是 $textElm,则不处理', () => { diff --git a/test/unit/text/index.test.ts b/test/unit/text/index.test.ts index 374b5d4..85542f5 100644 --- a/test/unit/text/index.test.ts +++ b/test/unit/text/index.test.ts @@ -6,6 +6,7 @@ import createEditor from '../../helpers/create-editor' import $ from '../../../src/utils/dom-core' import dispatchEvent from '../../helpers/mock-dispatch-event' import { UA } from '../../../src/utils/util' +import { EMPTY_P } from '../../../src/utils/const' let editor: ReturnType let id = 1 @@ -73,13 +74,13 @@ describe('Editor Text test', () => { expect(editor.$textContainerElem.find('.placeholder').elems[0]).toHaveStyle('display:none') }) - test('编辑器初始化后,调用 txt clear 方法,清空编辑内容,只留下 p>

', () => { + test('编辑器初始化后,调用 txt clear 方法,清空编辑内容,只留下 EMPTY_P', () => { editor.txt.html('

123

') editor.txt.clear() expect(editor.txt.html()).toBe('') - expect(editor.$textElem.elems[0].innerHTML).toBe('


') + expect(editor.$textElem.elems[0].innerHTML).toBe(EMPTY_P) }) test('编辑器初始化后,调用 txt setJSON 方法将 JSON 内容设置成 html', () => {