Skip to content

Commit

Permalink
modify print method
Browse files Browse the repository at this point in the history
  • Loading branch information
pejman-hkh committed Mar 18, 2024
1 parent b25d185 commit d49b172
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 85 deletions.
2 changes: 1 addition & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func main() {

document = gdp.Default(`<div class="parent"><div class="prev">test</div><div class="middle" id="middle">test1</div><span class="next"></span></div>`)

fmt.Printf("%+v\n", document)
document.Print()

document.Find(".prev,.middle,.next").Each(func(index int, tag *gdp.Tag) {
fmt.Printf("%+v\n", tag)
Expand Down
20 changes: 10 additions & 10 deletions gdp/attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (
)

type Attr struct {
attrs map[string]*string
Attrs map[string]*string
}

func (a *Attr) makeAttr() string {
ret := ""
pre := ""
if a.attrs == nil {
if a.Attrs == nil {
return ""
}

for name, value := range a.attrs {
for name, value := range a.Attrs {

if name == "class" && *value == "" {
continue
Expand All @@ -32,14 +32,14 @@ func (a *Attr) makeAttr() string {
}

func (a *Attr) setValue(key string, value string) {
if a.attrs != nil {
(a.attrs)[key] = &value
if a.Attrs != nil {
(a.Attrs)[key] = &value
}
}

func (a *Attr) valueOf(key string) string {
if a.attrs != nil {
v := (a.attrs)[key]
if a.Attrs != nil {
v := (a.Attrs)[key]
if v != nil {
return *v
}
Expand All @@ -48,7 +48,7 @@ func (a *Attr) valueOf(key string) string {
}

func (a *Attr) RemoveClass(class string) {
if a.attrs == nil {
if a.Attrs == nil {
return
}

Expand All @@ -65,7 +65,7 @@ func (a *Attr) RemoveClass(class string) {
}

func (a *Attr) AddClass(class string) {
if a.attrs == nil {
if a.Attrs == nil {
return
}

Expand All @@ -80,7 +80,7 @@ func (a *Attr) AddClass(class string) {

func (a *Attr) HasClass(v string) bool {

if a.attrs == nil {
if a.Attrs == nil {
return false
}

Expand Down
46 changes: 23 additions & 23 deletions gdp/gdp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ func Default(html string) Tag {
p.len = len(p.html)

var document Tag
document.tag = "document"
document.children = p.parse(&document)
document.attrs = Attr{nil}
document.Tag = "document"
document.Childrens = p.parse(&document)
document.TagAttrs = Attr{nil}
p.current = &document

return document
Expand Down Expand Up @@ -133,7 +133,7 @@ func (p *Parser) parseAttr() Attr {
func (p *Parser) parseTag(tag *Tag) bool {
if p.isEqual("![CDATA[") {
p.i += 8
tag.tag = "cdata"
tag.Tag = "cdata"
return true
}

Expand Down Expand Up @@ -162,18 +162,18 @@ func (p *Parser) parseTag(tag *Tag) bool {
}
name := buffer.String()

tag.tag = name
tag.attrs = attrs
tag.Tag = name
tag.TagAttrs = attrs
tag.isEnd = false

if name[0] == '/' {
tag.isEnd = true
tag.tag = name[1:]
tag.Tag = name[1:]
}

if name[len(name)-1] == '/' {
name = name[0 : len(name)-1]
tag.tag = name
tag.Tag = name
}
return true
}
Expand All @@ -183,8 +183,8 @@ func (p *Parser) parseContent(first byte, tag *Tag) bool {
content := p.getUntil("<", first)
p.i--

tag.tag = "empty"
tag.content = content
tag.Tag = "empty"
tag.Contents = content

return true
}
Expand All @@ -196,8 +196,8 @@ func (p *Parser) parseComment(tag *Tag) bool {

p.i += 2

tag.tag = "comment"
tag.content = content
tag.Tag = "comment"
tag.Contents = content

return true
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func (p *Parser) isEqual(text string) bool {

func isEndTag(tag *Tag) bool {
for i := 0; i < 18; i++ {
if tag.tag == hasNoEndTags[i] {
if tag.Tag == hasNoEndTags[i] {
return true
}
}
Expand Down Expand Up @@ -277,11 +277,11 @@ func (p *Parser) getTag(tag *Tag) bool {
return false
}

if tag.tag == "cdata" {
tag.content = p.parseCdata()
if tag.Tag == "cdata" {
tag.Contents = p.parseCdata()
return true
}
name := tag.tag
name := tag.Tag
if len(name) >= 4 && name[0:4] == "?xml" {
p.isXml = true
return true
Expand All @@ -295,20 +295,20 @@ func (p *Parser) getTag(tag *Tag) bool {
return true
}

if tag.tag == "script" {
tag.content = p.parseScript()
if tag.Tag == "script" {
tag.Contents = p.parseScript()
} else {
tag.children = p.parse(tag)
tag.Childrens = p.parse(tag)
}

if tag.tag == p.current.tag {
if tag.Tag == p.current.Tag {
return true
}

var etag Tag

for p.next(&etag) {
if tag.tag == etag.tag {
if tag.Tag == etag.Tag {
break
}
}
Expand All @@ -329,13 +329,13 @@ func (p *Parser) parse(parent *Tag) []*Tag {
break
}

if tag.isEnd && parent.tag == tag.tag {
if tag.isEnd && parent.Tag == tag.Tag {
break
}

if !tag.isEnd {

tag.eq = eq
tag.Eq = eq
eq++
tag.prev = stag
tag.parent = parent
Expand Down
Loading

0 comments on commit d49b172

Please sign in to comment.