Skip to content

Commit b488388

Browse files
committed
Add 'body' and 'head' properties on document
1 parent 2286a2d commit b488388

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ Short Doc
3838
---------
3939

4040

41-
The AdvancedHTMLParser can read in a file (or string) of HTML, and will create a modifiable DOM tree from it. It can also be constructed manually from AdvancedHTMLParser.AdvancedTag objects.
41+
**AdvancedHTMLParser**
4242

4343
Think of this like "document" in a browser.
4444

4545

46+
The AdvancedHTMLParser can read in a file (or string) of HTML, and will create a modifiable DOM tree from it. It can also be constructed manually from AdvancedHTMLParser.AdvancedTag objects.
47+
48+
4649
To populate an AdvancedHTMLParser from existing HTML:
4750

4851
parser = AdvancedHTMLParser.AdvancedHTMLParser()
@@ -83,6 +86,14 @@ The parser then exposes many "standard" functions as you'd find on the web for a
8386
The results of all of these getElement\* functions are TagCollection objects. These objects can be modified, and will be reflected in the parent DOM.
8487

8588

89+
The parser also contains some expected properties, like
90+
91+
92+
head - The "head" tag associated with this document, or None
93+
94+
body - The "body" tag associated with this document, or None
95+
96+
8697
**General Attributes**
8798

8899
In general, attributes can be accessed with dot-syntax, i.e.

README.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ Various examples can be found in the "tests" directory.
3535
Short Doc
3636
---------
3737

38-
The AdvancedHTMLParser can read in a file (or string) of HTML, and will create a modifiable DOM tree from it. It can also be constructed manually from AdvancedHTMLParser.AdvancedTag objects.
38+
39+
**AdvancedHTMLParser**
40+
3941

4042
Think of this like "document" in a browser.
4143

44+
The AdvancedHTMLParser can read in a file (or string) of HTML, and will create a modifiable DOM tree from it. It can also be constructed manually from AdvancedHTMLParser.AdvancedTag objects.
45+
4246

4347
To populate an AdvancedHTMLParser from existing HTML:
4448

@@ -84,6 +88,14 @@ The parser then exposes many "standard" functions as you'd find on the web for a
8488
The results of all of these getElement\* functions are TagCollection objects. These objects can be modified, and will be reflected in the parent DOM.
8589

8690

91+
The parser also contains some expected properties, like
92+
93+
94+
head - The "head" tag associated with this document, or None
95+
96+
body - The "body" tag associated with this document, or None
97+
98+
8799
**General Attributes**
88100

89101
In general, attributes can be accessed with dot-syntax, i.e.

tests/AdvancedHTMLParserTests/test_General.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ def test_getBody(self):
187187
assert bodyEm , 'Expected ".body" property to fetch an element'
188188
assert bodyEm.tagName == 'body' , 'Got wrong body element'
189189

190+
parser = AdvancedHTMLParser.AdvancedHTMLParser()
191+
parser.parseStr('<div id="hello"> <span> One </span> </div>')
192+
193+
bodyEm = parser.body
194+
195+
assert not bodyEm , 'Expected to find no "body" when no <body> tag is present.'
196+
197+
parser = AdvancedHTMLParser.AdvancedHTMLParser()
198+
parser.parseStr('<div id="hello"> <span> One </span> </div> <div id="other">Cheese</div>')
199+
200+
bodyEm = parser.body
201+
202+
assert not bodyEm , 'Expected to find no "body" when no <body> tag is present.'
203+
204+
190205
def test_getHead(self):
191206
parser = AdvancedHTMLParser.AdvancedHTMLParser()
192207
parser.parseStr(self.testHTML)
@@ -196,6 +211,19 @@ def test_getHead(self):
196211
assert headEm , 'Expected ".head" property to fetch an element'
197212
assert headEm.tagName == 'head' , 'Got wrong head element'
198213

214+
parser = AdvancedHTMLParser.AdvancedHTMLParser()
215+
parser.parseStr('<div id="hello"> <span> One </span> </div>')
216+
217+
headEm = parser.head
218+
219+
assert not headEm , 'Expected to find no "head" when no <head> tag is present.'
220+
221+
parser = AdvancedHTMLParser.AdvancedHTMLParser()
222+
parser.parseStr('<div id="hello"> <span> One </span> </div> <div id="other">Cheese</div>')
223+
224+
headEm = parser.head
225+
226+
assert not headEm , 'Expected to find no "head" when no <head> tag is present.'
199227

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

0 commit comments

Comments
 (0)