Skip to content

Commit 49fb0b0

Browse files
committed
Added some magic (methods) and tests
1 parent 9fc1828 commit 49fb0b0

File tree

6 files changed

+167
-13
lines changed

6 files changed

+167
-13
lines changed

src/PHPHtmlParser/Dom/HtmlNode.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ class HtmlNode extends Node {
66
/**
77
* Sets up the tag of this node.
88
*/
9-
public function __construct(Tag $tag)
9+
public function __construct($tag)
1010
{
11+
if ( ! $tag instanceof Tag)
12+
{
13+
$tag = new Tag($tag);
14+
}
1115
$this->tag = $tag;
1216
parent::__construct();
1317
}

src/PHPHtmlParser/Dom/Node.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public function __construct()
5757
public function __get($key)
5858
{
5959
// check attribute first
60-
if ( ! is_null($this->tag->getAttribute($key)))
60+
if ( ! is_null($this->getAttribute($key)))
6161
{
62-
return $this->tag->getAttribute($key);
62+
return $this->getAttribute($key);
6363
}
6464
switch (strtolower($key))
6565
{
@@ -367,7 +367,12 @@ public function getTag()
367367
*/
368368
public function getAttributes()
369369
{
370-
return $this->tag->getAttributes();
370+
$attributes = $this->tag->getAttributes();
371+
foreach ($attributes as $name => $info)
372+
{
373+
$attributes[$name] = $info['value'];
374+
}
375+
return $attributes;
371376
}
372377

373378
/**
@@ -379,7 +384,13 @@ public function getAttributes()
379384
*/
380385
public function getAttribute($key)
381386
{
382-
return $this->tag->getAttribute($key);
387+
$attribute = $this->tag->getAttribute($key);
388+
if ( ! is_null($attribute))
389+
{
390+
$attribute = $attribute['value'];
391+
}
392+
393+
return $attribute;
383394
}
384395

385396
/**

src/PHPHtmlParser/Dom/Tag.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function __construct($name)
3434
$this->name = $name;
3535
}
3636

37+
public function __get($key)
38+
{
39+
return $this->getAttribute($key);
40+
}
41+
3742
/**
3843
* Returns the name of this tag.
3944
*

tests/Node/HtmlTest.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,32 @@ public function testInnerHtml()
4141
$this->assertEquals("<a href='http://google.com'>link</a><br />", $parent->innerHtml());
4242
}
4343

44+
public function testInnerHtmlMagic()
45+
{
46+
$parent = new HtmlNode('div');
47+
$parent->getTag()->setAttributes([
48+
'class' => [
49+
'value' => 'all',
50+
'doubleQuote' => true,
51+
],
52+
]);
53+
$childa = new HtmlNode('a');
54+
$childa->getTag()->setAttributes([
55+
'href' => [
56+
'value' => 'http://google.com',
57+
'doubleQuote' => false,
58+
],
59+
]);
60+
$childbr = new HtmlNode('br');
61+
$childbr->getTag()->selfClosing();
62+
63+
$parent->addChild($childa);
64+
$parent->addChild($childbr);
65+
$childa->addChild(new TextNode('link'));
66+
67+
$this->assertEquals("<a href='http://google.com'>link</a><br />", $parent->innerHtml);
68+
}
69+
4470
public function testOuterHtml()
4571
{
4672
$div = new Tag('div');
@@ -84,6 +110,32 @@ public function testOuterHtmlEmpty()
84110
$this->assertEquals("<a href='http://google.com'></a>", $node->OuterHtml());
85111
}
86112

113+
public function testOuterHtmlMagic()
114+
{
115+
$parent = new HtmlNode('div');
116+
$parent->getTag()->setAttributes([
117+
'class' => [
118+
'value' => 'all',
119+
'doubleQuote' => true,
120+
],
121+
]);
122+
$childa = new HtmlNode('a');
123+
$childa->getTag()->setAttributes([
124+
'href' => [
125+
'value' => 'http://google.com',
126+
'doubleQuote' => false,
127+
],
128+
]);
129+
$childbr = new HtmlNode('br');
130+
$childbr->getTag()->selfClosing();
131+
132+
$parent->addChild($childa);
133+
$parent->addChild($childbr);
134+
$childa->addChild(new TextNode('link'));
135+
136+
$this->assertEquals('<div class="all"><a href=\'http://google.com\'>link</a><br /></div>', $parent->outerHtml);
137+
}
138+
87139
public function testText()
88140
{
89141
$a = new Tag('a');
@@ -100,4 +152,63 @@ public function testTextNone()
100152

101153
$this->assertEmpty($node->text());
102154
}
155+
156+
public function testTextMagic()
157+
{
158+
$node = new HtmlNode('a');
159+
$node->addChild(new TextNode('link'));
160+
161+
$this->assertEquals('link', $node->text);
162+
}
163+
164+
public function testGetAttribute()
165+
{
166+
$node = new HtmlNode('a');
167+
$node->getTag()->setAttributes([
168+
'href' => [
169+
'value' => 'http://google.com',
170+
'doubleQuote' => false,
171+
],
172+
'class' => [
173+
'value' => 'outerlink rounded',
174+
'doubleQuote' => true,
175+
],
176+
]);
177+
178+
$this->assertEquals('outerlink rounded', $node->getAttribute('class'));
179+
}
180+
181+
public function testGetAttributeMagic()
182+
{
183+
$node = new HtmlNode('a');
184+
$node->getTag()->setAttributes([
185+
'href' => [
186+
'value' => 'http://google.com',
187+
'doubleQuote' => false,
188+
],
189+
'class' => [
190+
'value' => 'outerlink rounded',
191+
'doubleQuote' => true,
192+
],
193+
]);
194+
195+
$this->assertEquals('http://google.com', $node->href);
196+
}
197+
198+
public function testGetAttributes()
199+
{
200+
$node = new HtmlNode('a');
201+
$node->getTag()->setAttributes([
202+
'href' => [
203+
'value' => 'http://google.com',
204+
'doubleQuote' => false,
205+
],
206+
'class' => [
207+
'value' => 'outerlink rounded',
208+
'doubleQuote' => true,
209+
],
210+
]);
211+
212+
$this->assertEquals('outerlink rounded', $node->getAttributes()['class']);
213+
}
103214
}

tests/Node/TagTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ public function testSetAttributes()
3131
$this->assertEquals('http://google.com', $tag->getAttribute('href')['value']);
3232
}
3333

34+
public function testGetAttributeMagic()
35+
{
36+
$attr = [
37+
'href' => [
38+
'value' => 'http://google.com',
39+
'doublequote' => false,
40+
],
41+
];
42+
43+
$tag = new Tag('a');
44+
$tag->setAttributes($attr);
45+
$this->assertEquals('http://google.com', $tag->href['value']);
46+
}
47+
3448
public function testMakeOpeningTag()
3549
{
3650
$attr = [

tests/SelectorTest.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public function testParseSelectorStringNoKey()
4242

4343
public function testFind()
4444
{
45-
$root = new HtmlNode(new Tag('root'));
46-
$parent = new HtmlNode(new Tag('div'));
47-
$child1 = new HtmlNode(new Tag('a'));
48-
$child2 = new HtmlNode(new Tag('p'));
45+
$root = new HtmlNode('root');
46+
$parent = new HtmlNode('div');
47+
$child1 = new HtmlNode('a');
48+
$child2 = new HtmlNode('p');
4949
$parent->addChild($child1);
5050
$parent->addChild($child2);
5151
$root->addChild($parent);
@@ -60,7 +60,10 @@ public function testFindId()
6060
$child1 = new HtmlNode(new Tag('a'));
6161
$child2 = new HtmlNode(new Tag('p'));
6262
$child2->getTag()->setAttributes([
63-
'id' => 'content',
63+
'id' => [
64+
'value' => 'content',
65+
'doubleQuote' => true,
66+
],
6467
]);
6568
$parent->addChild($child1);
6669
$parent->addChild($child2);
@@ -74,9 +77,12 @@ public function testFindClass()
7477
$parent = new HtmlNode(new Tag('div'));
7578
$child1 = new HtmlNode(new Tag('a'));
7679
$child2 = new HtmlNode(new Tag('p'));
77-
$child3 = new HtmlNode(new Tag('a'));
80+
$child3 = new HtmlNode('a');
7881
$child3->getTag()->setAttributes([
79-
'class' => 'link',
82+
'class' => [
83+
'value' => 'link',
84+
'doubleQuote' => true,
85+
],
8086
]);
8187
$parent->addChild($child1);
8288
$parent->addChild($child2);
@@ -93,7 +99,10 @@ public function testFindClassMultiple()
9399
$child2 = new HtmlNode(new Tag('p'));
94100
$child3 = new HtmlNode(new Tag('a'));
95101
$child3->getTag()->setAttributes([
96-
'class' => 'link outer',
102+
'class' => [
103+
'value' => 'link outer',
104+
'doubleQuote' => false,
105+
],
97106
]);
98107
$parent->addChild($child1);
99108
$parent->addChild($child2);

0 commit comments

Comments
 (0)