Пример #1
0
func TestMalformed(t *testing.T) {
	// some mal-formed html
	type test struct {
		src string
		exp string
	}
	tests := []test{
		{`<p><span>Foo</i></p>`, `<html><head></head><body><p><span>Foo</span></p></body></html>`},
	}

	for _, test := range tests {
		r := bytes.NewBufferString(test.src)
		nodes, err := html.ParseFragment(r, nil)
		if err != nil {
			t.Fatal(err)
		}

		b := new(bytes.Buffer)
		for _, v := range nodes {
			err := html.Render(b, v)
			if err != nil {
				t.Error(err)
			}
		}
		if b.String() != test.exp {
			t.Errorf("expected %q, got %q", test.exp, b)
		}
	}
}
Пример #2
0
// Just like jQuery's html() setter.
func (this *Selection) SetHtml(s string) *Selection {
	result := newEmptySelection(this.document)
	for _, n := range this.Nodes {
		newNodes, e := html.ParseFragment(strings.NewReader(s), n)
		if e == nil {
			for _, child := range newNodes {
				n.AppendChild(child)
			}
			result.AddNodes(newNodes...)
		}
	}
	return result
}