Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Do not add raw file reference to CSS image URL paths that do not exis…

…t. For #142
  • Loading branch information...
commit fdf461f640f6f4dca3b55b58c253ab6f302d9a6e 1 parent 023de16
@andrewdavey andrewdavey authored
View
39 src/Cassette.UnitTests/Stylesheets/ExpandCssUrlsAssetTransformer.cs
@@ -1,6 +1,8 @@
-using System.IO;
+using System.Diagnostics;
+using System.IO;
using Cassette.IO;
using Cassette.Utilities;
+using Cassette.Web;
using Moq;
using Should;
using Xunit;
@@ -57,15 +59,44 @@ public void GivenCssWithRelativeUrl_WhenTransformed_ThenUrlIsExpanded()
[Fact]
public void GivenCssUrlFileIsNotFound_WhenTransform_ThenUrlIsNotExpanded()
{
+ var output = TransformCssWhereUrlDoesNotExist();
+
+ output.ShouldEqual("p { background-image: url(test.png); }");
+ }
+
+ [Fact]
+ public void GivenCssUrlFileIsNotFound_WhenTransform_ThenRawFileReferenceIsNotAddedToAsset()
+ {
+ TransformCssWhereUrlDoesNotExist();
+
+ asset.Verify(a => a.AddRawFileReference(It.IsAny<string>()), Times.Never());
+ }
+
+ [Fact]
+ public void GivenCssUrlFileIsNotFound_WhenTransform_ThenWarningWrittenToTrace()
+ {
+ var listener = new StringBuilderTraceListener
+ {
+ Filter = new EventTypeFilter(SourceLevels.All)
+ };
+ Trace.Source.Switch.Level = SourceLevels.All;
+ Trace.Source.Listeners.Add(listener);
+
+ TransformCssWhereUrlDoesNotExist();
+
+ Trace.Source.Flush();
+ listener.ToString().ShouldContain("The file ~/styles/test.png, referenced by ~/styles/asset.css, does not exist.");
+ }
+
+ string TransformCssWhereUrlDoesNotExist()
+ {
var imageFile = StubImageFile();
imageFile.SetupGet(f => f.Exists).Returns(false);
SetupDirectoryGetFile(imageFile.Object);
var css = "p { background-image: url(test.png); }";
var getResult = transformer.Transform(css.AsStream, asset.Object);
- var output = getResult().ReadToEnd();
-
- output.ShouldEqual("p { background-image: url(test.png); }");
+ return getResult().ReadToEnd();
}
[Fact]
View
22 src/Cassette.Web/StringBuilderTraceListener.cs
@@ -9,27 +9,15 @@ class StringBuilderTraceListener : TraceListener
public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
{
- if (eventType == TraceEventType.Information)
- {
- WriteLine(eventCache.DateTime.TimeOfDay + " - " + message);
- }
- else
- {
- base.TraceEvent(eventCache, source, eventType, id, message);
- }
+ WriteLine(eventCache.DateTime.TimeOfDay + " - " + message);
+ base.TraceEvent(eventCache, source, eventType, id, message);
}
public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
{
var message = args == null ? format : string.Format(format, args);
- if (eventType == TraceEventType.Information)
- {
- WriteLine(eventCache.DateTime.TimeOfDay + " - " + message);
- }
- else
- {
- base.TraceEvent(eventCache, source, eventType, id, message);
- }
+ WriteLine(eventCache.DateTime.TimeOfDay + " - " + message);
+ base.TraceEvent(eventCache, source, eventType, id, message);
}
public override void Write(string message)
@@ -47,4 +35,4 @@ public override string ToString()
return builder.ToString();
}
}
-}
+}
View
27 src/Cassette/Stylesheets/ExpandCssUrlsAssetTransformer.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
@@ -40,9 +41,14 @@ public Func<Stream> Transform(Func<Stream> openSourceStream, IAsset asset)
{
var matchedUrlGroup = match.Groups["url"];
var relativeFilename = GetImageFilename(matchedUrlGroup, currentDirectory);
- ExpandUrl(builder, matchedUrlGroup, relativeFilename);
-
- asset.AddRawFileReference(relativeFilename);
+ if (ExpandUrl(builder, matchedUrlGroup, relativeFilename))
+ {
+ asset.AddRawFileReference(relativeFilename);
+ }
+ else
+ {
+ Trace.Source.TraceEvent(TraceEventType.Warning, 0, "The file {0}, referenced by {1}, does not exist.", relativeFilename, asset.SourceFile.FullPath);
+ }
}
return builder.ToString().AsStream();
};
@@ -70,21 +76,30 @@ IEnumerable<Match> UrlMatchesInReverse(string css)
return CssUrlRegex
.Matches(css)
.Cast<Match>()
- .Where(match => AbsoluteUrlRegex.IsMatch(match.Groups["url"].Value) == false)
+ .Where(IsRelativeUrl)
.OrderByDescending(match => match.Index)
.ToArray();
}
- void ExpandUrl(StringBuilder builder, Group matchedUrlGroup, string relativeFilename)
+ bool IsRelativeUrl(Match match)
+ {
+ return !AbsoluteUrlRegex.IsMatch(match.Groups["url"].Value);
+ }
+
+ bool ExpandUrl(StringBuilder builder, Group matchedUrlGroup, string relativeFilename)
{
relativeFilename = RemoveFragment(relativeFilename);
var file = sourceDirectory.GetFile(relativeFilename.TrimStart('~', '/'));
- if (!file.Exists) return;
+ if (!file.Exists)
+ {
+ return false;
+ }
var hash = HashFileContents(file);
var absoluteUrl = urlGenerator.CreateRawFileUrl(relativeFilename, hash);
builder.Remove(matchedUrlGroup.Index, matchedUrlGroup.Length);
builder.Insert(matchedUrlGroup.Index, absoluteUrl);
+ return true;
}
string RemoveFragment(string relativeFilename)
View
3  src/Cassette/Trace.cs
@@ -6,5 +6,4 @@ static class Trace
{
public static readonly TraceSource Source = new TraceSource("Cassette");
}
-}
-
+}
Please sign in to comment.
Something went wrong with that request. Please try again.