MCPcopy Index your code
hub / github.com/clips/pattern / testTagReplacement

Method testTagReplacement

pattern/web/soup/BeautifulSoupTests.py:335–407  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

333 self.assertEqual(soup.b['class'], 'bar')
334
335 def testTagReplacement(self):
336 # Make sure you can replace an element with itself.
337 text = "<a><b></b><c>Foo<d></d></c></a><a><e></e></a>"
338 soup = BeautifulSoup(text)
339 c = soup.c
340 soup.c.replaceWith(c)
341 self.assertEquals(str(soup), text)
342
343 # A very simple case
344 soup = BeautifulSoup("<b>Argh!</b>")
345 soup.find(text="Argh!").replaceWith("Hooray!")
346 newText = soup.find(text="Hooray!")
347 b = soup.b
348 self.assertEqual(newText.previous, b)
349 self.assertEqual(newText.parent, b)
350 self.assertEqual(newText.previous.next, newText)
351 self.assertEqual(newText.next, None)
352
353 # A more complex case
354 soup = BeautifulSoup("<a><b>Argh!</b><c></c><d></d></a>")
355 soup.b.insert(1, "Hooray!")
356 newText = soup.find(text="Hooray!")
357 self.assertEqual(newText.previous, "Argh!")
358 self.assertEqual(newText.previous.next, newText)
359
360 self.assertEqual(newText.previousSibling, "Argh!")
361 self.assertEqual(newText.previousSibling.nextSibling, newText)
362
363 self.assertEqual(newText.nextSibling, None)
364 self.assertEqual(newText.next, soup.c)
365
366 text = "<html>There's <b>no</b> business like <b>show</b> business</html>"
367 soup = BeautifulSoup(text)
368 no, show = soup.findAll('b')
369 show.replaceWith(no)
370 self.assertEquals(str(soup), "<html>There's business like <b>no</b> business</html>")
371
372 # Even more complex
373 soup = BeautifulSoup("<a><b>Find</b><c>lady!</c><d></d></a>")
374 tag = Tag(soup, 'magictag')
375 tag.insert(0, "the")
376 soup.a.insert(1, tag)
377
378 b = soup.b
379 c = soup.c
380 theText = tag.find(text=True)
381 findText = b.find(text="Find")
382
383 self.assertEqual(findText.next, tag)
384 self.assertEqual(tag.previous, findText)
385 self.assertEqual(b.nextSibling, tag)
386 self.assertEqual(tag.previousSibling, b)
387 self.assertEqual(tag.nextSibling, c)
388 self.assertEqual(c.previousSibling, tag)
389
390 self.assertEqual(theText.next, c)
391 self.assertEqual(c.previous, theText)
392

Callers

nothing calls this directly

Calls 7

findMethod · 0.95
BeautifulSoupClass · 0.90
strFunction · 0.85
TagClass · 0.85
replaceWithMethod · 0.80
findAllMethod · 0.80
insertMethod · 0.45

Tested by

no test coverage detected