Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new features #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Library for parsing and manipulating SVG files.

### Installation

go get github.com/JoshVarga/svgparser
go get svgparser

### Features

Expand Down
5 changes: 2 additions & 3 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package svgparser_test

import (
"fmt"
"github.com/sergesheff/svgparser"
"github.com/sergesheff/svgparser/utils"
"strings"

"github.com/JoshVarga/svgparser"
"github.com/JoshVarga/svgparser/utils"
)

func ExampleParse() {
Expand Down
24 changes: 24 additions & 0 deletions find.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package svgparser

import "strings"

// FindID finds the first child with the specified ID.
func (e *Element) FindID(id string) *Element {
for _, child := range e.Children {
Expand All @@ -24,3 +26,25 @@ func (e *Element) FindAll(name string) []*Element {
}
return elements
}

// FindByCharData finds all children with the given chardata.
func (e *Element) FindByCharData(text string) []*Element {
if e == nil {
return nil
}

text = strings.ToLower(text)

var elements []*Element
for _, child := range e.Children {
if child == nil {
continue
}

if strings.Contains(strings.ToLower(child.Content), text) {
elements = append(elements, child)
}
elements = append(elements, child.FindByCharData(text)...)
}
return elements
}
3 changes: 1 addition & 2 deletions find_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package svgparser_test

import (
"github.com/sergesheff/svgparser"
"testing"

"github.com/JoshVarga/svgparser"
)

func testElement() *svgparser.Element {
Expand Down
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/sergesheff/svgparser

go 1.20.0

require golang.org/x/net v0.26.0

require golang.org/x/text v0.16.0 // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
59 changes: 55 additions & 4 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package svgparser
import (
"bytes"
"encoding/xml"
"golang.org/x/net/html/charset"
"io"
"io/ioutil"
"strings"
"golang.org/x/net/html/charset"
)

// ValidationError contains errors which have occured when parsing svg input.
Expand All @@ -23,18 +23,21 @@ type Element struct {
Name string
Attributes map[string]string
Children []*Element
Parent *Element
Content string
}

// NewElement creates element from decoder token.
func NewElement(token xml.StartElement) *Element {
func NewElement(token xml.StartElement, parent *Element) *Element {
element := &Element{}
attributes := make(map[string]string)
for _, attr := range token.Attr {
attributes[attr.Name.Local] = attr.Value
}
element.Name = token.Name.Local
element.Attributes = attributes
element.Parent = parent

return element
}

Expand Down Expand Up @@ -74,7 +77,7 @@ func DecodeFirst(decoder *xml.Decoder) (*Element, error) {

switch element := token.(type) {
case xml.StartElement:
return NewElement(element), nil
return NewElement(element, nil), nil
}
}
return &Element{}, nil
Expand All @@ -94,7 +97,7 @@ func (e *Element) Decode(decoder *xml.Decoder) error {

switch element := token.(type) {
case xml.StartElement:
nextElement := NewElement(element)
nextElement := NewElement(element, e)
err := nextElement.Decode(decoder)
if err != nil {
return err
Expand Down Expand Up @@ -134,3 +137,51 @@ func Parse(source io.Reader, validate bool) (*Element, error) {
}
return element, nil
}

func (el Element) MarshalXML(e *xml.Encoder, start xml.StartElement) error {

openToken := xml.StartElement{
Name: xml.Name{
Local: el.Name,
},
}

if len(el.Attributes) > 0 {
openToken.Attr = []xml.Attr{}

for key, value := range el.Attributes {
openToken.Attr = append(openToken.Attr, xml.Attr{
Name: xml.Name{
Local: key,
},
Value: value,
})
}
}

if err := e.EncodeToken(openToken); err != nil {
return err
}

if len(el.Content) > 0 {
if err := e.EncodeToken(xml.CharData(el.Content)); err != nil {
return err
}
}

for _, c := range el.Children {
if c != nil {
if err := c.MarshalXML(e, openToken); err != nil {
return err
}
}
}

closeToken := xml.EndElement{openToken.Name}

if err := e.EncodeToken(closeToken); err != nil {
return err
}

return e.Flush()
}
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package svgparser_test
import (
"testing"

"github.com/JoshVarga/svgparser"
"github.com/sergesheff/svgparser"
)

func TestParser(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package svgparser_test
import (
"strings"

"github.com/JoshVarga/svgparser"
"github.com/sergesheff/svgparser"
)

func element(name string, attrs map[string]string) *svgparser.Element {
Expand Down
2 changes: 1 addition & 1 deletion utils/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package utils_test
import (
"testing"

"."
"github.com/sergesheff/svgparser/utils"
)

func TestPathParser(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion utils/style_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package utils_test
import (
"testing"

"."
"github.com/sergesheff/svgparser/utils"
)

func TestStyleParser(t *testing.T) {
Expand Down