Last Updated | | Ratings | | Unique User Downloads | | Download Rankings |
2024-01-08 (5 days ago) | | Not yet rated by the users | | Total: 8 This week: 8 | | All time: 11,220 This week: 34 |
|
Description | | Author |
This package can modify HTML with CSS to optimize the styles.
It can take a string with the HTML of a Web page and parses it to make certain modifications.
The package can read configuration files to determine how the modifications of the HTML page will be applied.
Currently, it can parse the CSS style definitions and rewrite the styles to compact the style definitions. | |
|
Example
<?php
include '../src/CSSParser.php';
$html = file_get_contents('sample.html');
$cssParser = new CSSParser();
$modifiedHtml = $cssParser->parse($html);
echo $modifiedHtml;
|
Details
StyleParser
This PHP class reduces download overhead by excluding unused CSS classes.
Introduction
StyleParser
analyses an HTML string and identifies the CSS styles being used. It then injects the corresponding classes from your own custom library directly back into the HTML <style>
tag. If there is no <style>
tag it will create one.
StyleParser
includes an optional, responsive utility library that provides all the reusable classes you need to build a site using a simple and intuitive syntax. This utility library uses the data-util
attribute, instead of the standard class
attribute, in order to keep utilities separate from your own custom classes. You can change the attribute by editing config.ini
.
Usage
-
Instantiate StyleParser:
$styleParser = new StyleParser();
-
Parse HTML:
$html = '<html>...</html>';
$modifiedHtml = $styleParser->parse($html);
-
Customisation:
- Modify configuration files to tailor the styling behaviour.
Configuration Files
Please note that classes must be written using PHP .ini
syntax inside the config files, and not standard CSS syntax.
-
`config.ini` for general settings.
-
`root.ini` for root styles whiach are applied globally.
-
`elements.ini` for standard CSS elements (e.g. `div`).
-
`custom.ini` for custom classes (e.g. `header-hero`).
-
`utilities.ini` for built-in utility attributes (e.g. `row(lc)s+`).
-
`resolutions.ini` for built-in utility media query resolutions.
Dependencies
-
PHP: Requires PHP 7.0 or later.
-
DOMDocument: Uses the `DOMDocument` class for HTML parsing.
Error Handling
The class throws a RuntimeException
if parsing any configuration file fails. It is essential to handle this exception appropriately in your application.
License
This project is licensed under the MIT License.
Acknowledgments
Feel free to contribute, report issues, or suggest improvements!
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.