Skip to content

Commit 7f9d52e

Browse files
committed
Add a drop_node method to Parser class
1 parent 3e9f156 commit 7f9d52e

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

goose/parsers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929

3030
class Parser(object):
3131

32+
@classmethod
33+
def drop_tag(self, nodes):
34+
for node in nodes:
35+
node.drop_tag()
36+
3237
@classmethod
3338
def css_select(self, node, selector):
3439
return node.cssselect(selector)

tests/tests.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,35 @@ def test_replacetag(self):
123123
centers = Parser.getElementsByTag(div, tag='center')
124124
self.assertEqual(len(centers), 1)
125125

126+
def test_droptag(self):
127+
# test with 1 node
128+
html = '<div>Hello <b>World!</b></div>'
129+
expecte_html = '<div>Hello World!</div>'
130+
doc = Parser.fromstring(html)
131+
nodes = Parser.css_select(doc, "b")
132+
self.assertEqual(len(nodes), 1)
133+
Parser.drop_tag(nodes)
134+
135+
nodes = Parser.css_select(doc, "b")
136+
self.assertEqual(len(nodes), 0)
137+
138+
result_html = Parser.nodeToString(doc)
139+
self.assertEqual(expecte_html, result_html)
140+
141+
# test with 2 nodes
142+
html = '<div>Hello <b>World!</b> bla <b>World!</b></div>'
143+
expecte_html = '<div>Hello World! bla World!</div>'
144+
doc = Parser.fromstring(html)
145+
nodes = Parser.css_select(doc, "b")
146+
self.assertEqual(len(nodes), 2)
147+
Parser.drop_tag(nodes)
148+
149+
nodes = Parser.css_select(doc, "b")
150+
self.assertEqual(len(nodes), 0)
151+
152+
result_html = Parser.nodeToString(doc)
153+
self.assertEqual(expecte_html, result_html)
154+
126155
def test_tostring(self):
127156
html = '<html><body>'
128157
html += '<p>this is a test <a>link</a> and this is <strong>strong</strong></p>'

0 commit comments

Comments
 (0)