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

Method wrap_all

pyquery/pyquery.py:1362–1408  ·  view source on GitHub ↗

Wrap all the elements in the matched set into a single wrapper element:: >>> d = PyQuery(' Hey you ! ') >>> print(d('span').wrap_all(' ')) Hey you !

(self, value)

Source from the content-addressed store, hash-verified

1360
1361 @with_camel_case_alias
1362 def wrap_all(self, value):
1363 """Wrap all the elements in the matched set into a single wrapper
1364 element::
1365
1366 >>> d = PyQuery('<div><span>Hey</span><span>you !</span></div>')
1367 >>> print(d('span').wrap_all('<div id="wrapper"></div>'))
1368 <div id="wrapper"><span>Hey</span><span>you !</span></div>
1369
1370 >>> d = PyQuery('<div><span>Hey</span><span>you !</span></div>')
1371 >>> print(d('span').wrapAll('<div id="wrapper"></div>'))
1372 <div id="wrapper"><span>Hey</span><span>you !</span></div>
1373
1374 ..
1375 """
1376 if not self:
1377 return self
1378
1379 assert isinstance(value, basestring)
1380 value = fromstring(value)[0]
1381 wrapper = deepcopy(value)
1382 if not wrapper.getchildren():
1383 child = wrapper
1384 else:
1385 childs = [c for c in wrapper.iterchildren()]
1386 child = childs[-1]
1387
1388 replace_childs = True
1389 parent = self[0].getparent()
1390 if parent is None:
1391 parent = no_default
1392
1393 # add nodes to wrapper and check parent
1394 for tag in self:
1395 child.append(deepcopy(tag))
1396 if tag.getparent() is not parent:
1397 replace_childs = False
1398
1399 # replace nodes i parent if possible
1400 if parent is not no_default and replace_childs:
1401 childs = [c for c in parent.iterchildren()]
1402 if len(childs) == len(self):
1403 for tag in self:
1404 parent.remove(tag)
1405 parent.append(wrapper)
1406
1407 self[:] = [wrapper]
1408 return self
1409
1410 @with_camel_case_alias
1411 def replace_with(self, value):

Callers

nothing calls this directly

Calls 3

fromstringFunction · 0.85
appendMethod · 0.80
removeMethod · 0.80

Tested by

no test coverage detected