Skip to content

fix(Paragraph): addChild() should throw on invalid children instead of silent discard #72

@usernane

Description

@usernane

Bug Description

Paragraph::addChild() silently discards elements not in ALLOWED_CHILDREN. No exception, no warning — the child vanishes.

$p = new Paragraph();
$p->addChild(new HTMLNode('div')); // Silently ignored
$p->childrenCount(); // 0

Proposed Fix

Throw InvalidArgumentException listing allowed elements:

public function addChild($node, $attrsOrChain = [], bool $chainOnParent = true): HTMLNode {
    $nodeName = $node instanceof HTMLNode ? $node->getNodeName() : $node;

    if (!in_array($nodeName, self::ALLOWED_CHILDREN) && $nodeName !== HTMLNode::TEXT_NODE) {
        throw new \InvalidArgumentException(
            "Element '$nodeName' is not allowed in <p>. Allowed: "
            . implode(', ', self::ALLOWED_CHILDREN)
        );
    }
    // ... proceed with add
}

Related

Acceptance Criteria

  • Invalid children throw InvalidArgumentException with allowed list
  • Text nodes still added without error
  • All existing Paragraph tests updated

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions