| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | |
| | | 3 | | namespace AspxLint.Core.Rules; |
| | | 4 | | |
| | | 5 | | public sealed class Com001NestedDashes : IRule |
| | | 6 | | { |
| | 201 | 7 | | public string Id => "COM-001"; |
| | 28 | 8 | | public string Name => "Commentaire HTML imbrique (-- dans <!-- -->)"; |
| | 25 | 9 | | public Severity Severity => Severity.Warning; |
| | | 10 | | public string Description => |
| | 25 | 11 | | "La sequence -- ne doit pas apparaitre a l'interieur d'un commentaire HTML <!-- -->. Cela invalide le commentair |
| | 46 | 12 | | public bool HasFix => false; |
| | | 13 | | |
| | 5 | 14 | | private static readonly Regex HtmlComment = |
| | 5 | 15 | | new(@"<!--([\s\S]*?)-->", RegexOptions.Compiled); |
| | | 16 | | |
| | | 17 | | public IEnumerable<Issue> Detect(string content, string[] lines, RuleContext ctx) |
| | | 18 | | { |
| | | 19 | | // Pre-calcul des offsets de debut de ligne. |
| | | 20 | | var lineStarts = new int[lines.Length]; |
| | | 21 | | int pos = 0; |
| | | 22 | | for (int i = 0; i < lines.Length; i++) |
| | | 23 | | { |
| | | 24 | | lineStarts[i] = pos; |
| | | 25 | | pos += lines[i].Length + 1; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | foreach (Match m in HtmlComment.Matches(content)) |
| | | 29 | | { |
| | | 30 | | if (!m.Groups[1].Value.Contains("--")) continue; |
| | | 31 | | var snippet = m.Value.Length > 60 ? m.Value[..60] + "…" : m.Value; |
| | | 32 | | yield return new Issue(Id, Name, Severity, |
| | | 33 | | LineFromOffset(m.Index, lineStarts), 1, snippet, |
| | | 34 | | "Le commentaire contient \"--\", ce qui est invalide en XHTML strict."); |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | |
| | 4 | 38 | | public string? Fix(string content, RuleContext ctx) => null; |
| | | 39 | | |
| | | 40 | | private static int LineFromOffset(int offset, int[] lineStarts) |
| | | 41 | | { |
| | 4 | 42 | | int lo = 0, hi = lineStarts.Length - 1; |
| | 4 | 43 | | while (lo < hi) |
| | | 44 | | { |
| | 2 | 45 | | int mid = (lo + hi + 1) / 2; |
| | 2 | 46 | | if (lineStarts[mid] <= offset) lo = mid; |
| | 2 | 47 | | else hi = mid - 1; |
| | | 48 | | } |
| | 2 | 49 | | return lo + 1; |
| | | 50 | | } |
| | | 51 | | } |