Skip to content

Commit 4a3823e

Browse files
authored
Merge pull request #11 for v0.8 Release
2 parents 8f7e38b + 282680d commit 4a3823e

31 files changed

Lines changed: 583 additions & 307 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ branches:
1212
go:
1313
- 1.8
1414
- 1.9
15+
- "1.10"
1516
- tip
1617

1718
go_import_path: aahframework.org/view.v0
1819

1920
install:
20-
- git config --global http.https://aahframework.org.followRedirects true
2121
- go get -t -v ./...
2222

2323
script:

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2016-2017 Jeevanandam M., https://myjeeva.com <jeeva@myjeeva.com>
3+
Copyright (c) 2016-2018 Jeevanandam M., https://myjeeva.com <jeeva@myjeeva.com>
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# view - aah framework
2-
[![Build Status](https://travis-ci.org/go-aah/view.svg?branch=master)](https://travis-ci.org/go-aah/view) [![codecov](https://codecov.io/gh/go-aah/view/branch/master/graph/badge.svg)](https://codecov.io/gh/go-aah/view/branch/master) [![Go Report Card](https://goreportcard.com/badge/aahframework.org/view.v0)](https://goreportcard.com/report/aahframework.org/view.v0) [![Version](https://img.shields.io/badge/version-0.7-blue.svg)](https://github.com/go-aah/view/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/view.v0?status.svg)](https://godoc.org/aahframework.org/view.v0) [![License](https://img.shields.io/github/license/go-aah/view.svg)](LICENSE) [![Twitter](https://img.shields.io/badge/twitter-@aahframework-55acee.svg)](https://twitter.com/aahframework)
2+
[![Build Status](https://travis-ci.org/go-aah/view.svg?branch=master)](https://travis-ci.org/go-aah/view) [![codecov](https://codecov.io/gh/go-aah/view/branch/master/graph/badge.svg)](https://codecov.io/gh/go-aah/view/branch/master) [![Go Report Card](https://goreportcard.com/badge/aahframework.org/view.v0)](https://goreportcard.com/report/aahframework.org/view.v0) [![Version](https://img.shields.io/badge/version-0.8-blue.svg)](https://github.com/go-aah/view/releases/latest) [![GoDoc](https://godoc.org/aahframework.org/view.v0?status.svg)](https://godoc.org/aahframework.org/view.v0) [![License](https://img.shields.io/github/license/go-aah/view.svg)](LICENSE) [![Twitter](https://img.shields.io/badge/twitter-@aahframework-55acee.svg)](https://twitter.com/aahframework)
33

4-
***v0.7 [released](https://github.com/go-aah/view/releases/latest) and tagged on Oct 04, 2017***
4+
***v0.8 [released](https://github.com/go-aah/view/releases/latest) and tagged on Mar 26, 2018***
55

66
Go HTML template library which supports partial template inheritance, imports, etc.
77

anti_csrf_field.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2+
// go-aah/view source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package view
6+
7+
import (
8+
"fmt"
9+
"io/ioutil"
10+
"path/filepath"
11+
"strings"
12+
13+
"aahframework.org/essentials.v0"
14+
"aahframework.org/log.v0"
15+
)
16+
17+
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
18+
// type AntiCSRFField and methods
19+
//________________________________________
20+
21+
// AntiCSRFField is used to insert Anti-CSRF HTML field dynamically
22+
// while parsing templates on view engine.
23+
type AntiCSRFField struct {
24+
engineName string
25+
field string
26+
inserter *strings.Replacer
27+
leftDelim string
28+
rightDelim string
29+
}
30+
31+
// NewAntiCSRFField method creates new instance of Anti-CSRF HTML field
32+
// parser.
33+
func NewAntiCSRFField(engineName, leftDelim, rightDelim string) *AntiCSRFField {
34+
csft := &AntiCSRFField{engineName: engineName, leftDelim: leftDelim, rightDelim: rightDelim}
35+
36+
csft.field = fmt.Sprintf(` <input type="hidden" name="anti_csrf_token" value="%s anitcsrftoken . %s">
37+
</form>`, csft.leftDelim, csft.rightDelim)
38+
csft.inserter = strings.NewReplacer("</form>", csft.field)
39+
40+
return csft
41+
}
42+
43+
// InsertOnFile method inserts the Anti-CSRF HTML field for given HTML file and
44+
// writes a processed file into temp directory then return the new file path.
45+
func (ft *AntiCSRFField) InsertOnFiles(files ...string) []string {
46+
var ofiles []string
47+
48+
for _, f := range files {
49+
fpath, err := ft.InsertOnFile(f)
50+
if err != nil {
51+
log.Errorf("anitcsrffield: unable to insert Anti-CSRF field for file: %s", f)
52+
ofiles = append(ofiles, f)
53+
continue
54+
}
55+
ofiles = append(ofiles, fpath)
56+
}
57+
58+
return ofiles
59+
}
60+
61+
// InsertOnFile method inserts the Anti-CSRF HTML filed for given HTML file and
62+
// writes a processed file into temp directory then return the new file path.
63+
func (ft *AntiCSRFField) InsertOnFile(file string) (string, error) {
64+
tmpDir, _ := ioutil.TempDir("", ft.engineName+"_anti_csrf")
65+
66+
fileBytes, err := ioutil.ReadFile(file)
67+
if err != nil {
68+
return "", err
69+
}
70+
71+
fileStr := string(fileBytes)
72+
f := StripPathPrefixAt(file, "views")
73+
fpath := filepath.Join(tmpDir, f)
74+
if strings.Contains(fileStr, "</form>") {
75+
log.Tracef("Inserting Anti-CSRF field for file: %s", filepath.Join("views", f))
76+
fileStr = ft.InsertOnString(fileStr)
77+
if err = ess.MkDirAll(filepath.Dir(fpath), 0755); err != nil {
78+
return "", err
79+
}
80+
81+
if err = ioutil.WriteFile(fpath, []byte(fileStr), 0755); err != nil {
82+
return "", err
83+
}
84+
85+
return fpath, nil
86+
}
87+
88+
return file, nil
89+
}
90+
91+
// InsertOnString method inserts the Anti-CSRF HTML field on
92+
// given HTML string and returns the processed HTML string.
93+
func (ft *AntiCSRFField) InsertOnString(str string) string {
94+
return ft.inserter.Replace(str)
95+
}

anti_csrf_field_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2+
// go-aah/view source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package view
6+
7+
import (
8+
"io/ioutil"
9+
"path/filepath"
10+
"strings"
11+
"testing"
12+
13+
"aahframework.org/test.v0/assert"
14+
)
15+
16+
func TestAntiCSRFFieldNoFormTag(t *testing.T) {
17+
acsrf := NewAntiCSRFField("go", "{{", "}}")
18+
fpath := filepath.Join(getTestdataPath(), "anti-csrf-field", "testhtml-noform.html")
19+
20+
files := acsrf.InsertOnFiles(fpath)
21+
bytes, err := ioutil.ReadFile(files[0])
22+
assert.Nil(t, err)
23+
assert.False(t, strings.Contains(string(bytes), "{{ anti_csrf_token . }}"))
24+
}
25+
26+
func TestAntiCSRFFieldFormTag(t *testing.T) {
27+
acsrf := NewAntiCSRFField("go", "%%", "%%")
28+
fpath := filepath.Join(getTestdataPath(), "anti-csrf-field", "testhtml-form.html")
29+
30+
files := acsrf.InsertOnFiles(fpath)
31+
bytes, err := ioutil.ReadFile(files[0])
32+
assert.Nil(t, err)
33+
assert.True(t, strings.Contains(string(bytes), "%% anitcsrftoken . %%"))
34+
}
35+
36+
func TestAntiCSRFFieldFormTagDelim(t *testing.T) {
37+
acsrf := NewAntiCSRFField("go", "[[", "]]")
38+
fpath := filepath.Join(getTestdataPath(), "anti-csrf-field", "not-exists.html")
39+
40+
files := acsrf.InsertOnFiles(fpath)
41+
assert.NotNil(t, files)
42+
assert.Equal(t, fpath, files[0])
43+
}

funcs.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package view
66

77
import (
88
"html/template"
9+
"path/filepath"
10+
"strings"
911

1012
"aahframework.org/log.v0"
1113
)
@@ -15,14 +17,26 @@ func tmplSafeHTML(str string) template.HTML {
1517
return template.HTML(str)
1618
}
1719

18-
// tmplImport method renders given template with View Args and imports into
20+
// tmplInclude method renders given template with View Args and imports into
1921
// current template.
20-
func tmplImport(name string, viewArgs map[string]interface{}) template.HTML {
21-
tmplStr, err := commonTemplate.Execute(name, viewArgs)
22-
if err != nil {
22+
func tmplInclude(name string, viewArgs map[string]interface{}) template.HTML {
23+
if !strings.HasPrefix(name, "common") {
24+
name = "common/" + name
25+
}
26+
name = filepath.ToSlash(name)
27+
28+
tmpl := commonTemplates.Lookup(name)
29+
if tmpl == nil {
30+
log.Warnf("goviewengine: common template not found: %s", name)
31+
return tmplSafeHTML("")
32+
}
33+
34+
buf := acquireBuffer()
35+
defer releaseBuffer(buf)
36+
if err := tmpl.Execute(buf, viewArgs); err != nil {
2337
log.Error(err)
2438
return template.HTML("")
2539
}
2640

27-
return tmplSafeHTML(tmplStr)
41+
return tmplSafeHTML(buf.String())
2842
}

0 commit comments

Comments
 (0)