-
Notifications
You must be signed in to change notification settings - Fork 3
/
textchunk.go
121 lines (105 loc) · 3.6 KB
/
textchunk.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
package lolhtml
/*
#include <stdlib.h>
#include "lol_html.h"
*/
import "C"
import "unsafe"
// TextChunk represents a text chunk.
type TextChunk C.lol_html_text_chunk_t
// TextChunkHandlerFunc is a callback handler function to do something with a TextChunk.
type TextChunkHandlerFunc func(*TextChunk) RewriterDirective
// Content returns the text chunk's content.
func (t *TextChunk) Content() string {
text := (textChunkContent)(C.lol_html_text_chunk_content_get((*C.lol_html_text_chunk_t)(t)))
return text.String()
}
// IsLastInTextNode returns whether the text chunk is the last in the text node.
func (t *TextChunk) IsLastInTextNode() bool {
return (bool)(C.lol_html_text_chunk_is_last_in_text_node((*C.lol_html_text_chunk_t)(t)))
}
type textChunkAlter int
const (
textChunkInsertBefore textChunkAlter = iota
textChunkInsertAfter
textChunkReplace
)
func (t *TextChunk) alter(content string, alter textChunkAlter, isHTML bool) error {
contentC := C.CString(content)
defer C.free(unsafe.Pointer(contentC))
contentLen := len(content)
var errCode C.int
switch alter {
case textChunkInsertBefore:
errCode = C.lol_html_text_chunk_before((*C.lol_html_text_chunk_t)(t), contentC, C.size_t(contentLen), C.bool(isHTML))
case textChunkInsertAfter:
errCode = C.lol_html_text_chunk_after((*C.lol_html_text_chunk_t)(t), contentC, C.size_t(contentLen), C.bool(isHTML))
case textChunkReplace:
errCode = C.lol_html_text_chunk_replace((*C.lol_html_text_chunk_t)(t), 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 text chunk.
//
// The rewriter will HTML-escape the content before insertion:
//
// `<` will be replaced with `<`
//
// `>` will be replaced with `>`
//
// `&` will be replaced with `&`
func (t *TextChunk) InsertBeforeAsText(content string) error {
return t.alter(content, textChunkInsertBefore, false)
}
// InsertBeforeAsHTML inserts the given content before the text chunk.
// The content is inserted as is.
func (t *TextChunk) InsertBeforeAsHTML(content string) error {
return t.alter(content, textChunkInsertBefore, true)
}
// InsertAfterAsText inserts the given content after the text chunk.
//
// The rewriter will HTML-escape the content before insertion:
//
// `<` will be replaced with `<`
//
// `>` will be replaced with `>`
//
// `&` will be replaced with `&`
func (t *TextChunk) InsertAfterAsText(content string) error {
return t.alter(content, textChunkInsertAfter, false)
}
// InsertAfterAsHTML inserts the given content after the text chunk.
// The content is inserted as is.
func (t *TextChunk) InsertAfterAsHTML(content string) error {
return t.alter(content, textChunkInsertAfter, true)
}
// ReplaceAsText replace the text chunk with the supplied content.
//
// The rewriter will HTML-escape the content:
//
// `<` will be replaced with `<`
//
// `>` will be replaced with `>`
//
// `&` will be replaced with `&`
func (t *TextChunk) ReplaceAsText(content string) error {
return t.alter(content, textChunkReplace, false)
}
// ReplaceAsHTML replace the text chunk with the supplied content.
// The content is kept as is.
func (t *TextChunk) ReplaceAsHTML(content string) error {
return t.alter(content, textChunkReplace, true)
}
// Remove removes the text chunk.
func (t *TextChunk) Remove() {
C.lol_html_text_chunk_remove((*C.lol_html_text_chunk_t)(t))
}
// IsRemoved returns whether the text chunk is removed or not.
func (t *TextChunk) IsRemoved() bool {
return (bool)(C.lol_html_text_chunk_is_removed((*C.lol_html_text_chunk_t)(t)))
}