Skip to main content

rumdl_lib/rules/
md044_proper_names.rs

1use crate::utils::fast_hash;
2use crate::utils::regex_cache::{escape_regex, get_cached_fancy_regex};
3
4use crate::rule::{Fix, LintError, LintResult, LintWarning, Rule, Severity};
5use std::collections::{HashMap, HashSet};
6use std::sync::{Arc, Mutex};
7
8mod md044_config;
9pub use md044_config::MD044Config;
10
11type WarningPosition = (usize, usize, String); // (line, column, found_name)
12
13/// Rule MD044: Proper names should be capitalized
14///
15/// See [docs/md044.md](../../docs/md044.md) for full documentation, configuration, and examples.
16///
17/// This rule is triggered when proper names are not capitalized correctly in the document.
18/// For example, if you have defined "JavaScript" as a proper name, the rule will flag any
19/// occurrences of "javascript" or "Javascript" as violations.
20///
21/// ## Purpose
22///
23/// Ensuring consistent capitalization of proper names improves document quality and
24/// professionalism. This is especially important for technical documentation where
25/// product names, programming languages, and technologies often have specific
26/// capitalization conventions.
27///
28/// ## Configuration Options
29///
30/// The rule supports the following configuration options:
31///
32/// ```yaml
33/// MD044:
34///   names: []                # List of proper names to check for correct capitalization
35///   code-blocks: false       # Whether to check code blocks (default: false)
36/// ```
37///
38/// Example configuration:
39///
40/// ```yaml
41/// MD044:
42///   names: ["JavaScript", "Node.js", "TypeScript"]
43///   code-blocks: true
44/// ```
45///
46/// ## Performance Optimizations
47///
48/// This rule implements several performance optimizations:
49///
50/// 1. **Regex Caching**: Pre-compiles and caches regex patterns for each proper name
51/// 2. **Content Caching**: Caches results based on content hashing for repeated checks
52/// 3. **Efficient Text Processing**: Uses optimized algorithms to avoid redundant text processing
53/// 4. **Smart Code Block Detection**: Efficiently identifies and optionally excludes code blocks
54///
55/// ## Edge Cases Handled
56///
57/// - **Word Boundaries**: Only matches complete words, not substrings within other words
58/// - **Case Sensitivity**: Properly handles case-specific matching
59/// - **Code Blocks**: Optionally checks code blocks (controlled by code-blocks setting)
60/// - **Markdown Formatting**: Handles proper names within Markdown formatting elements
61///
62/// ## Fix Behavior
63///
64/// When fixing issues, this rule replaces incorrect capitalization with the correct form
65/// as defined in the configuration.
66///
67/// Check if a trimmed line is an inline config comment from a linting tool.
68/// Recognized tools: rumdl, markdownlint, Vale, and remark-lint.
69fn is_inline_config_comment(trimmed: &str) -> bool {
70    trimmed.starts_with("<!-- rumdl-")
71        || trimmed.starts_with("<!-- markdownlint-")
72        || trimmed.starts_with("<!-- vale off")
73        || trimmed.starts_with("<!-- vale on")
74        || (trimmed.starts_with("<!-- vale ") && trimmed.contains(" = "))
75        || trimmed.starts_with("<!-- vale style")
76        || trimmed.starts_with("<!-- lint disable ")
77        || trimmed.starts_with("<!-- lint enable ")
78        || trimmed.starts_with("<!-- lint ignore ")
79}
80
81#[derive(Clone)]
82pub struct MD044ProperNames {
83    config: MD044Config,
84    // Cache the combined regex pattern string
85    combined_pattern: Option<String>,
86    // Precomputed lowercase name variants for fast pre-checks
87    name_variants: Vec<String>,
88    // Cache for name violations by content hash
89    content_cache: Arc<Mutex<HashMap<u64, Vec<WarningPosition>>>>,
90}
91
92impl MD044ProperNames {
93    pub fn new(names: Vec<String>, code_blocks: bool) -> Self {
94        let config = MD044Config {
95            names,
96            code_blocks,
97            html_elements: true, // Default to checking HTML elements
98            html_comments: true, // Default to checking HTML comments
99        };
100        let combined_pattern = Self::create_combined_pattern(&config);
101        let name_variants = Self::build_name_variants(&config);
102        Self {
103            config,
104            combined_pattern,
105            name_variants,
106            content_cache: Arc::new(Mutex::new(HashMap::new())),
107        }
108    }
109
110    // Helper function for consistent ASCII normalization
111    fn ascii_normalize(s: &str) -> String {
112        s.replace(['é', 'è', 'ê', 'ë'], "e")
113            .replace(['à', 'á', 'â', 'ä', 'ã', 'å'], "a")
114            .replace(['ï', 'î', 'í', 'ì'], "i")
115            .replace(['ü', 'ú', 'ù', 'û'], "u")
116            .replace(['ö', 'ó', 'ò', 'ô', 'õ'], "o")
117            .replace('ñ', "n")
118            .replace('ç', "c")
119    }
120
121    pub fn from_config_struct(config: MD044Config) -> Self {
122        let combined_pattern = Self::create_combined_pattern(&config);
123        let name_variants = Self::build_name_variants(&config);
124        Self {
125            config,
126            combined_pattern,
127            name_variants,
128            content_cache: Arc::new(Mutex::new(HashMap::new())),
129        }
130    }
131
132    // Create a combined regex pattern for all proper names
133    fn create_combined_pattern(config: &MD044Config) -> Option<String> {
134        if config.names.is_empty() {
135            return None;
136        }
137
138        // Create patterns for all names and their variations
139        let mut patterns: Vec<String> = config
140            .names
141            .iter()
142            .flat_map(|name| {
143                let mut variations = vec![];
144                let lower_name = name.to_lowercase();
145
146                // Add the lowercase version
147                variations.push(escape_regex(&lower_name));
148
149                // Add version without dots
150                let lower_name_no_dots = lower_name.replace('.', "");
151                if lower_name != lower_name_no_dots {
152                    variations.push(escape_regex(&lower_name_no_dots));
153                }
154
155                // Add ASCII-normalized versions for common accented characters
156                let ascii_normalized = Self::ascii_normalize(&lower_name);
157
158                if ascii_normalized != lower_name {
159                    variations.push(escape_regex(&ascii_normalized));
160
161                    // Also add version without dots
162                    let ascii_no_dots = ascii_normalized.replace('.', "");
163                    if ascii_normalized != ascii_no_dots {
164                        variations.push(escape_regex(&ascii_no_dots));
165                    }
166                }
167
168                variations
169            })
170            .collect();
171
172        // Sort patterns by length (longest first) to avoid shorter patterns matching within longer ones
173        patterns.sort_by_key(|b| std::cmp::Reverse(b.len()));
174
175        // Combine all patterns into a single regex with capture groups
176        // Don't use \b as it doesn't work with Unicode - we'll check boundaries manually
177        Some(format!(r"(?i)({})", patterns.join("|")))
178    }
179
180    fn build_name_variants(config: &MD044Config) -> Vec<String> {
181        let mut variants = HashSet::new();
182        for name in &config.names {
183            let lower_name = name.to_lowercase();
184            variants.insert(lower_name.clone());
185
186            let lower_no_dots = lower_name.replace('.', "");
187            if lower_name != lower_no_dots {
188                variants.insert(lower_no_dots);
189            }
190
191            let ascii_normalized = Self::ascii_normalize(&lower_name);
192            if ascii_normalized != lower_name {
193                variants.insert(ascii_normalized.clone());
194
195                let ascii_no_dots = ascii_normalized.replace('.', "");
196                if ascii_normalized != ascii_no_dots {
197                    variants.insert(ascii_no_dots);
198                }
199            }
200        }
201
202        variants.into_iter().collect()
203    }
204
205    // Find all name violations in the content and return positions.
206    // `content_lower` is the pre-computed lowercase version of `content` to avoid redundant allocations.
207    fn find_name_violations(
208        &self,
209        content: &str,
210        ctx: &crate::lint_context::LintContext,
211        content_lower: &str,
212    ) -> Vec<WarningPosition> {
213        // Early return: if no names configured or content is empty
214        if self.config.names.is_empty() || content.is_empty() || self.combined_pattern.is_none() {
215            return Vec::new();
216        }
217
218        // Early return: quick check if any of the configured names might be in content
219        let has_potential_matches = self.name_variants.iter().any(|name| content_lower.contains(name));
220
221        if !has_potential_matches {
222            return Vec::new();
223        }
224
225        // Check if we have cached results
226        let hash = fast_hash(content);
227        {
228            // Use a separate scope for borrowing to minimize lock time
229            if let Ok(cache) = self.content_cache.lock()
230                && let Some(cached) = cache.get(&hash)
231            {
232                return cached.clone();
233            }
234        }
235
236        let mut violations = Vec::new();
237
238        // Get the regex from global cache
239        let combined_regex = match &self.combined_pattern {
240            Some(pattern) => match get_cached_fancy_regex(pattern) {
241                Ok(regex) => regex,
242                Err(_) => return Vec::new(),
243            },
244            None => return Vec::new(),
245        };
246
247        // Use ctx.lines for better performance
248        for (line_idx, line_info) in ctx.lines.iter().enumerate() {
249            let line_num = line_idx + 1;
250            let line = line_info.content(ctx.content);
251
252            // Skip code fence lines (```language or ~~~language)
253            let trimmed = line.trim_start();
254            if trimmed.starts_with("```") || trimmed.starts_with("~~~") {
255                continue;
256            }
257
258            // Skip if in code block (when code_blocks = false)
259            if !self.config.code_blocks && line_info.in_code_block {
260                continue;
261            }
262
263            // Skip if in HTML block (when html_elements = false)
264            if !self.config.html_elements && line_info.in_html_block {
265                continue;
266            }
267
268            // Skip HTML comments using pre-computed line flag
269            if !self.config.html_comments && line_info.in_html_comment {
270                continue;
271            }
272
273            // Skip JSX expressions and MDX comments (MDX flavor)
274            if line_info.in_jsx_expression || line_info.in_mdx_comment {
275                continue;
276            }
277
278            // Skip Obsidian comments (Obsidian flavor)
279            if line_info.in_obsidian_comment {
280                continue;
281            }
282
283            // For frontmatter lines, determine offset where checkable value content starts.
284            // YAML keys should not be checked against proper names - only values.
285            let fm_value_offset = if line_info.in_front_matter {
286                Self::frontmatter_value_offset(line)
287            } else {
288                0
289            };
290            if fm_value_offset == usize::MAX {
291                continue;
292            }
293
294            // Skip inline config comments (rumdl, markdownlint, Vale, remark-lint directives)
295            if is_inline_config_comment(trimmed) {
296                continue;
297            }
298
299            // Early return: skip lines that don't contain any potential matches
300            let line_lower = line.to_lowercase();
301            let has_line_matches = self.name_variants.iter().any(|name| line_lower.contains(name));
302
303            if !has_line_matches {
304                continue;
305            }
306
307            // Use the combined regex to find all matches in one pass
308            for cap_result in combined_regex.find_iter(line) {
309                match cap_result {
310                    Ok(cap) => {
311                        let found_name = &line[cap.start()..cap.end()];
312
313                        // Check word boundaries manually for Unicode support
314                        let start_pos = cap.start();
315                        let end_pos = cap.end();
316
317                        // Skip matches in the key portion of frontmatter lines
318                        if start_pos < fm_value_offset {
319                            continue;
320                        }
321
322                        // Skip matches inside HTML tag attributes (handles multi-line tags)
323                        let byte_pos = line_info.byte_offset + start_pos;
324                        if ctx.is_in_html_tag(byte_pos) {
325                            continue;
326                        }
327
328                        if !Self::is_at_word_boundary(line, start_pos, true)
329                            || !Self::is_at_word_boundary(line, end_pos, false)
330                        {
331                            continue; // Not at word boundary
332                        }
333
334                        // Skip if in inline code when code_blocks is false
335                        if !self.config.code_blocks {
336                            if ctx.is_in_code_block_or_span(byte_pos) {
337                                continue;
338                            }
339                            // pulldown-cmark doesn't parse markdown syntax inside HTML
340                            // comments or HTML blocks, so backtick-wrapped text isn't
341                            // detected by is_in_code_block_or_span. Check directly.
342                            if (line_info.in_html_comment || line_info.in_html_block)
343                                && Self::is_in_backtick_code_in_line(line, start_pos)
344                            {
345                                continue;
346                            }
347                        }
348
349                        // Skip if in link URL or reference definition
350                        if Self::is_in_link(ctx, byte_pos) {
351                            continue;
352                        }
353
354                        // Skip if inside an angle-bracket URL (e.g., <https://...>)
355                        // The link parser skips autolinks inside HTML comments,
356                        // so we detect them directly in the line text.
357                        if Self::is_in_angle_bracket_url(line, start_pos) {
358                            continue;
359                        }
360
361                        // Find which proper name this matches
362                        if let Some(proper_name) = self.get_proper_name_for(found_name) {
363                            // Only flag if it's not already correct
364                            if found_name != proper_name {
365                                violations.push((line_num, cap.start() + 1, found_name.to_string()));
366                            }
367                        }
368                    }
369                    Err(e) => {
370                        eprintln!("Regex execution error on line {line_num}: {e}");
371                    }
372                }
373            }
374        }
375
376        // Store in cache (ignore if mutex is poisoned)
377        if let Ok(mut cache) = self.content_cache.lock() {
378            cache.insert(hash, violations.clone());
379        }
380        violations
381    }
382
383    /// Check if a byte position is within a link URL (not link text)
384    ///
385    /// Link text should be checked for proper names, but URLs should be skipped.
386    /// For `[text](url)` - check text, skip url
387    /// For `[text][ref]` - check text, skip reference portion
388    /// For `[[text]]` (WikiLinks) - check text, skip brackets
389    fn is_in_link(ctx: &crate::lint_context::LintContext, byte_pos: usize) -> bool {
390        use pulldown_cmark::LinkType;
391
392        // Binary search links (sorted by byte_offset) to find candidate containing byte_pos
393        let link_idx = ctx.links.partition_point(|link| link.byte_offset <= byte_pos);
394        if link_idx > 0 {
395            let link = &ctx.links[link_idx - 1];
396            if byte_pos < link.byte_end {
397                // WikiLinks [[text]] start with '[[', regular links [text] start with '['
398                let text_start = if matches!(link.link_type, LinkType::WikiLink { .. }) {
399                    link.byte_offset + 2
400                } else {
401                    link.byte_offset + 1
402                };
403                let text_end = text_start + link.text.len();
404
405                // If position is within the text portion, skip only if text is a URL
406                if byte_pos >= text_start && byte_pos < text_end {
407                    return Self::link_text_is_url(&link.text);
408                }
409                // Position is in the URL/reference portion, skip it
410                return true;
411            }
412        }
413
414        // Binary search images (sorted by byte_offset) to find candidate containing byte_pos
415        let image_idx = ctx.images.partition_point(|img| img.byte_offset <= byte_pos);
416        if image_idx > 0 {
417            let image = &ctx.images[image_idx - 1];
418            if byte_pos < image.byte_end {
419                // Image starts with '![' so alt text starts at byte_offset + 2
420                let alt_start = image.byte_offset + 2;
421                let alt_end = alt_start + image.alt_text.len();
422
423                // If position is within the alt text portion, don't skip
424                if byte_pos >= alt_start && byte_pos < alt_end {
425                    return false;
426                }
427                // Position is in the URL/reference portion, skip it
428                return true;
429            }
430        }
431
432        // Check pre-computed reference definitions
433        ctx.is_in_reference_def(byte_pos)
434    }
435
436    /// Check if link text is a URL that should not have proper name corrections.
437    /// Matches markdownlint behavior: skip text starting with `http://`, `https://`, or `www.`.
438    fn link_text_is_url(text: &str) -> bool {
439        let lower = text.trim().to_ascii_lowercase();
440        lower.starts_with("http://") || lower.starts_with("https://") || lower.starts_with("www.")
441    }
442
443    /// Check if a position within a line falls inside an angle-bracket URL (`<scheme://...>`).
444    ///
445    /// The link parser skips autolinks inside HTML comments, so `ctx.links` won't
446    /// contain them. This function detects angle-bracket URLs directly in the line
447    /// text, covering both HTML comments and regular text as a safety net.
448    fn is_in_angle_bracket_url(line: &str, pos: usize) -> bool {
449        let bytes = line.as_bytes();
450        let len = bytes.len();
451        let mut i = 0;
452        while i < len {
453            if bytes[i] == b'<' {
454                let after_open = i + 1;
455                // Check for a valid URI scheme per CommonMark autolink spec:
456                // scheme = [a-zA-Z][a-zA-Z0-9+.-]{0,31}
457                // followed by ':'
458                if after_open < len && bytes[after_open].is_ascii_alphabetic() {
459                    let mut s = after_open + 1;
460                    let scheme_max = (after_open + 32).min(len);
461                    while s < scheme_max
462                        && (bytes[s].is_ascii_alphanumeric()
463                            || bytes[s] == b'+'
464                            || bytes[s] == b'-'
465                            || bytes[s] == b'.')
466                    {
467                        s += 1;
468                    }
469                    if s < len && bytes[s] == b':' {
470                        // Valid scheme found; scan for closing '>' with no spaces or '<'
471                        let mut j = s + 1;
472                        let mut found_close = false;
473                        while j < len {
474                            match bytes[j] {
475                                b'>' => {
476                                    found_close = true;
477                                    break;
478                                }
479                                b' ' | b'<' => break,
480                                _ => j += 1,
481                            }
482                        }
483                        if found_close && pos >= i && pos <= j {
484                            return true;
485                        }
486                        if found_close {
487                            i = j + 1;
488                            continue;
489                        }
490                    }
491                }
492            }
493            i += 1;
494        }
495        false
496    }
497
498    /// Check if a position within a line falls inside backtick-delimited code.
499    ///
500    /// pulldown-cmark does not parse markdown syntax inside HTML comments, so
501    /// `ctx.is_in_code_block_or_span` returns false for backtick-wrapped text
502    /// within comments. This function detects backtick code spans directly in
503    /// the line text following CommonMark rules: a code span starts with N
504    /// backticks and ends with exactly N backticks.
505    fn is_in_backtick_code_in_line(line: &str, pos: usize) -> bool {
506        let bytes = line.as_bytes();
507        let len = bytes.len();
508        let mut i = 0;
509        while i < len {
510            if bytes[i] == b'`' {
511                // Count the opening backtick sequence length
512                let open_start = i;
513                while i < len && bytes[i] == b'`' {
514                    i += 1;
515                }
516                let tick_len = i - open_start;
517
518                // Scan forward for a closing sequence of exactly tick_len backticks
519                while i < len {
520                    if bytes[i] == b'`' {
521                        let close_start = i;
522                        while i < len && bytes[i] == b'`' {
523                            i += 1;
524                        }
525                        if i - close_start == tick_len {
526                            // Matched pair found; the code span content is between
527                            // the end of the opening backticks and the start of the
528                            // closing backticks (exclusive of the backticks themselves).
529                            let content_start = open_start + tick_len;
530                            let content_end = close_start;
531                            if pos >= content_start && pos < content_end {
532                                return true;
533                            }
534                            // Continue scanning after this pair
535                            break;
536                        }
537                        // Not the right length; keep scanning
538                    } else {
539                        i += 1;
540                    }
541                }
542            } else {
543                i += 1;
544            }
545        }
546        false
547    }
548
549    // Check if a character is a word boundary (handles Unicode)
550    fn is_word_boundary_char(c: char) -> bool {
551        !c.is_alphanumeric()
552    }
553
554    // Check if position is at a word boundary using byte-level lookups.
555    fn is_at_word_boundary(content: &str, pos: usize, is_start: bool) -> bool {
556        if is_start {
557            if pos == 0 {
558                return true;
559            }
560            match content[..pos].chars().next_back() {
561                None => true,
562                Some(c) => Self::is_word_boundary_char(c),
563            }
564        } else {
565            if pos >= content.len() {
566                return true;
567            }
568            match content[pos..].chars().next() {
569                None => true,
570                Some(c) => Self::is_word_boundary_char(c),
571            }
572        }
573    }
574
575    /// For a frontmatter line, return the byte offset where the checkable
576    /// value portion starts. Returns `usize::MAX` if the entire line should be
577    /// skipped (frontmatter delimiters, key-only lines, YAML comments, flow constructs).
578    fn frontmatter_value_offset(line: &str) -> usize {
579        let trimmed = line.trim();
580
581        // Skip frontmatter delimiters and empty lines
582        if trimmed == "---" || trimmed == "+++" || trimmed.is_empty() {
583            return usize::MAX;
584        }
585
586        // Skip YAML comments
587        if trimmed.starts_with('#') {
588            return usize::MAX;
589        }
590
591        // YAML list item: "  - item" or "  - key: value"
592        let stripped = line.trim_start();
593        if let Some(after_dash) = stripped.strip_prefix("- ") {
594            let leading = line.len() - stripped.len();
595            // Check if the list item contains a mapping (e.g., "- key: value")
596            if let Some(result) = Self::kv_value_offset(line, after_dash, leading + 2) {
597                return result;
598            }
599            // Bare list item value (no colon) - check content after "- "
600            return leading + 2;
601        }
602        if stripped == "-" {
603            return usize::MAX;
604        }
605
606        // Key-value pair with colon separator (YAML): "key: value"
607        if let Some(result) = Self::kv_value_offset(line, stripped, line.len() - stripped.len()) {
608            return result;
609        }
610
611        // Key-value pair with equals separator (TOML): "key = value"
612        if let Some(eq_pos) = line.find('=') {
613            let after_eq = eq_pos + 1;
614            if after_eq < line.len() && line.as_bytes()[after_eq] == b' ' {
615                let value_start = after_eq + 1;
616                let value_slice = &line[value_start..];
617                let value_trimmed = value_slice.trim();
618                if value_trimmed.is_empty() {
619                    return usize::MAX;
620                }
621                // For quoted values, skip the opening quote character
622                if (value_trimmed.starts_with('"') && value_trimmed.ends_with('"'))
623                    || (value_trimmed.starts_with('\'') && value_trimmed.ends_with('\''))
624                {
625                    let quote_offset = value_slice.find(['"', '\'']).unwrap_or(0);
626                    return value_start + quote_offset + 1;
627                }
628                return value_start;
629            }
630            // Equals with no space after or at end of line -> no value to check
631            return usize::MAX;
632        }
633
634        // No separator found - continuation line or bare value, check the whole line
635        0
636    }
637
638    /// Parse a key-value pair using colon separator within `content` that starts
639    /// at `base_offset` in the original line. Returns `Some(offset)` if a colon
640    /// separator is found, `None` if no colon is present.
641    fn kv_value_offset(line: &str, content: &str, base_offset: usize) -> Option<usize> {
642        let colon_pos = content.find(':')?;
643        let abs_colon = base_offset + colon_pos;
644        let after_colon = abs_colon + 1;
645        if after_colon < line.len() && line.as_bytes()[after_colon] == b' ' {
646            let value_start = after_colon + 1;
647            let value_slice = &line[value_start..];
648            let value_trimmed = value_slice.trim();
649            if value_trimmed.is_empty() {
650                return Some(usize::MAX);
651            }
652            // Skip flow mappings and flow sequences - too complex for heuristic parsing
653            if value_trimmed.starts_with('{') || value_trimmed.starts_with('[') {
654                return Some(usize::MAX);
655            }
656            // For quoted values, skip the opening quote character
657            if (value_trimmed.starts_with('"') && value_trimmed.ends_with('"'))
658                || (value_trimmed.starts_with('\'') && value_trimmed.ends_with('\''))
659            {
660                let quote_offset = value_slice.find(['"', '\'']).unwrap_or(0);
661                return Some(value_start + quote_offset + 1);
662            }
663            return Some(value_start);
664        }
665        // Colon with no space after or at end of line -> no value to check
666        Some(usize::MAX)
667    }
668
669    // Get the proper name that should be used for a found name
670    fn get_proper_name_for(&self, found_name: &str) -> Option<String> {
671        let found_lower = found_name.to_lowercase();
672
673        // Iterate through the configured proper names
674        for name in &self.config.names {
675            let lower_name = name.to_lowercase();
676            let lower_name_no_dots = lower_name.replace('.', "");
677
678            // Direct match
679            if found_lower == lower_name || found_lower == lower_name_no_dots {
680                return Some(name.clone());
681            }
682
683            // Check ASCII-normalized version
684            let ascii_normalized = Self::ascii_normalize(&lower_name);
685
686            let ascii_no_dots = ascii_normalized.replace('.', "");
687
688            if found_lower == ascii_normalized || found_lower == ascii_no_dots {
689                return Some(name.clone());
690            }
691        }
692        None
693    }
694}
695
696impl Rule for MD044ProperNames {
697    fn name(&self) -> &'static str {
698        "MD044"
699    }
700
701    fn description(&self) -> &'static str {
702        "Proper names should have the correct capitalization"
703    }
704
705    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
706        if self.config.names.is_empty() {
707            return true;
708        }
709        // Quick check if any configured name variants exist (case-insensitive)
710        let content_lower = if ctx.content.is_ascii() {
711            ctx.content.to_ascii_lowercase()
712        } else {
713            ctx.content.to_lowercase()
714        };
715        !self.name_variants.iter().any(|name| content_lower.contains(name))
716    }
717
718    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
719        let content = ctx.content;
720        if content.is_empty() || self.config.names.is_empty() || self.combined_pattern.is_none() {
721            return Ok(Vec::new());
722        }
723
724        // Compute lowercase content once and reuse across all checks
725        let content_lower = if content.is_ascii() {
726            content.to_ascii_lowercase()
727        } else {
728            content.to_lowercase()
729        };
730
731        // Early return: use pre-computed name_variants for the quick check
732        let has_potential_matches = self.name_variants.iter().any(|name| content_lower.contains(name));
733
734        if !has_potential_matches {
735            return Ok(Vec::new());
736        }
737
738        let line_index = &ctx.line_index;
739        let violations = self.find_name_violations(content, ctx, &content_lower);
740
741        let warnings = violations
742            .into_iter()
743            .filter_map(|(line, column, found_name)| {
744                self.get_proper_name_for(&found_name).map(|proper_name| LintWarning {
745                    rule_name: Some(self.name().to_string()),
746                    line,
747                    column,
748                    end_line: line,
749                    end_column: column + found_name.len(),
750                    message: format!("Proper name '{found_name}' should be '{proper_name}'"),
751                    severity: Severity::Warning,
752                    fix: Some(Fix {
753                        range: line_index.line_col_to_byte_range(line, column),
754                        replacement: proper_name,
755                    }),
756                })
757            })
758            .collect();
759
760        Ok(warnings)
761    }
762
763    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
764        let content = ctx.content;
765        if content.is_empty() || self.config.names.is_empty() {
766            return Ok(content.to_string());
767        }
768
769        let content_lower = if content.is_ascii() {
770            content.to_ascii_lowercase()
771        } else {
772            content.to_lowercase()
773        };
774        let violations = self.find_name_violations(content, ctx, &content_lower);
775        if violations.is_empty() {
776            return Ok(content.to_string());
777        }
778
779        // Process lines and build the fixed content
780        let mut fixed_lines = Vec::new();
781
782        // Group violations by line
783        let mut violations_by_line: HashMap<usize, Vec<(usize, String)>> = HashMap::new();
784        for (line_num, col_num, found_name) in violations {
785            violations_by_line
786                .entry(line_num)
787                .or_default()
788                .push((col_num, found_name));
789        }
790
791        // Sort violations within each line in reverse order
792        for violations in violations_by_line.values_mut() {
793            violations.sort_by_key(|b| std::cmp::Reverse(b.0));
794        }
795
796        // Process each line
797        for (line_idx, line_info) in ctx.lines.iter().enumerate() {
798            let line_num = line_idx + 1;
799
800            if let Some(line_violations) = violations_by_line.get(&line_num) {
801                // This line has violations, fix them
802                let mut fixed_line = line_info.content(ctx.content).to_string();
803
804                for (col_num, found_name) in line_violations {
805                    if let Some(proper_name) = self.get_proper_name_for(found_name) {
806                        let start_col = col_num - 1; // Convert to 0-based
807                        let end_col = start_col + found_name.len();
808
809                        if end_col <= fixed_line.len()
810                            && fixed_line.is_char_boundary(start_col)
811                            && fixed_line.is_char_boundary(end_col)
812                        {
813                            fixed_line.replace_range(start_col..end_col, &proper_name);
814                        }
815                    }
816                }
817
818                fixed_lines.push(fixed_line);
819            } else {
820                // No violations on this line, keep it as is
821                fixed_lines.push(line_info.content(ctx.content).to_string());
822            }
823        }
824
825        // Join lines with newlines, preserving the original ending
826        let mut result = fixed_lines.join("\n");
827        if content.ends_with('\n') && !result.ends_with('\n') {
828            result.push('\n');
829        }
830        Ok(result)
831    }
832
833    fn as_any(&self) -> &dyn std::any::Any {
834        self
835    }
836
837    fn default_config_section(&self) -> Option<(String, toml::Value)> {
838        let json_value = serde_json::to_value(&self.config).ok()?;
839        Some((
840            self.name().to_string(),
841            crate::rule_config_serde::json_to_toml_value(&json_value)?,
842        ))
843    }
844
845    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
846    where
847        Self: Sized,
848    {
849        let rule_config = crate::rule_config_serde::load_rule_config::<MD044Config>(config);
850        Box::new(Self::from_config_struct(rule_config))
851    }
852}
853
854#[cfg(test)]
855mod tests {
856    use super::*;
857    use crate::lint_context::LintContext;
858
859    fn create_context(content: &str) -> LintContext<'_> {
860        LintContext::new(content, crate::config::MarkdownFlavor::Standard, None)
861    }
862
863    #[test]
864    fn test_correctly_capitalized_names() {
865        let rule = MD044ProperNames::new(
866            vec![
867                "JavaScript".to_string(),
868                "TypeScript".to_string(),
869                "Node.js".to_string(),
870            ],
871            true,
872        );
873
874        let content = "This document uses JavaScript, TypeScript, and Node.js correctly.";
875        let ctx = create_context(content);
876        let result = rule.check(&ctx).unwrap();
877        assert!(result.is_empty(), "Should not flag correctly capitalized names");
878    }
879
880    #[test]
881    fn test_incorrectly_capitalized_names() {
882        let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);
883
884        let content = "This document uses javascript and typescript incorrectly.";
885        let ctx = create_context(content);
886        let result = rule.check(&ctx).unwrap();
887
888        assert_eq!(result.len(), 2, "Should flag two incorrect capitalizations");
889        assert_eq!(result[0].message, "Proper name 'javascript' should be 'JavaScript'");
890        assert_eq!(result[0].line, 1);
891        assert_eq!(result[0].column, 20);
892        assert_eq!(result[1].message, "Proper name 'typescript' should be 'TypeScript'");
893        assert_eq!(result[1].line, 1);
894        assert_eq!(result[1].column, 35);
895    }
896
897    #[test]
898    fn test_names_at_beginning_of_sentences() {
899        let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "Python".to_string()], true);
900
901        let content = "javascript is a great language. python is also popular.";
902        let ctx = create_context(content);
903        let result = rule.check(&ctx).unwrap();
904
905        assert_eq!(result.len(), 2, "Should flag names at beginning of sentences");
906        assert_eq!(result[0].line, 1);
907        assert_eq!(result[0].column, 1);
908        assert_eq!(result[1].line, 1);
909        assert_eq!(result[1].column, 33);
910    }
911
912    #[test]
913    fn test_names_in_code_blocks_checked_by_default() {
914        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
915
916        let content = r#"Here is some text with JavaScript.
917
918```javascript
919// This javascript should be checked
920const lang = "javascript";
921```
922
923But this javascript should be flagged."#;
924
925        let ctx = create_context(content);
926        let result = rule.check(&ctx).unwrap();
927
928        assert_eq!(result.len(), 3, "Should flag javascript inside and outside code blocks");
929        assert_eq!(result[0].line, 4);
930        assert_eq!(result[1].line, 5);
931        assert_eq!(result[2].line, 8);
932    }
933
934    #[test]
935    fn test_names_in_code_blocks_ignored_when_disabled() {
936        let rule = MD044ProperNames::new(
937            vec!["JavaScript".to_string()],
938            false, // code_blocks = false means skip code blocks
939        );
940
941        let content = r#"```
942javascript in code block
943```"#;
944
945        let ctx = create_context(content);
946        let result = rule.check(&ctx).unwrap();
947
948        assert_eq!(
949            result.len(),
950            0,
951            "Should not flag javascript in code blocks when code_blocks is false"
952        );
953    }
954
955    #[test]
956    fn test_names_in_inline_code_checked_by_default() {
957        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
958
959        let content = "This is `javascript` in inline code and javascript outside.";
960        let ctx = create_context(content);
961        let result = rule.check(&ctx).unwrap();
962
963        // When code_blocks=true, inline code should be checked
964        assert_eq!(result.len(), 2, "Should flag javascript inside and outside inline code");
965        assert_eq!(result[0].column, 10); // javascript in inline code
966        assert_eq!(result[1].column, 41); // javascript outside
967    }
968
969    #[test]
970    fn test_multiple_names_in_same_line() {
971        let rule = MD044ProperNames::new(
972            vec!["JavaScript".to_string(), "TypeScript".to_string(), "React".to_string()],
973            true,
974        );
975
976        let content = "I use javascript, typescript, and react in my projects.";
977        let ctx = create_context(content);
978        let result = rule.check(&ctx).unwrap();
979
980        assert_eq!(result.len(), 3, "Should flag all three incorrect names");
981        assert_eq!(result[0].message, "Proper name 'javascript' should be 'JavaScript'");
982        assert_eq!(result[1].message, "Proper name 'typescript' should be 'TypeScript'");
983        assert_eq!(result[2].message, "Proper name 'react' should be 'React'");
984    }
985
986    #[test]
987    fn test_case_sensitivity() {
988        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
989
990        let content = "JAVASCRIPT, Javascript, javascript, and JavaScript variations.";
991        let ctx = create_context(content);
992        let result = rule.check(&ctx).unwrap();
993
994        assert_eq!(result.len(), 3, "Should flag all incorrect case variations");
995        // JavaScript (correct) should not be flagged
996        assert!(result.iter().all(|w| w.message.contains("should be 'JavaScript'")));
997    }
998
999    #[test]
1000    fn test_configuration_with_custom_name_list() {
1001        let config = MD044Config {
1002            names: vec!["GitHub".to_string(), "GitLab".to_string(), "DevOps".to_string()],
1003            code_blocks: true,
1004            html_elements: true,
1005            html_comments: true,
1006        };
1007        let rule = MD044ProperNames::from_config_struct(config);
1008
1009        let content = "We use github, gitlab, and devops for our workflow.";
1010        let ctx = create_context(content);
1011        let result = rule.check(&ctx).unwrap();
1012
1013        assert_eq!(result.len(), 3, "Should flag all custom names");
1014        assert_eq!(result[0].message, "Proper name 'github' should be 'GitHub'");
1015        assert_eq!(result[1].message, "Proper name 'gitlab' should be 'GitLab'");
1016        assert_eq!(result[2].message, "Proper name 'devops' should be 'DevOps'");
1017    }
1018
1019    #[test]
1020    fn test_empty_configuration() {
1021        let rule = MD044ProperNames::new(vec![], true);
1022
1023        let content = "This has javascript and typescript but no configured names.";
1024        let ctx = create_context(content);
1025        let result = rule.check(&ctx).unwrap();
1026
1027        assert!(result.is_empty(), "Should not flag anything with empty configuration");
1028    }
1029
1030    #[test]
1031    fn test_names_with_special_characters() {
1032        let rule = MD044ProperNames::new(
1033            vec!["Node.js".to_string(), "ASP.NET".to_string(), "C++".to_string()],
1034            true,
1035        );
1036
1037        let content = "We use nodejs, asp.net, ASP.NET, and c++ in our stack.";
1038        let ctx = create_context(content);
1039        let result = rule.check(&ctx).unwrap();
1040
1041        // nodejs should match Node.js (dotless variation)
1042        // asp.net should be flagged (wrong case)
1043        // ASP.NET should not be flagged (correct)
1044        // c++ should be flagged
1045        assert_eq!(result.len(), 3, "Should handle special characters correctly");
1046
1047        let messages: Vec<&str> = result.iter().map(|w| w.message.as_str()).collect();
1048        assert!(messages.contains(&"Proper name 'nodejs' should be 'Node.js'"));
1049        assert!(messages.contains(&"Proper name 'asp.net' should be 'ASP.NET'"));
1050        assert!(messages.contains(&"Proper name 'c++' should be 'C++'"));
1051    }
1052
1053    #[test]
1054    fn test_word_boundaries() {
1055        let rule = MD044ProperNames::new(vec!["Java".to_string(), "Script".to_string()], true);
1056
1057        let content = "JavaScript is not java or script, but Java and Script are separate.";
1058        let ctx = create_context(content);
1059        let result = rule.check(&ctx).unwrap();
1060
1061        // Should only flag lowercase "java" and "script" as separate words
1062        assert_eq!(result.len(), 2, "Should respect word boundaries");
1063        assert!(result.iter().any(|w| w.column == 19)); // "java" position
1064        assert!(result.iter().any(|w| w.column == 27)); // "script" position
1065    }
1066
1067    #[test]
1068    fn test_fix_method() {
1069        let rule = MD044ProperNames::new(
1070            vec![
1071                "JavaScript".to_string(),
1072                "TypeScript".to_string(),
1073                "Node.js".to_string(),
1074            ],
1075            true,
1076        );
1077
1078        let content = "I love javascript, typescript, and nodejs!";
1079        let ctx = create_context(content);
1080        let fixed = rule.fix(&ctx).unwrap();
1081
1082        assert_eq!(fixed, "I love JavaScript, TypeScript, and Node.js!");
1083    }
1084
1085    #[test]
1086    fn test_fix_multiple_occurrences() {
1087        let rule = MD044ProperNames::new(vec!["Python".to_string()], true);
1088
1089        let content = "python is great. I use python daily. PYTHON is powerful.";
1090        let ctx = create_context(content);
1091        let fixed = rule.fix(&ctx).unwrap();
1092
1093        assert_eq!(fixed, "Python is great. I use Python daily. Python is powerful.");
1094    }
1095
1096    #[test]
1097    fn test_fix_checks_code_blocks_by_default() {
1098        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1099
1100        let content = r#"I love javascript.
1101
1102```
1103const lang = "javascript";
1104```
1105
1106More javascript here."#;
1107
1108        let ctx = create_context(content);
1109        let fixed = rule.fix(&ctx).unwrap();
1110
1111        let expected = r#"I love JavaScript.
1112
1113```
1114const lang = "JavaScript";
1115```
1116
1117More JavaScript here."#;
1118
1119        assert_eq!(fixed, expected);
1120    }
1121
1122    #[test]
1123    fn test_multiline_content() {
1124        let rule = MD044ProperNames::new(vec!["Rust".to_string(), "Python".to_string()], true);
1125
1126        let content = r#"First line with rust.
1127Second line with python.
1128Third line with RUST and PYTHON."#;
1129
1130        let ctx = create_context(content);
1131        let result = rule.check(&ctx).unwrap();
1132
1133        assert_eq!(result.len(), 4, "Should flag all incorrect occurrences");
1134        assert_eq!(result[0].line, 1);
1135        assert_eq!(result[1].line, 2);
1136        assert_eq!(result[2].line, 3);
1137        assert_eq!(result[3].line, 3);
1138    }
1139
1140    #[test]
1141    fn test_default_config() {
1142        let config = MD044Config::default();
1143        assert!(config.names.is_empty());
1144        assert!(!config.code_blocks);
1145        assert!(config.html_elements);
1146        assert!(config.html_comments);
1147    }
1148
1149    #[test]
1150    fn test_default_config_checks_html_comments() {
1151        let config = MD044Config {
1152            names: vec!["JavaScript".to_string()],
1153            ..MD044Config::default()
1154        };
1155        let rule = MD044ProperNames::from_config_struct(config);
1156
1157        let content = "# Guide\n\n<!-- javascript mentioned here -->\n";
1158        let ctx = create_context(content);
1159        let result = rule.check(&ctx).unwrap();
1160
1161        assert_eq!(result.len(), 1, "Default config should check HTML comments");
1162        assert_eq!(result[0].line, 3);
1163    }
1164
1165    #[test]
1166    fn test_default_config_skips_code_blocks() {
1167        let config = MD044Config {
1168            names: vec!["JavaScript".to_string()],
1169            ..MD044Config::default()
1170        };
1171        let rule = MD044ProperNames::from_config_struct(config);
1172
1173        let content = "# Guide\n\n```\njavascript in code\n```\n";
1174        let ctx = create_context(content);
1175        let result = rule.check(&ctx).unwrap();
1176
1177        assert_eq!(result.len(), 0, "Default config should skip code blocks");
1178    }
1179
1180    #[test]
1181    fn test_standalone_html_comment_checked() {
1182        let config = MD044Config {
1183            names: vec!["Test".to_string()],
1184            ..MD044Config::default()
1185        };
1186        let rule = MD044ProperNames::from_config_struct(config);
1187
1188        let content = "# Heading\n\n<!-- this is a test example -->\n";
1189        let ctx = create_context(content);
1190        let result = rule.check(&ctx).unwrap();
1191
1192        assert_eq!(result.len(), 1, "Should flag proper name in standalone HTML comment");
1193        assert_eq!(result[0].line, 3);
1194    }
1195
1196    #[test]
1197    fn test_inline_config_comments_not_flagged() {
1198        let config = MD044Config {
1199            names: vec!["RUMDL".to_string()],
1200            ..MD044Config::default()
1201        };
1202        let rule = MD044ProperNames::from_config_struct(config);
1203
1204        // Lines 1, 3, 4, 6 are inline config comments — should not be flagged.
1205        // Lines 2, 5 contain "rumdl" in regular text — flagged by rule.check(),
1206        // but would be suppressed by the linting engine's inline config filtering.
1207        let content = "<!-- rumdl-disable MD044 -->\nSome rumdl text here.\n<!-- rumdl-enable MD044 -->\n<!-- markdownlint-disable -->\nMore rumdl text.\n<!-- markdownlint-enable -->\n";
1208        let ctx = create_context(content);
1209        let result = rule.check(&ctx).unwrap();
1210
1211        assert_eq!(result.len(), 2, "Should only flag body lines, not config comments");
1212        assert_eq!(result[0].line, 2);
1213        assert_eq!(result[1].line, 5);
1214    }
1215
1216    #[test]
1217    fn test_html_comment_skipped_when_disabled() {
1218        let config = MD044Config {
1219            names: vec!["Test".to_string()],
1220            code_blocks: true,
1221            html_elements: true,
1222            html_comments: false,
1223        };
1224        let rule = MD044ProperNames::from_config_struct(config);
1225
1226        let content = "# Heading\n\n<!-- this is a test example -->\n\nRegular test here.\n";
1227        let ctx = create_context(content);
1228        let result = rule.check(&ctx).unwrap();
1229
1230        assert_eq!(
1231            result.len(),
1232            1,
1233            "Should only flag 'test' outside HTML comment when html_comments=false"
1234        );
1235        assert_eq!(result[0].line, 5);
1236    }
1237
1238    #[test]
1239    fn test_fix_corrects_html_comment_content() {
1240        let config = MD044Config {
1241            names: vec!["JavaScript".to_string()],
1242            ..MD044Config::default()
1243        };
1244        let rule = MD044ProperNames::from_config_struct(config);
1245
1246        let content = "# Guide\n\n<!-- javascript mentioned here -->\n";
1247        let ctx = create_context(content);
1248        let fixed = rule.fix(&ctx).unwrap();
1249
1250        assert_eq!(fixed, "# Guide\n\n<!-- JavaScript mentioned here -->\n");
1251    }
1252
1253    #[test]
1254    fn test_fix_does_not_modify_inline_config_comments() {
1255        let config = MD044Config {
1256            names: vec!["RUMDL".to_string()],
1257            ..MD044Config::default()
1258        };
1259        let rule = MD044ProperNames::from_config_struct(config);
1260
1261        let content = "<!-- rumdl-disable -->\nSome rumdl text.\n<!-- rumdl-enable -->\n";
1262        let ctx = create_context(content);
1263        let fixed = rule.fix(&ctx).unwrap();
1264
1265        // Config comments should be untouched; body text should be fixed
1266        assert!(fixed.contains("<!-- rumdl-disable -->"));
1267        assert!(fixed.contains("<!-- rumdl-enable -->"));
1268        assert!(fixed.contains("Some RUMDL text."));
1269    }
1270
1271    #[test]
1272    fn test_performance_with_many_names() {
1273        let mut names = vec![];
1274        for i in 0..50 {
1275            names.push(format!("ProperName{i}"));
1276        }
1277
1278        let rule = MD044ProperNames::new(names, true);
1279
1280        let content = "This has propername0, propername25, and propername49 incorrectly.";
1281        let ctx = create_context(content);
1282        let result = rule.check(&ctx).unwrap();
1283
1284        assert_eq!(result.len(), 3, "Should handle many configured names efficiently");
1285    }
1286
1287    #[test]
1288    fn test_large_name_count_performance() {
1289        // Verify MD044 can handle large numbers of names without regex limitations
1290        // This test confirms that fancy-regex handles large patterns well
1291        let names = (0..1000).map(|i| format!("ProperName{i}")).collect::<Vec<_>>();
1292
1293        let rule = MD044ProperNames::new(names, true);
1294
1295        // The combined pattern should be created successfully
1296        assert!(rule.combined_pattern.is_some());
1297
1298        // Should be able to check content without errors
1299        let content = "This has propername0 and propername999 in it.";
1300        let ctx = create_context(content);
1301        let result = rule.check(&ctx).unwrap();
1302
1303        // Should detect both incorrect names
1304        assert_eq!(result.len(), 2, "Should handle 1000 names without issues");
1305    }
1306
1307    #[test]
1308    fn test_cache_behavior() {
1309        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1310
1311        let content = "Using javascript here.";
1312        let ctx = create_context(content);
1313
1314        // First check
1315        let result1 = rule.check(&ctx).unwrap();
1316        assert_eq!(result1.len(), 1);
1317
1318        // Second check should use cache
1319        let result2 = rule.check(&ctx).unwrap();
1320        assert_eq!(result2.len(), 1);
1321
1322        // Results should be identical
1323        assert_eq!(result1[0].line, result2[0].line);
1324        assert_eq!(result1[0].column, result2[0].column);
1325    }
1326
1327    #[test]
1328    fn test_html_comments_not_checked_when_disabled() {
1329        let config = MD044Config {
1330            names: vec!["JavaScript".to_string()],
1331            code_blocks: true,    // Check code blocks
1332            html_elements: true,  // Check HTML elements
1333            html_comments: false, // Don't check HTML comments
1334        };
1335        let rule = MD044ProperNames::from_config_struct(config);
1336
1337        let content = r#"Regular javascript here.
1338<!-- This javascript in HTML comment should be ignored -->
1339More javascript outside."#;
1340
1341        let ctx = create_context(content);
1342        let result = rule.check(&ctx).unwrap();
1343
1344        assert_eq!(result.len(), 2, "Should only flag javascript outside HTML comments");
1345        assert_eq!(result[0].line, 1);
1346        assert_eq!(result[1].line, 3);
1347    }
1348
1349    #[test]
1350    fn test_html_comments_checked_when_enabled() {
1351        let config = MD044Config {
1352            names: vec!["JavaScript".to_string()],
1353            code_blocks: true,   // Check code blocks
1354            html_elements: true, // Check HTML elements
1355            html_comments: true, // Check HTML comments
1356        };
1357        let rule = MD044ProperNames::from_config_struct(config);
1358
1359        let content = r#"Regular javascript here.
1360<!-- This javascript in HTML comment should be checked -->
1361More javascript outside."#;
1362
1363        let ctx = create_context(content);
1364        let result = rule.check(&ctx).unwrap();
1365
1366        assert_eq!(
1367            result.len(),
1368            3,
1369            "Should flag all javascript occurrences including in HTML comments"
1370        );
1371    }
1372
1373    #[test]
1374    fn test_multiline_html_comments() {
1375        let config = MD044Config {
1376            names: vec!["Python".to_string(), "JavaScript".to_string()],
1377            code_blocks: true,    // Check code blocks
1378            html_elements: true,  // Check HTML elements
1379            html_comments: false, // Don't check HTML comments
1380        };
1381        let rule = MD044ProperNames::from_config_struct(config);
1382
1383        let content = r#"Regular python here.
1384<!--
1385This is a multiline comment
1386with javascript and python
1387that should be ignored
1388-->
1389More javascript outside."#;
1390
1391        let ctx = create_context(content);
1392        let result = rule.check(&ctx).unwrap();
1393
1394        assert_eq!(result.len(), 2, "Should only flag names outside HTML comments");
1395        assert_eq!(result[0].line, 1); // python
1396        assert_eq!(result[1].line, 7); // javascript
1397    }
1398
1399    #[test]
1400    fn test_fix_preserves_html_comments_when_disabled() {
1401        let config = MD044Config {
1402            names: vec!["JavaScript".to_string()],
1403            code_blocks: true,    // Check code blocks
1404            html_elements: true,  // Check HTML elements
1405            html_comments: false, // Don't check HTML comments
1406        };
1407        let rule = MD044ProperNames::from_config_struct(config);
1408
1409        let content = r#"javascript here.
1410<!-- javascript in comment -->
1411More javascript."#;
1412
1413        let ctx = create_context(content);
1414        let fixed = rule.fix(&ctx).unwrap();
1415
1416        let expected = r#"JavaScript here.
1417<!-- javascript in comment -->
1418More JavaScript."#;
1419
1420        assert_eq!(
1421            fixed, expected,
1422            "Should not fix names inside HTML comments when disabled"
1423        );
1424    }
1425
1426    #[test]
1427    fn test_proper_names_in_link_text_are_flagged() {
1428        let rule = MD044ProperNames::new(
1429            vec!["JavaScript".to_string(), "Node.js".to_string(), "Python".to_string()],
1430            true,
1431        );
1432
1433        let content = r#"Check this [javascript documentation](https://javascript.info) for info.
1434
1435Visit [node.js homepage](https://nodejs.org) and [python tutorial](https://python.org).
1436
1437Real javascript should be flagged.
1438
1439Also see the [typescript guide][ts-ref] for more.
1440
1441Real python should be flagged too.
1442
1443[ts-ref]: https://typescript.org/handbook"#;
1444
1445        let ctx = create_context(content);
1446        let result = rule.check(&ctx).unwrap();
1447
1448        // Link text should be checked, URLs should not be checked
1449        // Line 1: [javascript documentation] - "javascript" should be flagged
1450        // Line 3: [node.js homepage] - "node.js" should be flagged (matches "Node.js")
1451        // Line 3: [python tutorial] - "python" should be flagged
1452        // Line 5: standalone javascript
1453        // Line 9: standalone python
1454        assert_eq!(result.len(), 5, "Expected 5 warnings: 3 in link text + 2 standalone");
1455
1456        // Verify line numbers for link text warnings
1457        let line_1_warnings: Vec<_> = result.iter().filter(|w| w.line == 1).collect();
1458        assert_eq!(line_1_warnings.len(), 1);
1459        assert!(
1460            line_1_warnings[0]
1461                .message
1462                .contains("'javascript' should be 'JavaScript'")
1463        );
1464
1465        let line_3_warnings: Vec<_> = result.iter().filter(|w| w.line == 3).collect();
1466        assert_eq!(line_3_warnings.len(), 2); // node.js and python
1467
1468        // Standalone warnings
1469        assert!(result.iter().any(|w| w.line == 5 && w.message.contains("'javascript'")));
1470        assert!(result.iter().any(|w| w.line == 9 && w.message.contains("'python'")));
1471    }
1472
1473    #[test]
1474    fn test_link_urls_not_flagged() {
1475        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1476
1477        // URL contains "javascript" but should NOT be flagged
1478        let content = r#"[Link Text](https://javascript.info/guide)"#;
1479
1480        let ctx = create_context(content);
1481        let result = rule.check(&ctx).unwrap();
1482
1483        // URL should not be checked
1484        assert!(result.is_empty(), "URLs should not be checked for proper names");
1485    }
1486
1487    #[test]
1488    fn test_proper_names_in_image_alt_text_are_flagged() {
1489        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1490
1491        let content = r#"Here is a ![javascript logo](javascript.png "javascript icon") image.
1492
1493Real javascript should be flagged."#;
1494
1495        let ctx = create_context(content);
1496        let result = rule.check(&ctx).unwrap();
1497
1498        // Image alt text should be checked, URL and title should not be checked
1499        // Line 1: ![javascript logo] - "javascript" should be flagged
1500        // Line 3: standalone javascript
1501        assert_eq!(result.len(), 2, "Expected 2 warnings: 1 in alt text + 1 standalone");
1502        assert!(result[0].message.contains("'javascript' should be 'JavaScript'"));
1503        assert!(result[0].line == 1); // "![javascript logo]"
1504        assert!(result[1].message.contains("'javascript' should be 'JavaScript'"));
1505        assert!(result[1].line == 3); // "Real javascript should be flagged."
1506    }
1507
1508    #[test]
1509    fn test_image_urls_not_flagged() {
1510        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1511
1512        // URL contains "javascript" but should NOT be flagged
1513        let content = r#"![Logo](https://javascript.info/logo.png)"#;
1514
1515        let ctx = create_context(content);
1516        let result = rule.check(&ctx).unwrap();
1517
1518        // Image URL should not be checked
1519        assert!(result.is_empty(), "Image URLs should not be checked for proper names");
1520    }
1521
1522    #[test]
1523    fn test_reference_link_text_flagged_but_definition_not() {
1524        let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);
1525
1526        let content = r#"Check the [javascript guide][js-ref] for details.
1527
1528Real javascript should be flagged.
1529
1530[js-ref]: https://javascript.info/typescript/guide"#;
1531
1532        let ctx = create_context(content);
1533        let result = rule.check(&ctx).unwrap();
1534
1535        // Link text should be checked, reference definitions should not
1536        // Line 1: [javascript guide] - should be flagged
1537        // Line 3: standalone javascript - should be flagged
1538        // Line 5: reference definition - should NOT be flagged
1539        assert_eq!(result.len(), 2, "Expected 2 warnings: 1 in link text + 1 standalone");
1540        assert!(result.iter().any(|w| w.line == 1 && w.message.contains("'javascript'")));
1541        assert!(result.iter().any(|w| w.line == 3 && w.message.contains("'javascript'")));
1542    }
1543
1544    #[test]
1545    fn test_reference_definitions_not_flagged() {
1546        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1547
1548        // Reference definition should NOT be flagged
1549        let content = r#"[js-ref]: https://javascript.info/guide"#;
1550
1551        let ctx = create_context(content);
1552        let result = rule.check(&ctx).unwrap();
1553
1554        // Reference definition URLs should not be checked
1555        assert!(result.is_empty(), "Reference definitions should not be checked");
1556    }
1557
1558    #[test]
1559    fn test_wikilinks_text_is_flagged() {
1560        let rule = MD044ProperNames::new(vec!["JavaScript".to_string()], true);
1561
1562        // WikiLinks [[destination]] should have their text checked
1563        let content = r#"[[javascript]]
1564
1565Regular javascript here.
1566
1567[[JavaScript|display text]]"#;
1568
1569        let ctx = create_context(content);
1570        let result = rule.check(&ctx).unwrap();
1571
1572        // Line 1: [[javascript]] - should be flagged (WikiLink text)
1573        // Line 3: standalone javascript - should be flagged
1574        // Line 5: [[JavaScript|display text]] - correct capitalization, no flag
1575        assert_eq!(result.len(), 2, "Expected 2 warnings: 1 in WikiLink + 1 standalone");
1576        assert!(
1577            result
1578                .iter()
1579                .any(|w| w.line == 1 && w.column == 3 && w.message.contains("'javascript'"))
1580        );
1581        assert!(result.iter().any(|w| w.line == 3 && w.message.contains("'javascript'")));
1582    }
1583
1584    #[test]
1585    fn test_url_link_text_not_flagged() {
1586        let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
1587
1588        // Link text that is itself a URL should not be flagged
1589        let content = r#"[https://github.com/org/repo](https://github.com/org/repo)
1590
1591[http://github.com/org/repo](http://github.com/org/repo)
1592
1593[www.github.com/org/repo](https://www.github.com/org/repo)"#;
1594
1595        let ctx = create_context(content);
1596        let result = rule.check(&ctx).unwrap();
1597
1598        assert!(
1599            result.is_empty(),
1600            "URL-like link text should not be flagged, got: {result:?}"
1601        );
1602    }
1603
1604    #[test]
1605    fn test_url_link_text_with_leading_space_not_flagged() {
1606        let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
1607
1608        // Leading/trailing whitespace in link text should be trimmed before URL check
1609        let content = r#"[ https://github.com/org/repo](https://github.com/org/repo)"#;
1610
1611        let ctx = create_context(content);
1612        let result = rule.check(&ctx).unwrap();
1613
1614        assert!(
1615            result.is_empty(),
1616            "URL-like link text with leading space should not be flagged, got: {result:?}"
1617        );
1618    }
1619
1620    #[test]
1621    fn test_url_link_text_uppercase_scheme_not_flagged() {
1622        let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
1623
1624        let content = r#"[HTTPS://GITHUB.COM/org/repo](https://github.com/org/repo)"#;
1625
1626        let ctx = create_context(content);
1627        let result = rule.check(&ctx).unwrap();
1628
1629        assert!(
1630            result.is_empty(),
1631            "URL-like link text with uppercase scheme should not be flagged, got: {result:?}"
1632        );
1633    }
1634
1635    #[test]
1636    fn test_non_url_link_text_still_flagged() {
1637        let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
1638
1639        // Link text that is NOT a URL should still be flagged
1640        let content = r#"[github.com/org/repo](https://github.com/org/repo)
1641
1642[Visit github](https://github.com/org/repo)
1643
1644[//github.com/org/repo](//github.com/org/repo)
1645
1646[ftp://github.com/org/repo](ftp://github.com/org/repo)"#;
1647
1648        let ctx = create_context(content);
1649        let result = rule.check(&ctx).unwrap();
1650
1651        assert_eq!(result.len(), 4, "Non-URL link text should be flagged, got: {result:?}");
1652        assert!(result.iter().any(|w| w.line == 1)); // github.com (no protocol)
1653        assert!(result.iter().any(|w| w.line == 3)); // Visit github
1654        assert!(result.iter().any(|w| w.line == 5)); // //github.com (protocol-relative)
1655        assert!(result.iter().any(|w| w.line == 7)); // ftp://github.com
1656    }
1657
1658    #[test]
1659    fn test_url_link_text_fix_not_applied() {
1660        let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
1661
1662        let content = "[https://github.com/org/repo](https://github.com/org/repo)\n";
1663
1664        let ctx = create_context(content);
1665        let result = rule.fix(&ctx).unwrap();
1666
1667        assert_eq!(result, content, "Fix should not modify URL-like link text");
1668    }
1669
1670    #[test]
1671    fn test_mixed_url_and_regular_link_text() {
1672        let rule = MD044ProperNames::new(vec!["GitHub".to_string()], true);
1673
1674        // Mix of URL link text (should skip) and regular text (should flag)
1675        let content = r#"[https://github.com/org/repo](https://github.com/org/repo)
1676
1677Visit [github documentation](https://github.com/docs) for details.
1678
1679[www.github.com/pricing](https://www.github.com/pricing)"#;
1680
1681        let ctx = create_context(content);
1682        let result = rule.check(&ctx).unwrap();
1683
1684        // Only line 3 should be flagged ("github documentation" is not a URL)
1685        assert_eq!(
1686            result.len(),
1687            1,
1688            "Only non-URL link text should be flagged, got: {result:?}"
1689        );
1690        assert_eq!(result[0].line, 3);
1691    }
1692
1693    #[test]
1694    fn test_html_attribute_values_not_flagged() {
1695        // Matches inside HTML tag attributes (between `<` and `>`) are not flagged.
1696        // Attribute values are not prose — they hold URLs, class names, data values, etc.
1697        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1698        let content = "# Heading\n\ntest\n\n<img src=\"www.example.test/test_image.png\">\n";
1699        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1700        let result = rule.check(&ctx).unwrap();
1701
1702        // Nothing on line 5 should be flagged — everything is inside the `<img ...>` tag
1703        let line5_violations: Vec<_> = result.iter().filter(|w| w.line == 5).collect();
1704        assert!(
1705            line5_violations.is_empty(),
1706            "Should not flag anything inside HTML tag attributes: {line5_violations:?}"
1707        );
1708
1709        // Plain text on line 3 is still flagged
1710        let line3_violations: Vec<_> = result.iter().filter(|w| w.line == 3).collect();
1711        assert_eq!(line3_violations.len(), 1, "Plain 'test' on line 3 should be flagged");
1712    }
1713
1714    #[test]
1715    fn test_html_text_content_still_flagged() {
1716        // Text between HTML tags (not inside `<...>`) is still checked.
1717        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1718        let content = "# Heading\n\n<a href=\"https://example.test/page\">test link</a>\n";
1719        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1720        let result = rule.check(&ctx).unwrap();
1721
1722        // "example.test" in the href attribute → not flagged (inside `<...>`)
1723        // "test link" in the anchor text → flagged (between `>` and `<`)
1724        assert_eq!(
1725            result.len(),
1726            1,
1727            "Should flag only 'test' in anchor text, not in href: {result:?}"
1728        );
1729        assert_eq!(result[0].column, 37, "Should flag col 37 ('test link' in anchor text)");
1730    }
1731
1732    #[test]
1733    fn test_html_attribute_various_not_flagged() {
1734        // All attribute types are ignored: src, href, alt, class, data-*, title, etc.
1735        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1736        let content = concat!(
1737            "# Heading\n\n",
1738            "<img src=\"test.png\" alt=\"test image\">\n",
1739            "<span class=\"test-class\" data-test=\"value\">test content</span>\n",
1740        );
1741        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1742        let result = rule.check(&ctx).unwrap();
1743
1744        // Only "test content" (between tags on line 4) should be flagged
1745        assert_eq!(
1746            result.len(),
1747            1,
1748            "Should flag only 'test content' between tags: {result:?}"
1749        );
1750        assert_eq!(result[0].line, 4);
1751    }
1752
1753    #[test]
1754    fn test_plain_text_underscore_boundary_unchanged() {
1755        // Plain text (outside HTML tags) still uses original word boundary semantics where
1756        // underscore is a boundary character, matching markdownlint's behavior via AST splitting.
1757        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1758        let content = "# Heading\n\ntest_image is here and just_test ends here\n";
1759        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
1760        let result = rule.check(&ctx).unwrap();
1761
1762        // Both "test_image" (test at start) and "just_test" (test at end) are flagged
1763        // because in plain text, "_" is a word boundary
1764        assert_eq!(
1765            result.len(),
1766            2,
1767            "Should flag 'test' in both 'test_image' and 'just_test': {result:?}"
1768        );
1769        let cols: Vec<usize> = result.iter().map(|w| w.column).collect();
1770        assert!(cols.contains(&1), "Should flag col 1 (test_image): {cols:?}");
1771        assert!(cols.contains(&29), "Should flag col 29 (just_test): {cols:?}");
1772    }
1773
1774    #[test]
1775    fn test_frontmatter_yaml_keys_not_flagged() {
1776        // YAML keys in frontmatter should NOT be checked for proper name violations.
1777        // Only values should be checked.
1778        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1779
1780        let content = "---\ntitle: Heading\ntest: Some Test value\n---\n\nTest\n";
1781        let ctx = create_context(content);
1782        let result = rule.check(&ctx).unwrap();
1783
1784        // "test" in the YAML key (line 3) should NOT be flagged
1785        // "Test" in the YAML value (line 3) is correct capitalization, no flag
1786        // "Test" in body (line 6) is correct capitalization, no flag
1787        assert!(
1788            result.is_empty(),
1789            "Should not flag YAML keys or correctly capitalized values: {result:?}"
1790        );
1791    }
1792
1793    #[test]
1794    fn test_frontmatter_yaml_values_flagged() {
1795        // Incorrectly capitalized names in YAML values should be flagged.
1796        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1797
1798        let content = "---\ntitle: Heading\nkey: a test value\n---\n\nTest\n";
1799        let ctx = create_context(content);
1800        let result = rule.check(&ctx).unwrap();
1801
1802        // "test" in the YAML value (line 3) SHOULD be flagged
1803        assert_eq!(result.len(), 1, "Should flag 'test' in YAML value: {result:?}");
1804        assert_eq!(result[0].line, 3);
1805        assert_eq!(result[0].column, 8); // "key: a " = 7 chars, then "test" at column 8
1806    }
1807
1808    #[test]
1809    fn test_frontmatter_key_matches_name_not_flagged() {
1810        // A YAML key that happens to match a configured name should NOT be flagged.
1811        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1812
1813        let content = "---\ntest: other value\n---\n\nBody text\n";
1814        let ctx = create_context(content);
1815        let result = rule.check(&ctx).unwrap();
1816
1817        assert!(
1818            result.is_empty(),
1819            "Should not flag YAML key that matches configured name: {result:?}"
1820        );
1821    }
1822
1823    #[test]
1824    fn test_frontmatter_empty_value_not_flagged() {
1825        // YAML key with no value should be skipped entirely.
1826        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1827
1828        let content = "---\ntest:\ntest: \n---\n\nBody text\n";
1829        let ctx = create_context(content);
1830        let result = rule.check(&ctx).unwrap();
1831
1832        assert!(
1833            result.is_empty(),
1834            "Should not flag YAML keys with empty values: {result:?}"
1835        );
1836    }
1837
1838    #[test]
1839    fn test_frontmatter_nested_yaml_key_not_flagged() {
1840        // Nested/indented YAML keys should also be skipped.
1841        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1842
1843        let content = "---\nparent:\n  test: nested value\n---\n\nBody text\n";
1844        let ctx = create_context(content);
1845        let result = rule.check(&ctx).unwrap();
1846
1847        // "test" as a nested key should NOT be flagged
1848        assert!(result.is_empty(), "Should not flag nested YAML keys: {result:?}");
1849    }
1850
1851    #[test]
1852    fn test_frontmatter_list_items_checked() {
1853        // YAML list items are values and should be checked for proper names.
1854        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1855
1856        let content = "---\ntags:\n  - test\n  - other\n---\n\nBody text\n";
1857        let ctx = create_context(content);
1858        let result = rule.check(&ctx).unwrap();
1859
1860        // "test" as a list item value SHOULD be flagged
1861        assert_eq!(result.len(), 1, "Should flag 'test' in YAML list item: {result:?}");
1862        assert_eq!(result[0].line, 3);
1863    }
1864
1865    #[test]
1866    fn test_frontmatter_value_with_multiple_colons() {
1867        // For "key: value: more", key is before first colon.
1868        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1869
1870        let content = "---\ntest: description: a test thing\n---\n\nBody text\n";
1871        let ctx = create_context(content);
1872        let result = rule.check(&ctx).unwrap();
1873
1874        // "test" as key should NOT be flagged
1875        // "test" in value portion ("description: a test thing") SHOULD be flagged
1876        assert_eq!(
1877            result.len(),
1878            1,
1879            "Should flag 'test' in value after first colon: {result:?}"
1880        );
1881        assert_eq!(result[0].line, 2);
1882        assert!(result[0].column > 6, "Violation column should be in value portion");
1883    }
1884
1885    #[test]
1886    fn test_frontmatter_does_not_affect_body() {
1887        // Body text after frontmatter should still be fully checked.
1888        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1889
1890        let content = "---\ntitle: Heading\n---\n\ntest should be flagged here\n";
1891        let ctx = create_context(content);
1892        let result = rule.check(&ctx).unwrap();
1893
1894        assert_eq!(result.len(), 1, "Should flag 'test' in body text: {result:?}");
1895        assert_eq!(result[0].line, 5);
1896    }
1897
1898    #[test]
1899    fn test_frontmatter_fix_corrects_values_preserves_keys() {
1900        // Fix should correct YAML values but preserve keys.
1901        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1902
1903        let content = "---\ntest: a test value\n---\n\ntest here\n";
1904        let ctx = create_context(content);
1905        let fixed = rule.fix(&ctx).unwrap();
1906
1907        // Key "test" should remain lowercase; value "test" should become "Test"
1908        assert_eq!(fixed, "---\ntest: a Test value\n---\n\nTest here\n");
1909    }
1910
1911    #[test]
1912    fn test_frontmatter_multiword_value_flagged() {
1913        // Multiple proper names in a single YAML value should all be flagged.
1914        let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);
1915
1916        let content = "---\ndescription: Learn javascript and typescript\n---\n\nBody\n";
1917        let ctx = create_context(content);
1918        let result = rule.check(&ctx).unwrap();
1919
1920        assert_eq!(result.len(), 2, "Should flag both names in YAML value: {result:?}");
1921        assert!(result.iter().all(|w| w.line == 2));
1922    }
1923
1924    #[test]
1925    fn test_frontmatter_yaml_comments_not_checked() {
1926        // YAML comments inside frontmatter should be skipped entirely.
1927        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1928
1929        let content = "---\n# test comment\ntitle: Heading\n---\n\nBody text\n";
1930        let ctx = create_context(content);
1931        let result = rule.check(&ctx).unwrap();
1932
1933        assert!(result.is_empty(), "Should not flag names in YAML comments: {result:?}");
1934    }
1935
1936    #[test]
1937    fn test_frontmatter_delimiters_not_checked() {
1938        // Frontmatter delimiter lines (--- or +++) should never be checked.
1939        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1940
1941        let content = "---\ntitle: Heading\n---\n\ntest here\n";
1942        let ctx = create_context(content);
1943        let result = rule.check(&ctx).unwrap();
1944
1945        // Only the body "test" on line 5 should be flagged
1946        assert_eq!(result.len(), 1, "Should only flag body text: {result:?}");
1947        assert_eq!(result[0].line, 5);
1948    }
1949
1950    #[test]
1951    fn test_frontmatter_continuation_lines_checked() {
1952        // Continuation lines (indented, no colon) are value content and should be checked.
1953        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1954
1955        let content = "---\ndescription: >\n  a test value\n  continued here\n---\n\nBody\n";
1956        let ctx = create_context(content);
1957        let result = rule.check(&ctx).unwrap();
1958
1959        // "test" on the continuation line should be flagged
1960        assert_eq!(result.len(), 1, "Should flag 'test' in continuation line: {result:?}");
1961        assert_eq!(result[0].line, 3);
1962    }
1963
1964    #[test]
1965    fn test_frontmatter_quoted_values_checked() {
1966        // Quoted YAML values should have their content checked (inside the quotes).
1967        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1968
1969        let content = "---\ntitle: \"a test title\"\n---\n\nBody\n";
1970        let ctx = create_context(content);
1971        let result = rule.check(&ctx).unwrap();
1972
1973        assert_eq!(result.len(), 1, "Should flag 'test' in quoted YAML value: {result:?}");
1974        assert_eq!(result[0].line, 2);
1975    }
1976
1977    #[test]
1978    fn test_frontmatter_single_quoted_values_checked() {
1979        // Single-quoted YAML values should have their content checked.
1980        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
1981
1982        let content = "---\ntitle: 'a test title'\n---\n\nBody\n";
1983        let ctx = create_context(content);
1984        let result = rule.check(&ctx).unwrap();
1985
1986        assert_eq!(
1987            result.len(),
1988            1,
1989            "Should flag 'test' in single-quoted YAML value: {result:?}"
1990        );
1991        assert_eq!(result[0].line, 2);
1992    }
1993
1994    #[test]
1995    fn test_frontmatter_fix_multiword_values() {
1996        // Fix should correct all proper names in frontmatter values.
1997        let rule = MD044ProperNames::new(vec!["JavaScript".to_string(), "TypeScript".to_string()], true);
1998
1999        let content = "---\ndescription: Learn javascript and typescript\n---\n\nBody\n";
2000        let ctx = create_context(content);
2001        let fixed = rule.fix(&ctx).unwrap();
2002
2003        assert_eq!(
2004            fixed,
2005            "---\ndescription: Learn JavaScript and TypeScript\n---\n\nBody\n"
2006        );
2007    }
2008
2009    #[test]
2010    fn test_frontmatter_fix_preserves_yaml_structure() {
2011        // Fix should preserve YAML structure while correcting values.
2012        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2013
2014        let content = "---\ntags:\n  - test\n  - other\ntitle: a test doc\n---\n\ntest body\n";
2015        let ctx = create_context(content);
2016        let fixed = rule.fix(&ctx).unwrap();
2017
2018        assert_eq!(
2019            fixed,
2020            "---\ntags:\n  - Test\n  - other\ntitle: a Test doc\n---\n\nTest body\n"
2021        );
2022    }
2023
2024    #[test]
2025    fn test_frontmatter_toml_delimiters_not_checked() {
2026        // TOML frontmatter with +++ delimiters should also be handled.
2027        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2028
2029        let content = "+++\ntitle = \"a test title\"\n+++\n\ntest body\n";
2030        let ctx = create_context(content);
2031        let result = rule.check(&ctx).unwrap();
2032
2033        // "title" as TOML key should NOT be flagged
2034        // "test" in TOML quoted value SHOULD be flagged (line 2)
2035        // "test" in body SHOULD be flagged (line 5)
2036        assert_eq!(result.len(), 2, "Should flag TOML value and body: {result:?}");
2037        let fm_violations: Vec<_> = result.iter().filter(|w| w.line == 2).collect();
2038        assert_eq!(fm_violations.len(), 1, "Should flag 'test' in TOML value: {result:?}");
2039        let body_violations: Vec<_> = result.iter().filter(|w| w.line == 5).collect();
2040        assert_eq!(body_violations.len(), 1, "Should flag body 'test': {result:?}");
2041    }
2042
2043    #[test]
2044    fn test_frontmatter_toml_key_not_flagged() {
2045        // TOML keys should NOT be flagged, only values.
2046        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2047
2048        let content = "+++\ntest = \"other value\"\n+++\n\nBody text\n";
2049        let ctx = create_context(content);
2050        let result = rule.check(&ctx).unwrap();
2051
2052        assert!(
2053            result.is_empty(),
2054            "Should not flag TOML key that matches configured name: {result:?}"
2055        );
2056    }
2057
2058    #[test]
2059    fn test_frontmatter_toml_fix_preserves_keys() {
2060        // Fix should correct TOML values but preserve keys.
2061        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2062
2063        let content = "+++\ntest = \"a test value\"\n+++\n\ntest here\n";
2064        let ctx = create_context(content);
2065        let fixed = rule.fix(&ctx).unwrap();
2066
2067        // Key "test" should remain lowercase; value "test" should become "Test"
2068        assert_eq!(fixed, "+++\ntest = \"a Test value\"\n+++\n\nTest here\n");
2069    }
2070
2071    #[test]
2072    fn test_frontmatter_list_item_mapping_key_not_flagged() {
2073        // In "- test: nested value", "test" is a YAML key within a list-item mapping.
2074        // The key should NOT be flagged; only the value should be checked.
2075        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2076
2077        let content = "---\nitems:\n  - test: nested value\n---\n\nBody text\n";
2078        let ctx = create_context(content);
2079        let result = rule.check(&ctx).unwrap();
2080
2081        assert!(
2082            result.is_empty(),
2083            "Should not flag YAML key in list-item mapping: {result:?}"
2084        );
2085    }
2086
2087    #[test]
2088    fn test_frontmatter_list_item_mapping_value_flagged() {
2089        // In "- key: test value", the value portion should be checked.
2090        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2091
2092        let content = "---\nitems:\n  - key: a test value\n---\n\nBody text\n";
2093        let ctx = create_context(content);
2094        let result = rule.check(&ctx).unwrap();
2095
2096        assert_eq!(
2097            result.len(),
2098            1,
2099            "Should flag 'test' in list-item mapping value: {result:?}"
2100        );
2101        assert_eq!(result[0].line, 3);
2102    }
2103
2104    #[test]
2105    fn test_frontmatter_bare_list_item_still_flagged() {
2106        // Bare list items without a colon (e.g., "- test") are values and should be flagged.
2107        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2108
2109        let content = "---\ntags:\n  - test\n  - other\n---\n\nBody text\n";
2110        let ctx = create_context(content);
2111        let result = rule.check(&ctx).unwrap();
2112
2113        assert_eq!(result.len(), 1, "Should flag 'test' in bare list item: {result:?}");
2114        assert_eq!(result[0].line, 3);
2115    }
2116
2117    #[test]
2118    fn test_frontmatter_flow_mapping_not_flagged() {
2119        // Flow mappings like {test: value} contain YAML keys that should not be flagged.
2120        // The entire flow construct should be skipped.
2121        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2122
2123        let content = "---\nflow_map: {test: value, other: test}\n---\n\nBody text\n";
2124        let ctx = create_context(content);
2125        let result = rule.check(&ctx).unwrap();
2126
2127        assert!(
2128            result.is_empty(),
2129            "Should not flag names inside flow mappings: {result:?}"
2130        );
2131    }
2132
2133    #[test]
2134    fn test_frontmatter_flow_sequence_not_flagged() {
2135        // Flow sequences like [test, other] should also be skipped.
2136        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2137
2138        let content = "---\nitems: [test, other, test]\n---\n\nBody text\n";
2139        let ctx = create_context(content);
2140        let result = rule.check(&ctx).unwrap();
2141
2142        assert!(
2143            result.is_empty(),
2144            "Should not flag names inside flow sequences: {result:?}"
2145        );
2146    }
2147
2148    #[test]
2149    fn test_frontmatter_list_item_mapping_fix_preserves_key() {
2150        // Fix should correct values in list-item mappings but preserve keys.
2151        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2152
2153        let content = "---\nitems:\n  - test: a test value\n---\n\ntest here\n";
2154        let ctx = create_context(content);
2155        let fixed = rule.fix(&ctx).unwrap();
2156
2157        // "test" as list-item key should remain lowercase;
2158        // "test" in value portion should become "Test"
2159        assert_eq!(fixed, "---\nitems:\n  - test: a Test value\n---\n\nTest here\n");
2160    }
2161
2162    // --- Angle-bracket URL tests (issue #457) ---
2163
2164    #[test]
2165    fn test_angle_bracket_url_in_html_comment_not_flagged() {
2166        // Angle-bracket URLs inside HTML comments should be skipped
2167        let config = MD044Config {
2168            names: vec!["Test".to_string()],
2169            ..MD044Config::default()
2170        };
2171        let rule = MD044ProperNames::from_config_struct(config);
2172
2173        let content = "---\ntitle: Level 1 heading\n---\n\n<https://www.example.test>\n\n<!-- This is a Test https://www.example.test -->\n<!-- This is a Test <https://www.example.test> -->\n";
2174        let ctx = create_context(content);
2175        let result = rule.check(&ctx).unwrap();
2176
2177        // Line 7: "Test" in comment prose before bare URL -- already correct capitalization
2178        // Line 7: "test" in bare URL (not in angle brackets) -- but "test" is in URL domain, not prose.
2179        //   However, .example.test has "test" at a word boundary (after '.'), so it IS flagged.
2180        // Line 8: "Test" in comment prose -- correct capitalization, not flagged
2181        // Line 8: "test" in <https://www.example.test> -- inside angle-bracket URL, NOT flagged
2182
2183        // The key assertion: line 8's angle-bracket URL should NOT produce a warning
2184        let line8_warnings: Vec<_> = result.iter().filter(|w| w.line == 8).collect();
2185        assert!(
2186            line8_warnings.is_empty(),
2187            "Should not flag names inside angle-bracket URLs in HTML comments: {line8_warnings:?}"
2188        );
2189    }
2190
2191    #[test]
2192    fn test_bare_url_in_html_comment_still_flagged() {
2193        // Bare URLs (not in angle brackets) inside HTML comments should still be checked
2194        let config = MD044Config {
2195            names: vec!["Test".to_string()],
2196            ..MD044Config::default()
2197        };
2198        let rule = MD044ProperNames::from_config_struct(config);
2199
2200        let content = "<!-- This is a test https://www.example.test -->\n";
2201        let ctx = create_context(content);
2202        let result = rule.check(&ctx).unwrap();
2203
2204        // "test" appears as prose text before URL and also in the bare URL domain
2205        // At minimum, the prose "test" should be flagged
2206        assert!(
2207            !result.is_empty(),
2208            "Should flag 'test' in prose text of HTML comment with bare URL"
2209        );
2210    }
2211
2212    #[test]
2213    fn test_angle_bracket_url_in_regular_markdown_not_flagged() {
2214        // Angle-bracket URLs in regular markdown are already handled by the link parser,
2215        // but the angle-bracket check provides a safety net
2216        let rule = MD044ProperNames::new(vec!["Test".to_string()], true);
2217
2218        let content = "<https://www.example.test>\n";
2219        let ctx = create_context(content);
2220        let result = rule.check(&ctx).unwrap();
2221
2222        assert!(
2223            result.is_empty(),
2224            "Should not flag names inside angle-bracket URLs in regular markdown: {result:?}"
2225        );
2226    }
2227
2228    #[test]
2229    fn test_multiple_angle_bracket_urls_in_one_comment() {
2230        let config = MD044Config {
2231            names: vec!["Test".to_string()],
2232            ..MD044Config::default()
2233        };
2234        let rule = MD044ProperNames::from_config_struct(config);
2235
2236        let content = "<!-- See <https://test.example.com> and <https://www.example.test> for details -->\n";
2237        let ctx = create_context(content);
2238        let result = rule.check(&ctx).unwrap();
2239
2240        // Both URLs are inside angle brackets, so "test" inside them should NOT be flagged
2241        assert!(
2242            result.is_empty(),
2243            "Should not flag names inside multiple angle-bracket URLs: {result:?}"
2244        );
2245    }
2246
2247    #[test]
2248    fn test_angle_bracket_non_url_still_flagged() {
2249        // <Test> is NOT a URL (no scheme), so is_in_angle_bracket_url does NOT protect it.
2250        // Whether it gets flagged depends on HTML tag detection, not on our URL check.
2251        assert!(
2252            !MD044ProperNames::is_in_angle_bracket_url("<test> which is not a URL.", 1),
2253            "is_in_angle_bracket_url should return false for non-URL angle brackets"
2254        );
2255    }
2256
2257    #[test]
2258    fn test_angle_bracket_mailto_url_not_flagged() {
2259        let config = MD044Config {
2260            names: vec!["Test".to_string()],
2261            ..MD044Config::default()
2262        };
2263        let rule = MD044ProperNames::from_config_struct(config);
2264
2265        let content = "<!-- Contact <mailto:test@example.com> for help -->\n";
2266        let ctx = create_context(content);
2267        let result = rule.check(&ctx).unwrap();
2268
2269        assert!(
2270            result.is_empty(),
2271            "Should not flag names inside angle-bracket mailto URLs: {result:?}"
2272        );
2273    }
2274
2275    #[test]
2276    fn test_angle_bracket_ftp_url_not_flagged() {
2277        let config = MD044Config {
2278            names: vec!["Test".to_string()],
2279            ..MD044Config::default()
2280        };
2281        let rule = MD044ProperNames::from_config_struct(config);
2282
2283        let content = "<!-- Download from <ftp://test.example.com/file> -->\n";
2284        let ctx = create_context(content);
2285        let result = rule.check(&ctx).unwrap();
2286
2287        assert!(
2288            result.is_empty(),
2289            "Should not flag names inside angle-bracket FTP URLs: {result:?}"
2290        );
2291    }
2292
2293    #[test]
2294    fn test_angle_bracket_url_fix_preserves_url() {
2295        // Fix should not modify text inside angle-bracket URLs
2296        let config = MD044Config {
2297            names: vec!["Test".to_string()],
2298            ..MD044Config::default()
2299        };
2300        let rule = MD044ProperNames::from_config_struct(config);
2301
2302        let content = "<!-- test text <https://www.example.test> -->\n";
2303        let ctx = create_context(content);
2304        let fixed = rule.fix(&ctx).unwrap();
2305
2306        // "test" in prose should be fixed, URL should be preserved
2307        assert!(
2308            fixed.contains("<https://www.example.test>"),
2309            "Fix should preserve angle-bracket URLs: {fixed}"
2310        );
2311        assert!(
2312            fixed.contains("Test text"),
2313            "Fix should correct prose 'test' to 'Test': {fixed}"
2314        );
2315    }
2316
2317    #[test]
2318    fn test_is_in_angle_bracket_url_helper() {
2319        // Direct tests of the helper function
2320        let line = "text <https://example.test> more text";
2321
2322        // Inside the URL
2323        assert!(MD044ProperNames::is_in_angle_bracket_url(line, 5)); // '<'
2324        assert!(MD044ProperNames::is_in_angle_bracket_url(line, 6)); // 'h'
2325        assert!(MD044ProperNames::is_in_angle_bracket_url(line, 15)); // middle of URL
2326        assert!(MD044ProperNames::is_in_angle_bracket_url(line, 26)); // '>'
2327
2328        // Outside the URL
2329        assert!(!MD044ProperNames::is_in_angle_bracket_url(line, 0)); // 't' at start
2330        assert!(!MD044ProperNames::is_in_angle_bracket_url(line, 4)); // space before '<'
2331        assert!(!MD044ProperNames::is_in_angle_bracket_url(line, 27)); // space after '>'
2332
2333        // Non-URL angle brackets
2334        assert!(!MD044ProperNames::is_in_angle_bracket_url("<notaurl>", 1));
2335
2336        // mailto scheme
2337        assert!(MD044ProperNames::is_in_angle_bracket_url(
2338            "<mailto:test@example.com>",
2339            10
2340        ));
2341
2342        // ftp scheme
2343        assert!(MD044ProperNames::is_in_angle_bracket_url(
2344            "<ftp://test.example.com>",
2345            10
2346        ));
2347    }
2348
2349    #[test]
2350    fn test_is_in_angle_bracket_url_uppercase_scheme() {
2351        // RFC 3986: URI schemes are case-insensitive
2352        assert!(MD044ProperNames::is_in_angle_bracket_url(
2353            "<HTTPS://test.example.com>",
2354            10
2355        ));
2356        assert!(MD044ProperNames::is_in_angle_bracket_url(
2357            "<Http://test.example.com>",
2358            10
2359        ));
2360    }
2361
2362    #[test]
2363    fn test_is_in_angle_bracket_url_uncommon_schemes() {
2364        // ssh scheme
2365        assert!(MD044ProperNames::is_in_angle_bracket_url(
2366            "<ssh://test@example.com>",
2367            10
2368        ));
2369        // file scheme
2370        assert!(MD044ProperNames::is_in_angle_bracket_url("<file:///test/path>", 10));
2371        // data scheme (no authority, just colon)
2372        assert!(MD044ProperNames::is_in_angle_bracket_url("<data:text/plain;test>", 10));
2373    }
2374
2375    #[test]
2376    fn test_is_in_angle_bracket_url_unclosed() {
2377        // Unclosed angle bracket should NOT match
2378        assert!(!MD044ProperNames::is_in_angle_bracket_url(
2379            "<https://test.example.com",
2380            10
2381        ));
2382    }
2383
2384    #[test]
2385    fn test_vale_inline_config_comments_not_flagged() {
2386        let config = MD044Config {
2387            names: vec!["Vale".to_string(), "JavaScript".to_string()],
2388            ..MD044Config::default()
2389        };
2390        let rule = MD044ProperNames::from_config_struct(config);
2391
2392        let content = "\
2393<!-- vale off -->
2394Some javascript text here.
2395<!-- vale on -->
2396<!-- vale Style.Rule = NO -->
2397More javascript text.
2398<!-- vale Style.Rule = YES -->
2399<!-- vale JavaScript.Grammar = NO -->
2400";
2401        let ctx = create_context(content);
2402        let result = rule.check(&ctx).unwrap();
2403
2404        // Only the body text lines (2, 5) should be flagged for "javascript"
2405        assert_eq!(result.len(), 2, "Should only flag body lines, not Vale config comments");
2406        assert_eq!(result[0].line, 2);
2407        assert_eq!(result[1].line, 5);
2408    }
2409
2410    #[test]
2411    fn test_remark_lint_inline_config_comments_not_flagged() {
2412        let config = MD044Config {
2413            names: vec!["JavaScript".to_string()],
2414            ..MD044Config::default()
2415        };
2416        let rule = MD044ProperNames::from_config_struct(config);
2417
2418        let content = "\
2419<!-- lint disable remark-lint-some-rule -->
2420Some javascript text here.
2421<!-- lint enable remark-lint-some-rule -->
2422<!-- lint ignore remark-lint-some-rule -->
2423More javascript text.
2424";
2425        let ctx = create_context(content);
2426        let result = rule.check(&ctx).unwrap();
2427
2428        assert_eq!(
2429            result.len(),
2430            2,
2431            "Should only flag body lines, not remark-lint config comments"
2432        );
2433        assert_eq!(result[0].line, 2);
2434        assert_eq!(result[1].line, 5);
2435    }
2436
2437    #[test]
2438    fn test_fix_does_not_modify_vale_remark_lint_comments() {
2439        let config = MD044Config {
2440            names: vec!["JavaScript".to_string(), "Vale".to_string()],
2441            ..MD044Config::default()
2442        };
2443        let rule = MD044ProperNames::from_config_struct(config);
2444
2445        let content = "\
2446<!-- vale off -->
2447Some javascript text.
2448<!-- vale on -->
2449<!-- lint disable remark-lint-some-rule -->
2450More javascript text.
2451<!-- lint enable remark-lint-some-rule -->
2452";
2453        let ctx = create_context(content);
2454        let fixed = rule.fix(&ctx).unwrap();
2455
2456        // Config directive lines must be preserved unchanged
2457        assert!(fixed.contains("<!-- vale off -->"));
2458        assert!(fixed.contains("<!-- vale on -->"));
2459        assert!(fixed.contains("<!-- lint disable remark-lint-some-rule -->"));
2460        assert!(fixed.contains("<!-- lint enable remark-lint-some-rule -->"));
2461        // Body text should be fixed
2462        assert!(fixed.contains("Some JavaScript text."));
2463        assert!(fixed.contains("More JavaScript text."));
2464    }
2465
2466    #[test]
2467    fn test_mixed_tool_directives_all_skipped() {
2468        let config = MD044Config {
2469            names: vec!["JavaScript".to_string(), "Vale".to_string()],
2470            ..MD044Config::default()
2471        };
2472        let rule = MD044ProperNames::from_config_struct(config);
2473
2474        let content = "\
2475<!-- rumdl-disable MD044 -->
2476Some javascript text.
2477<!-- markdownlint-disable -->
2478More javascript text.
2479<!-- vale off -->
2480Even more javascript text.
2481<!-- lint disable some-rule -->
2482Final javascript text.
2483<!-- rumdl-enable MD044 -->
2484<!-- markdownlint-enable -->
2485<!-- vale on -->
2486<!-- lint enable some-rule -->
2487";
2488        let ctx = create_context(content);
2489        let result = rule.check(&ctx).unwrap();
2490
2491        // Only body text lines should be flagged (lines 2, 4, 6, 8)
2492        assert_eq!(
2493            result.len(),
2494            4,
2495            "Should only flag body lines, not any tool directive comments"
2496        );
2497        assert_eq!(result[0].line, 2);
2498        assert_eq!(result[1].line, 4);
2499        assert_eq!(result[2].line, 6);
2500        assert_eq!(result[3].line, 8);
2501    }
2502
2503    #[test]
2504    fn test_vale_remark_lint_edge_cases_not_matched() {
2505        let config = MD044Config {
2506            names: vec!["JavaScript".to_string(), "Vale".to_string()],
2507            ..MD044Config::default()
2508        };
2509        let rule = MD044ProperNames::from_config_struct(config);
2510
2511        // These are regular HTML comments, NOT tool directives:
2512        // - "<!-- vale -->" is not a valid Vale directive (no action keyword)
2513        // - "<!-- vale is a tool -->" starts with "vale" but is prose, not a directive
2514        // - "<!-- valedictorian javascript -->" does not start with "<!-- vale "
2515        // - "<!-- linting javascript tips -->" does not start with "<!-- lint "
2516        // - "<!-- vale javascript -->" starts with "vale" but has no action keyword
2517        // - "<!-- lint your javascript code -->" starts with "lint" but has no action keyword
2518        let content = "\
2519<!-- vale -->
2520<!-- vale is a tool for writing -->
2521<!-- valedictorian javascript -->
2522<!-- linting javascript tips -->
2523<!-- vale javascript -->
2524<!-- lint your javascript code -->
2525";
2526        let ctx = create_context(content);
2527        let result = rule.check(&ctx).unwrap();
2528
2529        // Line 1: "<!-- vale -->" contains "vale" (wrong case for "Vale") -> flagged
2530        // Line 2: "<!-- vale is a tool for writing -->" contains "vale" -> flagged
2531        // Line 3: "<!-- valedictorian javascript -->" contains "javascript" -> flagged
2532        // Line 4: "<!-- linting javascript tips -->" contains "javascript" -> flagged
2533        // Line 5: "<!-- vale javascript -->" contains "vale" and "javascript" -> flagged for both
2534        // Line 6: "<!-- lint your javascript code -->" contains "javascript" -> flagged
2535        assert_eq!(
2536            result.len(),
2537            7,
2538            "Should flag proper names in non-directive HTML comments: got {result:?}"
2539        );
2540        assert_eq!(result[0].line, 1); // "vale" in <!-- vale -->
2541        assert_eq!(result[1].line, 2); // "vale" in <!-- vale is a tool -->
2542        assert_eq!(result[2].line, 3); // "javascript" in <!-- valedictorian javascript -->
2543        assert_eq!(result[3].line, 4); // "javascript" in <!-- linting javascript tips -->
2544        assert_eq!(result[4].line, 5); // "vale" in <!-- vale javascript -->
2545        assert_eq!(result[5].line, 5); // "javascript" in <!-- vale javascript -->
2546        assert_eq!(result[6].line, 6); // "javascript" in <!-- lint your javascript code -->
2547    }
2548
2549    #[test]
2550    fn test_vale_style_directives_skipped() {
2551        let config = MD044Config {
2552            names: vec!["JavaScript".to_string(), "Vale".to_string()],
2553            ..MD044Config::default()
2554        };
2555        let rule = MD044ProperNames::from_config_struct(config);
2556
2557        // These ARE valid Vale directives and should be skipped:
2558        let content = "\
2559<!-- vale style = MyStyle -->
2560<!-- vale styles = Style1, Style2 -->
2561<!-- vale MyRule.Name = YES -->
2562<!-- vale MyRule.Name = NO -->
2563Some javascript text.
2564";
2565        let ctx = create_context(content);
2566        let result = rule.check(&ctx).unwrap();
2567
2568        // Only line 5 (body text) should be flagged
2569        assert_eq!(
2570            result.len(),
2571            1,
2572            "Should only flag body lines, not Vale style/rule directives: got {result:?}"
2573        );
2574        assert_eq!(result[0].line, 5);
2575    }
2576
2577    // --- is_in_backtick_code_in_line unit tests ---
2578
2579    #[test]
2580    fn test_backtick_code_single_backticks() {
2581        let line = "hello `world` bye";
2582        // 'w' is at index 7, inside the backtick span (content between backticks at 6 and 12)
2583        assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 7));
2584        // 'h' at index 0 is outside
2585        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 0));
2586        // 'b' at index 14 is outside
2587        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 14));
2588    }
2589
2590    #[test]
2591    fn test_backtick_code_double_backticks() {
2592        let line = "a ``code`` b";
2593        // 'c' is at index 4, inside ``...``
2594        assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 4));
2595        // 'a' at index 0 is outside
2596        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 0));
2597        // 'b' at index 11 is outside
2598        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 11));
2599    }
2600
2601    #[test]
2602    fn test_backtick_code_unclosed() {
2603        let line = "a `code b";
2604        // No closing backtick, so nothing is a code span
2605        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 3));
2606    }
2607
2608    #[test]
2609    fn test_backtick_code_mismatched_count() {
2610        // Single backtick opening, double backtick is not a match
2611        let line = "a `code`` b";
2612        // The single ` at index 2 doesn't match `` at index 7-8
2613        // So 'c' at index 3 is NOT in a code span
2614        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 3));
2615    }
2616
2617    #[test]
2618    fn test_backtick_code_multiple_spans() {
2619        let line = "`first` and `second`";
2620        // 'f' at index 1 (inside first span)
2621        assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 1));
2622        // 'a' at index 8 (between spans)
2623        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 8));
2624        // 's' at index 13 (inside second span)
2625        assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 13));
2626    }
2627
2628    #[test]
2629    fn test_backtick_code_on_backtick_boundary() {
2630        let line = "`code`";
2631        // Position 0 is the opening backtick itself, not inside the span
2632        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 0));
2633        // Position 5 is the closing backtick, not inside the span
2634        assert!(!MD044ProperNames::is_in_backtick_code_in_line(line, 5));
2635        // Position 1-4 are inside the span
2636        assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 1));
2637        assert!(MD044ProperNames::is_in_backtick_code_in_line(line, 4));
2638    }
2639}