Skip to content

Commit

Permalink
🐛 Vditor 支持 Vanessa219/vditor#118
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Feb 8, 2020
1 parent a0e9ceb commit d62e700
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
32 changes: 32 additions & 0 deletions html/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,38 @@ type Node struct {
Attr []Attribute
}

// Unlink 用于将节点从树上移除,后一个兄弟节点会接替该节点。
func (n *Node) Unlink() {
if nil != n.PrevSibling {
n.PrevSibling.NextSibling = n.NextSibling
} else if nil != n.Parent {
n.Parent.FirstChild = n.NextSibling
}
if nil != n.NextSibling {
n.NextSibling.PrevSibling = n.PrevSibling
} else if nil != n.Parent {
n.Parent.LastChild = n.PrevSibling
}
n.Parent = nil
n.NextSibling = nil
n.PrevSibling = nil
}

// InsertAfter 在当前节点后插入一个兄弟节点。
func (n *Node) InsertAfter(sibling *Node) {
sibling.Unlink()
sibling.NextSibling = n.NextSibling
if nil != sibling.NextSibling {
sibling.NextSibling.PrevSibling = sibling
}
sibling.PrevSibling = n
n.NextSibling = sibling
sibling.Parent = n.Parent
if nil == sibling.NextSibling {
sibling.Parent.LastChild = sibling
}
}

// InsertBefore inserts newChild as a child of n, immediately before oldChild
// in the sequence of n's children. oldChild may be nil, in which case newChild
// is appended to the end of n's children.
Expand Down
6 changes: 3 additions & 3 deletions javascript/lute.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion javascript/lute.min.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test/spinv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

var spinVditorDOMTests = []*parseTest{

{"85", "<p data-block=\"0\"><span data-marker=\"**\"><b>foo </b><span>b<wbr></span></span>", "<p data-block=\"0\"><strong data-marker=\"**\">foo</strong> b<wbr>\n</p>"},
{"84", "<ol data-tight=\"true\" data-block=\"0\"><li data-marker=\"1)\"><p>f<wbr></p></li></ol>", "<ol data-tight=\"true\" data-block=\"0\"><li data-marker=\"1)\">f<wbr></li></ol>"},
{"83", "<p data-block=\"0\">1) f<wbr></p>", "<ol data-tight=\"true\" data-block=\"0\"><li data-marker=\"1)\">f<wbr></li></ol>"},
{"82", "<ol data-tight=\"true\" data-block=\"0\"><li data-marker=\"2.\"><p>bar<wbr></p></li></ol>", "<ol data-tight=\"true\" data-block=\"0\"><li data-marker=\"1.\">bar<wbr></li></ol>"},
Expand Down
2 changes: 1 addition & 1 deletion test/v2m_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var vditorDOM2MdTests = []parseTest{
{"61", "<ul data-tight=\"true\"><li data-marker=\"*\">foo</li><li data-marker=\"*\"><wbr><br></li></ul>", "* foo\n*\n"},
{"60", "<ul><li data-marker=\"-\" class=\"vditor-task\"><p><input type=\"checkbox\"> foo\n</p></li><li data-marker=\"-\" class=\"vditor-task\"><p><input type=\"checkbox\"> b<wbr>\n</p></li></ul>", "- [ ] foo\n- [ ] b\n"},
{"59", "<ul><li data-marker=\"-\" class=\"vditor-task\"><p><input type=\"checkbox\" /> foo\n</p></li><li data-marker=\"-\" class=\"vditor-task\"><p><input type=\"checkbox\" /> b<wbr>\n</p></li></ul>", "- [ ] foo\n- [ ] b\n"},
{"58", "<p><em data-marker=\"*\">foo </em>bar<wbr>\n</p>", "*foo*bar\n"},
{"58", "<p><em data-marker=\"*\">foo </em>bar<wbr>\n</p>", "*foo* bar\n"},
{"57", "<h3>隐藏细节</h3><div class=\"vditor-wysiwyg__block\" data-type=\"html-block\"><pre><code>&lt;details&gt;\n&lt;summary&gt;\n这里是摘要部分。\n&lt;/summary&gt;\n这里是细节部分。&lt;/details&gt;<br></code></pre><div class=\"vditor-wysiwyg__preview\" contenteditable=\"false\" data-render=\"false\"></div></div><p>1<wbr></p>", "### 隐藏细节\n\n<details>\n<summary>\n这里是摘要部分。\n</summary>\n这里是细节部分。</details>\n\n1\n"},
{"56", "<p>~删除线~</p>", "~删除线~\n"},
{"55", "<ul data-tight=\"true\"><li data-marker=\"*\">foo</li><li data-marker=\"*\"><br></li><li data-marker=\"*\"><wbr>bar</li></ul>", "* foo\n*\n* bar\n"},
Expand Down
17 changes: 13 additions & 4 deletions vditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,11 @@ func (lute *Lute) genASTByVditorDOM(n *html.Node, tree *Tree) {
return
}

// 删掉前后空格,否则输入空格后会形成 *foo * 导致自旋失败
n.FirstChild.Data = strings.TrimSpace(n.FirstChild.Data)
// 结尾空格后会形成 *foo * 导致强调、加粗删除线标记失效,这里将空格移到右标记符后 *foo*
if strings.HasSuffix(n.FirstChild.Data, " ") {
n.FirstChild.Data = strings.TrimRight(n.FirstChild.Data, " ")
n.InsertAfter(&html.Node{Type: html.TextNode, Data: " "})
}

tree.context.tip = node
defer tree.context.parentTip(n)
Expand Down Expand Up @@ -474,7 +477,10 @@ func (lute *Lute) genASTByVditorDOM(n *html.Node, tree *Tree) {
return
}

n.FirstChild.Data = strings.TrimSpace(n.FirstChild.Data)
if strings.HasSuffix(n.FirstChild.Data, " ") {
n.FirstChild.Data = strings.TrimRight(n.FirstChild.Data, " ")
n.InsertAfter(&html.Node{Type: html.TextNode, Data: " "})
}

tree.context.tip = node
defer tree.context.parentTip(n)
Expand Down Expand Up @@ -623,7 +629,10 @@ func (lute *Lute) genASTByVditorDOM(n *html.Node, tree *Tree) {
return
}

n.FirstChild.Data = strings.TrimSpace(n.FirstChild.Data)
if strings.HasSuffix(n.FirstChild.Data, " ") {
n.FirstChild.Data = strings.TrimRight(n.FirstChild.Data, " ")
n.InsertAfter(&html.Node{Type: html.TextNode, Data: " "})
}

tree.context.tip = node
defer tree.context.parentTip(n)
Expand Down

0 comments on commit d62e700

Please sign in to comment.