github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/www/css.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package www
     4  
     5  import (
     6  	"bytes"
     7  
     8  	"../minify"
     9  	"../protocol"
    10  )
    11  
    12  // mixCSSToHTML add given CSS file to end of HTML file and returns new html file.
    13  func mixCSSToHTML(html, css []byte) (mixedData []byte) {
    14  	mixedData = make([]byte, len(html), len(html)+len(css)+len("\n<style></style>"))
    15  	copy(mixedData, html)
    16  	mixedData = append(mixedData, "\n<style>"...)
    17  	mixedData = append(mixedData, css...)
    18  	mixedData = append(mixedData, "</style>"...)
    19  	return
    20  }
    21  
    22  // mixCSSToJS add given CSS file to specific part of JS file and returns new js file.
    23  func mixCSSToJS(js, css []byte) (mixedData []byte, err protocol.Error) {
    24  	css, err = minify.CSS.MinifyBytes(css)
    25  	if err != nil {
    26  		return
    27  	}
    28  
    29  	var funcLoc = bytes.Index(js, []byte("CSS: '"))
    30  	if funcLoc < 0 {
    31  		var minifiedCSS = append([]byte(`CSS = '`), css...)
    32  		mixedData = bytes.Replace(js, []byte(`CSS = '`), minifiedCSS, 1)
    33  	} else {
    34  		var minifiedCSS = append([]byte(`CSS: '`), css...)
    35  		mixedData = bytes.Replace(js, []byte(`CSS: '`), minifiedCSS, 1)
    36  	}
    37  	return
    38  }