Newer
Older
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package html
import (
"bytes"
"github.com/andybalholm/cascadia"
"gitlab.schukai.com/oss/bob/types"
"golang.org/x/net/html"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
)
// Helper function to create a temporary file with content
func createTempFile(content string) (string, error) {
tmpfile, err := ioutil.TempFile("", "example.*.html")
if err != nil {
return "", err
}
if _, err := tmpfile.Write([]byte(content)); err != nil {
return "", err
}
if err := tmpfile.Close(); err != nil {
return "", err
}
return tmpfile.Name(), nil
}
// TestCutHtml tests the CutHtml function
func TestCutHtml(t *testing.T) {
// Create temporary directory
tempDir, err := ioutil.TempDir("", "testcuthtml")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(tempDir)
// Create a temporary source HTML file
sourceHTML := `<div><p class="content">Original content</p></div>`
sourcePath := filepath.Join(tempDir, "source.html")
if err := ioutil.WriteFile(sourcePath, []byte(sourceHTML), 0644); err != nil {
t.Fatalf("Failed to write source HTML file: %v", err)
}
// Create a SnippetsSpecification in YAML format
spec := types.SnippetsSpecification{
Snippets: []types.Snippet{
{
Source: sourcePath,
Destination: sourcePath,
Replacement: []types.ContentReplacement{
{
Selector: ".content",
Content: `<p class="content">Replaced content</p>`,
},
},
},
},
}
specBytes, err := yaml.Marshal(spec)
if err != nil {
t.Fatalf("Failed to marshal YAML: %v", err)
}
// Write the YAML to a temporary file
specPath := filepath.Join(tempDir, "spec.yaml")
if err := ioutil.WriteFile(specPath, specBytes, 0644); err != nil {
t.Fatalf("Failed to write spec YAML file: %v", err)
}
// Run the CutHtml function
if err := CutHtml(specPath); err != nil {
t.Fatalf("CutHtml failed: %v", err)
}
// Verify the result
modifiedHTML, err := ioutil.ReadFile(sourcePath)
if err != nil {
t.Fatalf("Failed to read modified HTML file: %v", err)
}
expectedHTML := `<div><p class="content">Replaced content</p></div>`
if string(modifiedHTML) != expectedHTML {
t.Errorf("Expected HTML to be '%v', but got '%v'", expectedHTML, string(modifiedHTML))
}
}
// TestSetAttributes tests the setAttributes function
func TestSetAttributes(t *testing.T) {
// Example HTML node
rawHTML := `<div><p class="old-class">Hello</p></div>`
node, _ := html.Parse(strings.NewReader(rawHTML))
attrs := []types.Attributes{{Selector: "p", Name: "class", Value: "new-class"}}
// Perform the attribute setting
err := setAttributes(node, attrs)
if err != nil {
t.Errorf("setAttributes failed: %v", err)
}
// Check if the attribute was set correctly
query, _ := cascadia.Compile("p")
pNode := query.MatchFirst(node)
if pNode == nil {
t.Errorf("p node not found")
} else if pNode.Attr[0].Val != "new-class" {
t.Errorf("Attribute not set correctly, got: %s, want: new-class", pNode.Attr[0].Val)
}
}
// TestRemoveAttribute tests the removeAttribute function
func TestRemoveAttribute(t *testing.T) {
// Example attributes
attrs := []html.Attribute{{Key: "class", Val: "old-class"}, {Key: "id", Val: "test-id"}}
// Remove the 'class' attribute
updatedAttrs := removeAttribute(attrs, "class")
// Check if the 'class' attribute is removed
for _, attr := range updatedAttrs {
if attr.Key == "class" {
t.Errorf("Attribute 'class' was not removed")
}
}
}
func TestReplaceNodes(t *testing.T) {
// Example HTML node
rawHTML := `<div><p class="target">Old Content</p><p class="untouched">Don't touch this</p></div>`
node, _ := html.Parse(strings.NewReader(rawHTML))
replacements := []types.ContentReplacement{
{Selector: ".target", Content: "<p>New Content</p>"},
}
// Perform the replacement
replacedNode, err := replaceNodes(node, replacements)
if err != nil {
t.Fatalf("replaceNodes failed: %v", err)
}
// Convert the node back to HTML for easy verification
var buf bytes.Buffer
html.Render(&buf, replacedNode)
replacedHTML := buf.String()
// Expected HTML after replacement
expectedHTML := `<html><head></head><body><div><p>New Content</p><p class="untouched">Don't touch this</p></div></body></html>`
// Verify the replacement
if replacedHTML != expectedHTML {
t.Errorf("Expected HTML to be '%v', but got '%v'", expectedHTML, replacedHTML)
}
}