Skip to content

Commit

Permalink
rework quoting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Feb 28, 2024
1 parent a58feaf commit f81f622
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
14 changes: 3 additions & 11 deletions uiimg.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,15 @@ type UiImg struct {
StringSetter
}

func (ui *UiImg) SrcAttr(e *Element) string {
src := ui.JawsGetString(e)
if len(src) < 1 || src[0] != '"' {
return strconv.Quote(src)
}
return src
}

func (ui *UiImg) JawsRender(e *Element, w io.Writer, params []any) error {
ui.parseGetter(e, ui.StringSetter)
srcattr := template.HTMLAttr("src=" + ui.SrcAttr(e)) // #nosec G203
srcattr := template.HTMLAttr("src=" + strconv.Quote(ui.JawsGetString(e))) // #nosec G203
attrs := append(e.ParseParams(params), srcattr)
return WriteHtmlInner(w, e.Jid(), "img", "", "", attrs...)
}

func (ui *UiImg) JawsUpdate(u *Element) {
u.SetAttr("src", ui.SrcAttr(u))
func (ui *UiImg) JawsUpdate(e *Element) {
e.SetAttr("src", ui.JawsGetString(e))
}

func NewUiImg(g StringSetter) *UiImg {
Expand Down
8 changes: 4 additions & 4 deletions uiimg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ func TestRequest_Img(t *testing.T) {
rq := newTestRequest()
defer rq.Close()

ts := newTestSetter("\"quoted.png\"")
ts := newTestSetter("image.png")

wantHtml := "<img id=\"Jid.1\" hidden src=\"quoted.png\">"
wantHtml := "<img id=\"Jid.1\" hidden src=\"image.png\">"
rq.Img(ts, "hidden")
if gotHtml := rq.BodyString(); gotHtml != wantHtml {
t.Errorf("Request.Img() = %q, want %q", gotHtml, wantHtml)
}

ts.Set("unquoted.jpg")
ts.Set("image2.jpg")
rq.Dirty(ts)

select {
case <-th.C:
th.Timeout()
case msg := <-rq.outCh:
s := msg.Format()
if s != "SAttr\tJid.1\t\"src\\n\\\"unquoted.jpg\\\"\"\n" {
if s != "SAttr\tJid.1\t\"src\\nimage2.jpg\"\n" {
t.Error(strconv.Quote(s))
}
}
Expand Down

0 comments on commit f81f622

Please sign in to comment.