selectolax 0.3.28__cp38-cp38-musllinux_1_2_aarch64.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.

@@ -0,0 +1,969 @@
1
+ cimport cython
2
+
3
+ from libc.stdlib cimport free
4
+ from libc.stdlib cimport malloc
5
+ from libc.stdlib cimport realloc
6
+ from libc.string cimport memcpy
7
+
8
+ DEF _STACK_SIZE = 100
9
+ DEF _ENCODING = 'UTF-8'
10
+
11
+ @cython.final
12
+ cdef class Stack:
13
+ def __cinit__(self, size_t capacity=25):
14
+ self.capacity = capacity
15
+ self.top = 0
16
+ self._stack = <myhtml_tree_node_t**> malloc(capacity * sizeof(myhtml_tree_node_t))
17
+
18
+ def __dealloc__(self):
19
+ free(self._stack)
20
+
21
+ cdef bint is_empty(self):
22
+ return self.top <= 0
23
+
24
+ cdef push(self, myhtml_tree_node_t* res):
25
+ if self.top >= self.capacity:
26
+ self.resize()
27
+ self._stack[self.top] = res
28
+ self.top += 1
29
+
30
+ cdef myhtml_tree_node_t * pop(self):
31
+ self.top = self.top - 1
32
+ return self._stack[self.top]
33
+
34
+ cdef resize(self):
35
+ self.capacity *= 2
36
+ self._stack = <myhtml_tree_node_t**> realloc(<void*> self._stack, self.capacity * sizeof(myhtml_tree_node_t))
37
+
38
+
39
+ cdef class _Attributes:
40
+ """A dict-like object that represents attributes."""
41
+ cdef myhtml_tree_node_t * node
42
+ cdef unicode decode_errors
43
+
44
+ @staticmethod
45
+ cdef _Attributes create(myhtml_tree_node_t *node, unicode decode_errors):
46
+ obj = <_Attributes>_Attributes.__new__(_Attributes)
47
+ obj.node = node
48
+ obj.decode_errors = decode_errors
49
+ return obj
50
+
51
+ def __iter__(self):
52
+ cdef myhtml_tree_attr_t *attr = myhtml_node_attribute_first(self.node)
53
+ while attr:
54
+ if attr.key.data == NULL:
55
+ attr = attr.next
56
+ continue
57
+ key = attr.key.data.decode(_ENCODING, self.decode_errors)
58
+ attr = attr.next
59
+ yield key
60
+
61
+ def __setitem__(self, str key, value):
62
+ value = str(value)
63
+ bytes_key = key.encode(_ENCODING)
64
+ bytes_value = value.encode(_ENCODING)
65
+ myhtml_attribute_remove_by_key(self.node, <char*>bytes_key, len(bytes_key))
66
+ myhtml_attribute_add(self.node, <char*>bytes_key, len(bytes_key), <char*>bytes_value, len(bytes_value),
67
+ MyENCODING_UTF_8)
68
+
69
+ def __delitem__(self, key):
70
+ try:
71
+ self.__getitem__(key)
72
+ except KeyError:
73
+ raise KeyError(key)
74
+ bytes_key = key.encode(_ENCODING)
75
+ myhtml_attribute_remove_by_key(self.node, <char*>bytes_key, len(bytes_key))
76
+
77
+ def __getitem__(self, str key):
78
+ bytes_key = key.encode(_ENCODING)
79
+ cdef myhtml_tree_attr_t * attr = myhtml_attribute_by_key(self.node, <char*>bytes_key, len(bytes_key))
80
+ if attr != NULL:
81
+ if attr.value.data != NULL:
82
+ return attr.value.data.decode(_ENCODING, self.decode_errors)
83
+ elif attr.key.data != NULL:
84
+ return None
85
+ raise KeyError(key)
86
+
87
+ def __len__(self):
88
+ return len(list(self.__iter__()))
89
+
90
+ def keys(self):
91
+ return self.__iter__()
92
+
93
+ def items(self):
94
+ for key in self.__iter__():
95
+ yield key, self[key]
96
+
97
+ def values(self):
98
+ for key in self.__iter__():
99
+ yield self[key]
100
+
101
+ def get(self, key, default=None):
102
+ try:
103
+ return self[key]
104
+ except KeyError:
105
+ return default
106
+
107
+ def sget(self, key, default=""):
108
+ """Same as get, but returns empty strings instead of None values for empty attributes."""
109
+ try:
110
+ val = self[key]
111
+ if val is None:
112
+ val = ""
113
+ return val
114
+ except KeyError:
115
+ return default
116
+
117
+ def __contains__(self, key):
118
+ try:
119
+ self[key]
120
+ except KeyError:
121
+ return False
122
+ else:
123
+ return True
124
+
125
+ def __repr__(self):
126
+ cdef const char *c_text
127
+ c_text = myhtml_tag_name_by_id(self.node.tree, self.node.tag_id, NULL)
128
+ tag_name = c_text.decode(_ENCODING, 'ignore') if c_text != NULL else 'unknown'
129
+ return "<%s attributes, %s items>" % (tag_name, len(self))
130
+
131
+
132
+
133
+ ctypedef fused str_or_Node:
134
+ basestring
135
+ bytes
136
+ Node
137
+
138
+
139
+ cdef class Node:
140
+ """A class that represents HTML node (element)."""
141
+ cdef myhtml_tree_node_t *node
142
+ cdef public HTMLParser parser
143
+
144
+
145
+ cdef _init(self, myhtml_tree_node_t *node, HTMLParser parser):
146
+ # custom init, because __cinit__ doesn't accept C types
147
+ self.node = node
148
+ # Keep reference to the selector object, so myhtml structures will not be garbage collected prematurely
149
+ self.parser = parser
150
+
151
+ @property
152
+ def attributes(self):
153
+ """Get all attributes that belong to the current node.
154
+
155
+ The value of empty attributes is None.
156
+
157
+ Returns
158
+ -------
159
+ attributes : dictionary of all attributes.
160
+
161
+ Examples
162
+ --------
163
+
164
+ >>> tree = HTMLParser("<div data id='my_id'></div>")
165
+ >>> node = tree.css_first('div')
166
+ >>> node.attributes
167
+ {'data': None, 'id': 'my_id'}
168
+ """
169
+ cdef myhtml_tree_attr_t *attr = myhtml_node_attribute_first(self.node)
170
+ attributes = dict()
171
+
172
+ while attr:
173
+ if attr.key.data == NULL:
174
+ attr = attr.next
175
+ continue
176
+ key = attr.key.data.decode(_ENCODING, self.parser.decode_errors)
177
+ if attr.value.data:
178
+ value = attr.value.data.decode(_ENCODING, self.parser.decode_errors)
179
+ else:
180
+ value = None
181
+ attributes[key] = value
182
+
183
+ attr = attr.next
184
+
185
+ return attributes
186
+
187
+ @property
188
+ def attrs(self):
189
+ """A dict-like object that is similar to the ``attributes`` property, but operates directly on the Node data.
190
+
191
+ .. warning:: Use ``attributes`` instead, if you don't want to modify Node attributes.
192
+
193
+ Returns
194
+ -------
195
+ attributes : Attributes mapping object.
196
+
197
+ Examples
198
+ --------
199
+
200
+ >>> tree = HTMLParser("<div id='a'></div>")
201
+ >>> node = tree.css_first('div')
202
+ >>> node.attrs
203
+ <div attributes, 1 items>
204
+ >>> node.attrs['id']
205
+ 'a'
206
+ >>> node.attrs['foo'] = 'bar'
207
+ >>> del node.attrs['id']
208
+ >>> node.attributes
209
+ {'foo': 'bar'}
210
+ >>> node.attrs['id'] = 'new_id'
211
+ >>> node.html
212
+ '<div foo="bar" id="new_id"></div>'
213
+ """
214
+ cdef _Attributes attributes = _Attributes.create(self.node, self.parser.decode_errors)
215
+ return attributes
216
+
217
+ @property
218
+ def mem_id(self):
219
+ """Get the mem_id attribute of the node.
220
+
221
+ Returns
222
+ -------
223
+ text : int
224
+ """
225
+ return <size_t> self.node
226
+
227
+ @property
228
+ def id(self):
229
+ """Get the id attribute of the node.
230
+
231
+ Returns None if id does not set.
232
+
233
+ Returns
234
+ -------
235
+ text : str
236
+ """
237
+ cdef char* key = 'id'
238
+ cdef myhtml_tree_attr_t *attr
239
+ attr = myhtml_attribute_by_key(self.node, key, 2)
240
+ return None if attr == NULL else attr.value.data.decode(_ENCODING, self.parser.decode_errors)
241
+
242
+ def __hash__(self):
243
+ return self.mem_id
244
+
245
+ def text(self, bool deep=True, str separator='', bool strip=False):
246
+ """Returns the text of the node including text of all its child nodes.
247
+
248
+ Parameters
249
+ ----------
250
+ strip : bool, default False
251
+ If true, calls ``str.strip()`` on each text part to remove extra white spaces.
252
+ separator : str, default ''
253
+ The separator to use when joining text from different nodes.
254
+ deep : bool, default True
255
+ If True, includes text from all child nodes.
256
+
257
+ Returns
258
+ -------
259
+ text : str
260
+
261
+ """
262
+ text = ""
263
+ cdef const char* c_text
264
+ cdef myhtml_tree_node_t *node = self.node.child
265
+
266
+ if not deep:
267
+ if self.node.tag_id == MyHTML_TAG__TEXT:
268
+ c_text = myhtml_node_text(self.node, NULL)
269
+ if c_text != NULL:
270
+ node_text = c_text.decode(_ENCODING, self.parser.decode_errors)
271
+ text = append_text(text, node_text, separator, strip)
272
+
273
+ while node != NULL:
274
+ if node.tag_id == MyHTML_TAG__TEXT:
275
+ c_text = myhtml_node_text(node, NULL)
276
+ if c_text != NULL:
277
+ node_text = c_text.decode(_ENCODING, self.parser.decode_errors)
278
+ text = append_text(text, node_text, separator, strip)
279
+ node = node.next
280
+ else:
281
+ text = self._text_deep(self.node, separator=separator, strip=strip)
282
+ if separator and text and text.endswith(separator):
283
+ text = text[:-len(separator)]
284
+ return text
285
+
286
+ cdef inline _text_deep(self, myhtml_tree_node_t *node, separator='', strip=False):
287
+ text = ""
288
+ cdef Stack stack = Stack(_STACK_SIZE)
289
+ cdef myhtml_tree_node_t* current_node = NULL;
290
+
291
+ if node.tag_id == MyHTML_TAG__TEXT:
292
+ c_text = myhtml_node_text(node, NULL)
293
+ if c_text != NULL:
294
+ node_text = c_text.decode(_ENCODING, self.parser.decode_errors)
295
+ text = append_text(text, node_text, separator, strip)
296
+
297
+ if node.child == NULL:
298
+ return text
299
+
300
+ stack.push(node.child)
301
+
302
+ # Depth-first left-to-right tree traversal
303
+ while not stack.is_empty():
304
+ current_node = stack.pop()
305
+
306
+ if current_node != NULL:
307
+ if current_node.tag_id == MyHTML_TAG__TEXT:
308
+ c_text = myhtml_node_text(current_node, NULL)
309
+ if c_text != NULL:
310
+ node_text = c_text.decode(_ENCODING, self.parser.decode_errors)
311
+ text = append_text(text, node_text, separator, strip)
312
+
313
+ if current_node.next is not NULL:
314
+ stack.push(current_node.next)
315
+
316
+ if current_node.child is not NULL:
317
+ stack.push(current_node.child)
318
+
319
+ return text
320
+
321
+ def iter(self, include_text=False):
322
+ """Iterate over nodes on the current level.
323
+
324
+ Parameters
325
+ ----------
326
+ include_text : bool
327
+ If True, includes text nodes as well.
328
+
329
+ Yields
330
+ -------
331
+ node
332
+ """
333
+
334
+ cdef myhtml_tree_node_t *node = self.node.child
335
+ cdef Node next_node
336
+
337
+ while node != NULL:
338
+ if node.tag_id == MyHTML_TAG__TEXT and not include_text:
339
+ node = node.next
340
+ continue
341
+
342
+ next_node = Node()
343
+ next_node._init(node, self.parser)
344
+ yield next_node
345
+ node = node.next
346
+
347
+
348
+ def traverse(self, include_text=False):
349
+ """Iterate over all child and next nodes starting from the current level.
350
+
351
+ Parameters
352
+ ----------
353
+ include_text : bool
354
+ If True, includes text nodes as well.
355
+
356
+ Yields
357
+ -------
358
+ node
359
+ """
360
+ cdef Stack stack = Stack(_STACK_SIZE)
361
+ cdef myhtml_tree_node_t* current_node = NULL;
362
+ cdef Node next_node;
363
+
364
+ stack.push(self.node)
365
+
366
+ while not stack.is_empty():
367
+ current_node = stack.pop()
368
+ if current_node != NULL and not (current_node.tag_id == MyHTML_TAG__TEXT and not include_text):
369
+ next_node = Node()
370
+ next_node._init(current_node, self.parser)
371
+ yield next_node
372
+
373
+ if current_node.next is not NULL:
374
+ stack.push(current_node.next)
375
+
376
+ if current_node.child is not NULL:
377
+ stack.push(current_node.child)
378
+
379
+ @property
380
+ def tag(self):
381
+ """Return the name of the current tag (e.g. div, p, img).
382
+
383
+ Returns
384
+ -------
385
+ text : str
386
+ """
387
+ cdef const char *c_text
388
+ c_text = myhtml_tag_name_by_id(self.node.tree, self.node.tag_id, NULL)
389
+ text = None
390
+ if c_text:
391
+ text = c_text.decode(_ENCODING, self.parser.decode_errors)
392
+ return text
393
+
394
+ @property
395
+ def child(self):
396
+ """Return the child node."""
397
+ cdef Node node
398
+ if self.node.child:
399
+ node = Node()
400
+ node._init(self.node.child, self.parser)
401
+ return node
402
+ return None
403
+
404
+ @property
405
+ def parent(self):
406
+ """Return the parent node."""
407
+ cdef Node node
408
+ if self.node.parent:
409
+ node = Node()
410
+ node._init(self.node.parent, self.parser)
411
+ return node
412
+ return None
413
+
414
+ @property
415
+ def next(self):
416
+ """Return next node."""
417
+ cdef Node node
418
+ if self.node.next:
419
+ node = Node()
420
+ node._init(self.node.next, self.parser)
421
+ return node
422
+ return None
423
+
424
+ @property
425
+ def prev(self):
426
+ """Return previous node."""
427
+ cdef Node node
428
+ if self.node.prev:
429
+ node = Node()
430
+ node._init(self.node.prev, self.parser)
431
+ return node
432
+ return None
433
+
434
+ @property
435
+ def last_child(self):
436
+ """Return last child node."""
437
+ cdef Node node
438
+ if self.node.last_child:
439
+ node = Node()
440
+ node._init(self.node.last_child, self.parser)
441
+ return node
442
+ return None
443
+
444
+ @property
445
+ def html(self):
446
+ """Return HTML representation of the current node including all its child nodes.
447
+
448
+ Returns
449
+ -------
450
+ text : str
451
+ """
452
+ cdef mycore_string_raw_t c_str
453
+ c_str.data = NULL
454
+ c_str.length = 0
455
+ c_str.size = 0
456
+
457
+ cdef mystatus_t status
458
+ status = myhtml_serialization(self.node, &c_str)
459
+
460
+ if status == 0 and c_str.data:
461
+ html = c_str.data.decode(_ENCODING).replace('<-undef>', '')
462
+ free(c_str.data)
463
+ return html
464
+
465
+ return None
466
+
467
+ def css(self, str query):
468
+ """Evaluate CSS selector against current node and its child nodes."""
469
+ return find_nodes(self.parser, self.node, query)
470
+
471
+ def any_css_matches(self, tuple selectors):
472
+ """Returns True if any of CSS selectors matches a node"""
473
+ return find_matches(self.parser, self.node, selectors)
474
+
475
+ def css_matches(self, str selector):
476
+ """Returns True if CSS selector matches a node."""
477
+ return find_matches(self.parser, self.node, (selector, ))
478
+
479
+ def css_first(self, str query, default=None, bool strict=False):
480
+ """Evaluate CSS selector against current node and its child nodes."""
481
+ results = self.css(query)
482
+ n_results = len(results)
483
+
484
+ if n_results > 0:
485
+
486
+ if strict and n_results > 1:
487
+ raise ValueError("Expected 1 match, but found %s matches" % n_results)
488
+
489
+ return results[0]
490
+
491
+ return default
492
+
493
+ def decompose(self, bool recursive=True):
494
+ """Remove a Node from the tree.
495
+
496
+ Parameters
497
+ ----------
498
+ recursive : bool, default True
499
+ Whenever to delete all its child nodes
500
+
501
+ Examples
502
+ --------
503
+
504
+ >>> tree = HTMLParser(html)
505
+ >>> for tag in tree.css('script'):
506
+ >>> tag.decompose()
507
+
508
+ """
509
+ if recursive:
510
+ myhtml_node_delete_recursive(self.node)
511
+ else:
512
+ myhtml_node_delete(self.node)
513
+
514
+ def remove(self, bool recursive=True):
515
+ """An alias for the decompose method."""
516
+ self.decompose(recursive)
517
+
518
+ def unwrap(self):
519
+ """Replace node with whatever is inside this node.
520
+
521
+ Examples
522
+ --------
523
+
524
+ >>> tree = HTMLParser("<div>Hello <i>world</i>!</div>")
525
+ >>> tree.css_first('i').unwrap()
526
+ >>> tree.html
527
+ '<html><head></head><body><div>Hello world!</div></body></html>'
528
+
529
+ """
530
+ if self.node.child == NULL:
531
+ return
532
+ cdef myhtml_tree_node_t* next_node;
533
+ cdef myhtml_tree_node_t* current_node;
534
+
535
+ if self.node.child.next != NULL:
536
+ current_node = self.node.child
537
+ next_node = current_node.next
538
+
539
+ while next_node != NULL:
540
+ next_node = current_node.next
541
+ myhtml_node_insert_before(self.node, current_node)
542
+ current_node = next_node
543
+ else:
544
+ myhtml_node_insert_before(self.node, self.node.child)
545
+ myhtml_node_delete(self.node)
546
+
547
+ def strip_tags(self, list tags, bool recursive = False):
548
+ """Remove specified tags from the HTML tree.
549
+
550
+ Parameters
551
+ ----------
552
+ tags : list
553
+ List of tags to remove.
554
+ recursive : bool, default True
555
+ Whenever to delete all its child nodes
556
+
557
+ Examples
558
+ --------
559
+
560
+ >>> tree = HTMLParser('<html><head></head><body><script></script><div>Hello world!</div></body></html>')
561
+ >>> tags = ['head', 'style', 'script', 'xmp', 'iframe', 'noembed', 'noframes']
562
+ >>> tree.strip_tags(tags)
563
+ >>> tree.html
564
+ '<html><body><div>Hello world!</div></body></html>'
565
+
566
+ """
567
+ for tag in tags:
568
+ for element in self.css(tag):
569
+ element.decompose(recursive=recursive)
570
+
571
+ def unwrap_tags(self, list tags):
572
+ """Unwraps specified tags from the HTML tree.
573
+
574
+ Works the same as the ``unwrap`` method, but applied to a list of tags.
575
+
576
+ Parameters
577
+ ----------
578
+ tags : list
579
+ List of tags to remove.
580
+
581
+ Examples
582
+ --------
583
+
584
+ >>> tree = HTMLParser("<div><a href="">Hello</a> <i>world</i>!</div>")
585
+ >>> tree.body.unwrap_tags(['i','a'])
586
+ >>> tree.body.html
587
+ '<body><div>Hello world!</div></body>'
588
+ """
589
+
590
+ for tag in tags:
591
+ for element in self.css(tag):
592
+ element.unwrap()
593
+
594
+ def replace_with(self, str_or_Node value):
595
+ """Replace current Node with specified value.
596
+
597
+ Parameters
598
+ ----------
599
+ value : str, bytes or Node
600
+ The text or Node instance to replace the Node with.
601
+ When a text string is passed, it's treated as text. All HTML tags will be escaped.
602
+ Convert and pass the ``Node`` object when you want to work with HTML.
603
+ Does not clone the ``Node`` object.
604
+ All future changes to the passed ``Node`` object will also be taken into account.
605
+
606
+ Examples
607
+ --------
608
+
609
+ >>> tree = HTMLParser('<div>Get <img src="" alt="Laptop"></div>')
610
+ >>> img = tree.css_first('img')
611
+ >>> img.replace_with(img.attributes.get('alt', ''))
612
+ >>> tree.body.child.html
613
+ '<div>Get Laptop</div>'
614
+
615
+ >>> html_parser = HTMLParser('<div>Get <span alt="Laptop"><img src="/jpg"> <div></div></span></div>')
616
+ >>> html_parser2 = HTMLParser('<div>Test</div>')
617
+ >>> img_node = html_parser.css_first('img')
618
+ >>> img_node.replace_with(html_parser2.body.child)
619
+ '<div>Get <span alt="Laptop"><div>Test</div> <div></div></span></div>'
620
+ """
621
+ cdef myhtml_tree_node_t *node
622
+ if isinstance(value, (str, bytes, unicode)):
623
+ bytes_val = to_bytes(value)
624
+ node = myhtml_node_create(self.parser.html_tree, MyHTML_TAG__TEXT, MyHTML_NAMESPACE_HTML)
625
+ myhtml_node_text_set(node, <char*> bytes_val, len(bytes_val), MyENCODING_UTF_8)
626
+ myhtml_node_insert_before(self.node, node)
627
+ myhtml_node_delete(self.node)
628
+ elif isinstance(value, Node):
629
+ node = myhtml_node_clone_deep(self.parser.html_tree, <myhtml_tree_node_t *> value.node)
630
+ myhtml_node_insert_before(self.node, node)
631
+ myhtml_node_delete(self.node)
632
+ else:
633
+ raise TypeError("Expected a string or Node instance, but %s found" % type(value).__name__)
634
+
635
+ def insert_before(self, str_or_Node value):
636
+ """
637
+ Insert a node before the current Node.
638
+
639
+ Parameters
640
+ ----------
641
+ value : str, bytes or Node
642
+ The text or Node instance to insert before the Node.
643
+ When a text string is passed, it's treated as text. All HTML tags will be escaped.
644
+ Convert and pass the ``Node`` object when you want to work with HTML.
645
+ Does not clone the ``Node`` object.
646
+ All future changes to the passed ``Node`` object will also be taken into account.
647
+
648
+ Examples
649
+ --------
650
+
651
+ >>> tree = HTMLParser('<div>Get <img src="" alt="Laptop"></div>')
652
+ >>> img = tree.css_first('img')
653
+ >>> img.insert_before(img.attributes.get('alt', ''))
654
+ >>> tree.body.child.html
655
+ '<div>Get Laptop<img src="" alt="Laptop"></div>'
656
+
657
+ >>> html_parser = HTMLParser('<div>Get <span alt="Laptop"><img src="/jpg"> <div></div></span></div>')
658
+ >>> html_parser2 = HTMLParser('<div>Test</div>')
659
+ >>> img_node = html_parser.css_first('img')
660
+ >>> img_node.insert_before(html_parser2.body.child)
661
+ <div>Get <span alt="Laptop"><div>Test</div><img src="/jpg"> <div></div></span></div>'
662
+ """
663
+ cdef myhtml_tree_node_t *node
664
+ if isinstance(value, (str, bytes, unicode)):
665
+ bytes_val = to_bytes(value)
666
+ node = myhtml_node_create(self.parser.html_tree, MyHTML_TAG__TEXT, MyHTML_NAMESPACE_HTML)
667
+ myhtml_node_text_set(node, <char*> bytes_val, len(bytes_val), MyENCODING_UTF_8)
668
+ myhtml_node_insert_before(self.node, node)
669
+ elif isinstance(value, Node):
670
+ node = myhtml_node_clone_deep(self.parser.html_tree, <myhtml_tree_node_t *> value.node)
671
+ myhtml_node_insert_before(self.node, node)
672
+ else:
673
+ raise TypeError("Expected a string or Node instance, but %s found" % type(value).__name__)
674
+
675
+ def insert_after(self, str_or_Node value):
676
+ """
677
+ Insert a node after the current Node.
678
+
679
+ Parameters
680
+ ----------
681
+ value : str, bytes or Node
682
+ The text or Node instance to insert after the Node.
683
+ When a text string is passed, it's treated as text. All HTML tags will be escaped.
684
+ Convert and pass the ``Node`` object when you want to work with HTML.
685
+ Does not clone the ``Node`` object.
686
+ All future changes to the passed ``Node`` object will also be taken into account.
687
+
688
+ Examples
689
+ --------
690
+
691
+ >>> tree = HTMLParser('<div>Get <img src="" alt="Laptop"></div>')
692
+ >>> img = tree.css_first('img')
693
+ >>> img.insert_after(img.attributes.get('alt', ''))
694
+ >>> tree.body.child.html
695
+ '<div>Get <img src="" alt="Laptop">Laptop</div>'
696
+
697
+ >>> html_parser = HTMLParser('<div>Get <span alt="Laptop"><img src="/jpg"> <div></div></span></div>')
698
+ >>> html_parser2 = HTMLParser('<div>Test</div>')
699
+ >>> img_node = html_parser.css_first('img')
700
+ >>> img_node.insert_after(html_parser2.body.child)
701
+ <div>Get <span alt="Laptop"><img src="/jpg"><div>Test</div> <div></div></span></div>'
702
+ """
703
+ cdef myhtml_tree_node_t *node
704
+ if isinstance(value, (str, bytes, unicode)):
705
+ bytes_val = to_bytes(value)
706
+ node = myhtml_node_create(self.parser.html_tree, MyHTML_TAG__TEXT, MyHTML_NAMESPACE_HTML)
707
+ myhtml_node_text_set(node, <char*> bytes_val, len(bytes_val), MyENCODING_UTF_8)
708
+ myhtml_node_insert_after(self.node, node)
709
+ elif isinstance(value, Node):
710
+ node = myhtml_node_clone_deep(self.parser.html_tree, <myhtml_tree_node_t *> value.node)
711
+ myhtml_node_insert_after(self.node, node)
712
+ else:
713
+ raise TypeError("Expected a string or Node instance, but %s found" % type(value).__name__)
714
+
715
+ def insert_child(self, str_or_Node value):
716
+ """
717
+ Insert a node inside (at the end of) the current Node.
718
+
719
+ Parameters
720
+ ----------
721
+ value : str, bytes or Node
722
+ The text or Node instance to insert inside the Node.
723
+ When a text string is passed, it's treated as text. All HTML tags will be escaped.
724
+ Convert and pass the ``Node`` object when you want to work with HTML.
725
+ Does not clone the ``Node`` object.
726
+ All future changes to the passed ``Node`` object will also be taken into account.
727
+
728
+ Examples
729
+ --------
730
+
731
+ >>> tree = HTMLParser('<div>Get <img src=""></div>')
732
+ >>> div = tree.css_first('div')
733
+ >>> div.insert_child('Laptop')
734
+ >>> tree.body.child.html
735
+ '<div>Get <img src="">Laptop</div>'
736
+
737
+ >>> html_parser = HTMLParser('<div>Get <span alt="Laptop"> <div>Laptop</div> </span></div>')
738
+ >>> html_parser2 = HTMLParser('<div>Test</div>')
739
+ >>> span_node = html_parser.css_first('span')
740
+ >>> span_node.insert_child(html_parser2.body.child)
741
+ <div>Get <span alt="Laptop"> <div>Laptop</div> <div>Test</div> </span></div>'
742
+ """
743
+ cdef myhtml_tree_node_t *node
744
+ if isinstance(value, (str, bytes, unicode)):
745
+ bytes_val = to_bytes(value)
746
+ node = myhtml_node_create(self.parser.html_tree, MyHTML_TAG__TEXT, MyHTML_NAMESPACE_HTML)
747
+ myhtml_node_text_set(node, <char*> bytes_val, len(bytes_val), MyENCODING_UTF_8)
748
+ myhtml_node_append_child(self.node, node)
749
+ elif isinstance(value, Node):
750
+ node = myhtml_node_clone_deep(self.parser.html_tree, <myhtml_tree_node_t *> value.node)
751
+ myhtml_node_append_child(self.node, node)
752
+ else:
753
+ raise TypeError("Expected a string or Node instance, but %s found" % type(value).__name__)
754
+
755
+ def unwrap_tags(self, list tags):
756
+ """Unwraps specified tags from the HTML tree.
757
+
758
+ Works the same as th ``unwrap`` method, but applied to a list of tags.
759
+
760
+ Parameters
761
+ ----------
762
+ tags : list
763
+ List of tags to remove.
764
+
765
+ Examples
766
+ --------
767
+
768
+ >>> tree = HTMLParser("<div><a href="">Hello</a> <i>world</i>!</div>")
769
+ >>> tree.body.unwrap_tags(['i','a'])
770
+ >>> tree.body.html
771
+ '<body><div>Hello world!</div></body>'
772
+ """
773
+
774
+ for tag in tags:
775
+ for element in self.css(tag):
776
+ element.unwrap()
777
+
778
+ @property
779
+ def raw_value(self):
780
+ """Return the raw (unparsed, original) value of a node.
781
+
782
+ Currently, works on text nodes only.
783
+
784
+ Returns
785
+ -------
786
+
787
+ raw_value : bytes
788
+
789
+ Examples
790
+ --------
791
+
792
+ >>> html_parser = HTMLParser('<div>&#x3C;test&#x3E;</div>')
793
+ >>> selector = html_parser.css_first('div')
794
+ >>> selector.child.html
795
+ '&lt;test&gt;'
796
+ >>> selector.child.raw_value
797
+ b'&#x3C;test&#x3E;'
798
+ """
799
+ cdef int begin = self.node.token.element_begin
800
+ cdef int length = self.node.token.element_length
801
+ if self.node.tag_id != MyHTML_TAG__TEXT:
802
+ raise ValueError("Can't obtain raw value for non-text node.")
803
+ return self.parser.raw_html[begin:begin + length]
804
+
805
+ def select(self, query=None):
806
+ """Select nodes given a CSS selector.
807
+
808
+ Works similarly to the ``css`` method, but supports chained filtering and extra features.
809
+
810
+ Parameters
811
+ ----------
812
+ query : str or None
813
+ The CSS selector to use when searching for nodes.
814
+
815
+ Returns
816
+ -------
817
+ selector : The `Selector` class.
818
+ """
819
+ return Selector(self, query)
820
+
821
+ def scripts_contain(self, str query):
822
+ """Returns True if any of the script tags contain specified text.
823
+
824
+ Caches script tags on the first call to improve performance.
825
+
826
+ Parameters
827
+ ----------
828
+ query : str
829
+ The query to check.
830
+
831
+ """
832
+ if self.parser.cached_script_texts is None:
833
+ nodes = find_nodes(self.parser, self.node, 'script')
834
+ text_nodes = []
835
+ for node in nodes:
836
+ node_text = node.text(deep=True)
837
+ if node_text:
838
+ text_nodes.append(node_text)
839
+ self.parser.cached_script_texts = text_nodes
840
+
841
+ for text in self.parser.cached_script_texts:
842
+ if query in text:
843
+ return True
844
+ return False
845
+
846
+ def script_srcs_contain(self, tuple queries):
847
+ """Returns True if any of the script SRCs attributes contain on of the specified text.
848
+
849
+ Caches values on the first call to improve performance.
850
+
851
+ Parameters
852
+ ----------
853
+ queries : tuple of str
854
+
855
+ """
856
+ if self.parser.cached_script_srcs is None:
857
+ nodes = find_nodes(self.parser, self.node, 'script')
858
+ src_nodes = []
859
+ for node in nodes:
860
+ node_src = node.attrs.get('src')
861
+ if node_src:
862
+ src_nodes.append(node_src)
863
+ self.parser.cached_script_srcs = src_nodes
864
+
865
+ for text in self.parser.cached_script_srcs:
866
+ for query in queries:
867
+ if query in text:
868
+ return True
869
+ return False
870
+
871
+ def __repr__(self):
872
+ return '<Node %s>' % self.tag
873
+
874
+ def __eq__(self, other):
875
+ if isinstance(other, str):
876
+ return self.html == other
877
+ if not isinstance(other, Node):
878
+ return False
879
+ return self.html == other.html
880
+ @property
881
+ def text_content(self):
882
+ """Returns the text of the node if it is a text node.
883
+
884
+ Returns None for other nodes.
885
+ Unlike the ``text`` method, does not include child nodes.
886
+
887
+ Returns
888
+ -------
889
+ text : str or None.
890
+ """
891
+ text = ""
892
+ cdef const char* c_text
893
+ cdef myhtml_tree_node_t *node = self.node.child
894
+
895
+ if self.node.tag_id == MyHTML_TAG__TEXT:
896
+ c_text = myhtml_node_text(self.node, NULL)
897
+ if c_text != NULL:
898
+ node_text = c_text.decode(_ENCODING, self.parser.decode_errors)
899
+ return append_text(text, node_text)
900
+ return None
901
+
902
+ def merge_text_nodes(self):
903
+ """Iterates over all text nodes and merges all text nodes that are close to each other.
904
+
905
+ This is useful for text extraction.
906
+ Use it when you need to strip HTML tags and merge "dangling" text.
907
+
908
+ Examples
909
+ --------
910
+
911
+ >>> tree = HTMLParser("<div><p><strong>J</strong>ohn</p><p>Doe</p></div>")
912
+ >>> node = tree.css_first('div')
913
+ >>> tree.unwrap_tags(["strong"])
914
+ >>> tree.text(deep=True, separator=" ", strip=True)
915
+ "J ohn Doe" # Text extraction produces an extra space because the strong tag was removed.
916
+ >>> node.merge_text_nodes()
917
+ >>> tree.text(deep=True, separator=" ", strip=True)
918
+ "John Doe"
919
+ """
920
+ cdef Stack stack = Stack(_STACK_SIZE)
921
+ cdef myhtml_tree_node_t * current_node = NULL
922
+ cdef Node next_node
923
+ cdef const char* left_text
924
+ cdef const char* right_text
925
+ cdef char* final_text
926
+ cdef size_t left_length, right_length, final_length
927
+
928
+ stack.push(self.node)
929
+
930
+ while not stack.is_empty():
931
+ current_node = stack.pop()
932
+
933
+ if current_node.tag_id == MyHTML_TAG__TEXT and current_node.prev and \
934
+ current_node.prev.tag_id == MyHTML_TAG__TEXT:
935
+ left_text = myhtml_node_text(current_node.prev, &left_length)
936
+ right_text = myhtml_node_text(current_node, &right_length)
937
+ if left_text and right_text:
938
+ final_length = left_length + right_length
939
+ final_text = <char *>malloc(final_length + 1)
940
+ if final_text == NULL:
941
+ raise MemoryError("Can't allocate memory for a new node.")
942
+ memcpy(final_text, left_text, left_length)
943
+ memcpy(final_text + left_length, right_text, right_length + 1)
944
+ myhtml_node_text_set(current_node, <const char *>final_text, final_length, MyENCODING_UTF_8)
945
+ myhtml_node_delete(current_node.prev)
946
+ free(final_text)
947
+
948
+ if current_node.next is not NULL:
949
+ stack.push(current_node.next)
950
+
951
+ if current_node.child is not NULL:
952
+ stack.push(current_node.child)
953
+
954
+ cdef inline str append_text(str text, str node_text, str separator='', bint strip=False):
955
+ if strip:
956
+ text += node_text.strip() + separator
957
+ else:
958
+ text += node_text + separator
959
+
960
+ return text
961
+
962
+
963
+ cdef inline bytes to_bytes(str_or_Node value):
964
+ cdef bytes bytes_val
965
+ if isinstance(value, (str, unicode)):
966
+ bytes_val = value.encode(_ENCODING)
967
+ elif isinstance(value, bytes):
968
+ bytes_val = <char*> value
969
+ return bytes_val