|
| 1 | +import threading |
| 2 | + |
| 3 | +import lolhtml |
| 4 | + |
| 5 | + |
| 6 | +def test_text(): |
| 7 | + comment_processed: threading.Event = threading.Event() |
| 8 | + |
| 9 | + class DocumentHandler: |
| 10 | + # noinspection PyMethodMayBeStatic |
| 11 | + def comments(self, comment: lolhtml.Comment): |
| 12 | + assert not comment_processed.is_set() |
| 13 | + comment_processed.set() |
| 14 | + assert comment.text == " Hello World " |
| 15 | + |
| 16 | + output: bytearray = bytearray() |
| 17 | + rewriter: lolhtml.HTMLRewriter = lolhtml.HTMLRewriter(output.extend) |
| 18 | + rewriter.on_document(DocumentHandler()) |
| 19 | + rewriter.write(b"<html><!-- Hello World --></html>") |
| 20 | + rewriter.end() |
| 21 | + |
| 22 | + assert output == b"<html><!-- Hello World --></html>" |
| 23 | + assert comment_processed.is_set() |
| 24 | + |
| 25 | + |
| 26 | +def test_removed(): |
| 27 | + comment_processed: threading.Event = threading.Event() |
| 28 | + |
| 29 | + class ElementHandler: |
| 30 | + # noinspection PyMethodMayBeStatic |
| 31 | + def comments(self, comment: lolhtml.Comment): |
| 32 | + assert not comment_processed.is_set() |
| 33 | + comment_processed.set() |
| 34 | + assert not comment.removed |
| 35 | + |
| 36 | + output: bytearray = bytearray() |
| 37 | + rewriter: lolhtml.HTMLRewriter = lolhtml.HTMLRewriter(output.extend) |
| 38 | + rewriter.on("*", ElementHandler()) |
| 39 | + rewriter.write(b"<html><!-- Hello World --></html>") |
| 40 | + rewriter.end() |
| 41 | + |
| 42 | + assert output == b"<html><!-- Hello World --></html>" |
| 43 | + assert comment_processed.is_set() |
| 44 | + |
| 45 | + |
| 46 | +def test_set_text(): |
| 47 | + comment_processed: threading.Event = threading.Event() |
| 48 | + |
| 49 | + class DocumentHandler: |
| 50 | + # noinspection PyMethodMayBeStatic |
| 51 | + def comments(self, comment: lolhtml.Comment): |
| 52 | + assert not comment_processed.is_set() |
| 53 | + comment_processed.set() |
| 54 | + comment.text = "Set Comment" |
| 55 | + |
| 56 | + output: bytearray = bytearray() |
| 57 | + rewriter: lolhtml.HTMLRewriter = lolhtml.HTMLRewriter(output.extend) |
| 58 | + rewriter.on_document(DocumentHandler()) |
| 59 | + |
| 60 | + rewriter.write(b"<html><!-- Hello World --></html>") |
| 61 | + rewriter.end() |
| 62 | + |
| 63 | + assert output == b"<html><!--Set Comment--></html>" |
| 64 | + assert comment_processed.is_set() |
| 65 | + |
| 66 | + |
| 67 | +def test_remove(): |
| 68 | + comment_processed: threading.Event = threading.Event() |
| 69 | + |
| 70 | + class DocumentHandler: |
| 71 | + # noinspection PyMethodMayBeStatic |
| 72 | + def comments(self, comment: lolhtml.Comment): |
| 73 | + assert not comment_processed.is_set() |
| 74 | + comment_processed.set() |
| 75 | + comment.remove() |
| 76 | + assert comment.removed |
| 77 | + |
| 78 | + output: bytearray = bytearray() |
| 79 | + rewriter: lolhtml.HTMLRewriter = lolhtml.HTMLRewriter(output.extend) |
| 80 | + rewriter.on_document(DocumentHandler()) |
| 81 | + |
| 82 | + rewriter.write(b"<html><!-- Hello World --></html>") |
| 83 | + rewriter.end() |
| 84 | + |
| 85 | + assert output == b"<html></html>" |
| 86 | + assert comment_processed.is_set() |
| 87 | + |
| 88 | + |
| 89 | +def test_before_text_inferred(): |
| 90 | + comment_processed: threading.Event = threading.Event() |
| 91 | + |
| 92 | + class DocumentHandler: |
| 93 | + def comments(self, comment: lolhtml.Comment): |
| 94 | + assert not comment_processed.is_set() |
| 95 | + comment_processed.set() |
| 96 | + comment.before("<Test>") |
| 97 | + |
| 98 | + output: bytearray = bytearray() |
| 99 | + rewriter: lolhtml.HTMLRewriter = lolhtml.HTMLRewriter(output.extend) |
| 100 | + rewriter.on_document(DocumentHandler()) |
| 101 | + |
| 102 | + rewriter.write(b"<html><!-- Hello World --></html>") |
| 103 | + rewriter.end() |
| 104 | + |
| 105 | + assert output == b"<html><Test><!-- Hello World --></html>" |
| 106 | + assert comment_processed.is_set() |
| 107 | + |
| 108 | + |
| 109 | +def test_before_text_explicit(): |
| 110 | + comment_processed: threading.Event = threading.Event() |
| 111 | + |
| 112 | + class DocumentHandler: |
| 113 | + def comments(self, comment: lolhtml.Comment): |
| 114 | + assert not comment_processed.is_set() |
| 115 | + comment_processed.set() |
| 116 | + comment.before("<Test>", lolhtml.ContentType.TEXT) |
| 117 | + |
| 118 | + output: bytearray = bytearray() |
| 119 | + rewriter: lolhtml.HTMLRewriter = lolhtml.HTMLRewriter(output.extend) |
| 120 | + rewriter.on_document(DocumentHandler()) |
| 121 | + |
| 122 | + rewriter.write(b"<html><!-- Hello World --></html>") |
| 123 | + rewriter.end() |
| 124 | + |
| 125 | + assert output == b"<html><Test><!-- Hello World --></html>" |
| 126 | + assert comment_processed.is_set() |
| 127 | + |
| 128 | + |
| 129 | +def test_before_html(): |
| 130 | + comment_processed: threading.Event = threading.Event() |
| 131 | + |
| 132 | + class DocumentHandler: |
| 133 | + def comments(self, comment: lolhtml.Comment): |
| 134 | + assert not comment_processed.is_set() |
| 135 | + comment_processed.set() |
| 136 | + comment.before("<Test>", lolhtml.ContentType.HTML) |
| 137 | + |
| 138 | + output: bytearray = bytearray() |
| 139 | + rewriter: lolhtml.HTMLRewriter = lolhtml.HTMLRewriter(output.extend) |
| 140 | + rewriter.on_document(DocumentHandler()) |
| 141 | + |
| 142 | + rewriter.write(b"<html><!-- Hello World --></html>") |
| 143 | + rewriter.end() |
| 144 | + |
| 145 | + assert output == b"<html><Test><!-- Hello World --></html>" |
| 146 | + assert comment_processed.is_set() |
| 147 | + |
| 148 | + |
| 149 | +def test_after_text_inferred(): |
| 150 | + comment_processed: threading.Event = threading.Event() |
| 151 | + |
| 152 | + class DocumentHandler: |
| 153 | + def comments(self, comment: lolhtml.Comment): |
| 154 | + assert not comment_processed.is_set() |
| 155 | + comment_processed.set() |
| 156 | + comment.after("<Test>") |
| 157 | + |
| 158 | + output: bytearray = bytearray() |
| 159 | + rewriter: lolhtml.HTMLRewriter = lolhtml.HTMLRewriter(output.extend) |
| 160 | + rewriter.on_document(DocumentHandler()) |
| 161 | + |
| 162 | + rewriter.write(b"<html><!-- Hello World --></html>") |
| 163 | + rewriter.end() |
| 164 | + |
| 165 | + assert output == b"<html><!-- Hello World --><Test></html>" |
| 166 | + assert comment_processed.is_set() |
| 167 | + |
| 168 | + |
| 169 | +def test_after_text_explicit(): |
| 170 | + comment_processed: threading.Event = threading.Event() |
| 171 | + |
| 172 | + class DocumentHandler: |
| 173 | + def comments(self, comment: lolhtml.Comment): |
| 174 | + assert not comment_processed.is_set() |
| 175 | + comment_processed.set() |
| 176 | + comment.after("<Test>", lolhtml.ContentType.TEXT) |
| 177 | + |
| 178 | + output: bytearray = bytearray() |
| 179 | + rewriter: lolhtml.HTMLRewriter = lolhtml.HTMLRewriter(output.extend) |
| 180 | + rewriter.on_document(DocumentHandler()) |
| 181 | + |
| 182 | + rewriter.write(b"<html><!-- Hello World --></html>") |
| 183 | + rewriter.end() |
| 184 | + |
| 185 | + assert output == b"<html><!-- Hello World --><Test></html>" |
| 186 | + assert comment_processed.is_set() |
| 187 | + |
| 188 | + |
| 189 | +def test_after_html(): |
| 190 | + comment_processed: threading.Event = threading.Event() |
| 191 | + |
| 192 | + class DocumentHandler: |
| 193 | + def comments(self, comment: lolhtml.Comment): |
| 194 | + assert not comment_processed.is_set() |
| 195 | + comment_processed.set() |
| 196 | + comment.after("<Test>", lolhtml.ContentType.HTML) |
| 197 | + |
| 198 | + output: bytearray = bytearray() |
| 199 | + rewriter: lolhtml.HTMLRewriter = lolhtml.HTMLRewriter(output.extend) |
| 200 | + rewriter.on_document(DocumentHandler()) |
| 201 | + |
| 202 | + rewriter.write(b"<html><!-- Hello World --></html>") |
| 203 | + rewriter.end() |
| 204 | + |
| 205 | + assert output == b"<html><!-- Hello World --><Test></html>" |
| 206 | + assert comment_processed.is_set() |
0 commit comments