| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | |
| | | 3 | | namespace AspxLint.Core.Rules; |
| | | 4 | | |
| | | 5 | | public sealed class Doc001MissingDoctype : IRule |
| | | 6 | | { |
| | 207 | 7 | | public string Id => "DOC-001"; |
| | 32 | 8 | | public string Name => "DOCTYPE manquant (ASPX seulement)"; |
| | 29 | 9 | | public Severity Severity => Severity.Warning; |
| | | 10 | | public string Description => |
| | 25 | 11 | | "Un fichier ASPX standalone (non-Content) devrait declarer un DOCTYPE pour activer le mode standards des navigat |
| | 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 DoctypePresent = new( |
| | 5 | 17 | | @"<!DOCTYPE", RegexOptions.IgnoreCase | RegexOptions.Compiled); |
| | 5 | 18 | | private static readonly Regex HtmlOpen = new( |
| | 5 | 19 | | @"<html\b", RegexOptions.IgnoreCase | RegexOptions.Compiled); |
| | | 20 | | |
| | | 21 | | public IEnumerable<Issue> Detect(string content, string[] lines, RuleContext ctx) |
| | | 22 | | { |
| | | 23 | | if (ctx.Ext != "aspx") yield break; |
| | | 24 | | if (MasterPageFile.IsMatch(content)) yield break; // content page : le master fournit le DOCTYPE |
| | | 25 | | if (DoctypePresent.IsMatch(content)) yield break; |
| | | 26 | | if (!HtmlOpen.IsMatch(content)) yield break; // pas de <html> = sans doute un fragment |
| | | 27 | | |
| | | 28 | | yield return new Issue(Id, Name, Severity, 1, 1, |
| | | 29 | | "(absence de <!DOCTYPE>)", |
| | | 30 | | "Ajouter \"<!DOCTYPE html>\" avant la balise <html>."); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public string? Fix(string content, RuleContext ctx) |
| | | 34 | | { |
| | 21 | 35 | | if (ctx.Ext != "aspx") return content; |
| | 25 | 36 | | if (DoctypePresent.IsMatch(content) || MasterPageFile.IsMatch(content)) return content; |
| | 17 | 37 | | var match = HtmlOpen.Match(content); |
| | 26 | 38 | | if (!match.Success) return content; |
| | 8 | 39 | | return content[..match.Index] + "<!DOCTYPE html>\n" + content[match.Index..]; |
| | | 40 | | } |
| | | 41 | | } |