1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 15.04.16 at 12:47 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonframework\html2less; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Internal HTML DOM node tree. |
10
|
|
|
* |
11
|
|
|
* @author Vitaly Egorov <[email protected]> |
12
|
|
|
* @copyright 2016 SamsonOS |
13
|
|
|
*/ |
14
|
|
|
class HTMLDOMNode |
15
|
|
|
{ |
16
|
|
|
/** @var string HTML tag name */ |
17
|
|
|
public $tag; |
18
|
|
|
/** @var string HTML identifier */ |
19
|
|
|
public $identifier; |
20
|
|
|
/** @var string[] CSS class names collection */ |
21
|
|
|
public $class = array(); |
22
|
|
|
/** @var string HTML name attribute value */ |
23
|
|
|
public $name; |
24
|
|
|
/** @var string Selector for node */ |
25
|
|
|
public $selector; |
26
|
|
|
/** @var \DOMNode pointer to DOM node */ |
27
|
|
|
public $dom; |
28
|
|
|
/** @var $this Pointer to parent node */ |
29
|
|
|
public $parent; |
30
|
|
|
/** @var $this [] Collection of nested nodes */ |
31
|
|
|
public $children = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create LESS Node |
35
|
|
|
* |
36
|
|
|
* @param \DOMNode $node Pointer to DOM node |
37
|
|
|
* @param $this $parent Pointer to parent node |
38
|
|
|
* @param string $selector Forced LESS node selector |
39
|
|
|
*/ |
40
|
|
|
public function __construct(\DOMNode &$node, self &$parent = null, $selector = null) |
41
|
|
|
{ |
42
|
|
|
// Store pointer to DOM node |
43
|
|
|
$this->dom = &$node; |
44
|
|
|
// Store html tag |
45
|
|
|
$this->tag = $node->nodeName; |
46
|
|
|
// Pointer to parent LESS Node |
47
|
|
|
$this->parent = &$parent; |
48
|
|
|
|
49
|
|
|
// Fill all available node parameters |
50
|
|
|
if (null !== $node->attributes) { |
51
|
|
|
/**@var \DOMNode $attribute */ |
52
|
|
|
foreach ($node->attributes as $attribute) { |
53
|
|
|
$value = trim($attribute->nodeValue); |
54
|
|
|
if ($value !== '') { |
55
|
|
|
if ($attribute->name === 'class') { |
56
|
|
|
$this->class = explode(' ', $value); |
57
|
|
|
} elseif ($attribute->name === 'identifier') { |
58
|
|
|
$this->identifier = $value; |
59
|
|
|
} elseif ($attribute->name === 'name') { |
60
|
|
|
$this->name = $value; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->selector = $selector; |
67
|
|
|
if (null !== $selector) { |
68
|
|
|
// Choose default LESS selector for node |
69
|
|
|
$this->selector = $this->tag; |
70
|
|
|
// If we have class attribute |
71
|
|
|
if (count($this->class)) { |
72
|
|
|
// Use the first class by default |
73
|
|
|
$this->selector = '.' . $this->class[0]; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string Object string representation |
80
|
|
|
*/ |
81
|
|
|
public function __toString() |
82
|
|
|
{ |
83
|
|
|
return $this->tag . '[' . $this->selector . ']'; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|