func element() *svg.Element { element, _ := svg.Parse(strings.NewReader(` <svg width="10" height="15"> <g id="first"> <rect id="inFirst" fill="red"/> <rect id="inFirst" fill="blue"/> </g> <g id="second"> <path d="M2,3 L2,3"/> <rect id="inSecond"/> </g> </svg> `)) return element }
func ExampleParse() { reader := strings.NewReader(` <svg width="100" height="100"> <circle cx="50" cy="50" r="40" fill="red" /> </svg>`, ) element, _ := svg.Parse(reader) fmt.Printf("SVG width: %s\n", element.Attributes["width"]) fmt.Printf("Circle fill: %s", element.Children[0].Attributes["fill"]) // Output: // SVG width: 100 // Circle fill: red }
func TestElementContent(t *testing.T) { var testCases = []struct { raw string content string }{ {`<text>Hello</text>`, "Hello"}, {`<text> Hello </text>`, " Hello "}, } for _, test := range testCases { actual, _ := svg.Parse(strings.NewReader(test.raw)) if test.content != actual.Content { t.Errorf("Element: expected %v, actual %v\n", test.content, actual.Content) } } }
func TestParser(t *testing.T) { var testCases = []struct { raw string element svg.Element }{ { ` <svg width="100" height="100"> <circle cx="50" cy="50" /> </svg> `, svg.Element{ Name: "svg", Attributes: map[string]string{ "width": "100", "height": "100", }, Children: []*svg.Element{ { Name: "circle", Attributes: map[string]string{"cx": "50", "cy": "50"}, Children: []*svg.Element{}, }, }, }, }, { ` <svg height="400" width="450"> <g stroke="black" stroke-width="3"> <path d="M 10 20 L 15 -25" /> <path d="M 25 50 L 15 30" /> </g> </svg> `, svg.Element{ Name: "svg", Attributes: map[string]string{ "width": "450", "height": "400", }, Children: []*svg.Element{ { Name: "g", Attributes: map[string]string{ "stroke": "black", "stroke-width": "3", }, Children: []*svg.Element{ { Name: "path", Attributes: map[string]string{ "d": "M 10 20 L 15 -25", }, Children: []*svg.Element{}, }, { Name: "path", Attributes: map[string]string{ "d": "M 25 50 L 15 30", }, Children: []*svg.Element{}, }, }, }, }, }, }, } for _, test := range testCases { actual, err := svg.Parse(strings.NewReader(test.raw)) if !(test.element.Equals(actual) && err == nil) { t.Errorf("Parse: expected %v, actual %v\n", test.element, actual) } } }
func TestParseEmpty(t *testing.T) { actual, err := svg.Parse(strings.NewReader("")) if !(actual == nil && err == nil) { t.Errorf("Parse: expected %v, actual %v\n", nil, actual) } }