Skip to content

Commit

Permalink
TOOM-3733: code optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Miroslav Grgic committed Nov 3, 2016
1 parent 03374ac commit ea11614
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
53 changes: 27 additions & 26 deletions composition/html_content_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,31 +273,32 @@ func ParseHeadFragment(fragment *StringFragment, headPropertyMap map[string]stri
return z.Err()
}
break forloop

case tt == html.StartTagToken || tt == html.SelfClosingTagToken:

if string(tag) == "meta" {
if(processMetaTag(string(tag), attrs, headPropertyMap)) {
headBuff.Write(raw)
}
continue
}
if string(tag) == "link" {
if(processLinkTag(attrs, headPropertyMap)) {
switch {
case string(tag) == "meta":
if (processMetaTag(string(tag), attrs, headPropertyMap)) {
headBuff.Write(raw)
}
continue
continue forloop
case string(tag) == "link":
if (processLinkTag(attrs, headPropertyMap)) {
headBuff.Write(raw)
}
continue forloop
case string(tag) == "title":
if (headPropertyMap["title"] == "") {
headPropertyMap["title"] = "title"
headBuff.Write(raw)
} else if (tt != html.SelfClosingTagToken) {
skipCompleteTag(z, "title")
}
continue forloop
default:
headBuff.Write(raw)
}
if string(tag) == "title" {
if(headPropertyMap["title"] == "") {
headPropertyMap["title"] = "title"
headBuff.Write(raw)
} else if (tt != html.SelfClosingTagToken) {
skipCompleteTag(z, "title")
continue
}
} else {
headBuff.Write(raw)
}

default:
headBuff.Write(raw)
}
Expand Down Expand Up @@ -371,16 +372,16 @@ func processLinkTag(attrs []html.Attribute, metaMap map[string]string) bool {
}

const canonical = "canonical"
key := ""
value := ""
var key string
var value string

// e.g.: <link rel="canonical" href="/baumarkt/suche"> => key = canonical; val = /baumarkt/suche
for i := 0; i < len(attrs); i++ {
if (attrs[i].Key == "rel" && attrs[i].Val == canonical) {
for _, attr := range attrs {
if (attr.Key == "rel" && attr.Val == canonical) {
key = canonical
}
if (attrs[i].Key == "href") {
value = attrs[i].Val
if (attr.Key == "href") {
value = attr.Val
}
}
if (key == canonical && metaMap[canonical] != "") {
Expand Down
3 changes: 2 additions & 1 deletion composition/html_content_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,7 @@ func Test_ParseHeadFragment_Filter_Link_Canonical_Tag_without_existing_Map(t *te
<meta charset="utf-8" />
<link rel="canonical"
href="/navigationservice">
<foo bar=""/>
<title>navigationservice</title>
`

Expand All @@ -926,7 +927,7 @@ func Test_ParseHeadFragment_Filter_Link_Canonical_Tag_without_existing_Map(t *te
<link />
<link rel="canonical" href="/baumarkt/bauen-renovieren/suche">
<meta charset="utf-8" />
<foo bar=""/>
<title>navigationservice</title>
`

Expand Down

0 comments on commit ea11614

Please sign in to comment.