Skip to content

Commit 58f1c56

Browse files
committed
- Added ability to strip attributes from attributed strings.
- `Context` is now provided at more places in the parser. - Added `ItemList` component and parser for parsing `ul` and `ol` elements.
1 parent 15ac547 commit 58f1c56

35 files changed

+260
-44
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

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

5-
## 1.1 - 2025-07-24
5+
## v1.2 - 2025-07-24
6+
- Added ability to strip attributes from attributed strings.
7+
- `Context` is now provided at more places in the parser.
8+
- Added `ItemList` component and parser for parsing `ul` and `ol` elements.
9+
10+
## v1.1 - 2025-07-24
611
- Simplified the main parser and moved options to the `parse` method.
712
- Introduced `Context` class to provide access to the options and for storing context-specific values.
813
- Renamed the `Basic` implementation namespace to `Standard` to better reflect its purpose.
914
- Moved node parsers into the `Core` namespace that are also usable outside the standard implementation.
1015

11-
## 1.0 - 2025-07-23
16+
## v1.0 - 2025-07-23
1217
Initial release of PHP MarkupKit

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ $components = $parser->parse('<strong>Hello World!</strong> <img src="https://pi
6060
assert(count($components) === 2);
6161

6262
assert($components[0] instanceof Components\Text);
63-
$string = $components[0]->string;
63+
$string = $components[0]->content;
6464
assert(count($string->elements) === 1);
6565
assert($string->elements[0] instanceof AttributedSubstring);
6666
assert($string->elements[0]->string === 'Hello World!');

src/MarkupKit/Core/Parsers/Flow/AttributedStringFlowNodeParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(
2525
) {
2626
}
2727

28-
public function isFlowNodeSupported(DOMElement $node): bool
28+
public function isFlowNodeSupported(DOMElement $node, Context $context): bool
2929
{
3030
return true;
3131
}

src/MarkupKit/Core/Parsers/Flow/FlowNodeParser.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
*/
1111
interface FlowNodeParser
1212
{
13-
public function isFlowNodeSupported(DOMElement $node): bool;
13+
/**
14+
* @param Context<T> $context
15+
*/
16+
public function isFlowNodeSupported(DOMElement $node, Context $context): bool;
1417

1518
/**
1619
* @param Context<T> $context

src/MarkupKit/Core/Parsers/NodeParserBundle.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public function __construct(
2525
) {
2626
}
2727

28-
public function isFlowNodeSupported(DOMElement $node): bool
28+
public function isFlowNodeSupported(DOMElement $node, Context $context): bool
2929
{
3030
foreach ($this->nodeParsers as $nodeParser) {
31-
if ($nodeParser instanceof FlowNodeParser && $nodeParser->isFlowNodeSupported($node)) {
31+
if ($nodeParser instanceof FlowNodeParser && $nodeParser->isFlowNodeSupported($node, $context)) {
3232
return true;
3333
}
3434
}
@@ -44,18 +44,21 @@ public function isFlowNodeSupported(DOMElement $node): bool
4444
public function parseFlowNode(DOMElement $node, Context $context): array
4545
{
4646
foreach ($this->nodeParsers as $nodeParser) {
47-
if ($nodeParser instanceof FlowNodeParser && $nodeParser->isFlowNodeSupported($node)) {
47+
if ($nodeParser instanceof FlowNodeParser && $nodeParser->isFlowNodeSupported($node, $context)) {
4848
return $nodeParser->parseFlowNode($node, $context);
4949
}
5050
}
5151

5252
return [];
5353
}
5454

55-
public function isPhrasingNodeSupported(DOMElement|DOMText $node): bool
55+
/**
56+
* @param Context<mixed> $context
57+
*/
58+
public function isPhrasingNodeSupported(DOMElement|DOMText $node, Context $context): bool
5659
{
5760
foreach ($this->nodeParsers as $nodeParser) {
58-
if ($nodeParser instanceof PhrasingNodeParser && $nodeParser->isPhrasingNodeSupported($node)) {
61+
if ($nodeParser instanceof PhrasingNodeParser && $nodeParser->isPhrasingNodeSupported($node, $context)) {
5962
return true;
6063
}
6164
}
@@ -70,7 +73,7 @@ public function parsePhrasingNode(
7073
Context $context
7174
): void {
7275
foreach ($this->nodeParsers as $nodeParser) {
73-
if ($nodeParser instanceof PhrasingNodeParser && $nodeParser->isPhrasingNodeSupported($node)) {
76+
if ($nodeParser instanceof PhrasingNodeParser && $nodeParser->isPhrasingNodeSupported($node, $context)) {
7477
$nodeParser->parsePhrasingNode($node, $stringBuilder, $attributes, $context);
7578
return;
7679
}

src/MarkupKit/Core/Parsers/Phrasing/ChildPhrasingNodeParser.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
{
1414
use ParsePhrasingChildNodes;
1515

16-
public function isPhrasingNodeSupported(DOMElement|DOMText $node): bool
16+
/**
17+
* @param Context<mixed> $context
18+
*/
19+
public function isPhrasingNodeSupported(DOMElement|DOMText $node, Context $context): bool
1720
{
1821
return $node instanceof DOMElement;
1922
}

src/MarkupKit/Core/Parsers/Phrasing/PhrasingNodeParser.php

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

1111
interface PhrasingNodeParser
1212
{
13-
public function isPhrasingNodeSupported(DOMElement|DOMText $node): bool;
13+
/**
14+
* @param Context<mixed> $context
15+
*/
16+
public function isPhrasingNodeSupported(DOMElement|DOMText $node, Context $context): bool;
1417

1518
/**
1619
* @param Context<mixed> $context

src/MarkupKit/Core/Parsers/Phrasing/TextPhrasingNodeParser.php

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

1111
readonly class TextPhrasingNodeParser implements PhrasingNodeParser
1212
{
13-
public function isPhrasingNodeSupported(DOMElement|DOMText $node): bool
13+
/**
14+
* @param Context<mixed> $context
15+
*/
16+
public function isPhrasingNodeSupported(DOMElement|DOMText $node, Context $context): bool
1417
{
1518
return $node instanceof DOMText;
1619
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace MarkupKit\Core\String;
4+
5+
abstract readonly class AbstractAttributedElement implements AttributedElement
6+
{
7+
public function __construct(
8+
public AttributeContainer $attributes = new AttributeContainer()
9+
) {
10+
}
11+
12+
public function withAttribute(Attribute $attribute): static
13+
{
14+
return $this->replacingAttributes(
15+
$this->attributes->withAttribute($attribute)
16+
);
17+
}
18+
19+
public function withoutAttributes(): static
20+
{
21+
return $this->replacingAttributes(new AttributeContainer());
22+
}
23+
24+
abstract public function replacingAttributes(AttributeContainer $attributes): static;
25+
}

src/MarkupKit/Core/String/Attachment.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
namespace MarkupKit\Core\String;
44

5-
readonly abstract class Attachment implements AttributedElement
5+
readonly abstract class Attachment extends AbstractAttributedElement
66
{
7-
public function __construct(
8-
public AttributeContainer $attributes = new AttributeContainer()
9-
) {
10-
}
117
}

0 commit comments

Comments
 (0)