Rich Churcher
Rich Churcher

Reputation: 7664

Using go-html-transform to preprocess HTML: Replace fails

Following on from this question on whitelisting HTML tags, I've been experimenting with Jeremy Wall's go-html-transform. In the hopes of improving searchable documentation I'm asking this here rather than pestering the author directly... hopefully this isn't too tool-specific for SO.

App Engine, latest SDK. Post.Body is a []byte. This works:

package posts

import (
    // ...      
    "html/template"
    "code.google.com/p/go-html-transform/html/transform"
    "code.google.com/p/go-html-transform/h5"
)

// ...

// Pre-process post body, then return it to the template as HTML()
// to avoid html/template's escaping allowable tags
func (p *Post) BodyHTML() template.HTML {
    doc, _ := transform.NewDoc(string(p.Body))
    t := transform.NewTransform(doc)

    // Add some text to the end of any <strong></strong> nodes.
    t.Apply(transform.AppendChildren(h5.Text("<em>Foo</em>")), "strong")
    return template.HTML(t.String())
}

Result:

<strong>Blarg.<em>Foo</em></strong>

However, if instead of AppendChildren() I use something like the following:

t.Apply(transform.Replace(h5.Text("<em>Foo</em>")), "strong")

I get an internal server error. Have I misunderstood the use of Replace()? The existing documentation suggests this sort of thing should be possible.

Upvotes: 1

Views: 604

Answers (1)

Sonia
Sonia

Reputation: 28375

Running your transform code outside of App Engine, it panics and you can see a TODO in the source at that point. Then it's not too much harder to read the code and see that it's going to panic if given a root node.

Upvotes: 2

Related Questions