11from inspect import cleandoc
22import pytest
3- from selectolax .lexbor import LexborHTMLParser
3+ from selectolax .lexbor import LexborHTMLParser , SelectolaxError
44
55
66def clean_doc (text : str ) -> str :
@@ -418,14 +418,12 @@ def test_attributes_access_on_non_element():
418418 ],
419419)
420420def test_fragment_parsing_malformed_html (malformed_html ):
421- """Test fragment parsing with malformed HTML."""
422421 parser = LexborHTMLParser (malformed_html , is_fragment = True )
423422 html_result = parser .html
424423 assert html_result is None or isinstance (html_result , str )
425424
426425
427426def test_fragment_only_text ():
428- """Test fragment parsing with only text."""
429427 text_only = "Just plain text"
430428 parser = LexborHTMLParser (text_only , is_fragment = True )
431429 html_result = parser .html
@@ -434,7 +432,6 @@ def test_fragment_only_text():
434432
435433
436434def test_fragment_only_comment ():
437- """Test fragment parsing with only comment."""
438435 comment_only = "<!-- Just a comment -->"
439436 parser = LexborHTMLParser (comment_only , is_fragment = True )
440437 html_result = parser .html
@@ -443,10 +440,61 @@ def test_fragment_only_comment():
443440
444441
445442def test_fragment_mixed_content ():
446- """Test fragment parsing with mixed content."""
447443 mixed = "Text <!-- comment --> <div>element</div> more text"
448444 parser = LexborHTMLParser (mixed , is_fragment = True )
449445 html_result = parser .html
450446 assert html_result is not None
451447 assert "Text" in html_result
452448 assert "element" in html_result
449+
450+
451+ def test_fragment_create_node_basic ():
452+ parser = LexborHTMLParser ("<div></div>" , is_fragment = True )
453+ assert parser .root is not None
454+ new_node = parser .create_node ("span" )
455+ assert new_node .tag == "span"
456+ assert new_node .parent is None
457+
458+ parser .root .insert_child (new_node )
459+ expected_html = "<div><span></span></div>"
460+ assert parser .html == expected_html
461+
462+
463+ def test_fragment_create_node_different_tags ():
464+ parser = LexborHTMLParser ("<div></div>" , is_fragment = True )
465+ root = parser .root
466+ assert root is not None
467+
468+ tags_to_test = ["p" , "span" , "div" , "h1" , "custom-tag" ]
469+ for tag in tags_to_test :
470+ new_node = parser .create_node (tag )
471+ assert new_node .tag == tag
472+ root .insert_child (new_node )
473+
474+ html = parser .html
475+ assert html is not None
476+ for tag in tags_to_test :
477+ assert f"<{ tag } ></{ tag } >" in html
478+
479+
480+ def test_fragment_create_node_with_attributes ():
481+ parser = LexborHTMLParser ("<div></div>" , is_fragment = True )
482+ assert parser .root is not None
483+ new_node = parser .create_node ("a" )
484+ new_node .attrs ["href" ] = "https://example.com"
485+ new_node .attrs ["class" ] = "link"
486+
487+ parser .root .insert_child (new_node )
488+ html = parser .html
489+ assert html is not None
490+ assert 'href="https://example.com"' in html
491+ assert 'class="link"' in html
492+
493+
494+ def test_fragment_create_node_empty_tag_name ():
495+ parser = LexborHTMLParser ("<div></div>" , is_fragment = True )
496+ try :
497+ parser .create_node ("" )
498+ assert False , "Should have raised an exception"
499+ except SelectolaxError :
500+ pass
0 commit comments