MCPcopy Create free account
hub / github.com/clips/pattern / _smartPop

Method _smartPop

pattern/web/soup/BeautifulSoup.py:1284–1328  ·  view source on GitHub ↗

We need to pop up to the previous tag of this type, unless one of this tag's nesting reset triggers comes between this tag and the previous tag of this type, OR unless this tag is a generic nesting trigger and another generic nesting trigger comes between this tag and

(self, name)

Source from the content-addressed store, hash-verified

1282 return mostRecentTag
1283
1284 def _smartPop(self, name):
1285
1286 """We need to pop up to the previous tag of this type, unless
1287 one of this tag's nesting reset triggers comes between this
1288 tag and the previous tag of this type, OR unless this tag is a
1289 generic nesting trigger and another generic nesting trigger
1290 comes between this tag and the previous tag of this type.
1291
1292 Examples:
1293 <p>Foo<b>Bar *<p>* should pop to 'p', not 'b'.
1294 <p>Foo<table>Bar *<p>* should pop to 'table', not 'p'.
1295 <p>Foo<table><tr>Bar *<p>* should pop to 'tr', not 'p'.
1296
1297 <li><ul><li> *<li>* should pop to 'ul', not the first 'li'.
1298 <tr><table><tr> *<tr>* should pop to 'table', not the first 'tr'
1299 <td><tr><td> *<td>* should pop to 'tr', not the first 'td'
1300 """
1301
1302 nestingResetTriggers = self.NESTABLE_TAGS.get(name)
1303 isNestable = nestingResetTriggers != None
1304 isResetNesting = self.RESET_NESTING_TAGS.has_key(name)
1305 popTo = None
1306 inclusive = True
1307 for i in range(len(self.tagStack)-1, 0, -1):
1308 p = self.tagStack[i]
1309 if (not p or p.name == name) and not isNestable:
1310 #Non-nestable tags get popped to the top or to their
1311 #last occurance.
1312 popTo = name
1313 break
1314 if (nestingResetTriggers is not None
1315 and p.name in nestingResetTriggers) \
1316 or (nestingResetTriggers is None and isResetNesting
1317 and self.RESET_NESTING_TAGS.has_key(p.name)):
1318
1319 #If we encounter one of the nesting reset triggers
1320 #peculiar to this tag, or we encounter another tag
1321 #that causes nesting to reset, pop up to but not
1322 #including that tag.
1323 popTo = p.name
1324 inclusive = False
1325 break
1326 p = p.parent
1327 if popTo:
1328 self._popToTag(popTo, inclusive)
1329
1330 def unknown_starttag(self, name, attrs, selfClosing=0):
1331 #print "Start tag %s: %s" % (name, attrs)

Callers 1

unknown_starttagMethod · 0.95

Calls 4

_popToTagMethod · 0.95
lenFunction · 0.85
getMethod · 0.45
has_keyMethod · 0.45

Tested by

no test coverage detected