Skip to content

Commit 6521ee4

Browse files
committed
dom operate
1 parent e29cf40 commit 6521ee4

File tree

4 files changed

+210
-1
lines changed

4 files changed

+210
-1
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ $dom = $parser->parse($html);
6969

7070
## Query Method
7171
```php
72-
7372
# parse dom
7473
$dom = $parser->parse($html);
7574

@@ -91,4 +90,29 @@ $node = $dom->genIdQuery('genid-1588151808')->parentQuery();
9190
$node = $dom->genIdQuery('genid-1588151808')->childsQuery();
9291
# siblingsQuery
9392
$node = $dom->genIdQuery('genid-1588151808')->siblingsQuery();
93+
```
94+
95+
## Dom Operate
96+
```php
97+
98+
$dom = $parser->setConfig([
99+
])->parse($html);
100+
101+
echo '<pre>';
102+
103+
$node = $dom->idQuery('main-view')->addClass('hello-world');
104+
$node = $dom->idQuery('main-view')->removeClass('hello-world');
105+
$node = $dom->idQuery('main-view')->resetClass();
106+
107+
$node = $dom->nameQuery('nav')->addStyle('border', '10px solid red');
108+
$node = $dom->nameQuery('nav')->addMultStyle(['border' => '10px solid red', 'color' => 'blue']);
109+
$node = $dom->nameQuery('nav')->removeStyle('border');
110+
$node = $dom->nameQuery('nav')->resetStyle();
111+
112+
$node = $dom->nameQuery('nav')->addAttr(':click', "alter('hello world')");
113+
$node = $dom->nameQuery('nav')->addMultAttr([':click' => "alter('hello world')", 'v-model' => 'testvari']);
114+
$node = $dom->nameQuery('nav')->removeAttr(':click');
115+
$node = $dom->nameQuery('nav')->resetAttr();
116+
117+
$node = $dom->nameQuery('nav')->resetAllAttr();
94118
```

examples/dom_operate.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
require_once '../vendor/autoload.php';
4+
5+
$parser = new \Faitheir\DomParser\DomParser();
6+
7+
$html = <<< 'HTML'
8+
<template data-genid="genid-1588151809" >
9+
<!-- doc hello
10+
world
11+
-->
12+
<view id="main-view" data-genid="12323" class="container" >
13+
<navigator name="nav" data-genid="genid-1588151802" style="margin-left: 1px; margin-right: 10px; color: red; float: left; " url="/pages/show?id=1" >
14+
<image name="image" data-genid="genid-1588151801" class="hello world" src="../../static/images/home-by.jpg" mode="widthFix" >
15+
</image>
16+
</navigator>
17+
<view data-genid="genid-1588151808" class="layout" >
18+
<view name="scan" data-genid="genid-1588151804" class="scan" >
19+
<text name="hello" data-genid="genid-1588151803" class="iconfont icon-saoyisao" style="float: left; border: 1px solid #000; " @click="scan()" >
20+
</text>
21+
</view>
22+
<search-box data-genid="genid-1588151805" class="search" :lable="search_lable" :value="serch" />
23+
<view data-genid="genid-1588151807" class="view-box" >
24+
Hello World !
25+
</view>
26+
</view>
27+
</view>
28+
</template>
29+
HTML;
30+
31+
$dom = $parser->setConfig([
32+
])->parse($html);
33+
34+
echo '<pre>';
35+
36+
$node = $dom->idQuery('main-view')->addClass('hello-world');
37+
$node = $dom->idQuery('main-view')->removeClass('hello-world');
38+
$node = $dom->idQuery('main-view')->resetClass();
39+
40+
$node = $dom->nameQuery('nav')->addStyle('border', '10px solid red');
41+
$node = $dom->nameQuery('nav')->addMultStyle(['border' => '10px solid red', 'color' => 'blue']);
42+
$node = $dom->nameQuery('nav')->removeStyle('border');
43+
$node = $dom->nameQuery('nav')->resetStyle();
44+
45+
$node = $dom->nameQuery('nav')->addAttr(':click', "alter('hello world')");
46+
$node = $dom->nameQuery('nav')->addMultAttr([':click' => "alter('hello world')", 'v-model' => 'testvari']);
47+
$node = $dom->nameQuery('nav')->removeAttr(':click');
48+
$node = $dom->nameQuery('nav')->resetAttr();
49+
50+
$node = $dom->nameQuery('nav')->resetAllAttr();
51+
52+
53+
54+
print_r($node);

src/DomParser/Node.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class Node
3131
# node content
3232
public $content = '';
3333

34+
#
3435
use NodeQuery;
36+
use NodeOperate;
3537

3638
/**
3739
* Node constructor.

src/DomParser/NodeOperate.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
namespace Faitheir\DomParser;
3+
4+
/**
5+
* the methods of node edit .
6+
*/
7+
trait NodeOperate
8+
{
9+
#
10+
# class
11+
#
12+
public function addClass($class)
13+
{
14+
$classArr = empty($this->domAttrs['class']) ? [] : $this->domAttrs['class'];
15+
if (!in_array($class, $classArr)) {
16+
$classArr[] = $class;
17+
$this->domAttrs['class'] = $classArr;
18+
}
19+
return $this;
20+
}
21+
public function removeClass($class)
22+
{
23+
if (empty($this->domAttrs['class']))
24+
return $this;
25+
26+
$key = array_search($class, $this->domAttrs['class']);
27+
if ($key !== false) {
28+
unset($this->domAttrs['class'][$key]);
29+
if (empty($this->domAttrs['class']))
30+
unset($this->domAttrs['class']);
31+
}
32+
return $this;
33+
}
34+
public function resetClass()
35+
{
36+
if (empty($this->domAttrs['class']))
37+
return $this;
38+
39+
unset($this->domAttrs['class']);
40+
return $this;
41+
}
42+
43+
#
44+
# style
45+
#
46+
public function addStyle($styleKey, $styleValue)
47+
{
48+
if (empty($styleKey) || empty($styleValue))
49+
return $this;
50+
51+
$this->domAttrs['style'][$styleKey] = $styleValue;
52+
return $this;
53+
}
54+
public function addMultStyle($styles = [])
55+
{
56+
if (empty($styles))
57+
return $this;
58+
59+
$curStyle = empty($this->domAttrs['style']) ? [] : $this->domAttrs['style'];
60+
$this->domAttrs['style'] = array_merge($curStyle, $styles);
61+
return $this;
62+
}
63+
public function removeStyle($styleKey)
64+
{
65+
if (empty($this->domAttrs['style'][$styleKey]))
66+
return $this;
67+
68+
unset($this->domAttrs['style'][$styleKey]);
69+
return $this;
70+
}
71+
public function resetStyle()
72+
{
73+
if (empty($this->domAttrs['style']))
74+
return $this;
75+
76+
unset($this->domAttrs['style']);
77+
return $this;
78+
}
79+
80+
#
81+
# normal attrs
82+
#
83+
public function addAttr($attrKey, $attrValue)
84+
{
85+
if (empty($attrKey) || empty($attrValue))
86+
return $this;
87+
88+
$this->domAttrs[$attrKey] = $attrValue;
89+
return $this;
90+
}
91+
public function addMultAttr($attrs = [])
92+
{
93+
if (empty($attrs))
94+
return $this;
95+
96+
$curAttrs = empty($this->domAttrs) ? [] : $this->domAttrs;
97+
$this->domAttrs = array_merge($curAttrs, $attrs);
98+
return $this;
99+
}
100+
public function removeAttr($attrKey)
101+
{
102+
if (empty($this->domAttrs[$attrKey]))
103+
return $this;
104+
105+
unset($this->domAttrs[$attrKey]);
106+
return $this;
107+
}
108+
public function resetAttr()
109+
{
110+
$attrs = [];
111+
if (!empty($this->domAttrs['class']))
112+
$attrs['class'] = $this->domAttrs['class'];
113+
if (!empty($this->domAttrs['style']))
114+
$attrs['style'] = $this->domAttrs['style'];
115+
116+
$this->domAttrs = $attrs;
117+
return $this;
118+
}
119+
120+
#
121+
# other
122+
#
123+
public function resetAllAttr()
124+
{
125+
$this->domAttrs = [];
126+
return $this;
127+
}
128+
129+
}

0 commit comments

Comments
 (0)