Skip to content

Commit daeb6d9

Browse files
committed
Removed CSSdoc as it is now a separate project, added to composer dependencies.
Updated how custom tags are handled, they can now be configured through a separate config, and built in handlers are supplied for script and style tags. Elements with white-space: pre; are now handled differently, you can now specify minifier options for specific elements, and defaults are provided to make <pre>, <textarea>, and <code> keep their whitespace. Can now specify output options per element, this enables SVG's to be output as XML compliant. Added a test for SVG output.
1 parent a8722de commit daeb6d9

23 files changed

+267
-1042
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
}
1212
],
1313
"minimum-stability": "beta",
14-
"require": {},
14+
"require": {
15+
"hexydec/cssdoc": "@dev"
16+
},
1517
"autoload": {
16-
"psr-4": {
17-
"hexydec\\htmldoc\\": "src/"
18-
}
18+
"classmap": ["src/"]
1919
}
2020
}

composer.lock

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api/construct.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ The options set into the object are setup for general use, but can be configured
1818
| `inline` | HTML elements that are considered inline | `['b', 'u', 'big', 'i', 'small', 'ttspan', 'em', 'a', 'strong', 'sub', 'sup', 'abbr', 'acronym', 'cite', 'code', 'dfn', 'em', 'kbd', 'strong', 'samp', 'var', 'span']` |
1919
| `singleton` | HTML elements that are singletons | `['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr']` |
2020
| `closeoptional` | HTML elements that don't have to be closed | `['head', 'body', 'p', 'dt', 'dd', 'li', 'option', 'thead', 'th', 'tbody', 'tr', 'td', 'tfoot', 'colgroup']` |
21-
| `pre` | HTML elements that contain pre-formatted content | `['textarea', 'pre', 'code']` |
22-
| `plugins` | HTML elements that have a custom handler class | `['script', 'style']` |
2321

2422
#### `attributes`
2523

@@ -31,6 +29,26 @@ The options set into the object are setup for general use, but can be configured
3129
| `urls` | Attributes that contain urls | `['href', 'src', 'action', 'poster']` |
3230
| `urlskip` | Skips compressing URLs if tag => attribute => value matches | <code>[<br>&nbsp; &nbsp; 'link' => [<br>&nbsp; &nbsp; &nbsp; &nbsp; 'rel' => ['stylesheet', 'icon', 'shortcut icon', 'apple-touch-icon-precomposed', 'apple-touch-icon', 'preload', 'prefetch', 'author', 'help']<br>&nbsp; &nbsp; ]<br>]</code> |
3331

32+
#### `custom`
33+
34+
This option enables you to specify custom handlers for specific tags. By default there are built in handlers for `style` and `script` tags, currently CSS is minified using [hexydec\\css\\cssdoc](https://github.com/hexydec/cssdoc) if you install the module using composer.
35+
36+
There is currently no built-in minifier for Javascript, although an external minifier can be specified in the custom tag options:
37+
38+
| option | Description |
39+
|---------------|-----------------------------------------------------------------------------------|
40+
| `class` | The name of the handler class (Should implement \hexydec\html\token) |
41+
| `config` | An array of custom configuration to be passed to the handler class |
42+
43+
Note that the built in `style` and `script` handlers both specify `minifier` in the `config` array, here you can specify a callback for a custom minifier with the following pattern:
44+
45+
```php
46+
function (string $css) : string {
47+
// manipulate the css
48+
return $css;
49+
}
50+
```
51+
3452
## Returns
3553

3654
A new HTMLdoc object.

docs/api/html.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ if ($doc->load($html)) {
2121
</thead>
2222
<tbody>
2323
<tr>
24-
<td rowspan="5"><code>$options</code></td>
25-
<td rowspan="5">Array</td>
24+
<td rowspan="6"><code>$options</code></td>
25+
<td rowspan="6">Array</td>
2626
<td colspan="2">An array of output options, the input is merged with `htmldoc::$config['output']`. <em>Note that for most scenarios, specifying this argument is not required</em></td>
27-
<td><code>Array()</code</td>
27+
<td><code>[]</code></td>
2828
</tr>
2929
<tr>
3030
<td><code>quotestyle</code></td>
@@ -46,6 +46,17 @@ if ($doc->load($html)) {
4646
<td>A boolean specifying whether to render XML compliant output. Setting this to <code>true</code> automatically sets <code>quotestyle = &quot;double&quot;</code>, <code>singletonclose = &quot;/&gt;&quot;</code>, and <code>closetag = true</code></td>
4747
<td><code>false</code></td>
4848
</tr>
49+
<tr>
50+
<td><code>elements</code></td>
51+
<td>An array specifying output options for specific tags</td>
52+
<td><code>[<br>
53+
&nbsp; 'svg' => [<br>
54+
&nbsp; &nbsp; 'xml' => true,<br>
55+
&nbsp; &nbsp; 'quotestyle' => 'double',<br>
56+
&nbsp; &nbsp; 'singletonclose' => '/>',<br>
57+
&nbsp; &nbsp; 'closetags' => true<br>
58+
&nbsp; ]</code></td>
59+
</tr>
4960
</tbody>
5061
</table>
5162

docs/how-to-use.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ if ($doc->open($url, $context, $error)) {
5454
}
5555
```
5656

57-
For more information, see the API documentation for the [`load()` method](api/load.md) and the [`open()` method](api/load.md).
57+
For more information, see the API documentation for the [`load()` method](api/load.md) and the [`open()` method](api/open.md).
5858

5959
## Finding Elements and Extracting Information
6060

gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function(grunt) {
1010
},
1111
options: {
1212
bin: "php tools/phpunit.phar",
13-
bootstrap: "src/autoload.php",
13+
bootstrap: "vendor/autoload.php",
1414
testdox: true,
1515
colors: true,
1616
//debug: true

index.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
require(__DIR__.'/src/autoload.php');
2+
require(__DIR__.'/vendor/autoload.php');
33

44
$base = empty($_POST['base']) ? '' : $_POST['base'];
55
$input = '';
@@ -62,15 +62,19 @@
6262
// retrieve the user posted options
6363
$isset = isset($_POST['minify']) && is_array($_POST['minify']);
6464
foreach ($options AS $key => $item) {
65-
$minify[$key] = $isset && in_array($key, $_POST['minify']) ? (is_array($item) ? [] : (is_bool($options[$key]) ? true : $options[$key])) : false;
66-
if (is_array($item)) {
67-
foreach ($item AS $sub => $value) {
68-
if ($minify[$key] !== false && isset($_POST['minify'][$key]) && is_array($_POST['minify'][$key]) && in_array($sub, $_POST['minify'][$key])) {
69-
$minify[$key][$sub] = true;
70-
} elseif ($minify[$key]) {
71-
$minify[$key][$sub] = false;
65+
if ($key != 'elements') {
66+
$minify[$key] = $isset && in_array($key, $_POST['minify']) ? (is_array($item) ? [] : (is_bool($options[$key]) ? true : $options[$key])) : false;
67+
if (is_array($item)) {
68+
foreach ($item AS $sub => $value) {
69+
if ($minify[$key] !== false && isset($_POST['minify'][$key]) && is_array($_POST['minify'][$key]) && in_array($sub, $_POST['minify'][$key])) {
70+
$minify[$key][$sub] = true;
71+
} elseif ($minify[$key]) {
72+
$minify[$key][$sub] = false;
73+
}
7274
}
7375
}
76+
} else {
77+
unset($options[$key]);
7478
}
7579
}
7680

readme.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# HTMLDoc: PHP HTML Document Parser and Minifier
22

3-
A tokeniser based HTML and CSS document parser and minifier, written in PHP.
3+
A tokeniser based HTML document parser and minifier, written in PHP.
44

55
![Licence](https://img.shields.io/badge/Licence-MIT-lightgrey.svg)
66
[![Build Status](https://api.travis-ci.org/hexydec/htmldoc.svg?branch=master)](https://travis-ci.org/hexydec/htmldoc)
77
![Code Coverage](https://codecov.io/gh/hexydec/htmldoc/branch/master/graph/badge.svg)
88

99
## Description
1010

11-
An HTML and CSS parser, primarily designed for minifying HTML documents, it also enables the document structure to be queried allowing attribute and textnode values to be extracted.
11+
An HTML parser, primarily designed for minifying HTML documents, it also enables the document structure to be queried allowing attribute and textnode values to be extracted.
1212

1313
Both parsers are designed around a tokeniser to make the document processing more reliable than regex based minifiers, which are a bit blunt and can be problematic if they match patterns in the wrong places.
1414

15+
The software is also capable of processing and minifying SVG documents.
16+
1517
## Usage
1618

1719
To minify an HTML document:

src/autoload.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@
1111
'hexydec\\html\\script' => $dir.'/tokens/script.php',
1212
'hexydec\\html\\style' => $dir.'/tokens/style.php',
1313
'hexydec\\html\\tag' => $dir.'/tokens/tag.php',
14-
'hexydec\\html\\text' => $dir.'/tokens/text.php',
15-
'hexydec\\html\\cssmin' => $dir.'/cssmin.php',
16-
'hexydec\\css\\cssdoc' => __DIR__.'/cssdoc/cssdoc.php',
17-
'hexydec\\css\\document' => __DIR__.'/cssdoc/tokens/document.php',
18-
'hexydec\\css\\directive' => __DIR__.'/cssdoc/tokens/directive.php',
19-
'hexydec\\css\\rule' => __DIR__.'/cssdoc/tokens/rule.php',
20-
'hexydec\\css\\selector' => __DIR__.'/cssdoc/tokens/selector.php',
21-
'hexydec\\css\\property' => __DIR__.'/cssdoc/tokens/property.php',
22-
'hexydec\\css\\value' => __DIR__.'/cssdoc/tokens/value.php'
14+
'hexydec\\html\\text' => $dir.'/tokens/text.php'
2315
];
2416
if (isset($classes[$class])) {
2517
return require($classes[$class]);

0 commit comments

Comments
 (0)