Skip to main content

rumdl_lib/rules/
md055_table_pipe_style.rs

1use crate::rule::{LintError, LintResult, LintWarning, Rule, RuleCategory, Severity};
2use crate::utils::range_utils::calculate_line_range;
3use crate::utils::table_utils::{TableBlock, TableUtils};
4
5mod md055_config;
6use md055_config::MD055Config;
7
8/// Rule MD055: Table pipe style
9///
10/// See [docs/md055.md](../../docs/md055.md) for full documentation, configuration, and examples.
11///
12/// This rule enforces consistent use of leading and trailing pipe characters in Markdown tables,
13/// which improves readability and ensures uniform document styling.
14///
15/// ## Purpose
16///
17/// - **Consistency**: Ensures uniform table formatting throughout documents
18/// - **Readability**: Well-formatted tables are easier to read and understand
19/// - **Maintainability**: Consistent table syntax makes documents easier to maintain
20/// - **Compatibility**: Some Markdown processors handle different table styles differently
21///
22/// ## Configuration Options
23///
24/// The rule supports the following configuration options:
25///
26/// ```yaml
27/// MD055:
28///   style: "consistent"  # Can be "consistent", "leading_and_trailing", or "no_leading_or_trailing"
29/// ```
30///
31/// ### Style Options
32///
33/// - **consistent**: All tables must use the same style (default)
34/// - **leading_and_trailing**: All tables must have both leading and trailing pipes
35/// - **no_leading_or_trailing**: Tables must not have leading or trailing pipes
36///
37/// ## Examples
38///
39/// ### Leading and Trailing Pipes
40///
41/// ```markdown
42/// | Header 1 | Header 2 | Header 3 |
43/// |----------|----------|----------|
44/// | Cell 1   | Cell 2   | Cell 3   |
45/// | Cell 4   | Cell 5   | Cell 6   |
46/// ```
47///
48/// ### No Leading or Trailing Pipes
49///
50/// ```markdown
51/// Header 1 | Header 2 | Header 3
52/// ---------|----------|---------
53/// Cell 1   | Cell 2   | Cell 3
54/// Cell 4   | Cell 5   | Cell 6
55/// ```
56///
57/// ## Behavior Details
58///
59/// - The rule analyzes each table in the document to determine its pipe style
60/// - With "consistent" style, the first table's style is used as the standard for all others
61/// - The rule handles both the header row, separator row, and content rows
62/// - Tables inside code blocks are ignored
63///
64/// ## Fix Behavior
65///
66/// When applying automatic fixes, this rule:
67/// - Adds or removes leading and trailing pipes as needed
68/// - Preserves the content and alignment of table cells
69/// - Maintains proper spacing around pipe characters
70/// - Updates both header and content rows to match the required style
71///
72/// ## Performance Considerations
73///
74/// The rule includes performance optimizations:
75/// - Efficient table detection with quick checks before detailed analysis
76/// - Smart line-by-line processing to avoid redundant operations
77/// - Optimized string manipulation for pipe character handling
78///
79/// Enforces consistent use of leading and trailing pipe characters in tables
80#[derive(Debug, Default, Clone)]
81pub struct MD055TablePipeStyle {
82    config: MD055Config,
83}
84
85impl MD055TablePipeStyle {
86    pub fn new(style: String) -> Self {
87        Self {
88            config: MD055Config { style },
89        }
90    }
91
92    pub fn from_config_struct(config: MD055Config) -> Self {
93        Self { config }
94    }
95
96    /// Determine the most prevalent table style in a table block
97    fn determine_table_style(&self, table_block: &TableBlock, lines: &[&str]) -> Option<&'static str> {
98        let mut leading_and_trailing_count = 0;
99        let mut no_leading_or_trailing_count = 0;
100        let mut leading_only_count = 0;
101        let mut trailing_only_count = 0;
102
103        // Count style of header row (table line index 0)
104        let header_content = TableUtils::extract_table_row_content(lines[table_block.header_line], table_block, 0);
105        if let Some(style) = TableUtils::determine_pipe_style(header_content) {
106            match style {
107                "leading_and_trailing" => leading_and_trailing_count += 1,
108                "no_leading_or_trailing" => no_leading_or_trailing_count += 1,
109                "leading_only" => leading_only_count += 1,
110                "trailing_only" => trailing_only_count += 1,
111                _ => {}
112            }
113        }
114
115        // Count style of content rows (table line indices 2, 3, 4, ...)
116        for (i, &line_idx) in table_block.content_lines.iter().enumerate() {
117            let content = TableUtils::extract_table_row_content(lines[line_idx], table_block, 2 + i);
118            if let Some(style) = TableUtils::determine_pipe_style(content) {
119                match style {
120                    "leading_and_trailing" => leading_and_trailing_count += 1,
121                    "no_leading_or_trailing" => no_leading_or_trailing_count += 1,
122                    "leading_only" => leading_only_count += 1,
123                    "trailing_only" => trailing_only_count += 1,
124                    _ => {}
125                }
126            }
127        }
128
129        // Determine most prevalent style
130        // In case of tie, prefer leading_and_trailing (most common, widely supported)
131        let max_count = leading_and_trailing_count
132            .max(no_leading_or_trailing_count)
133            .max(leading_only_count)
134            .max(trailing_only_count);
135
136        if max_count > 0 {
137            if leading_and_trailing_count == max_count {
138                Some("leading_and_trailing")
139            } else if no_leading_or_trailing_count == max_count {
140                Some("no_leading_or_trailing")
141            } else if leading_only_count == max_count {
142                Some("leading_only")
143            } else if trailing_only_count == max_count {
144                Some("trailing_only")
145            } else {
146                None
147            }
148        } else {
149            None
150        }
151    }
152
153    /// Simple table row fix for tests - creates a dummy TableBlock without list context
154    #[cfg(test)]
155    fn fix_table_row(&self, line: &str, target_style: &str) -> String {
156        let dummy_block = TableBlock {
157            start_line: 0,
158            end_line: 0,
159            header_line: 0,
160            delimiter_line: 0,
161            content_lines: vec![],
162            list_context: None,
163        };
164        self.fix_table_row_with_context(line, target_style, &dummy_block, 0)
165    }
166
167    /// Fix a table row to match the target style, with full context for list tables
168    ///
169    /// This handles tables inside list items by stripping the list prefix,
170    /// fixing the table content, then restoring the appropriate prefix.
171    fn fix_table_row_with_context(
172        &self,
173        line: &str,
174        target_style: &str,
175        table_block: &TableBlock,
176        table_line_index: usize,
177    ) -> String {
178        // Extract blockquote prefix first
179        let (bq_prefix, after_bq) = TableUtils::extract_blockquote_prefix(line);
180
181        // Handle list context if present
182        if let Some(ref list_ctx) = table_block.list_context {
183            if table_line_index == 0 {
184                // Header line: strip list prefix (handles both markers and indentation)
185                let stripped = after_bq
186                    .strip_prefix(&list_ctx.list_prefix)
187                    .unwrap_or_else(|| TableUtils::extract_list_prefix(after_bq).1);
188                let fixed_content = self.fix_table_content(stripped.trim(), target_style);
189
190                // Restore prefixes: blockquote + list prefix + fixed content
191                let lp = &list_ctx.list_prefix;
192                if bq_prefix.is_empty() && lp.is_empty() {
193                    fixed_content
194                } else {
195                    format!("{bq_prefix}{lp}{fixed_content}")
196                }
197            } else {
198                // Continuation lines: strip indentation, then restore it
199                let content_indent = list_ctx.content_indent;
200                let stripped = TableUtils::extract_table_row_content(line, table_block, table_line_index);
201                let fixed_content = self.fix_table_content(stripped.trim(), target_style);
202
203                // Restore prefixes: blockquote + indentation + fixed content
204                let indent = " ".repeat(content_indent);
205                format!("{bq_prefix}{indent}{fixed_content}")
206            }
207        } else {
208            // No list context, just handle blockquote prefix
209            let fixed_content = self.fix_table_content(after_bq.trim(), target_style);
210            if bq_prefix.is_empty() {
211                fixed_content
212            } else {
213                format!("{bq_prefix}{fixed_content}")
214            }
215        }
216    }
217
218    /// Fix the table content (without any prefix handling)
219    fn fix_table_content(&self, trimmed: &str, target_style: &str) -> String {
220        if !trimmed.contains('|') {
221            return trimmed.to_string();
222        }
223
224        let has_leading = trimmed.starts_with('|');
225        let has_trailing = trimmed.ends_with('|');
226
227        match target_style {
228            "leading_and_trailing" => {
229                let mut result = trimmed.to_string();
230
231                // Add leading pipe if missing
232                if !has_leading {
233                    result = format!("| {result}");
234                }
235
236                // Add trailing pipe if missing
237                if !has_trailing {
238                    result = format!("{result} |");
239                }
240
241                result
242            }
243            "no_leading_or_trailing" => {
244                let mut result = trimmed;
245
246                // Remove leading pipe if present
247                if has_leading {
248                    result = result.strip_prefix('|').unwrap_or(result);
249                    result = result.trim_start();
250                }
251
252                // Remove trailing pipe if present
253                if has_trailing {
254                    result = result.strip_suffix('|').unwrap_or(result);
255                    result = result.trim_end();
256                }
257
258                result.to_string()
259            }
260            "leading_only" => {
261                let mut result = trimmed.to_string();
262
263                // Add leading pipe if missing
264                if !has_leading {
265                    result = format!("| {result}");
266                }
267
268                // Remove trailing pipe if present
269                if has_trailing {
270                    result = result.strip_suffix('|').unwrap_or(&result).trim_end().to_string();
271                }
272
273                result
274            }
275            "trailing_only" => {
276                let mut result = trimmed;
277
278                // Remove leading pipe if present
279                if has_leading {
280                    result = result.strip_prefix('|').unwrap_or(result).trim_start();
281                }
282
283                let mut result = result.to_string();
284
285                // Add trailing pipe if missing
286                if !has_trailing {
287                    result = format!("{result} |");
288                }
289
290                result
291            }
292            _ => trimmed.to_string(),
293        }
294    }
295}
296
297impl Rule for MD055TablePipeStyle {
298    fn name(&self) -> &'static str {
299        "MD055"
300    }
301
302    fn description(&self) -> &'static str {
303        "Table pipe style should be consistent"
304    }
305
306    fn category(&self) -> RuleCategory {
307        RuleCategory::Table
308    }
309
310    fn should_skip(&self, ctx: &crate::lint_context::LintContext) -> bool {
311        // Skip if no tables present (uses cached pipe count)
312        !ctx.likely_has_tables()
313    }
314
315    fn check(&self, ctx: &crate::lint_context::LintContext) -> LintResult {
316        let line_index = &ctx.line_index;
317        let mut warnings = Vec::new();
318
319        // Early return handled by should_skip()
320
321        let lines = ctx.raw_lines();
322
323        // Get the configured style explicitly and validate it
324        let configured_style = match self.config.style.as_str() {
325            "leading_and_trailing" | "no_leading_or_trailing" | "leading_only" | "trailing_only" | "consistent" => {
326                self.config.style.as_str()
327            }
328            _ => {
329                // Invalid style provided, default to "leading_and_trailing"
330                "leading_and_trailing"
331            }
332        };
333
334        // Use pre-computed table blocks from context
335        let table_blocks = &ctx.table_blocks;
336
337        // Process each table block
338        for table_block in table_blocks {
339            // First pass: determine the table's style for "consistent" mode
340            // Count all rows to determine most prevalent style (prevalence-based approach)
341            let table_style = if configured_style == "consistent" {
342                self.determine_table_style(table_block, lines)
343            } else {
344                None
345            };
346
347            // Determine target style for this table
348            let target_style = if configured_style == "consistent" {
349                table_style.unwrap_or("leading_and_trailing")
350            } else {
351                configured_style
352            };
353
354            // Collect all table lines for building the whole-table fix
355            let all_line_indices: Vec<usize> = std::iter::once(table_block.header_line)
356                .chain(std::iter::once(table_block.delimiter_line))
357                .chain(table_block.content_lines.iter().copied())
358                .collect();
359
360            // Build the whole-table fix once for all warnings in this table
361            // This ensures that applying Quick Fix on any row fixes the entire table
362            let table_start_line = table_block.start_line + 1; // Convert to 1-indexed
363            let table_end_line = table_block.end_line + 1; // Convert to 1-indexed
364
365            // Build the complete fixed table content with proper table line indices
366            let mut fixed_table_lines: Vec<String> = Vec::with_capacity(all_line_indices.len());
367            for (table_line_idx, &line_idx) in all_line_indices.iter().enumerate() {
368                let line = lines[line_idx];
369                let fixed_line = self.fix_table_row_with_context(line, target_style, table_block, table_line_idx);
370                if line_idx < lines.len() - 1 {
371                    fixed_table_lines.push(format!("{fixed_line}\n"));
372                } else {
373                    fixed_table_lines.push(fixed_line);
374                }
375            }
376            let table_replacement = fixed_table_lines.concat();
377            let table_range = line_index.multi_line_range(table_start_line, table_end_line);
378
379            // Check all rows in the table
380            for (table_line_idx, &line_idx) in all_line_indices.iter().enumerate() {
381                let line = lines[line_idx];
382                // Extract content to properly check pipe style (handles list/blockquote prefixes)
383                let content = TableUtils::extract_table_row_content(line, table_block, table_line_idx);
384                if let Some(current_style) = TableUtils::determine_pipe_style(content) {
385                    // Only flag lines with actual style mismatches
386                    let needs_fixing = current_style != target_style;
387
388                    if needs_fixing {
389                        let (start_line, start_col, end_line, end_col) = calculate_line_range(line_idx + 1, line);
390
391                        let message = format!(
392                            "Table pipe style should be {}",
393                            match target_style {
394                                "leading_and_trailing" => "leading and trailing",
395                                "no_leading_or_trailing" => "no leading or trailing",
396                                "leading_only" => "leading only",
397                                "trailing_only" => "trailing only",
398                                _ => target_style,
399                            }
400                        );
401
402                        // Each warning uses the same whole-table fix
403                        // This ensures Quick Fix on any row fixes the entire table
404                        warnings.push(LintWarning {
405                            rule_name: Some(self.name().to_string()),
406                            severity: Severity::Warning,
407                            message,
408                            line: start_line,
409                            column: start_col,
410                            end_line,
411                            end_column: end_col,
412                            fix: Some(crate::rule::Fix {
413                                range: table_range.clone(),
414                                replacement: table_replacement.clone(),
415                            }),
416                        });
417                    }
418                }
419            }
420        }
421
422        Ok(warnings)
423    }
424
425    fn fix(&self, ctx: &crate::lint_context::LintContext) -> Result<String, LintError> {
426        let lines = ctx.raw_lines();
427
428        // Use the configured style but validate it first
429        let configured_style = match self.config.style.as_str() {
430            "leading_and_trailing" | "no_leading_or_trailing" | "leading_only" | "trailing_only" | "consistent" => {
431                self.config.style.as_str()
432            }
433            _ => {
434                // Invalid style provided, default to "leading_and_trailing"
435                "leading_and_trailing"
436            }
437        };
438
439        // Use pre-computed table blocks from context
440        let table_blocks = &ctx.table_blocks;
441
442        // Create a copy of lines that we can modify
443        let mut result_lines = lines.iter().map(|&s| s.to_string()).collect::<Vec<String>>();
444
445        // Process each table block
446        for table_block in table_blocks {
447            // First pass: determine the table's style for "consistent" mode
448            // Count all rows to determine most prevalent style (prevalence-based approach)
449            let table_style = if configured_style == "consistent" {
450                self.determine_table_style(table_block, lines)
451            } else {
452                None
453            };
454
455            // Determine target style for this table
456            let target_style = if configured_style == "consistent" {
457                table_style.unwrap_or("leading_and_trailing")
458            } else {
459                configured_style
460            };
461
462            // Fix all rows in the table with proper table line indices
463            let all_line_indices: Vec<usize> = std::iter::once(table_block.header_line)
464                .chain(std::iter::once(table_block.delimiter_line))
465                .chain(table_block.content_lines.iter().copied())
466                .collect();
467
468            for (table_line_idx, &line_idx) in all_line_indices.iter().enumerate() {
469                let line_num = line_idx + 1;
470                if ctx.inline_config().is_rule_disabled(self.name(), line_num) {
471                    continue;
472                }
473                let line = lines[line_idx];
474                let fixed_line = self.fix_table_row_with_context(line, target_style, table_block, table_line_idx);
475                result_lines[line_idx] = fixed_line;
476            }
477        }
478
479        let mut fixed = result_lines.join("\n");
480        // Preserve trailing newline if original content had one
481        if ctx.content.ends_with('\n') && !fixed.ends_with('\n') {
482            fixed.push('\n');
483        }
484        Ok(fixed)
485    }
486
487    fn as_any(&self) -> &dyn std::any::Any {
488        self
489    }
490
491    fn default_config_section(&self) -> Option<(String, toml::Value)> {
492        let json_value = serde_json::to_value(&self.config).ok()?;
493        Some((
494            self.name().to_string(),
495            crate::rule_config_serde::json_to_toml_value(&json_value)?,
496        ))
497    }
498
499    fn from_config(config: &crate::config::Config) -> Box<dyn Rule>
500    where
501        Self: Sized,
502    {
503        let rule_config = crate::rule_config_serde::load_rule_config::<MD055Config>(config);
504        Box::new(Self::from_config_struct(rule_config))
505    }
506}
507
508#[cfg(test)]
509mod tests {
510    use super::*;
511
512    #[test]
513    fn test_md055_delimiter_row_handling() {
514        // Test with no_leading_or_trailing style
515        let rule = MD055TablePipeStyle::new("no_leading_or_trailing".to_string());
516
517        let content = "| Header 1 | Header 2 | Header 3 |\n|----------|----------|----------|\n| Data 1   | Data 2   | Data 3   |";
518        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
519        let result = rule.fix(&ctx).unwrap();
520
521        // With the fixed implementation, the delimiter row should have pipes removed
522        // Spacing is preserved from original input
523        let expected = "Header 1 | Header 2 | Header 3\n----------|----------|----------\nData 1   | Data 2   | Data 3";
524
525        assert_eq!(result, expected);
526
527        // Test that the check method actually reports the delimiter row as an issue
528        let warnings = rule.check(&ctx).unwrap();
529        let delimiter_warning = &warnings[1]; // Second warning should be for delimiter row
530        assert_eq!(delimiter_warning.line, 2);
531        assert_eq!(
532            delimiter_warning.message,
533            "Table pipe style should be no leading or trailing"
534        );
535
536        // Test with leading_and_trailing style
537        let rule = MD055TablePipeStyle::new("leading_and_trailing".to_string());
538
539        let content = "Header 1 | Header 2 | Header 3\n----------|----------|----------\nData 1   | Data 2   | Data 3";
540        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
541        let result = rule.fix(&ctx).unwrap();
542
543        // The delimiter row should have pipes added
544        // Spacing is preserved from original input
545        let expected = "| Header 1 | Header 2 | Header 3 |\n| ----------|----------|---------- |\n| Data 1   | Data 2   | Data 3 |";
546
547        assert_eq!(result, expected);
548    }
549
550    #[test]
551    fn test_md055_check_finds_delimiter_row_issues() {
552        // Test that check() correctly identifies delimiter rows that don't match style
553        let rule = MD055TablePipeStyle::new("no_leading_or_trailing".to_string());
554
555        let content = "| Header 1 | Header 2 | Header 3 |\n|----------|----------|----------|\n| Data 1   | Data 2   | Data 3   |";
556        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
557        let warnings = rule.check(&ctx).unwrap();
558
559        // Should have 3 warnings - header row, delimiter row, and data row
560        assert_eq!(warnings.len(), 3);
561
562        // Specifically verify the delimiter row warning (line 2)
563        let delimiter_warning = &warnings[1];
564        assert_eq!(delimiter_warning.line, 2);
565        assert_eq!(
566            delimiter_warning.message,
567            "Table pipe style should be no leading or trailing"
568        );
569    }
570
571    #[test]
572    fn test_md055_real_world_example() {
573        // Test with a real-world example having content before and after the table
574        let rule = MD055TablePipeStyle::new("no_leading_or_trailing".to_string());
575
576        let content = "# Table Example\n\nHere's a table with leading and trailing pipes:\n\n| Header 1 | Header 2 | Header 3 |\n|----------|----------|----------|\n| Data 1   | Data 2   | Data 3   |\n| Data 4   | Data 5   | Data 6   |\n\nMore content after the table.";
577        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
578        let result = rule.fix(&ctx).unwrap();
579
580        // The table should be fixed, with pipes removed
581        // Spacing is preserved from original input
582        let expected = "# Table Example\n\nHere's a table with leading and trailing pipes:\n\nHeader 1 | Header 2 | Header 3\n----------|----------|----------\nData 1   | Data 2   | Data 3\nData 4   | Data 5   | Data 6\n\nMore content after the table.";
583
584        assert_eq!(result, expected);
585
586        // Ensure we get warnings for all table rows
587        let warnings = rule.check(&ctx).unwrap();
588        assert_eq!(warnings.len(), 4); // All four table rows should have warnings
589
590        // The line numbers should match the correct positions in the original content
591        assert_eq!(warnings[0].line, 5); // Header row
592        assert_eq!(warnings[1].line, 6); // Delimiter row
593        assert_eq!(warnings[2].line, 7); // Data row 1
594        assert_eq!(warnings[3].line, 8); // Data row 2
595    }
596
597    #[test]
598    fn test_md055_invalid_style() {
599        // Test with an invalid style setting
600        let rule = MD055TablePipeStyle::new("leading_or_trailing".to_string()); // Invalid style
601
602        let content = "| Header 1 | Header 2 | Header 3 |\n|----------|----------|----------|\n| Data 1   | Data 2   | Data 3   |";
603        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
604        let result = rule.fix(&ctx).unwrap();
605
606        // Should default to "leading_and_trailing"
607        // Already has leading and trailing pipes, so no changes needed - spacing is preserved
608        let expected = "| Header 1 | Header 2 | Header 3 |\n|----------|----------|----------|\n| Data 1   | Data 2   | Data 3   |";
609
610        assert_eq!(result, expected);
611
612        // Now check a content that needs actual modification
613        let content = "Header 1 | Header 2 | Header 3\n----------|----------|----------\nData 1   | Data 2   | Data 3";
614        let ctx2 = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
615        let result = rule.fix(&ctx2).unwrap();
616
617        // Should add pipes to match the default "leading_and_trailing" style
618        // Spacing is preserved from original input
619        let expected = "| Header 1 | Header 2 | Header 3 |\n| ----------|----------|---------- |\n| Data 1   | Data 2   | Data 3 |";
620        assert_eq!(result, expected);
621
622        // Check that warning messages also work with the fallback style
623        let warnings = rule.check(&ctx2).unwrap();
624
625        // Since content doesn't have leading/trailing pipes but defaults to "leading_and_trailing",
626        // there should be warnings for all rows
627        assert_eq!(warnings.len(), 3);
628    }
629
630    #[test]
631    fn test_underflow_protection() {
632        // Test case to ensure no underflow when parts is empty
633        let rule = MD055TablePipeStyle::new("leading_and_trailing".to_string());
634
635        // Test with empty string (edge case)
636        let result = rule.fix_table_row("", "leading_and_trailing");
637        assert_eq!(result, "");
638
639        // Test with string that doesn't contain pipes
640        let result = rule.fix_table_row("no pipes here", "leading_and_trailing");
641        assert_eq!(result, "no pipes here");
642
643        // Test with minimal pipe content
644        let result = rule.fix_table_row("|", "leading_and_trailing");
645        // Should not panic and should handle gracefully
646        assert!(!result.is_empty());
647    }
648
649    // === Issue #305: Blockquote table tests ===
650
651    #[test]
652    fn test_fix_table_row_in_blockquote() {
653        let rule = MD055TablePipeStyle::new("leading_and_trailing".to_string());
654
655        // Blockquote table without leading pipe
656        let result = rule.fix_table_row("> H1 | H2", "leading_and_trailing");
657        assert_eq!(result, "> | H1 | H2 |");
658
659        // Blockquote table that already has pipes
660        let result = rule.fix_table_row("> | H1 | H2 |", "leading_and_trailing");
661        assert_eq!(result, "> | H1 | H2 |");
662
663        // Removing pipes from blockquote table
664        let result = rule.fix_table_row("> | H1 | H2 |", "no_leading_or_trailing");
665        assert_eq!(result, "> H1 | H2");
666    }
667
668    #[test]
669    fn test_fix_table_row_in_nested_blockquote() {
670        let rule = MD055TablePipeStyle::new("leading_and_trailing".to_string());
671
672        // Double-nested blockquote
673        let result = rule.fix_table_row(">> H1 | H2", "leading_and_trailing");
674        assert_eq!(result, ">> | H1 | H2 |");
675
676        // Triple-nested blockquote
677        let result = rule.fix_table_row(">>> H1 | H2", "leading_and_trailing");
678        assert_eq!(result, ">>> | H1 | H2 |");
679    }
680
681    #[test]
682    fn test_blockquote_table_full_document() {
683        let rule = MD055TablePipeStyle::new("leading_and_trailing".to_string());
684
685        // Full table in blockquote (2 columns, matching delimiter)
686        let content = "> H1 | H2\n> ----|----\n> a  | b";
687        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
688        let result = rule.fix(&ctx).unwrap();
689
690        // Each line should have the blockquote prefix preserved and pipes added
691        // The leading_and_trailing style adds "| " after blockquote prefix
692        assert!(
693            result.starts_with("> |"),
694            "Header should start with blockquote + pipe. Got:\n{result}"
695        );
696        // Delimiter row gets leading pipe added, so check for "> | ---" pattern
697        assert!(
698            result.contains("> | ----"),
699            "Delimiter should have blockquote prefix + leading pipe. Got:\n{result}"
700        );
701    }
702
703    #[test]
704    fn test_blockquote_table_no_leading_trailing() {
705        let rule = MD055TablePipeStyle::new("no_leading_or_trailing".to_string());
706
707        // Table with pipes that should be removed
708        let content = "> | H1 | H2 |\n> |----|----|---|\n> | a  | b |";
709        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
710        let result = rule.fix(&ctx).unwrap();
711
712        // Pipes should be removed but blockquote prefix preserved
713        let lines: Vec<&str> = result.lines().collect();
714        assert!(lines[0].starts_with("> "), "Line should start with blockquote prefix");
715        assert!(
716            !lines[0].starts_with("> |"),
717            "Leading pipe should be removed. Got: {}",
718            lines[0]
719        );
720    }
721
722    #[test]
723    fn test_mixed_regular_and_blockquote_tables() {
724        let rule = MD055TablePipeStyle::new("leading_and_trailing".to_string());
725
726        // Document with both regular and blockquote tables
727        let content = "H1 | H2\n---|---\na | b\n\n> H3 | H4\n> ---|---\n> c | d";
728        let ctx = crate::lint_context::LintContext::new(content, crate::config::MarkdownFlavor::Standard, None);
729        let result = rule.fix(&ctx).unwrap();
730
731        // Both tables should be fixed
732        assert!(result.contains("| H1 | H2 |"), "Regular table should have pipes added");
733        assert!(
734            result.contains("> | H3 | H4 |"),
735            "Blockquote table should have pipes added with prefix preserved"
736        );
737    }
738}