fix: txt.html()获取的内容中将没有自闭和的标签输出为自闭和标签

This commit is contained in:
rsl140 2021-02-02 14:11:04 +08:00
parent af732713bb
commit 57690447ed
4 changed files with 18 additions and 4 deletions

View File

@ -19,12 +19,14 @@
<img src="http://www.wangeditor.com/imgs/logo.jpeg" />
</p>
</div>
<button id="btn">获取html内容</button>
<script src="../dist/wangEditor.js"></script>
<script>
// 改为使用var声明才能在window对象上获取到编辑器实例方便e2e测试
var E = window.wangEditor
var editor = new E('#div1')
editor.config.uploadImgServer = '/api/upload-img'
editor.config.onchange = function (newHtml) {
console.log('onchange', newHtml)
@ -33,6 +35,9 @@
editor.config.showFullScreen = true
editor.create()
editor.txt.html("<p>123</p><p>456</p>")
document.getElementById('btn').onclick = function () {
console.log(editor.txt.html())
}
</script>
</body>

View File

@ -159,6 +159,15 @@ class Text {
*/
// html = formatCodeHtml(editor, html)
// 将没有自闭和的标签过滤为自闭和
const selfCloseHtmls: RegExpMatchArray | null = html.match(/<(img|br|hr|input)[^>]*>/gi)
if (selfCloseHtmls !== null) {
selfCloseHtmls.forEach(item => {
if (!item.match(/\/>/)) {
html = html.replace(item, item.substring(0, item.length - 1) + '/>')
}
})
}
return html
}

View File

@ -27,7 +27,7 @@ describe('split-line menu', () => {
splitLineMenu.clickHandler()
expect((editor.txt.html() as string).indexOf('<hr>')).toBeGreaterThanOrEqual(0)
expect((editor.txt.html() as string).indexOf('<hr/>')).toBeGreaterThanOrEqual(0)
})
test('执行 splitLineEvents 里面的钩子函数会展示 tooltip 菜单点击其它地方会隐藏tooptip', () => {
@ -52,7 +52,7 @@ describe('split-line menu', () => {
splitLineMenu.clickHandler()
expect((editor.txt.html() as string).indexOf('<hr>')).toBeGreaterThanOrEqual(0)
expect((editor.txt.html() as string).indexOf('<hr/>')).toBeGreaterThanOrEqual(0)
editor.txt.eventHooks.splitLineEvents.forEach(fn => {
fn(splitLineMenu.$elem)

View File

@ -9,7 +9,7 @@ editor.txt.append('<p>abc</p><p>test</p>')
test('设置todo功能', () => {
boldMenu.clickHandler()
expect(editor.txt.html()).toEqual(
`<p>abc</p><ul class="w-e-todo"><li><span contenteditable="false"><input type="checkbox"></span>test</li></ul>`
`<p>abc</p><ul class="w-e-todo"><li><span contenteditable="false"><input type="checkbox"/></span>test</li></ul>`
)
})
@ -23,6 +23,6 @@ test('在第一行设置todo', () => {
const boldMenu = getMenuInstance(editor, todo)
boldMenu.clickHandler()
expect(editor.txt.html()).toEqual(
'<ul class="w-e-todo"><li><span contenteditable="false"><input type="checkbox"></span><br></li></ul>'
'<ul class="w-e-todo"><li><span contenteditable="false"><input type="checkbox"/></span><br/></li></ul>'
)
})