| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | |
| | | 3 | | namespace AspxLint.Core.Rules; |
| | | 4 | | |
| | | 5 | | public sealed class Form001FormMissingRunat : IRule |
| | | 6 | | { |
| | 203 | 7 | | public string Id => "FORM-001"; |
| | 28 | 8 | | public string Name => "Balise <form> sans runat=\"server\" dans une page ASPX"; |
| | 25 | 9 | | public Severity Severity => Severity.Error; |
| | | 10 | | public string Description => |
| | 25 | 11 | | "Une page ASPX qui utilise des controles serveur a besoin d'un <form runat=\"server\"> englobant. Sans cela, le |
| | 46 | 12 | | public bool HasFix => true; |
| | | 13 | | |
| | 5 | 14 | | private static readonly Regex MasterPageFile = new( |
| | 5 | 15 | | @"MasterPageFile\s*=", RegexOptions.IgnoreCase | RegexOptions.Compiled); |
| | 5 | 16 | | private static readonly Regex AspControl = new( |
| | 5 | 17 | | @"<asp:", RegexOptions.IgnoreCase | RegexOptions.Compiled); |
| | 5 | 18 | | private static readonly Regex DetectRegex = new( |
| | 5 | 19 | | @"<form\b([^>]*?)>", RegexOptions.IgnoreCase | RegexOptions.Compiled); |
| | 5 | 20 | | private static readonly Regex RunatPresent = new( |
| | 5 | 21 | | @"\brunat\s*=\s*[""']?server[""']?", RegexOptions.IgnoreCase | RegexOptions.Compiled); |
| | | 22 | | |
| | | 23 | | public IEnumerable<Issue> Detect(string content, string[] lines, RuleContext ctx) |
| | | 24 | | { |
| | | 25 | | if (ctx.Ext != "aspx") yield break; |
| | | 26 | | if (MasterPageFile.IsMatch(content)) yield break; // content page : le master a deja le form runat |
| | | 27 | | if (!AspControl.IsMatch(content)) yield break; // pas de controle serveur = pas pertinent |
| | | 28 | | |
| | | 29 | | for (int i = 0; i < lines.Length; i++) |
| | | 30 | | { |
| | | 31 | | foreach (Match m in DetectRegex.Matches(lines[i])) |
| | | 32 | | { |
| | | 33 | | if (RunatPresent.IsMatch(m.Groups[1].Value)) continue; |
| | | 34 | | yield return new Issue(Id, Name, Severity, |
| | | 35 | | i + 1, m.Index + 1, m.Value, |
| | | 36 | | "Ajouter runat=\"server\" a la balise <form>."); |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public string? Fix(string content, RuleContext ctx) => |
| | 21 | 42 | | DetectRegex.Replace(content, m => |
| | 21 | 43 | | { |
| | 21 | 44 | | var attrs = m.Groups[1].Value; |
| | 21 | 45 | | if (RunatPresent.IsMatch(attrs)) return m.Value; |
| | 21 | 46 | | var cleanAttrs = attrs.TrimEnd(); |
| | 21 | 47 | | return $"<form{cleanAttrs} runat=\"server\">"; |
| | 21 | 48 | | }); |
| | | 49 | | } |