| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | |
| | | 3 | | namespace AspxLint.Core.Rules; |
| | | 4 | | |
| | | 5 | | public sealed class Attr003DuplicateAttribute : IRule |
| | | 6 | | { |
| | 203 | 7 | | public string Id => "ATTR-003"; |
| | 26 | 8 | | public string Name => "Attributs en double dans une balise"; |
| | 23 | 9 | | public Severity Severity => Severity.Error; |
| | | 10 | | public string Description => |
| | 25 | 11 | | "Une meme balise ne doit pas contenir deux fois le meme attribut. Le navigateur ne conserve generalement que le |
| | 46 | 12 | | public bool HasFix => false; |
| | | 13 | | |
| | 5 | 14 | | private static readonly Regex TagRegex = |
| | 5 | 15 | | new(@"<([a-zA-Z][a-zA-Z0-9:_\-]*)\b([^>]*?)>", RegexOptions.Compiled); |
| | | 16 | | |
| | 5 | 17 | | private static readonly Regex AttrNameRegex = |
| | 5 | 18 | | new(@"\s([a-zA-Z][a-zA-Z0-9\-:_]*)\s*=", RegexOptions.Compiled); |
| | | 19 | | |
| | | 20 | | public IEnumerable<Issue> Detect(string content, string[] lines, RuleContext ctx) |
| | | 21 | | { |
| | | 22 | | for (int i = 0; i < lines.Length; i++) |
| | | 23 | | { |
| | | 24 | | foreach (Match m in TagRegex.Matches(lines[i])) |
| | | 25 | | { |
| | | 26 | | var attrs = m.Groups[2].Value; |
| | | 27 | | var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase); |
| | | 28 | | |
| | | 29 | | foreach (Match am in AttrNameRegex.Matches(attrs)) |
| | | 30 | | { |
| | | 31 | | var name = am.Groups[1].Value; |
| | | 32 | | if (!seen.Add(name)) |
| | | 33 | | { |
| | | 34 | | var snippet = m.Value.Length > 80 ? m.Value[..80] + "…" : m.Value; |
| | | 35 | | yield return new Issue(Id, Name, Severity, |
| | | 36 | | i + 1, m.Index + 1, snippet, |
| | | 37 | | $"L'attribut \"{name}\" apparait plusieurs fois dans cette balise."); |
| | | 38 | | break; // un par balise suffit |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | } |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | 4 | 45 | | public string? Fix(string content, RuleContext ctx) => null; |
| | | 46 | | } |