Skip to content

Commit d9c03f0

Browse files
committed
Deprecate Node.Text
Node.Text was intended to get a text value from some inline nodes. A 'text value' of a Text node is clear. But - BaseNode had a default implementation of Node.Text - Lacks of GoDoc description that Node.Text is valid only for some inline nodes So, some users are using Node.Text for BlockNodes. A 'text value' for a BlockNode is not clear. e.g. : Text value of a ListNode - It should be contains list markers? - What do characters concatinate List items with? newlines? spaces? - If it contains codeblocks, codeblocks should be fenced or indented? Now we would like to avoid such ambiguous method.
1 parent 65dcf6c commit d9c03f0

File tree

6 files changed

+61
-28
lines changed

6 files changed

+61
-28
lines changed

ast/ast.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ type Node interface {
127127
// If this node is a block node, Text returns a text value as reasonable as possible.
128128
// Notice that there are no 'correct' text values for the block nodes.
129129
// Result for the block nodes may be different from your expectation.
130+
//
131+
// Deprecated: Use other properties of the node to get the text value(i.e. Pragraph.Lines, Text.Value).
130132
Text(source []byte) []byte
131133

132134
// HasBlankPreviousLines returns true if the row before this node is blank,
@@ -378,7 +380,9 @@ func (n *BaseNode) OwnerDocument() *Document {
378380
return nil
379381
}
380382

381-
// Text implements Node.Text .
383+
// Text implements Node.Text .
384+
//
385+
// Deprecated: Use other properties of the node to get the text value(i.e. Pragraph.Lines, Text.Value).
382386
func (n *BaseNode) Text(source []byte) []byte {
383387
var buf bytes.Buffer
384388
for c := n.firstChild; c != nil; c = c.NextSibling() {

ast/block.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ func (n *TextBlock) Kind() NodeKind {
131131
}
132132

133133
// Text implements Node.Text.
134+
//
135+
// Deprecated: Use other properties of the node to get the text value(i.e. TextBlock.Lines).
134136
func (n *TextBlock) Text(source []byte) []byte {
135137
return n.Lines().Value(source)
136138
}
@@ -161,6 +163,8 @@ func (n *Paragraph) Kind() NodeKind {
161163
}
162164

163165
// Text implements Node.Text.
166+
//
167+
// Deprecated: Use other properties of the node to get the text value(i.e. Paragraph.Lines).
164168
func (n *Paragraph) Text(source []byte) []byte {
165169
return n.Lines().Value(source)
166170
}
@@ -260,6 +264,8 @@ func (n *CodeBlock) Kind() NodeKind {
260264
}
261265

262266
// Text implements Node.Text.
267+
//
268+
// Deprecated: Use other properties of the node to get the text value(i.e. CodeBlock.Lines).
263269
func (n *CodeBlock) Text(source []byte) []byte {
264270
return n.Lines().Value(source)
265271
}
@@ -320,6 +326,8 @@ func (n *FencedCodeBlock) Kind() NodeKind {
320326
}
321327

322328
// Text implements Node.Text.
329+
//
330+
// Deprecated: Use other properties of the node to get the text value(i.e. FencedCodeBlock.Lines).
323331
func (n *FencedCodeBlock) Text(source []byte) []byte {
324332
return n.Lines().Value(source)
325333
}
@@ -519,6 +527,8 @@ func (n *HTMLBlock) Kind() NodeKind {
519527
}
520528

521529
// Text implements Node.Text.
530+
//
531+
// Deprecated: Use other properties of the node to get the text value(i.e. HTMLBlock.Lines).
522532
func (n *HTMLBlock) Text(source []byte) []byte {
523533
ret := n.Lines().Value(source)
524534
if n.HasClosure() {

ast/inline.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,25 @@ func (n *Text) Merge(node Node, source []byte) bool {
143143
}
144144

145145
// Text implements Node.Text.
146+
//
147+
// Deprecated: Use other properties of the node to get the text value(i.e. Text.Value).
146148
func (n *Text) Text(source []byte) []byte {
147149
return n.Segment.Value(source)
148150
}
149151

152+
// Value returns a value of this node.
153+
// SoftLineBreaks are not included in the returned value.
154+
func (n *Text) Value(source []byte) []byte {
155+
return n.Segment.Value(source)
156+
}
157+
150158
// Dump implements Node.Dump.
151159
func (n *Text) Dump(source []byte, level int) {
152160
fs := textFlagsString(n.flags)
153161
if len(fs) != 0 {
154162
fs = "(" + fs + ")"
155163
}
156-
fmt.Printf("%sText%s: \"%s\"\n", strings.Repeat(" ", level), fs, strings.TrimRight(string(n.Text(source)), "\n"))
164+
fmt.Printf("%sText%s: \"%s\"\n", strings.Repeat(" ", level), fs, strings.TrimRight(string(n.Value(source)), "\n"))
157165
}
158166

159167
// KindText is a NodeKind of the Text node.
@@ -258,6 +266,8 @@ func (n *String) SetCode(v bool) {
258266
}
259267

260268
// Text implements Node.Text.
269+
//
270+
// Deprecated: Use other properties of the node to get the text value(i.e. String.Value).
261271
func (n *String) Text(source []byte) []byte {
262272
return n.Value
263273
}
@@ -492,20 +502,22 @@ func (n *AutoLink) URL(source []byte) []byte {
492502
ret := make([]byte, 0, len(n.Protocol)+s.Len()+3)
493503
ret = append(ret, n.Protocol...)
494504
ret = append(ret, ':', '/', '/')
495-
ret = append(ret, n.value.Text(source)...)
505+
ret = append(ret, n.value.Value(source)...)
496506
return ret
497507
}
498-
return n.value.Text(source)
508+
return n.value.Value(source)
499509
}
500510

501511
// Label returns a label of this node.
502512
func (n *AutoLink) Label(source []byte) []byte {
503-
return n.value.Text(source)
513+
return n.value.Value(source)
504514
}
505515

506516
// Text implements Node.Text.
517+
//
518+
// Deprecated: Use other properties of the node to get the text value(i.e. AutoLink.Label).
507519
func (n *AutoLink) Text(source []byte) []byte {
508-
return n.value.Text(source)
520+
return n.value.Value(source)
509521
}
510522

511523
// NewAutoLink returns a new AutoLink node.
@@ -547,6 +559,8 @@ func (n *RawHTML) Kind() NodeKind {
547559
}
548560

549561
// Text implements Node.Text.
562+
//
563+
// Deprecated: Use other properties of the node to get the text value(i.e. RawHTML.Segments).
550564
func (n *RawHTML) Text(source []byte) []byte {
551565
return n.Segments.Value(source)
552566
}

ast_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,15 @@ l4`,
141141
c1 = c1.FirstChild()
142142
c2 = c2.FirstChild()
143143
}
144-
if !bytes.Equal(c1.Text(s), []byte(cs.T1)) {
145-
t.Errorf("%s unmatch: %s", cs.Name, testutil.DiffPretty(c1.Text(s), []byte(cs.T1)))
144+
if !bytes.Equal(c1.Text(s), []byte(cs.T1)) { // nolint: staticcheck
145+
146+
t.Errorf("%s unmatch: %s", cs.Name, testutil.DiffPretty(c1.Text(s), []byte(cs.T1))) // nolint: staticcheck
147+
146148
}
147-
if !bytes.Equal(c2.Text(s), []byte(cs.T2)) {
148-
t.Errorf("%s(EOF) unmatch: %s", cs.Name, testutil.DiffPretty(c2.Text(s), []byte(cs.T2)))
149+
if !bytes.Equal(c2.Text(s), []byte(cs.T2)) { // nolint: staticcheck
150+
151+
t.Errorf("%s(EOF) unmatch: %s", cs.Name, testutil.DiffPretty(c2.Text(s), []byte(cs.T2))) // nolint: staticcheck
152+
149153
}
150154
})
151155
}
@@ -191,8 +195,8 @@ func TestASTInlineNodeText(t *testing.T) {
191195
md := New()
192196
n := md.Parser().Parse(text.NewReader(s))
193197
c1 := n.FirstChild().FirstChild()
194-
if !bytes.Equal(c1.Text(s), []byte(cs.T1)) {
195-
t.Errorf("%s unmatch:\n%s", cs.Name, testutil.DiffPretty(c1.Text(s), []byte(cs.T1)))
198+
if !bytes.Equal(c1.Text(s), []byte(cs.T1)) { // nolint: staticcheck
199+
t.Errorf("%s unmatch:\n%s", cs.Name, testutil.DiffPretty(c1.Text(s), []byte(cs.T1))) // nolint: staticcheck
196200
}
197201
})
198202
}

extension/ast_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ a
7171
c1 = c1.FirstChild()
7272
c2 = c2.FirstChild()
7373
}
74-
if !bytes.Equal(c1.Text(s), []byte(cs.T1)) {
75-
t.Errorf("%s unmatch:\n%s", cs.Name, testutil.DiffPretty(c1.Text(s), []byte(cs.T1)))
74+
if !bytes.Equal(c1.Text(s), []byte(cs.T1)) { // nolint: staticcheck
75+
76+
t.Errorf("%s unmatch:\n%s", cs.Name, testutil.DiffPretty(c1.Text(s), []byte(cs.T1))) // nolint: staticcheck
77+
7678
}
77-
if !bytes.Equal(c2.Text(s), []byte(cs.T2)) {
78-
t.Errorf("%s(EOF) unmatch: %s", cs.Name, testutil.DiffPretty(c2.Text(s), []byte(cs.T2)))
79+
if !bytes.Equal(c2.Text(s), []byte(cs.T2)) { // nolint: staticcheck
80+
81+
t.Errorf("%s(EOF) unmatch: %s", cs.Name, testutil.DiffPretty(c2.Text(s), []byte(cs.T2))) // nolint: staticcheck
82+
7983
}
8084
})
8185
}
@@ -108,8 +112,10 @@ func TestASTInlineNodeText(t *testing.T) {
108112
)
109113
n := md.Parser().Parse(text.NewReader(s))
110114
c1 := n.FirstChild().FirstChild()
111-
if !bytes.Equal(c1.Text(s), []byte(cs.T1)) {
112-
t.Errorf("%s unmatch:\n%s", cs.Name, testutil.DiffPretty(c1.Text(s), []byte(cs.T1)))
115+
if !bytes.Equal(c1.Text(s), []byte(cs.T1)) { // nolint: staticcheck
116+
117+
t.Errorf("%s unmatch:\n%s", cs.Name, testutil.DiffPretty(c1.Text(s), []byte(cs.T1))) // nolint: staticcheck
118+
113119
}
114120
})
115121
}

renderer/html/html.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ func (r *Renderer) renderImage(w util.BufWriter, source []byte, node ast.Node, e
680680
_, _ = w.Write(util.EscapeHTML(util.URLEscape(n.Destination, true)))
681681
}
682682
_, _ = w.WriteString(`" alt="`)
683-
r.renderAttribute(w, source, n)
683+
r.renderTexts(w, source, n)
684684
_ = w.WriteByte('"')
685685
if n.Title != nil {
686686
_, _ = w.WriteString(` title="`)
@@ -737,7 +737,7 @@ func (r *Renderer) renderText(w util.BufWriter, source []byte, node ast.Node, en
737737
if r.EastAsianLineBreaks != EastAsianLineBreaksNone && len(value) != 0 {
738738
sibling := node.NextSibling()
739739
if sibling != nil && sibling.Kind() == ast.KindText {
740-
if siblingText := sibling.(*ast.Text).Text(source); len(siblingText) != 0 {
740+
if siblingText := sibling.(*ast.Text).Value(source); len(siblingText) != 0 {
741741
thisLastRune := util.ToRune(value, len(value)-1)
742742
siblingFirstRune, _ := utf8.DecodeRune(siblingText)
743743
if r.EastAsianLineBreaks.softLineBreak(thisLastRune, siblingFirstRune) {
@@ -770,19 +770,14 @@ func (r *Renderer) renderString(w util.BufWriter, source []byte, node ast.Node,
770770
return ast.WalkContinue, nil
771771
}
772772

773-
func (r *Renderer) renderAttribute(w util.BufWriter, source []byte, n ast.Node) {
773+
func (r *Renderer) renderTexts(w util.BufWriter, source []byte, n ast.Node) {
774774
for c := n.FirstChild(); c != nil; c = c.NextSibling() {
775775
if s, ok := c.(*ast.String); ok {
776776
_, _ = r.renderString(w, source, s, true)
777-
} else if t, ok := c.(*ast.String); ok {
777+
} else if t, ok := c.(*ast.Text); ok {
778778
_, _ = r.renderText(w, source, t, true)
779-
} else if !c.HasChildren() {
780-
r.Writer.Write(w, c.Text(source))
781-
if t, ok := c.(*ast.Text); ok && t.SoftLineBreak() {
782-
_ = w.WriteByte('\n')
783-
}
784779
} else {
785-
r.renderAttribute(w, source, c)
780+
r.renderTexts(w, source, c)
786781
}
787782
}
788783
}

0 commit comments

Comments
 (0)