MCPcopy Create free account
hub / github.com/gawel/pyquery / replace_with

Method replace_with

pyquery/pyquery.py:1411–1436  ·  view source on GitHub ↗

replace nodes by value: >>> doc = PyQuery(" ") >>> node = PyQuery(" ") >>> child = doc.find('div') >>> child.replace_with(node) [ ] >>> print(doc)

(self, value)

Source from the content-addressed store, hash-verified

1409
1410 @with_camel_case_alias
1411 def replace_with(self, value):
1412 """replace nodes by value:
1413
1414 >>> doc = PyQuery("<html><div /></html>")
1415 >>> node = PyQuery("<span />")
1416 >>> child = doc.find('div')
1417 >>> child.replace_with(node)
1418 [<div>]
1419 >>> print(doc)
1420 <html><span/></html>
1421
1422 """
1423 if isinstance(value, PyQuery):
1424 value = str(value)
1425 if hasattr(value, '__call__'):
1426 for i, element in enumerate(self):
1427 self._copy(element).before(
1428 value(i, element) + (element.tail or ''))
1429 parent = element.getparent()
1430 parent.remove(element)
1431 else:
1432 for tag in self:
1433 self._copy(tag).before(value + (tag.tail or ''))
1434 parent = tag.getparent()
1435 parent.remove(tag)
1436 return self
1437
1438 @with_camel_case_alias
1439 def replace_all(self, expr):

Callers 3

replace_allMethod · 0.80
test_replaceWithMethod · 0.80

Calls 3

_copyMethod · 0.95
beforeMethod · 0.80
removeMethod · 0.80

Tested by 2

test_replaceWithMethod · 0.64