|
2 | 2 |
|
3 | 3 | from inspect import cleandoc |
4 | 4 |
|
5 | | -import pytest |
6 | 5 |
|
7 | 6 | from selectolax.lexbor import LexborHTMLParser, parse_fragment |
8 | 7 |
|
@@ -218,96 +217,3 @@ def test_comment_content_property() -> None: |
218 | 217 | assert text_node is not None |
219 | 218 | assert text_node.is_comment_node |
220 | 219 | assert text_node.comment_content == "hello" |
221 | | - |
222 | | - |
223 | | -def test_fragment_parser_top_level_tags(): |
224 | | - parser = LexborHTMLParser( |
225 | | - "<div><span>\n \n</span><title>X</title></div>", is_fragment=False |
226 | | - ) |
227 | | - assert parser is not None and isinstance(parser, LexborHTMLParser) |
228 | | - assert ( |
229 | | - parser.html |
230 | | - == "<html><head></head><body><div><span>\n \n</span><title>X</title></div></body></html>" |
231 | | - ) |
232 | | - assert ( |
233 | | - parser.root.html |
234 | | - == "<html><head></head><body><div><span>\n \n</span><title>X</title></div></body></html>" |
235 | | - ) |
236 | | - assert parser.head is not None |
237 | | - assert parser.body is not None |
238 | | - parser = LexborHTMLParser( |
239 | | - "<div><span>\n \n</span><title>X</title></div>", is_fragment=True |
240 | | - ) |
241 | | - assert parser.html == "<div><span>\n \n</span><title>X</title></div>" |
242 | | - assert parser.root.html == "<div><span>\n \n</span><title>X</title></div>" |
243 | | - assert parser.head is None |
244 | | - assert parser.body is None |
245 | | - parser = LexborHTMLParser( |
246 | | - "<html><body><div><span>\n \n</span><title>X</title></div></body></html>", |
247 | | - is_fragment=True, |
248 | | - ) |
249 | | - assert parser.html == "<div><span>\n \n</span><title>X</title></div>" |
250 | | - |
251 | | - |
252 | | -def test_fragment_parser_multiple_nodes_on_the_same_level(): |
253 | | - html = clean_doc(""" |
254 | | - <meta charset="utf-8"> |
255 | | - <meta content="width=device-width,initial-scale=1" name="viewport"> |
256 | | - <title>Title!</title> |
257 | | - <!-- My crazy comment --> |
258 | | - <p>Hello <strong>World</strong>!</p> |
259 | | - """) |
260 | | - parser = LexborHTMLParser(html, is_fragment=True) |
261 | | - expected_html = clean_doc(""" |
262 | | - <meta charset="utf-8"> |
263 | | - <meta content="width=device-width,initial-scale=1" name="viewport"> |
264 | | - <title>Title!</title> |
265 | | - <!-- My crazy comment --> |
266 | | - <p>Hello <strong>World</strong>!</p> |
267 | | -
|
268 | | - """) |
269 | | - assert parser.html == expected_html |
270 | | - |
271 | | - |
272 | | -def test_fragment_parser_whole_doc(): |
273 | | - html = """<html lang="en"> |
274 | | - <head><meta charset="utf-8"><title>Title!</title></head> |
275 | | - <body><p>Lorem <strong>Ipsum</strong>!</p></body> |
276 | | - </html>""" |
277 | | - parser = LexborHTMLParser(html, is_fragment=True) |
278 | | - expected_html = '<meta charset="utf-8"><title>Title!</title>\n <p>Lorem <strong>Ipsum</strong>!</p>' |
279 | | - html = parser.html |
280 | | - assert html is not None |
281 | | - assert html.strip() == expected_html |
282 | | - |
283 | | - |
284 | | -def test_fragment_parser_empty_doc(): |
285 | | - html = "" |
286 | | - parser = LexborHTMLParser(html, is_fragment=True) |
287 | | - assert parser.html is None |
288 | | - |
289 | | - |
290 | | -@pytest.mark.parametrize( |
291 | | - "html, expected_html", |
292 | | - [ |
293 | | - ("<body><div>Test</div></body>", "<div>Test</div>"), |
294 | | - (" <div>Lorep Ipsum</div>", " <div>Lorep Ipsum</div>"), |
295 | | - ("<div>Lorem</div><div>Ipsum</div>", "<div>Lorem</div><div>Ipsum</div>"), |
296 | | - (" \n <div>Lorem Ipsum</div> \t ", " \n <div>Lorem Ipsum</div> \t "), |
297 | | - ("<!-- Comment --><div>Content</div>", "<!-- Comment --><div>Content</div>"), |
298 | | - ( |
299 | | - "<template><p>Inside Template</p></template>", |
300 | | - "<template><p>Inside Template</p></template>", |
301 | | - ), |
302 | | - ], |
303 | | -) |
304 | | -def test_fragment_parser(html, expected_html): |
305 | | - parser = LexborHTMLParser(html, is_fragment=True) |
306 | | - assert parser.html == expected_html |
307 | | - |
308 | | - |
309 | | -def test_insert_node_fragment_parser(): |
310 | | - html = "<div></div>" |
311 | | - p = LexborHTMLParser(html, is_fragment=True) |
312 | | - p.root.insert_child("text") |
313 | | - assert p.html == "<div>text</div>" |
0 commit comments