@@ -32,11 +32,15 @@ cdef inline bytes to_bytes(str_or_LexborNode value):
3232cdef class LexborNode:
3333 """ A class that represents HTML node (element)."""
3434
35+ cdef void set_as_fragment_root(self ):
36+ self ._is_fragment_root = 1
37+
3538 @staticmethod
3639 cdef LexborNode new(lxb_dom_node_t * node, LexborHTMLParser parser):
3740 cdef LexborNode lxbnode = LexborNode.__new__ (LexborNode)
3841 lxbnode.node = node
3942 lxbnode.parser = parser
43+ lxbnode._is_fragment_root = 0
4044 return lxbnode
4145
4246 @property
@@ -108,7 +112,10 @@ cdef class LexborNode:
108112 cdef lxb_status_t status
109113
110114 lxb_str = lexbor_str_create()
111- status = lxb_html_serialize_tree_str(self .node, lxb_str)
115+ if self ._is_fragment_root:
116+ status = serialize_fragment(self .node, lxb_str)
117+ else :
118+ status = lxb_html_serialize_tree_str(self .node, lxb_str)
112119 if status == 0 and lxb_str.data:
113120 html = lxb_str.data.decode(_ENCODING).replace(' <-undef>' , ' ' )
114121 lexbor_str_destroy(lxb_str, self .node.owner_document.text, True )
@@ -1127,3 +1134,13 @@ cdef lexbor_action_t text_callback(lxb_dom_node_t *node, void *ctx):
11271134 cls = < TextContainer> ctx
11281135 cls .append(py_str)
11291136 return LEXBOR_ACTION_OK
1137+
1138+ cdef lxb_status_t serialize_fragment(lxb_dom_node_t * node, lexbor_str_t * lxb_str):
1139+ cdef lxb_status_t status
1140+ while node != NULL :
1141+ status = lxb_html_serialize_tree_str(node, lxb_str)
1142+ if status != LXB_STATUS_OK:
1143+ return status
1144+ node = node.next
1145+
1146+ return LXB_STATUS_OK
0 commit comments