Skip to content

Commit

Permalink
Updated error message
Browse files Browse the repository at this point in the history
  • Loading branch information
madskristensen committed Nov 16, 2013
1 parent eb0961e commit 05e45c7
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions EditorExtensions/Validation/HTML/BootstrapClassValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
using Microsoft.Web.Editor;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;

namespace MadsKristensen.EditorExtensions.Validation.HTML
namespace MadsKristensen.EditorExtensions
{
[Export(typeof(IHtmlElementValidatorProvider))]
[ContentType(HtmlContentTypeDefinition.HtmlContentType)]
Expand All @@ -16,6 +17,7 @@ public class BootstrapClassValidatorProvider : BaseHtmlElementValidatorProvider<
public class BootstrapClassValidator : BaseValidator
{
private static string[] _tokens = new[] { "btn", "glyphicon", "alert", "label", "fa" }; // fa is for FontAwesome
private static string _error = "When using \"{0}\", you must also specify the class \"{1}\".";

public override IList<IHtmlValidationError> ValidateElement(ElementNode element)
{
Expand All @@ -30,20 +32,29 @@ public override IList<IHtmlValidationError> ValidateElement(ElementNode element)
if (!IsCorrect(classNames.Value, token))
{
int index = element.Attributes.IndexOf(classNames);
results.AddAttributeError(element, "You must also specify the class \"" + token + "\"", HtmlValidationErrorLocation.AttributeValue, index);
string offender = GetOffendingClassName(classNames.Value, token);
string error = string.Format(_error, offender, token);

results.AddAttributeError(element, error, HtmlValidationErrorLocation.AttributeValue, index);
}
}

return results;
}

private bool IsCorrect(string input, string token)
private static bool IsCorrect(string input, string token)
{
if (input.Contains(token + "-") &&
!(input.Contains(token + " ") || input.EndsWith(token)))
return false;

return true;
}

private static string GetOffendingClassName(string input, string token)
{
string[] classes = input.Split(' ');
return classes.FirstOrDefault(c => c.StartsWith(token + "-"));
}
}
}

0 comments on commit 05e45c7

Please sign in to comment.