selectolax 0.3.21__cp38-cp38-win32.whl → 0.3.24__cp38-cp38-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of selectolax might be problematic. Click here for more details.

selectolax/__init__.py CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  __author__ = """Artem Golubin"""
5
5
  __email__ = 'me@rushter.com'
6
- __version__ = '0.3.21'
6
+ __version__ = '0.3.24'
7
7
 
8
8
  from . import parser
9
9
  from . import lexbor
@@ -655,6 +655,57 @@ cdef class LexborNode:
655
655
  else:
656
656
  raise SelectolaxError("Expected a string or LexborNode instance, but %s found" % type(value).__name__)
657
657
 
658
+ def insert_child(self, str_or_LexborNode value):
659
+ """
660
+ Insert a node inside (at the end of) the current Node.
661
+
662
+ Parameters
663
+ ----------
664
+ value : str, bytes or Node
665
+ The text or Node instance to insert inside the Node.
666
+ When a text string is passed, it's treated as text. All HTML tags will be escaped.
667
+ Convert and pass the ``Node`` object when you want to work with HTML.
668
+ Does not clone the ``Node`` object.
669
+ All future changes to the passed ``Node`` object will also be taken into account.
670
+
671
+ Examples
672
+ --------
673
+
674
+ >>> tree = LexborHTMLParser('<div>Get <img src=""></div>')
675
+ >>> div = tree.css_first('div')
676
+ >>> div.insert_child('Laptop')
677
+ >>> tree.body.child.html
678
+ '<div>Get <img src="">Laptop</div>'
679
+
680
+ >>> html_parser = LexborHTMLParser('<div>Get <span alt="Laptop"> <div>Laptop</div> </span></div>')
681
+ >>> html_parser2 = LexborHTMLParser('<div>Test</div>')
682
+ >>> span_node = html_parser.css_first('span')
683
+ >>> span_node.insert_child(html_parser2.body.child)
684
+ <div>Get <span alt="Laptop"> <div>Laptop</div> <div>Test</div> </span></div>'
685
+ """
686
+ cdef lxb_dom_node_t * new_node
687
+
688
+ if isinstance(value, (str, bytes, unicode)):
689
+ bytes_val = to_bytes(value)
690
+ new_node = <lxb_dom_node_t *> lxb_dom_document_create_text_node(
691
+ &self.parser.document.dom_document,
692
+ <lxb_char_t *> bytes_val, len(bytes_val)
693
+ )
694
+ if new_node == NULL:
695
+ raise SelectolaxError("Can't create a new node")
696
+ lxb_dom_node_insert_child(self.node, new_node)
697
+ elif isinstance(value, LexborNode):
698
+ new_node = lxb_dom_document_import_node(
699
+ &self.parser.document.dom_document,
700
+ <lxb_dom_node_t *> value.node,
701
+ <bint> True
702
+ )
703
+ if new_node == NULL:
704
+ raise SelectolaxError("Can't create a new node")
705
+ lxb_dom_node_insert_child(self.node, <lxb_dom_node_t *> new_node)
706
+ else:
707
+ raise SelectolaxError("Expected a string or LexborNode instance, but %s found" % type(value).__name__)
708
+
658
709
  @property
659
710
  def raw_value(self):
660
711
  """Return the raw (unparsed, original) value of a node.