-
Notifications
You must be signed in to change notification settings - Fork 3
/
comment.go
130 lines (114 loc) · 3.68 KB
/
comment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package lolhtml
/*
#include <stdlib.h>
#include "lol_html.h"
*/
import "C"
import "unsafe"
// Comment represents an HTML comment.
type Comment C.lol_html_comment_t
// CommentHandlerFunc is a callback handler function to do something with a Comment.
// Expected to return a RewriterDirective as instruction to continue or stop.
type CommentHandlerFunc func(*Comment) RewriterDirective
// Text returns the comment's text.
func (c *Comment) Text() string {
textC := (str)(C.lol_html_comment_text_get((*C.lol_html_comment_t)(c)))
defer textC.Free()
return textC.String()
}
// SetText sets the comment's text and returns an error if there is one.
func (c *Comment) SetText(text string) error {
textC := C.CString(text)
defer C.free(unsafe.Pointer(textC))
textLen := len(text)
errCode := C.lol_html_comment_text_set((*C.lol_html_comment_t)(c), textC, C.size_t(textLen))
if errCode == 0 {
return nil
}
return getError()
}
type commentAlter int
const (
commentInsertBefore commentAlter = iota
commentInsertAfter
commentReplace
)
func (c *Comment) alter(content string, alter commentAlter, isHTML bool) error {
contentC := C.CString(content)
defer C.free(unsafe.Pointer(contentC))
contentLen := len(content)
var errCode C.int
switch alter {
case commentInsertBefore:
errCode = C.lol_html_comment_before((*C.lol_html_comment_t)(c), contentC, C.size_t(contentLen), C.bool(isHTML))
case commentInsertAfter:
errCode = C.lol_html_comment_after((*C.lol_html_comment_t)(c), contentC, C.size_t(contentLen), C.bool(isHTML))
case commentReplace:
errCode = C.lol_html_comment_replace((*C.lol_html_comment_t)(c), contentC, C.size_t(contentLen), C.bool(isHTML))
default:
panic("not implemented")
}
if errCode == 0 {
return nil
}
return getError()
}
// InsertBeforeAsText inserts the given content before the comment.
//
// The rewriter will HTML-escape the content before insertion:
//
// `<` will be replaced with `<`
//
// `>` will be replaced with `>`
//
// `&` will be replaced with `&`
func (c *Comment) InsertBeforeAsText(content string) error {
return c.alter(content, commentInsertAfter, false)
}
// InsertBeforeAsHTML inserts the given content before the comment.
// The content is inserted as is.
func (c *Comment) InsertBeforeAsHTML(content string) error {
return c.alter(content, commentInsertBefore, true)
}
// InsertAfterAsText inserts the given content before the comment.
//
// The rewriter will HTML-escape the content before insertion:
//
// `<` will be replaced with `<`
//
// `>` will be replaced with `>`
//
// `&` will be replaced with `&`
func (c *Comment) InsertAfterAsText(content string) error {
return c.alter(content, commentInsertAfter, false)
}
// InsertAfterAsHTML inserts the given content before the comment.
// The content is inserted as is.
func (c *Comment) InsertAfterAsHTML(content string) error {
return c.alter(content, commentInsertAfter, true)
}
// ReplaceAsText replace the comment with the supplied content.
//
// The rewriter will HTML-escape the content:
//
// `<` will be replaced with `<`
//
// `>` will be replaced with `>`
//
// `&` will be replaced with `&`
func (c *Comment) ReplaceAsText(content string) error {
return c.alter(content, commentReplace, false)
}
// ReplaceAsHTML replace the comment with the supplied content.
// The content is kept as is.
func (c *Comment) ReplaceAsHTML(content string) error {
return c.alter(content, commentReplace, true)
}
// Remove removes the comment.
func (c *Comment) Remove() {
C.lol_html_comment_remove((*C.lol_html_comment_t)(c))
}
// IsRemoved returns whether the comment is removed or not.
func (c *Comment) IsRemoved() bool {
return (bool)(C.lol_html_comment_is_removed((*C.lol_html_comment_t)(c)))
}