forked from sass/libsass-net
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[wip] Initial support for SassTypes.
* Added two interface per type, one internal and a public facing one to prevent malicious pointers. * Added dependency walker for SassType for circular references. Require extensive testing.
- Loading branch information
Showing
12 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace Sass.Types | ||
{ | ||
internal interface ISassExportableType | ||
{ | ||
/// <summary> | ||
/// Intantiate the type on LibSass heap. | ||
/// </summary> | ||
IntPtr GetInternalTypePtr(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Sass.Types | ||
{ | ||
public interface ISassType | ||
{ } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
|
||
namespace Sass.Types | ||
{ | ||
public class SassColor: ISassType, ISassExportableType | ||
{ | ||
public double Value { get; set; } | ||
public SassUnit Unit { get; set; } | ||
|
||
public override string ToString() | ||
{ | ||
return $"{Value}{Unit}"; | ||
} | ||
|
||
IntPtr ISassExportableType.GetInternalTypePtr() | ||
{ | ||
return SassCompiler.sass_make_number(Value, Unit.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Sass.Types | ||
{ | ||
public class SassList : ISassType, ISassExportableType | ||
{ | ||
public List<ISassType> Values { get; set; } = new List<ISassType>(); | ||
public SassListSeparator Separator { get; set; } = SassListSeparator.Space; | ||
|
||
private bool _ensured; | ||
|
||
/// <summary> | ||
/// Recursively ensures: | ||
/// * Arbitrary ISassType implementation. | ||
/// * Circular reference on each "listy" item stored in the values. | ||
/// </summary> | ||
/// <param name="lists">List containing instances of SassList</param> | ||
internal static void WalkAndEnsureDependencies(SassList list, List<SassList> lists) | ||
{ | ||
// Prevent from re-entrance. | ||
if (list._ensured) | ||
return; | ||
|
||
// FIXME: Should we instead loop through the array and | ||
// report the exact index which violates this rule? | ||
if (!list.Values.All(v => v is ISassExportableType)) | ||
{ | ||
throw new SassTypeException( | ||
@"The value must not contain an object of type that is an arbitrary implementation | ||
of ISassType. Please use the predefined Sass types or extend the predefined type's | ||
functionality using inheritance or extension methods."); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
am11
Author
Owner
|
||
} | ||
|
||
// Detect the circular-referencing values. | ||
lists.Add(list); | ||
|
||
var filteredValues = list.Values | ||
.Where(v => v is SassList) | ||
.Select(v => v as SassList).ToList(); | ||
|
||
if (filteredValues.Any(v => lists.Contains(v))) | ||
This comment has been minimized.
Sorry, something went wrong.
darrenkopp
|
||
throw new SassTypeException( | ||
@"Circular reference detected. Values cannot contain | ||
self-referencing instance."); | ||
|
||
filteredValues.ForEach(v => WalkAndEnsureDependencies(v, lists)); | ||
list._ensured = true; | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return string.Join(Separator.ToString(), Values.Select(v => v.ToString())); | ||
} | ||
|
||
IntPtr ISassExportableType.GetInternalTypePtr() | ||
{ | ||
WalkAndEnsureDependencies(this, new List<SassList>()); | ||
|
||
var list = SassCompiler.sass_make_list(Values.Count, Separator); | ||
|
||
for (int index = 0; index < Values.Count; ++index) | ||
{ | ||
SassCompiler.sass_list_set_value(list, index, | ||
(Values[index] as ISassExportableType).GetInternalTypePtr()); | ||
} | ||
|
||
return list; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Sass.Types | ||
{ | ||
public enum SassListSeparator | ||
{ | ||
Comma = ',', | ||
Space = ' ' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
|
||
namespace Sass.Types | ||
{ | ||
public class SassMap : ISassType, ISassExportableType | ||
{ | ||
public double Value { get; set; } | ||
public SassUnit Unit { get; set; } | ||
|
||
public override string ToString() | ||
{ | ||
return $"{Value}{Unit}"; | ||
} | ||
|
||
IntPtr ISassExportableType.GetInternalTypePtr() | ||
{ | ||
return SassCompiler.sass_make_number(Value, Unit.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
|
||
namespace Sass.Types | ||
{ | ||
public class SassNumber : ISassType, ISassExportableType | ||
{ | ||
public double Value { get; set; } | ||
public SassUnit Unit { get; set; } | ||
|
||
public override string ToString() | ||
{ | ||
return $"{Value}{Unit}"; | ||
} | ||
|
||
IntPtr ISassExportableType.GetInternalTypePtr() | ||
{ | ||
return SassCompiler.sass_make_number(Value, Unit.ToString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System; | ||
|
||
namespace Sass.Types | ||
{ | ||
public class SassTypeException : Exception | ||
{ | ||
public SassTypeException(string message) : base(message) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Sass.Types | ||
{ | ||
public enum SassUnit | ||
{ | ||
IN, CM, PC, MM, PT, PX | ||
} | ||
} |
using @ for strings is gonna make it so you have new lines in your exception message. not sure if this was intended or if you are just doing this for code formatting. you likely don't want the newlines in the exception message