Skip to content

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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions