Skip to content

Commit 461e6ee

Browse files
committed
Improved usability of outside parser context.
1 parent eccaef3 commit 461e6ee

File tree

6 files changed

+28
-7
lines changed

6 files changed

+28
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## v1.3.1 - 2025-08-24
6+
- Improved usability of `AttributedStringBuilder` outside parser context.
7+
58
## v1.3 - 2025-08-23
69
- Added `replace` and `replaceMatches` methods to `AttributedString` for replacing substrings and regex matches within
710
attributed (sub-)strings.

src/MarkupKit/Core/Parsers/Traits/ParsePhrasingNodeGroup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function createStringBuilderForFlowNode(
2222
DOMElement|DOMText $node,
2323
Context $context
2424
): AttributedStringBuilder {
25-
return new AttributedStringBuilder();
25+
return new AttributedStringBuilder(preserveWhitespace: false, trimWhitespaceAroundAttachments: false);
2626
}
2727

2828
/**

src/MarkupKit/Core/String/AbstractAttributedElement.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66

77
abstract readonly class AbstractAttributedElement implements AttributedElement
88
{
9+
public AttributeContainer $attributes;
10+
11+
/**
12+
* @param AttributeContainer|array<int, Attribute> $attributes
13+
*/
914
public function __construct(
10-
public AttributeContainer $attributes = new AttributeContainer()
15+
AttributeContainer|array $attributes = []
1116
) {
17+
$this->attributes = $attributes instanceof AttributeContainer ? $attributes : new AttributeContainer($attributes);
1218
}
1319

1420
public function withAttribute(Attribute $attribute): static

src/MarkupKit/Core/String/AttributedStringBuilder.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class AttributedStringBuilder
1414
* @param bool $trimWhitespaceAroundAttachments Whether to trim whitespace around attachments.
1515
*/
1616
public function __construct(
17-
public readonly bool $preserveWhitespace = false,
17+
public readonly bool $preserveWhitespace = true,
1818
public readonly bool $trimWhitespaceAroundAttachments = false
1919
) {
2020
}
@@ -104,7 +104,10 @@ private function normalizeTrailingSpace(): array
104104
return $elements;
105105
}
106106

107-
public function appendString(string $string, AttributeContainer $attributes): void
107+
/**
108+
* @param AttributeContainer|array<int, Attribute> $attributes
109+
*/
110+
public function appendString(string $string, AttributeContainer|array $attributes = []): void
108111
{
109112
$string = $this->normalizeSpace($string, end($this->elements) ?: null);
110113
if (strlen($string) === 0) {
@@ -114,7 +117,10 @@ public function appendString(string $string, AttributeContainer $attributes): vo
114117
$this->elements[] = new AttributedSubstring($string, $attributes);
115118
}
116119

117-
public function appendLineBreak(AttributeContainer $attributes): void
120+
/**
121+
* @param AttributeContainer|array<int, Attribute> $attributes
122+
*/
123+
public function appendLineBreak(AttributeContainer|array $attributes = []): void
118124
{
119125
$this->elements[] = new AttributedSubstring("\n", $attributes);
120126
}

src/MarkupKit/Core/String/AttributedSubstring.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
{
1010
use OptimizeElements;
1111

12+
/**
13+
* @param AttributeContainer|array<int, Attribute> $attributes
14+
*/
1215
public function __construct(
1316
public string $string,
14-
AttributeContainer $attributes
17+
AttributeContainer|array $attributes = []
1518
) {
1619
parent::__construct($attributes);
1720
}

src/MarkupKit/Standard/String/ImageAttachment.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010

1111
final readonly class ImageAttachment extends Attachment implements HtmlElement, MarkdownElement
1212
{
13+
/**
14+
* @param AttributeContainer|array<int, Attribute> $attributes
15+
*/
1316
public function __construct(
1417
public string $src,
1518
public ?string $alt,
1619
public ?string $title,
17-
AttributeContainer $attributes
20+
AttributeContainer|array $attributes
1821
) {
1922
parent::__construct($attributes);
2023
}

0 commit comments

Comments
 (0)