| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | |
| | | 3 | | namespace AspxLint.Core.Rules; |
| | | 4 | | |
| | | 5 | | public sealed class Tag001VoidElementsXhtml : IRule |
| | | 6 | | { |
| | 237 | 7 | | public string Id => "TAG-001"; |
| | 52 | 8 | | public string Name => "Balise auto-fermante non conforme XHTML"; |
| | 49 | 9 | | public Severity Severity => Severity.Warning; |
| | | 10 | | public string Description => |
| | 25 | 11 | | "Les balises vides comme <br>, <hr>, <img>, <input>, <meta>, <link> doivent s'ecrire en auto-fermantes (ex: <br |
| | 46 | 12 | | public bool HasFix => true; |
| | | 13 | | |
| | | 14 | | private const string VoidTagAlternation = |
| | | 15 | | "br|hr|img|input|meta|link|area|base|col|embed|source|track|wbr"; |
| | | 16 | | |
| | | 17 | | // Detection : balises void sans / avant le >. |
| | 5 | 18 | | private static readonly Regex DetectRegex = new( |
| | 5 | 19 | | $@"<({VoidTagAlternation})(\s[^>]*?)?(?<!/)>", |
| | 5 | 20 | | RegexOptions.IgnoreCase | RegexOptions.Compiled); |
| | | 21 | | |
| | | 22 | | // Fix : meme chose mais on capture aussi un eventuel whitespace avant > pour le re-emettre proprement. |
| | 5 | 23 | | private static readonly Regex FixRegex = new( |
| | 5 | 24 | | $@"<({VoidTagAlternation})((?:\s[^>]*?)?)(?<!/)\s*>", |
| | 5 | 25 | | RegexOptions.IgnoreCase | RegexOptions.Compiled); |
| | | 26 | | |
| | | 27 | | public IEnumerable<Issue> Detect(string content, string[] lines, RuleContext ctx) |
| | | 28 | | { |
| | | 29 | | for (int i = 0; i < lines.Length; i++) |
| | | 30 | | { |
| | | 31 | | foreach (Match m in DetectRegex.Matches(lines[i])) |
| | | 32 | | { |
| | | 33 | | if (m.Value.EndsWith("/>")) continue; // garde-fou |
| | | 34 | | var fixedTag = m.Value[..^1].TrimEnd() + " />"; |
| | | 35 | | yield return new Issue(Id, Name, Severity, |
| | | 36 | | i + 1, m.Index + 1, |
| | | 37 | | m.Value, |
| | | 38 | | $"Remplacer par \"{fixedTag}\"."); |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public string? Fix(string content, RuleContext ctx) => |
| | 27 | 44 | | FixRegex.Replace(content, m => $"<{m.Groups[1].Value}{m.Groups[2].Value} />"); |
| | | 45 | | } |