Skip to content

Commit

Permalink
fix crash on empty Sass file
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnCoates committed Mar 17, 2016
1 parent b848f39 commit 4561fd0
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CSSketch Helper/CSSketch Helper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
FA56F7E31BC0E15E005673E0 /* CSSketch Helper.bundle in CopyFiles */ = {isa = PBXBuildFile; fileRef = FA56F7CF1BC0D6C4005673E0 /* CSSketch Helper.bundle */; };
FA6F46F41C5EB0A0002EE579 /* libCocoaSass.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FA6F46F31C5EB095002EE579 /* libCocoaSass.a */; };
FA6F46F71C5EB74F002EE579 /* libc++.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = FA6F46F61C5EB74F002EE579 /* libc++.tbd */; };
FA795BCD1C9A8FB20050571B /* NSError+CSSketch.m in Sources */ = {isa = PBXBuildFile; fileRef = FA795BCC1C9A8FB20050571B /* NSError+CSSketch.m */; };
FA7D76C21BC1A5BA00FCDEF5 /* CSKLess.m in Sources */ = {isa = PBXBuildFile; fileRef = FA7D76C11BC1A5BA00FCDEF5 /* CSKLess.m */; };
FA7D76D81BC1AB6900FCDEF5 /* CSK_Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = FA7D76D71BC1AB6900FCDEF5 /* CSK_Tests.m */; };
FA7D76E41BC1ABFD00FCDEF5 /* JavaScriptCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FA7D76E21BC1ABF600FCDEF5 /* JavaScriptCore.framework */; };
Expand Down Expand Up @@ -156,6 +157,8 @@
FA6F46EE1C5EB095002EE579 /* CocoaSass.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = CocoaSass.xcodeproj; path = ../external/CocoaSass/CocoaSass.xcodeproj; sourceTree = "<group>"; };
FA6F46F51C5EB0B7002EE579 /* CocoaSass.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = CocoaSass.h; path = ../external/CocoaSass/CocoaSass/CocoaSass.h; sourceTree = "<group>"; };
FA6F46F61C5EB74F002EE579 /* libc++.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = "libc++.tbd"; path = "usr/lib/libc++.tbd"; sourceTree = SDKROOT; };
FA795BCB1C9A8FB20050571B /* NSError+CSSketch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSError+CSSketch.h"; sourceTree = "<group>"; };
FA795BCC1C9A8FB20050571B /* NSError+CSSketch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSError+CSSketch.m"; sourceTree = "<group>"; };
FA7BFD801BCCAD5C000D9AFB /* RS_MSTextLayer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RS_MSTextLayer.h; sourceTree = "<group>"; };
FA7BFD811BCCAD5C000D9AFB /* RS_MSTextLayer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RS_MSTextLayer.m; sourceTree = "<group>"; };
FA7D76C01BC1A5BA00FCDEF5 /* CSKLess.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSKLess.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -224,6 +227,8 @@
children = (
FA0E95D91BCB279C00CDD8EF /* NSString+Paths.h */,
FA0E95DA1BCB279C00CDD8EF /* NSString+Paths.m */,
FA795BCB1C9A8FB20050571B /* NSError+CSSketch.h */,
FA795BCC1C9A8FB20050571B /* NSError+CSSketch.m */,
);
path = Categories;
sourceTree = "<group>";
Expand Down Expand Up @@ -720,6 +725,7 @@
FACF8FBA1BCC28320062D8F8 /* CSKDocumentController.m in Sources */,
FACF8FBE1BCC2DB10062D8F8 /* CSKLayers.m in Sources */,
FA38F4BE1BC3C93F00787508 /* RegExCategories.m in Sources */,
FA795BCD1C9A8FB20050571B /* NSError+CSSketch.m in Sources */,
FA56F7E01BC0DDE7005673E0 /* CSKFileMonitor.m in Sources */,
FA0E95DB1BCB279C00CDD8EF /* NSString+Paths.m in Sources */,
FA38F4B41BC3272100787508 /* CSKToolbarProxy.m in Sources */,
Expand Down
15 changes: 15 additions & 0 deletions CSSketch Helper/src/Categories/NSError+CSSketch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// NSError+CSSketch.h
// CSSketch Helper
//
// Created by John Coates on 3/17/16.
// Copyright © 2016 John Coates. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSError (CSSketch)

+ (instancetype)errorWithMessage:(NSString *)message;

@end
27 changes: 27 additions & 0 deletions CSSketch Helper/src/Categories/NSError+CSSketch.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// NSError+CSSketch.m
// CSSketch Helper
//
// Created by John Coates on 3/17/16.
// Copyright © 2016 John Coates. All rights reserved.
//

#import "NSError+CSSketch.h"

@implementation NSError (CSSketch)

+ (instancetype)errorWithMessage:(NSString *)message {
NSError *error;
NSDictionary *userInfo;

userInfo = @{
NSLocalizedDescriptionKey : message
};
error = [NSError errorWithDomain:@"CSK"
code:801
userInfo:userInfo];

return error;
}

@end
27 changes: 17 additions & 10 deletions CSSketch Helper/src/Controllers/CSKStylesheet.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ - (BOOL)parseStylesheet:(CSKStylesheetCompiled)completionBlock {
if ([CSKMainController inSandbox]) {
[self.fileURL startAccessingSecurityScopedResource];
}
NSString *lessStylesheet = [NSString stringWithContentsOfFile:self.fileURL.path encoding:NSUTF8StringEncoding error:&error];
NSString *stylesheet = [NSString stringWithContentsOfFile:self.fileURL.path encoding:NSUTF8StringEncoding error:&error];

if ([CSKMainController inSandbox]) {
[self.fileURL stopAccessingSecurityScopedResource];
}

if (error) {
NSLog(@"error retrieving contents of Less stylesheet: %@", error);
NSLog(@"error retrieving contents of stylesheet: %@", error);
NSString *errorString = [NSString stringWithFormat:@"Couldn't read stylesheet %@", self.fileURL.path];
[CSKMainController displayError:errorString];
return FALSE;
Expand All @@ -87,7 +87,7 @@ - (BOOL)parseStylesheet:(CSKStylesheetCompiled)completionBlock {
// process @import
// un-escaped regex: @import[\s]+?(\([^)]+\)[\s]+)?['"]?([^'"\n]+)['"]?;?
// targetting: @import (keyword) "filename";
NSArray *importMatches = [lessStylesheet matchesWithDetails:RX(@"@import[\\s]+?(\\([^)]+\\)[\\s]+)?['\"]([^'\"\\n]+)['\"]?;?")];
NSArray *importMatches = [stylesheet matchesWithDetails:RX(@"@import[\\s]+?(\\([^)]+\\)[\\s]+)?['\"]([^'\"\\n]+)['\"]?;?")];

if (importMatches.count) {
for (RxMatch *match in importMatches) {
Expand All @@ -109,7 +109,7 @@ - (BOOL)parseStylesheet:(CSKStylesheetCompiled)completionBlock {
[CSKMainController displayError:errorString];
return FALSE;
}
lessStylesheet = [lessStylesheet stringByReplacingOccurrencesOfString:wholeMatch.value withString:importContents];
stylesheet = [stylesheet stringByReplacingOccurrencesOfString:wholeMatch.value withString:importContents];

}
else {
Expand All @@ -122,15 +122,21 @@ - (BOOL)parseStylesheet:(CSKStylesheetCompiled)completionBlock {
}

if (DEBUG) {
NSLog(@"less stylesheet: %@", lessStylesheet);
NSLog(@"stylesheet length %d: %@", (int)stylesheet.length, stylesheet);
}
if (stylesheet.length == 0) {
completionBlock(nil, stylesheet);
return TRUE;
}

NSString *extension = [self fileURL].path.pathExtension.lowercaseString;

if ([extension isEqualToString:@"less"]) {
[CSKLess compileLessStylesheet:lessStylesheet completion:^(NSError *error, NSString *compiledCSS) {
[CSKLess compileLessStylesheet:stylesheet completion:^(NSError *error, NSString *compiledCSS) {
if (error) {
NSLog(@"error compiling stylesheet: %@", error);
NSString *errorMessage = [NSString stringWithFormat:@"CSSketch: Error compiling {less} stylesheet: %@", error];
[CSKMainController displayError:errorMessage];
NSLog(@"%@", errorMessage);
}

if (DEBUG) {
Expand All @@ -144,10 +150,11 @@ - (BOOL)parseStylesheet:(CSKStylesheetCompiled)completionBlock {
else if ([extension isEqualToString:@"sass"] || [extension isEqualToString:@"scss"]) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSError *error = nil;
NSString *compiledCSS = [CocoaSass compileSass:lessStylesheet extension:extension error:&error];
NSString *compiledCSS = [CocoaSass compileSass:stylesheet
extension:extension
error:&error];

if (error != nil || !compiledCSS) {
NSLog(@"Error compiling Sass stylesheet: %@", error);
compiledCSS = @"";
}

Expand All @@ -161,7 +168,7 @@ - (BOOL)parseStylesheet:(CSKStylesheetCompiled)completionBlock {
});
}
else {
completionBlock(nil, lessStylesheet);
completionBlock(nil, stylesheet);
}

return TRUE;
Expand Down
3 changes: 3 additions & 0 deletions CSSketch Helper/src/Headers/PrefixHeader.pch
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// Sass
#import "CocoaSass.h"

// Errors
#import "NSError+CSSketch.h"

#ifdef DEBUG
#define DEBUG 1
#else
Expand Down
36 changes: 25 additions & 11 deletions External/CocoaSass/CocoaSass/CocoaSass.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ + (NSString *)compileSass:(NSString *)contents
sass_compiler_execute(compiler);

int errorStatus = sass_context_get_error_status((struct Sass_Context *)context);

if (errorStatus) {
const char *errorMessage = sass_context_get_error_message((struct Sass_Context *)context);
NSError *error;
NSString *errorString;
NSDictionary *userInfo;

if (errorMessage) {
errorString = [NSString stringWithFormat:@"Error compiling Sass #%d: %s",
Expand All @@ -56,22 +53,39 @@ + (NSString *)compileSass:(NSString *)contents
errorString = [NSString stringWithFormat:@"Error compiling Sass #%d", errorStatus];
}

userInfo = @{
NSLocalizedDescriptionKey : errorString
};
error = [NSError errorWithDomain:@"CSK"
code:801
userInfo:userInfo];

*errorOut = error;
*errorOut = [self errorWithMessage:errorString];
sass_delete_compiler(compiler);
return nil;
}

const char *output = sass_context_get_output_string((struct Sass_Context *)context);
if (!output) {
NSString *errorString = [NSString stringWithFormat:@"Couldn't generate Sass output. Error code (%d)", errorStatus];
*errorOut = [self errorWithMessage:errorString];
NSLog(@"%@", errorString);
sass_delete_compiler(compiler);
return nil;

}
NSString *compiledContents = [NSString stringWithUTF8String:output];
sass_delete_compiler(compiler);

return compiledContents;
}

+ (NSError *)errorWithMessage:(NSString *)message {
NSError *error;
NSDictionary *userInfo;

userInfo = @{
NSLocalizedDescriptionKey : message
};
error = [NSError errorWithDomain:@"CSKSass"
code:801
userInfo:userInfo];

return error;
}


@end

0 comments on commit 4561fd0

Please sign in to comment.