(w http.ResponseWriter, r *http.Request, query url.Values, framework string)
| 556 | } |
| 557 | |
| 558 | func (s *Handler) ServeFrameworkCSS(w http.ResponseWriter, r *http.Request, query url.Values, framework string) { |
| 559 | indexHtmlFile, err := os.Open(filepath.Join(s.config.AppDir, "index.html")) |
| 560 | if err != nil { |
| 561 | fmt.Println(term.Red("[error] failed to open index.html: " + err.Error())) |
| 562 | http.Error(w, "Internal Server Error", 500) |
| 563 | return |
| 564 | } |
| 565 | defer indexHtmlFile.Close() |
| 566 | |
| 567 | var importMap *importmap.ImportMap |
| 568 | |
| 569 | contents := [][]byte{} |
| 570 | jsEntries := map[string]struct{}{} |
| 571 | tokenizer := html.NewTokenizer(indexHtmlFile) |
| 572 | for { |
| 573 | tt := tokenizer.Next() |
| 574 | if tt == html.ErrorToken { |
| 575 | break |
| 576 | } |
| 577 | if tt == html.StartTagToken { |
| 578 | name, moreAttr := tokenizer.TagName() |
| 579 | switch string(name) { |
| 580 | case "script": |
| 581 | var ( |
| 582 | typeAttr string |
| 583 | srcAttr string |
| 584 | hrefAttr string |
| 585 | ) |
| 586 | for moreAttr { |
| 587 | var key, val []byte |
| 588 | key, val, moreAttr = tokenizer.TagAttr() |
| 589 | if bytes.Equal(key, []byte("type")) { |
| 590 | typeAttr = string(val) |
| 591 | } else if bytes.Equal(key, []byte("src")) { |
| 592 | srcAttr = string(val) |
| 593 | } else if bytes.Equal(key, []byte("href")) { |
| 594 | hrefAttr = string(val) |
| 595 | } |
| 596 | } |
| 597 | if typeAttr == "importmap" { |
| 598 | tokenizer.Next() |
| 599 | innerText := bytes.TrimSpace(tokenizer.Text()) |
| 600 | if len(innerText) > 0 { |
| 601 | importMap, _ = importmap.Parse(nil, innerText) |
| 602 | } |
| 603 | } else if srcAttr == "" { |
| 604 | // inline script content |
| 605 | tokenizer.Next() |
| 606 | contents = append(contents, tokenizer.Text()) |
| 607 | } else { |
| 608 | if hrefAttr != "" && isHttpSepcifier(srcAttr) { |
| 609 | if !isHttpSepcifier(hrefAttr) && isModulePath(hrefAttr) { |
| 610 | jsEntries[hrefAttr] = struct{}{} |
| 611 | } |
| 612 | } else if !isHttpSepcifier(srcAttr) && isModulePath(srcAttr) { |
| 613 | jsEntries[srcAttr] = struct{}{} |
| 614 | } |
| 615 | } |
no test coverage detected