-
Notifications
You must be signed in to change notification settings - Fork 4
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
Collect stylesheets #46
Conversation
They will be put to the header tag when fragments are merged.
Error was: case-insensitive import collision: "github.com/Sirupsen/logrus" and "github.com/sirupsen/logrus"
This reverts commit daf9060. The commit couldn't fix the issue.
composition/html_content_parser.go
Outdated
if string(tag) == "link" && attrHasValue(attrs, "rel", "stylesheet") { | ||
href, _ := getAttr(attrs, "href") | ||
stylesheets = append(stylesheets, href.Val) | ||
logging.Logger.WithField("stylesheet", href.Val).Info() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be removed (to verbose)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
composition/html_content_parser.go
Outdated
if string(tag) == "link" && attrHasValue(attrs, "rel", "stylesheet") { | ||
href, _ := getAttr(attrs, "href") | ||
stylesheets = append(stylesheets, href.Val) | ||
logging.Logger.WithField("stylesheet", href.Val).Info() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be removed (to verbose)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
composition/html_content_parser.go
Outdated
@@ -161,6 +171,12 @@ forloop: | |||
continue | |||
} | |||
} | |||
if string(tag) == "link" && attrHasValue(attrs, "rel", "stylesheet") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This string(tag) == "link" && attrHasValue(attrs, "rel", "stylesheet")
could be extracted in a function e.g. getStylesheet()
(link string, isStyleheed bool)` would result in
if href, isStylesheed := getStylesheet(tag, attrs); isStylesheed {
stylesheets = append(stylesheets, href.Val)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
composition/html_content_parser.go
Outdated
@@ -224,7 +250,9 @@ forloop: | |||
buff.Write(raw) | |||
} | |||
|
|||
return NewStringFragment(buff.String()), dependencies, nil | |||
f = NewStringFragment(buff.String()) | |||
f.(*StringFragment).AddStylesheets(stylesheets) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not so nice, better use a new variable with the concrete type instead of using the interface type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
composition/string_fragment.go
Outdated
} | ||
|
||
func NewStringFragment(c string) *StringFragment { | ||
return &StringFragment { | ||
content: c, | ||
stylesheets: []string{}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to initialize a slice. This just waste memory in case you don't need it:
https://blog.golang.org/slices
That said, a nil slice is functionally equivalent to a zero-length slice, even though it points to nothing. It has length zero and can be appended to, with allocation. As an example, look at the one-liner above that copies a slice by appending to a nil slice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
<link uic-remove rel="stylesheet" href="basic.css"> | ||
</head> | ||
<body> | ||
<h1 uic-remove>Basic Web Elements</h1> | ||
<h3 uic-remove>The Header</h3> | ||
<uic-fragment name="header"> | ||
<link rel="stylesheet" href="/static/body/basic.css#header"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be good to also have on testcase with two or more stylesheets in a fragment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
|
||
for _, f := range cntx.Head { | ||
if err := f.Execute(w, cntx.MetaJSON, executeFragment); err != nil { | ||
cntx.collectStylesheets(f) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this done twice? Because there is also a collectStylesheet(f) call within the executeFragment() closure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The collectStylesheets(f) call within the closure is executed against nested fragments. Here, the stylesheets of the parent fragment are collected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, I understand.
composition/content_merge.go
Outdated
for _, f := range cntx.BodyAttrs { | ||
io.WriteString(w, " ") | ||
cntx.collectStylesheets(f) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to support stylesheets in body attributes ;-)
- moved duplicate code into a function - improved tests - removed excessive logging
cool, it's all fine for me. |
Extracts all <link rel="stylesheet" ...> from every Fragment and collects them.
When fragments are merged together, all referenced stylesheet tags are aggregated in the
section of the resulting html page.