func ExampleSprite_position() { in := bytes.NewBufferString(` $map: sprite-map("*.png", 10px); // One argument div { background-position: sprite-position($map, "140"); } div.retina { background-position: 10px ceil(nth(sprite-position($map, "140"), 2) /2 ); }`) comp, err := libsass.New(os.Stdout, in, libsass.Payload(payload.New()), libsass.ImgDir("../test/img"), libsass.BuildDir("../test/build"), libsass.ImgBuildDir("../test/build/img"), ) if err != nil { log.Fatal(err) } err = comp.Run() if err != nil { log.Fatal(err) } // Output: // div { // background-position: 0px, -149px; } // // div.retina { // background-position: 10px -74px; } }
func TestFontURLFail(t *testing.T) { in := bytes.NewBufferString(`@font-face { src: font-url("arial.eot"); }`) var out bytes.Buffer comp, err := libsass.New(&out, in, libsass.OutputStyle(libsass.NESTED_STYLE), libsass.BuildDir("../test/build"), libsass.ImgDir("../test/img"), libsass.ImgBuildDir("../test/build/img"), ) if err != nil { t.Fatal(err) } err = comp.Run() if err == nil { t.Fatal("no error reported") } e := "error in C function font-url: font-url: font path not set" if !strings.Contains(err.Error(), e) { t.Errorf("got:\n%s\nwanted:\n%s\n", err, e) } }
func TestFontURL_invalid(t *testing.T) { in := bytes.NewBufferString(`@font-face { src: font-url(5px); }`) var out bytes.Buffer comp, err := libsass.New(&out, in, libsass.OutputStyle(libsass.NESTED_STYLE), libsass.BuildDir("../test/build"), libsass.ImgDir("../test/img"), libsass.ImgBuildDir("../test/build/img"), ) if err != nil { t.Fatal(err) } err = comp.Run() if err == nil { t.Fatal("no error reported") } e := `Error > stdin:2 error in C function font-url: Invalid Sass type expected: string got: libs.SassNumber value: 5px` if !strings.HasPrefix(err.Error(), e) { t.Errorf("got:\n%s\nwanted:\n%s", err, e) } }
func TestSpriteHTTP(t *testing.T) { in := bytes.NewBufferString(` $map: sprite-map("*.png", 10px); div { background: sprite($map, "140"); }`) var out bytes.Buffer comp, err := libsass.New(&out, in, libsass.OutputStyle(libsass.NESTED_STYLE), libsass.BuildDir("../test/build"), libsass.ImgDir("../test/img"), libsass.FontDir("../test/font"), libsass.Payload(payload.New()), libsass.ImgBuildDir("../test/build/img"), libsass.HTTPPath("http://foo.com"), ) if err != nil { t.Fatal(err) } if err := comp.Run(); err != nil { t.Fatal(err) } e := `div { background: url("http://foo.com/build/b9d55f.png") 0px -149px; } ` if e != out.String() { t.Errorf("got:\n%s\nwanted:\n%s", out.String(), e) } }
func setupComp(t *testing.T, r io.Reader, out io.Writer) (libsass.Compiler, error) { comp, err := libsass.New(out, r, libsass.OutputStyle(libsass.NESTED_STYLE), libsass.BuildDir("../test/build"), libsass.ImgDir("../test/img"), libsass.FontDir("../test/font"), libsass.Payload(payload.New()), libsass.ImgBuildDir("../test/build/img"), ) if err != nil { t.Fatal(err) } testSprite(t, comp) done := make(chan struct{}) go func() { select { case <-time.After(5 * time.Second): t.Fatal("setupComp timeout") case <-done: return } }() err = comp.Run() close(done) return comp, err }
func loadAndBuild(sassFile string, gba *BuildArgs, partialMap *SafePartialMap, out io.WriteCloser, sout io.WriteCloser, buildDir string) error { defer func() { // BuildDir lets us know if we should closer out. If no buildDir, // specified out == os.Stdout and do not close. If buildDir != "", // then out must be something we should close. // This is important, since out can be many things and inspecting // them could be race unsafe. if len(buildDir) > 0 { out.Close() sout.Close() } }() // FIXME: move this elsewhere or make it so it doesn't need to be set imgdir := gba.ImageDir if len(imgdir) == 0 { imgdir = filepath.Dir(sassFile) } comp, err := libsass.New(out, nil, // Options overriding defaults libsass.Path(sassFile), libsass.ImgDir(imgdir), libsass.BuildDir(buildDir), libsass.Payload(gba.Payload), libsass.Comments(gba.Comments), libsass.OutputStyle(gba.Style), libsass.FontDir(gba.Font), libsass.ImgBuildDir(gba.Gen), libsass.IncludePaths(gba.Includes), libsass.SourceMap(gba.SourceMap, sout), ) if err != nil { return err } // Start Sass transformation err = comp.Run() if err != nil { return errors.New(color.RedString("%s", err)) } for _, inc := range comp.Imports() { partialMap.AddRelation(sassFile, inc) } // TODO: moves this method to *Build and wait on it to finish // go func(file string) { select { case <-testch: default: } // }(sassFile) return nil }
func TestError_warn(t *testing.T) { oo := os.Stdout defer func() { os.Stdout = oo }() r, w, _ := os.Pipe() defer w.Close() os.Stdout = w // Disabled while new warn integration is built in := bytes.NewBufferString(`@warn "!"; div { color: red; }`) libsass.RegisterHandler("@warn", WarnHandler) var out bytes.Buffer comp, err := libsass.New(&out, in, libsass.OutputStyle(libsass.NESTED_STYLE), libsass.BuildDir("../test/build"), libsass.ImgDir("../test/img"), libsass.ImgBuildDir("../test/build/img"), ) if err != nil { t.Fatal(err) } err = comp.Run() if err != nil { t.Fatal(err) } outC := make(chan string) go func() { var buf bytes.Buffer io.Copy(&buf, r) outC <- buf.String() }() w.Close() warnout := <-outC if len(warnout) == 0 { t.Fatal("no error reported") } e := `WARNING: !` if !strings.Contains(warnout, e) { t.Errorf("got: %q wanted: %q", warnout, e) } }
func TestFuncSpriteFile(t *testing.T) { comp, err := libsass.New(nil, nil, libsass.Payload(payload.New()), libsass.ImgDir("../test/img"), libsass.BuildDir("../test/build"), libsass.ImgBuildDir("../test/build/img"), ) if err != nil { t.Fatal(err) } // Add real arguments when sass lists can be [un]marshalled lst := []interface{}{"*.png", "139"} usv, err := libsass.Marshal(lst) if err != nil { t.Fatal(err) } rsv, err := SpriteFile(libsass.NewCompilerContext(comp), usv) if err != nil { t.Fatal(err) } var glob, path string err = libsass.Unmarshal(*rsv, &glob, &path) if err != nil { t.Error(err) } if e := "*.png"; e != glob { t.Errorf("got: %s wanted: %s", e, glob) } if e := "139"; e != path { t.Errorf("got: %s wanted: %s", e, path) } }
func TestFuncImageURL(t *testing.T) { comp, err := libsass.New(nil, nil, libsass.BuildDir("../test/build"), libsass.ImgDir("../test/img"), ) if err != nil { t.Fatal(err) } ctx := libsass.NewCompilerContext(comp) usv, _ := libsass.Marshal([]string{"139.png"}) rsv, err := ImageURL(ctx, usv) if err != nil { t.Fatal(err) } var path string err = libsass.Unmarshal(*rsv, &path) if err != nil { t.Fatal(err) } if e := "url('../img/139.png')"; e != path { t.Errorf("got: %s wanted: %s", path, e) } // Test sending invalid date to imageURL usv, _ = libsass.Marshal(libs.SassNumber{Value: 1, Unit: "px"}) _, err = ImageURL(ctx, usv) if err == nil { t.Fatal("error is nil") } e := "Invalid Sass type expected: slice got: libs.SassNumber value: 1px" if e != err.Error() { t.Errorf("got: %s wanted: %s", err, e) } }
func ExampleSprite() { in := bytes.NewBufferString(` $map: sprite-map("*.png", 10px); // One argument div { background: sprite($map, "140"); }`) comp, err := libsass.New(os.Stdout, in, libsass.Payload(payload.New()), libsass.ImgDir("../test/img"), libsass.BuildDir("../test/build"), libsass.ImgBuildDir("../test/build/img"), ) if err != nil { log.Fatal(err) } if err := comp.Run(); err != nil { log.Fatal(err) } // Output: // div { // background: url("img/b9d55f.png") 0px -149px; } }
// FromBuildArgs creates a compiler from BuildArgs func FromBuildArgs(dst io.Writer, dstmap io.Writer, src io.Reader, gba *BuildArgs) (libsass.Compiler, error) { if gba == nil { return libsass.New(dst, src) } if gba.Payload == nil { gba.init() } comp, err := libsass.New(dst, src, // Options overriding defaults // libsass.Path(sassFile), what path should be provided? libsass.ImgDir(gba.ImageDir), libsass.ImgBuildDir(gba.Gen), libsass.BuildDir(gba.BuildDir), libsass.Payload(gba.Payload), libsass.Comments(gba.Comments), libsass.OutputStyle(gba.Style), libsass.FontDir(gba.Font), libsass.IncludePaths(gba.Includes), libsass.CacheBust(gba.CacheBust), libsass.SourceMap(gba.SourceMap, dstmap), ) return comp, err }