| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | |
| | | 3 | | namespace AspxLint.Core.Rules; |
| | | 4 | | |
| | | 5 | | public sealed class Asp001ControlMissingRunat : IRule |
| | | 6 | | { |
| | 207 | 7 | | public string Id => "ASP-001"; |
| | 28 | 8 | | public string Name => "Controle serveur sans runat=\"server\""; |
| | 25 | 9 | | public Severity Severity => Severity.Error; |
| | | 10 | | public string Description => |
| | 25 | 11 | | "Tout controle <asp:...> doit avoir l'attribut runat=\"server\", sinon il est traite comme du HTML litteral et n |
| | 46 | 12 | | public bool HasFix => true; |
| | | 13 | | |
| | 5 | 14 | | private static readonly Regex DetectRegex = new( |
| | 5 | 15 | | @"<asp:([a-zA-Z]+)\b([^>]*?)/?>", |
| | 5 | 16 | | RegexOptions.Compiled); |
| | | 17 | | |
| | 5 | 18 | | private static readonly Regex FixRegex = new( |
| | 5 | 19 | | @"<asp:([a-zA-Z]+)\b([^>]*?)(\s*/?)>", |
| | 5 | 20 | | RegexOptions.Compiled); |
| | | 21 | | |
| | 5 | 22 | | private static readonly Regex RunatPresent = new( |
| | 5 | 23 | | @"\brunat\s*=\s*[""']?server[""']?", |
| | 5 | 24 | | RegexOptions.IgnoreCase | RegexOptions.Compiled); |
| | | 25 | | |
| | | 26 | | public IEnumerable<Issue> Detect(string content, string[] lines, RuleContext ctx) |
| | | 27 | | { |
| | | 28 | | for (int i = 0; i < lines.Length; i++) |
| | | 29 | | { |
| | | 30 | | foreach (Match m in DetectRegex.Matches(lines[i])) |
| | | 31 | | { |
| | | 32 | | if (RunatPresent.IsMatch(m.Groups[2].Value)) continue; |
| | | 33 | | yield return new Issue(Id, Name, Severity, |
| | | 34 | | i + 1, m.Index + 1, m.Value, |
| | | 35 | | $"Ajouter runat=\"server\" au controle <asp:{m.Groups[1].Value}>"); |
| | | 36 | | } |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public string? Fix(string content, RuleContext ctx) => |
| | 27 | 41 | | FixRegex.Replace(content, m => |
| | 27 | 42 | | { |
| | 27 | 43 | | var attrs = m.Groups[2].Value; |
| | 27 | 44 | | if (RunatPresent.IsMatch(attrs)) return m.Value; |
| | 27 | 45 | | var cleanAttrs = attrs.TrimEnd(); |
| | 27 | 46 | | var tail = m.Groups[3].Value.Contains('/') ? " />" : ">"; |
| | 27 | 47 | | return $"<asp:{m.Groups[1].Value}{cleanAttrs} runat=\"server\"{tail}"; |
| | 27 | 48 | | }); |
| | | 49 | | } |