Skip to content

Commit ea5d384

Browse files
committed
Propagate header cell alignment to column when no separator row
When using |=< |=> |=~ alignment markers on header cells, the alignment now applies to the entire column (subsequent data rows), not just the header cell itself. Separator row alignment still takes precedence if present.
1 parent bae5894 commit ea5d384

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/Parser/BlockParser.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,15 +2246,25 @@ protected function tryParseTable(Node $parent, array $lines, int $start): ?int
22462246
$trimmed = substr($trimmed, 1); // Remove =
22472247

22482248
// Check for alignment marker after =
2249+
// This sets column alignment if no separator row defined it
22492250
if (str_starts_with($trimmed, '<')) {
22502251
$cellAlignment = TableCell::ALIGN_LEFT;
22512252
$trimmed = substr($trimmed, 1);
2253+
if (!isset($alignments[$index])) {
2254+
$alignments[$index] = TableCell::ALIGN_LEFT;
2255+
}
22522256
} elseif (str_starts_with($trimmed, '>')) {
22532257
$cellAlignment = TableCell::ALIGN_RIGHT;
22542258
$trimmed = substr($trimmed, 1);
2259+
if (!isset($alignments[$index])) {
2260+
$alignments[$index] = TableCell::ALIGN_RIGHT;
2261+
}
22552262
} elseif (str_starts_with($trimmed, '~')) {
22562263
$cellAlignment = TableCell::ALIGN_CENTER;
22572264
$trimmed = substr($trimmed, 1);
2265+
if (!isset($alignments[$index])) {
2266+
$alignments[$index] = TableCell::ALIGN_CENTER;
2267+
}
22582268
}
22592269

22602270
$cellContent = $trimmed;

tests/TestCase/Parser/BlockParserTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,51 @@ public function testParseTableWithEqualsHeaderNoSeparatorNeeded(): void
284284
$this->assertFalse($rows[2]->isHeader());
285285
}
286286

287+
public function testParseTableWithEqualsHeaderAlignmentPropagates(): void
288+
{
289+
// Header alignment should propagate to data cells when no separator row
290+
$doc = $this->parser->parse("|=> Right |=< Left |=~ Center |\n| A | B | C |\n| D | E | F |");
291+
292+
$table = $doc->getChildren()[0];
293+
$rows = $table->getChildren();
294+
295+
// Header row alignments
296+
$headerCells = $rows[0]->getChildren();
297+
$this->assertSame(TableCell::ALIGN_RIGHT, $headerCells[0]->getAlignment());
298+
$this->assertSame(TableCell::ALIGN_LEFT, $headerCells[1]->getAlignment());
299+
$this->assertSame(TableCell::ALIGN_CENTER, $headerCells[2]->getAlignment());
300+
301+
// Data rows should inherit column alignment from header
302+
$dataCells1 = $rows[1]->getChildren();
303+
$this->assertSame(TableCell::ALIGN_RIGHT, $dataCells1[0]->getAlignment());
304+
$this->assertSame(TableCell::ALIGN_LEFT, $dataCells1[1]->getAlignment());
305+
$this->assertSame(TableCell::ALIGN_CENTER, $dataCells1[2]->getAlignment());
306+
307+
$dataCells2 = $rows[2]->getChildren();
308+
$this->assertSame(TableCell::ALIGN_RIGHT, $dataCells2[0]->getAlignment());
309+
$this->assertSame(TableCell::ALIGN_LEFT, $dataCells2[1]->getAlignment());
310+
$this->assertSame(TableCell::ALIGN_CENTER, $dataCells2[2]->getAlignment());
311+
}
312+
313+
public function testParseTableSeparatorRowOverridesHeaderAlignment(): void
314+
{
315+
// Separator row alignment takes precedence over header |= alignment
316+
$doc = $this->parser->parse("|=> Right |=< Left |\n|:--------|------:|\n| A | B |");
317+
318+
$table = $doc->getChildren()[0];
319+
$rows = $table->getChildren();
320+
321+
// Header cells get alignment from separator row, not from |= markers
322+
$headerCells = $rows[0]->getChildren();
323+
$this->assertSame(TableCell::ALIGN_LEFT, $headerCells[0]->getAlignment());
324+
$this->assertSame(TableCell::ALIGN_RIGHT, $headerCells[1]->getAlignment());
325+
326+
// Data row also uses separator row alignment
327+
$dataCells = $rows[1]->getChildren();
328+
$this->assertSame(TableCell::ALIGN_LEFT, $dataCells[0]->getAlignment());
329+
$this->assertSame(TableCell::ALIGN_RIGHT, $dataCells[1]->getAlignment());
330+
}
331+
287332
public function testParseBlockAttributes(): void
288333
{
289334
$doc = $this->parser->parse("{.highlight}\n# Heading");

0 commit comments

Comments
 (0)