267 lines
6.5 KiB
Go
267 lines
6.5 KiB
Go
package document
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"yetano.dev/yetano/core/gui/assets"
|
||
|
|
)
|
||
|
|
|
||
|
|
// rewriteCSS replaces url(...) and @import references in css. base is the
|
||
|
|
// virtual path of the stylesheet ("" for CSS embedded in the entry HTML);
|
||
|
|
// relative references resolve against it. Comments and strings outside of
|
||
|
|
// url()/@import are copied untouched.
|
||
|
|
func (p *preparer) rewriteCSS(ctx context.Context, css, base string) (string, error) {
|
||
|
|
var out strings.Builder
|
||
|
|
i := 0
|
||
|
|
n := len(css)
|
||
|
|
for i < n {
|
||
|
|
c := css[i]
|
||
|
|
switch {
|
||
|
|
case c == '/' && i+1 < n && css[i+1] == '*':
|
||
|
|
end := strings.Index(css[i+2:], "*/")
|
||
|
|
if end < 0 {
|
||
|
|
out.WriteString(css[i:])
|
||
|
|
return out.String(), nil
|
||
|
|
}
|
||
|
|
out.WriteString(css[i : i+2+end+2])
|
||
|
|
i += 2 + end + 2
|
||
|
|
case c == '"' || c == '\'':
|
||
|
|
j := skipString(css, i)
|
||
|
|
out.WriteString(css[i:j])
|
||
|
|
i = j
|
||
|
|
case c == '\\' && i+1 < n:
|
||
|
|
out.WriteString(css[i : i+2])
|
||
|
|
i += 2
|
||
|
|
case hasFoldPrefix(css[i:], "url(") && isIdentBoundary(css, i):
|
||
|
|
ref, end, ok := parseURLToken(css, i+4)
|
||
|
|
if !ok {
|
||
|
|
out.WriteByte(c)
|
||
|
|
i++
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
repl, err := p.cssRef(ctx, ref, base, false)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
if repl == strings.TrimSpace(ref) {
|
||
|
|
out.WriteString(css[i:end]) // not an asset: byte-identical
|
||
|
|
} else {
|
||
|
|
out.WriteString("url(" + quoteCSS(repl) + ")")
|
||
|
|
}
|
||
|
|
i = end
|
||
|
|
case c == '@' && hasFoldPrefix(css[i:], "@import") && i+7 < n && !isIdentChar(css[i+7]):
|
||
|
|
j := i + 7
|
||
|
|
for j < n && isSpace(css[j]) {
|
||
|
|
j++
|
||
|
|
}
|
||
|
|
var ref string
|
||
|
|
var end int
|
||
|
|
switch {
|
||
|
|
case j < n && (css[j] == '"' || css[j] == '\''):
|
||
|
|
end = skipString(css, j)
|
||
|
|
ref = unquoteCSS(css[j:end])
|
||
|
|
case hasFoldPrefix(css[j:], "url("):
|
||
|
|
var ok bool
|
||
|
|
ref, end, ok = parseURLToken(css, j+4)
|
||
|
|
if !ok {
|
||
|
|
out.WriteString(css[i:j])
|
||
|
|
i = j
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
default:
|
||
|
|
out.WriteString(css[i:j])
|
||
|
|
i = j
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
repl, err := p.cssRef(ctx, ref, base, true)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
if repl == strings.TrimSpace(ref) {
|
||
|
|
out.WriteString(css[i:end])
|
||
|
|
} else {
|
||
|
|
out.WriteString("@import url(" + quoteCSS(repl) + ")")
|
||
|
|
}
|
||
|
|
i = end
|
||
|
|
default:
|
||
|
|
out.WriteByte(c)
|
||
|
|
i++
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out.String(), nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// cssRef resolves one reference found in CSS. Data URLs, fragments and
|
||
|
|
// external URLs are left alone.
|
||
|
|
func (p *preparer) cssRef(ctx context.Context, ref, base string, isImport bool) (string, error) {
|
||
|
|
ref = strings.TrimSpace(ref)
|
||
|
|
var name string
|
||
|
|
var err error
|
||
|
|
switch {
|
||
|
|
case ref == "" || strings.HasPrefix(ref, "#") || strings.HasPrefix(ref, "data:"):
|
||
|
|
return ref, nil
|
||
|
|
case assets.IsAssetRef(ref):
|
||
|
|
name, err = assets.FromRef(ref)
|
||
|
|
case hasScheme(ref) || strings.HasPrefix(ref, "//") || strings.HasPrefix(ref, "/"):
|
||
|
|
return ref, nil // external or not an asset: untouched
|
||
|
|
case base != "":
|
||
|
|
pth, _ := assets.SplitRef(ref)
|
||
|
|
name, err = assets.Join(base, pth)
|
||
|
|
if err == nil && p.link {
|
||
|
|
// The WebView resolves relative URLs itself; only validate.
|
||
|
|
if _, err := p.resolve(ctx, name, base); err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
return ref, nil
|
||
|
|
}
|
||
|
|
default:
|
||
|
|
return ref, nil // relative ref in entry-level CSS: not an asset reference
|
||
|
|
}
|
||
|
|
if err != nil {
|
||
|
|
return "", p.fail(base, ref, err)
|
||
|
|
}
|
||
|
|
if p.link {
|
||
|
|
return p.assetDataURL(ctx, name, base)
|
||
|
|
}
|
||
|
|
if isImport {
|
||
|
|
css, err := p.loadCSS(ctx, name)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
return dataURL("text/css;charset=utf-8", []byte(css)), nil
|
||
|
|
}
|
||
|
|
if assets.BaseMIME(mimeOf(name)) == "text/css" {
|
||
|
|
css, err := p.loadCSS(ctx, name)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
return dataURL("text/css;charset=utf-8", []byte(css)), nil
|
||
|
|
}
|
||
|
|
return p.assetDataURL(ctx, name, base)
|
||
|
|
}
|
||
|
|
|
||
|
|
// loadCSS loads and rewrites a stylesheet asset, detecting @import cycles.
|
||
|
|
func (p *preparer) loadCSS(ctx context.Context, name string) (string, error) {
|
||
|
|
for _, s := range p.cssStack {
|
||
|
|
if s == name {
|
||
|
|
return "", fmt.Errorf("css @import cycle: %s -> %s", strings.Join(p.cssStack, " -> "), name)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
res, err := p.resolve(ctx, name, p.current())
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
if assets.BaseMIME(res.MIME) != "text/css" {
|
||
|
|
return "", fmt.Errorf("%s: expected text/css, got %s", name, res.MIME)
|
||
|
|
}
|
||
|
|
p.cssStack = append(p.cssStack, name)
|
||
|
|
defer func() { p.cssStack = p.cssStack[:len(p.cssStack)-1] }()
|
||
|
|
return p.rewriteCSS(ctx, string(res.Bytes), name)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (p *preparer) current() string {
|
||
|
|
if len(p.cssStack) == 0 {
|
||
|
|
return p.entry
|
||
|
|
}
|
||
|
|
return p.cssStack[len(p.cssStack)-1]
|
||
|
|
}
|
||
|
|
|
||
|
|
// parseURLToken parses the inside of url( ... ) starting at i and returns the
|
||
|
|
// reference and the index after ')'.
|
||
|
|
func parseURLToken(s string, i int) (string, int, bool) {
|
||
|
|
n := len(s)
|
||
|
|
for i < n && isSpace(s[i]) {
|
||
|
|
i++
|
||
|
|
}
|
||
|
|
if i < n && (s[i] == '"' || s[i] == '\'') {
|
||
|
|
end := skipString(s, i)
|
||
|
|
ref := unquoteCSS(s[i:end])
|
||
|
|
j := end
|
||
|
|
for j < n && isSpace(s[j]) {
|
||
|
|
j++
|
||
|
|
}
|
||
|
|
if j >= n || s[j] != ')' {
|
||
|
|
return "", 0, false
|
||
|
|
}
|
||
|
|
return ref, j + 1, true
|
||
|
|
}
|
||
|
|
start := i
|
||
|
|
for i < n && s[i] != ')' {
|
||
|
|
if s[i] == '\\' {
|
||
|
|
i++
|
||
|
|
}
|
||
|
|
i++
|
||
|
|
}
|
||
|
|
if i >= n {
|
||
|
|
return "", 0, false
|
||
|
|
}
|
||
|
|
return strings.TrimSpace(s[start:i]), i + 1, true
|
||
|
|
}
|
||
|
|
|
||
|
|
func skipString(s string, i int) int {
|
||
|
|
q := s[i]
|
||
|
|
j := i + 1
|
||
|
|
for j < len(s) {
|
||
|
|
switch s[j] {
|
||
|
|
case '\\':
|
||
|
|
j += 2
|
||
|
|
continue
|
||
|
|
case q:
|
||
|
|
return j + 1
|
||
|
|
case '\n':
|
||
|
|
return j
|
||
|
|
}
|
||
|
|
j++
|
||
|
|
}
|
||
|
|
return len(s)
|
||
|
|
}
|
||
|
|
|
||
|
|
func unquoteCSS(s string) string {
|
||
|
|
if len(s) >= 2 && (s[0] == '"' || s[0] == '\'') && s[len(s)-1] == s[0] {
|
||
|
|
s = s[1 : len(s)-1]
|
||
|
|
}
|
||
|
|
// Minimal unescape: "\x" -> "x" (hex escapes are not used in asset paths).
|
||
|
|
if strings.IndexByte(s, '\\') < 0 {
|
||
|
|
return s
|
||
|
|
}
|
||
|
|
var b strings.Builder
|
||
|
|
for i := 0; i < len(s); i++ {
|
||
|
|
if s[i] == '\\' && i+1 < len(s) {
|
||
|
|
i++
|
||
|
|
}
|
||
|
|
b.WriteByte(s[i])
|
||
|
|
}
|
||
|
|
return b.String()
|
||
|
|
}
|
||
|
|
|
||
|
|
func quoteCSS(s string) string {
|
||
|
|
return `"` + strings.NewReplacer(`\`, `\\`, `"`, `\"`, "\n", `\a `).Replace(s) + `"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func hasFoldPrefix(s, prefix string) bool {
|
||
|
|
return len(s) >= len(prefix) && strings.EqualFold(s[:len(prefix)], prefix)
|
||
|
|
}
|
||
|
|
|
||
|
|
func isIdentChar(c byte) bool {
|
||
|
|
return c == '-' || c == '_' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c >= 0x80
|
||
|
|
}
|
||
|
|
|
||
|
|
func isIdentBoundary(s string, i int) bool { return i == 0 || !isIdentChar(s[i-1]) }
|
||
|
|
|
||
|
|
func isSpace(c byte) bool { return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' }
|
||
|
|
|
||
|
|
func hasScheme(ref string) bool {
|
||
|
|
for i := 0; i < len(ref); i++ {
|
||
|
|
c := ref[i]
|
||
|
|
switch {
|
||
|
|
case c == ':':
|
||
|
|
return i > 0
|
||
|
|
case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', i > 0 && (c >= '0' && c <= '9' || c == '+' || c == '-' || c == '.'):
|
||
|
|
default:
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|