Skip to content

Commit 2286a2d

Browse files
committed
Add 'body' and 'head' properties to AdvancedHTMLParser
1 parent 710ec3e commit 2286a2d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

AdvancedHTMLParser/Parser.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,14 @@ def getFirstElementCustomFilter(self, filterFunc, root='root'):
419419

420420
return None
421421

422+
@property
423+
def body(self):
424+
return self.getFirstElementCustomFilter(lambda em : em.tagName == 'body')
425+
426+
@property
427+
def head(self):
428+
return self.getFirstElementCustomFilter(lambda em : em.tagName == 'head')
429+
422430
def contains(self, em):
423431
'''
424432
Checks if #em is found anywhere within this element tree

tests/AdvancedHTMLParserTests/test_General.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,24 @@ def test_escapeQuotes(self):
178178

179179
assert 'onclick="alert("hi");"' in tag.outerHTML , 'Expected to escape quotes in attribute. Got: %s' %(tag.outerHTML, )
180180

181+
def test_getBody(self):
182+
parser = AdvancedHTMLParser.AdvancedHTMLParser()
183+
parser.parseStr(self.testHTML)
184+
185+
bodyEm = parser.body
186+
187+
assert bodyEm , 'Expected ".body" property to fetch an element'
188+
assert bodyEm.tagName == 'body' , 'Got wrong body element'
189+
190+
def test_getHead(self):
191+
parser = AdvancedHTMLParser.AdvancedHTMLParser()
192+
parser.parseStr(self.testHTML)
193+
194+
headEm = parser.head
195+
196+
assert headEm , 'Expected ".head" property to fetch an element'
197+
assert headEm.tagName == 'head' , 'Got wrong head element'
198+
181199

182200
if __name__ == '__main__':
183201
subprocess.Popen('GoodTests.py "%s"' %(sys.argv[0],), shell=True).wait()

0 commit comments

Comments
 (0)