Skip to content

Commit

Permalink
improve the parse method and add the inverse parse
Browse files Browse the repository at this point in the history
  • Loading branch information
faitheir committed Apr 24, 2020
1 parent 6a8f8b4 commit eb245de
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 23 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# dom-parser

A PHP library , use to parse normal and special DOM string like vue react . to DOM Tree .
A PHP library , use to parse normal and special DOM string like vue react. to DOM Tree.
By modifying config variables, can parse other HTML-like markup languages string too.

## Installation
composer require faitheir/dom-parser

## Base Usage

#### Parse Method
parse Dom string to DOM Tree (\Faitheir\DomParser\Node $dom).
```php
$html = <<< 'HTML'
<template>
Expand Down Expand Up @@ -37,4 +39,29 @@ $parser = new \Faitheir\DomParser\DomParser();
$dom = $parser->setConfig([])->parse($html);

print_r($dom);
```

#### Inverse Parse Method
parse Dom Tree(\Faitheir\DomParser\Node $dom) to DOM string.
```php
$string = $parser->setConfig([])->invParse($dom);
or
Config::getInstance()->setConfig([
'tag_indent' => ' '
]);
$string = (string) $dom;
```

## Config Method
```php
# demo
# match string '{{php id="world"}}'

$configs = [
'start_tag_reg' => '/^\s*{{([^}\s\/!]+)/is'
];
# other confs see DomPaser/Config.php

$parser = $parser->setConfig($configs);
$dom = $parser->parse($html);
```
38 changes: 38 additions & 0 deletions examples/inverse_parse_com.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Created by PhpStorm.
* User: WangFei
* Date: 4/24/2020
* Time: 5:37 PM
*/

require_once '../vendor/autoload.php';

$parser = new \Faitheir\DomParser\DomParser();

$html = <<< 'HTML'
<template>
<!-- doc hello
world
-->
<view class="container" id="main-view" data-genid="12323">
<navigator url="/pages/show?id=1">
<image src="../../static/images/home-by.jpg" class="hello world" mode="widthFix"></image>
</navigator>
<view class="layout">
<view class="scan">
<text class="iconfont icon-saoyisao" @click="scan()"></text>
</view>
<search-box class="search" :lable="search_lable" :value="serch" />
<view class="view-box">
Hello World !
</view>
</view>
</view>
</template>
HTML;

$dom = $parser->parse($html);

$string = $parser->invParse($dom);
echo $string;
39 changes: 39 additions & 0 deletions examples/parse_dom_string.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Created by PhpStorm.
* User: WangFei
* Date: 4/24/2020
* Time: 5:34 PM
*/

require_once '../vendor/autoload.php';

$parser = new \Faitheir\DomParser\DomParser();

$html = <<< 'HTML'
<template>
<!-- doc hello
world
-->
<view class="container" id="main-view" data-genid="12323">
<navigator url="/pages/show?id=1">
<image src="../../static/images/home-by.jpg" class="hello world" mode="widthFix"></image>
</navigator>
<view class="layout">
<view class="scan">
<text class="iconfont icon-saoyisao" @click="scan()"></text>
</view>
<search-box class="search" :lable="search_lable" :value="serch" />
<view class="view-box">
Hello World !
</view>
</view>
</view>
</template>
HTML;


echo '<pre>';
$dom = $parser->parse($html);

print_r($dom);
22 changes: 17 additions & 5 deletions src/DomParser/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,37 @@

class Config
{
static private $instance;

private function __clone(){}
# parser configs
private $_config = [
# parse
'start_tag_reg' => '/^\s*<([^>\s\/!]+)/is',
'start_end_tag_reg' => '/(^\s*>)|(^\s*\/>)/is',
'end_tag_reg' => '/^\s*<([^>\s\/]+)/is',
'content_reg' => '/^\s*([^<]+)|(<!--.*-->)/is',
'attrs_reg' => '/^\s*([^=>< ]+)="([^"]*)"|\s([^=><\s]+)(?=\s|>)/iU',
# inverse parse
'tag_indent' => ' ',
'hide_genid' => false,
];

/**
* Config constructor.
* @param array $confs
*/
public function __construct($confs = [])
private function __construct($confs = [])
{
if (!empty($confs))
$this->setConfig($confs);
}
static public function getInstance($confs = [])
{
if (!self::$instance instanceof self) {
self::$instance = new self($confs);
}
if (!empty($confs))
self::$instance->setConfig($confs);

return self::$instance;
}

/**
* dynamic settings
Expand Down
18 changes: 13 additions & 5 deletions src/DomParser/DomParser.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* DomParser Index File
*/
Expand All @@ -12,9 +11,9 @@ class DomParser
private $orgDomString = '';

# config object
public $config;
private $config;
# parser object
public $parser;
private $parser;


/**
Expand All @@ -24,7 +23,7 @@ class DomParser
*/
public function __construct($domString = '', $config = [])
{
$this->config = new Config($config);
$this->config = Config::getInstance($config);
$this->parser = new Parser();

if ($domString)
Expand All @@ -50,7 +49,16 @@ public function parse($domString = '')
if ($domString)
$this->orgDomString = $domString;

return $this->parser->config($this->config)->parse($this->orgDomString);
return $this->parser->parse($this->orgDomString);
}

/**
* inverse method
* @param $domTree
* @return string
*/
public function invParse(Node $domTree)
{
return (string) $domTree;
}
}
73 changes: 73 additions & 0 deletions src/DomParser/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,78 @@ public static function generContentNode($content = '')
return (new self('content'))->setNodeId()->setContent($content);
}

/**
* @return string
*/
public function __toString()
{
# config
$config = Config::getInstance();
$tagIndent = $config->get('tag_indent');
$hideGenid = $config->get('hide_genid');
$tagIndentString = str_repeat($tagIndent, $this->level);

# content
if (in_array($this->nodeName, ['content'])) {
return $tagIndentString . $this->content . PHP_EOL;
}
# root
if (in_array($this->nodeName, ['root'])) {
return $this->_invParseChilds($this->childs);
}
if (empty($this->nodeName))
return '';

# normal
$string = $tagIndentString . '<' . $this->nodeName . ' ';
# id
if (!empty($this->domId))
$string .= ' id="' . $this->domId . '" ';
# gener id
if ($hideGenid == false)
$string .= ' data-genid="' . $this->nodeId . '" ';
# class
if (!empty($this->domAttrs['class'])) {
$string .= ' class="' . implode(' ', $this->domAttrs['class']) . '" ';
unset($this->domAttrs['class']);
}
# style
if (!empty($this->domAttrs['style'])) {
// $string .= implode(' ', $this->domAttrs['class']);
unset($this->domAttrs['style']);
}
# attrs
if (!empty($this->domAttrs)) {
foreach ($this->domAttrs as $k => $v) {
$string .= ' ' . $k . '="' . $v . '" ';
}
}
# is single
if ($this->isSingle == true)
return $string . ' />' . PHP_EOL;

# start tag end
$string .= ' >' . PHP_EOL ;

if (!empty($this->childs))
$string .= $this->_invParseChilds($this->childs);

# end
$string .= $tagIndentString . '</' . $this->nodeName . '>' . PHP_EOL;

return $string;
}

private function _invParseChilds($childs)
{
if (empty($childs))
return '';

$string = '';
foreach ($childs as $child) {
$string .= (string) $child;
}
return $string;
}

}
12 changes: 6 additions & 6 deletions src/DomParser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ class Parser
# config obj
private $_config;

# set config obj
public function config($config = null)
#
private function _getConfig()
{
$this->_config = $config;

$this->_config = Config::getInstance();
#
$this->_startTagReg = $this->_config->get('start_tag_reg');
$this->_endTagReg = $this->_config->get('end_tag_reg');
Expand All @@ -30,6 +29,9 @@ public function config($config = null)
*/
public function parse($domString = '')
{
# init parse config
$this->_getConfig();

$root = Node::generRootNode();

$domString = trim($domString);
Expand All @@ -53,13 +55,11 @@ private function _parseChildNodes($domString = '', $parent = null)
$stringLen = strlen($domString);
# match start tad
list($domString, $node) = $this->_parseStartTag($domString, $parent);
// $domString = $this->_parseStartTag($domString, $parent);

# match content
$domString = $this->_parseContent($domString, $parent);

# match end tag
// $domString = $this->_parseEndTag($domString, $parent);
$domString = $this->_parseEndTag($domString, $node);

if ($stringLen == strlen($domString))
Expand Down
13 changes: 8 additions & 5 deletions tests/DomParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

$html = <<< 'HTML'
<template>
<!-- doc commnet -->
<view class="container">
<!-- doc hello
world
-->
<view class="container" id="main-view" data-genid="12323">
<navigator url="/pages/show?id=1">
<image src="../../static/images/home-by.jpg" mode="widthFix"></image>
<image src="../../static/images/home-by.jpg" class="hello world" mode="widthFix"></image>
</navigator>
<view class="layout">
<view class="scan">
Expand All @@ -26,7 +28,8 @@
</template>
HTML;

echo '<pre>';
$dom = $parser->setConfig([])->parse($html);
//echo '<pre>';
$dom = $parser->setConfig([
])->parse($html);

print_r($dom);

0 comments on commit eb245de

Please sign in to comment.