Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Symfony7 support #45

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Tests

on:
push:
pull_request:

jobs:
tests:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php: [7.4, '8.0', 8.1]

name: PHP ${{ matrix.php }}

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip
coverage: none

- name: Install dependencies
run: |
composer update --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit --verbose
73 changes: 47 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
InlineStyle
===========
[![Build Status](https://travis-ci.org/christiaan/InlineStyle.svg?branch=master)](https://travis-ci.org/christiaan/InlineStyle)
[![Scrutinizer Quality Score](https://scrutinizer-ci.com/g/christiaan/InlineStyle/badges/quality-score.png?s=f731e792fb2eaa305e294a1a2928e9bc96dca12b)](https://scrutinizer-ci.com/g/christiaan/InlineStyle/)
<a href="https://github.com/codeat3/inlinestyle/actions?query=workflow%3ATests">
<img src="https://github.com/codeat3/inlinestyle/workflows/Tests/badge.svg" alt="Tests">
</a>
<a href="https://scrutinizer-ci.com/g/codeatt/InlineStyle/">
<img src="https://scrutinizer-ci.com/g/christiaan/InlineStyle/badges/quality-score.png?s=f731e792fb2eaa305e294a1a2928e9bc96dca12b" alt="Scrutinizer Quality Score" />
</a>
<a href="https://packagist.org/packages/codeat3/inlinestyle">
<img src="https://img.shields.io/packagist/v/codeat3/inlinestyle" alt="Latest Stable Version">
</a>
<a href="https://packagist.org/packages/codeat3/inlinestyle">
<img src="https://img.shields.io/packagist/dt/codeat3/inlinestyle" alt="Total Downloads">
</a>

This is a fork of the original repository (christiaan/InlineStyle)[https://github.com/christiaan/InlineStyle] by [Christiaan Baartse
](https://github.com/christiaan)

InlineStyle provides an easy way to apply embedded and external stylesheets
directly as inline styles on the HTML tags. This is especially targetted at mail
Expand All @@ -10,51 +23,59 @@ for HTML tags.

Installation
------------
Run
composer.phar require inlinestyle/inlinestyle
```
composer require inlinestyle/inlinestyle
```

Or add the following to your composer.json file
"require": {
"inlinestyle/inlinestyle": "1.*"
}
```
"require": {
"inlinestyle/inlinestyle": "^2.0"
}
```

Usage
## Usage
-----

Use composer to download required dependencies.

Import InlineStyle

use \InlineStyle\InlineStyle;

```
use \InlineStyle\InlineStyle;
```
Create a new InlineStyle object from either a HTML string or HTML file.

$htmldoc = new InlineStyle("testfiles/test.html");

```
$htmldoc = new InlineStyle("testfiles/test.html");
```
or

$htmldoc = new InlineStyle(file_get_contents("http://github.com"));

```
$htmldoc = new InlineStyle(file_get_contents("http://github.com"));
```
### Apply the embedded and external stylesheets

-----
First we'll have to extract the stylesheets from the document and then we have
to apply them.

$htmldoc->applyStylesheet($htmldoc->extractStylesheets());

```
$htmldoc->applyStylesheet($htmldoc->extractStylesheets());
```
The second param is the base url that is used to parse the links to external
stylesheets.

$htmldoc->applyStylesheet($htmldoc->extractStylesheets(null, "http://github.com"));
```
$htmldoc->applyStylesheet($htmldoc->extractStylesheets(null, "http://github.com"));
```

### Applying additional stylesheets

This class can also be used to apply a given css template to each processed HTML
file.

$htmldoc->applyStylesheet(file_get_contents("testfiles/external.css"));
```
$htmldoc->applyStylesheet(file_get_contents("testfiles/external.css"));
```

### Retrieving the modified HTML

After calling applyStylesheet various times the resulting HTML can be retrieved as a string using getHTML.

$html = $htmldoc->getHTML();
```
$html = $htmldoc->getHTML();
```
17 changes: 12 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "inlinestyle/inlinestyle",
"name": "codeat3/inlinestyle",
"type": "library",
"description": "Apply CSS stylesheets directly as inline styles to a HTML document",
"keywords": ["css", "email", "inline"],
Expand All @@ -13,13 +13,20 @@
}
],
"require": {
"php": ">=5.3.3",
"symfony/css-selector": ">=2.1"
"php": "^7.4|^8.0",
"symfony/css-selector": "^5.0|^6.0|^7.0"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-0": { "InlineStyle": "" }
"psr-4": {
"Codeat3\\InlineStyle\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests"
}
}
}
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
>
<testsuites>
<testsuite name="InlineStyle Test Suite">
<directory>InlineStyle</directory>
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
22 changes: 20 additions & 2 deletions InlineStyle/InlineStyle.php → src/InlineStyle.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace InlineStyle;
namespace Codeat3\InlineStyle;

/*
* InlineStyle MIT License
Expand Down Expand Up @@ -170,6 +170,24 @@ public function applyRule($selector, $style)
return $this;
}

public function getDomObject() {
$clone = clone $this;
foreach ($clone->_getNodesForCssSelector('[inlinestyle-original-style]') as $node) {
$current = $node->hasAttribute("style") ?
$this->_styleToArray($node->getAttribute("style")) :
array();
$original = $node->hasAttribute("inlinestyle-original-style") ?
$this->_styleToArray($node->getAttribute("inlinestyle-original-style")) :
array();

$current = $clone->_mergeStyles($current, $original);

$node->setAttribute("style", $this->_arrayToStyle($current));
$node->removeAttribute('inlinestyle-original-style');
}
return $clone->_dom;
}

/**
* Returns the DOMDocument as html
*
Expand Down Expand Up @@ -380,7 +398,7 @@ private function _stripStylesheet($s)
{
// strip comments
$s = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!','', $s);

// strip keyframes rules
$s = preg_replace('/@[-|keyframes].*?\{.*?\}[ \r\n]*\}/s', '', $s);

Expand Down
Loading