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