diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..4926c28 --- /dev/null +++ b/.github/workflows/tests.yml @@ -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 diff --git a/README.md b/README.md index d125461..30bb658 100644 --- a/README.md +++ b/README.md @@ -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/) + + Tests + + + Scrutinizer Quality Score + + + Latest Stable Version + + + Total Downloads + + +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 @@ -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(); +``` diff --git a/composer.json b/composer.json index 155edd3..388ed2d 100644 --- a/composer.json +++ b/composer.json @@ -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"], @@ -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" + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6cd729e..5a7c485 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -13,7 +13,7 @@ > - InlineStyle + tests diff --git a/InlineStyle/InlineStyle.php b/src/InlineStyle.php similarity index 94% rename from InlineStyle/InlineStyle.php rename to src/InlineStyle.php index ed3c6f4..e47fece 100644 --- a/InlineStyle/InlineStyle.php +++ b/src/InlineStyle.php @@ -1,5 +1,5 @@ _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 * @@ -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); diff --git a/InlineStyle/Tests/InlineStyleTest.php b/tests/InlineStyleTest.php similarity index 80% rename from InlineStyle/Tests/InlineStyleTest.php rename to tests/InlineStyleTest.php index f7bb648..9780d3a 100644 --- a/InlineStyle/Tests/InlineStyleTest.php +++ b/tests/InlineStyleTest.php @@ -6,12 +6,14 @@ * Test class for InlineStyle. * Generated by PHPUnit on 2010-03-10 at 21:52:44. */ -use InlineStyle\InlineStyle; +use PHPUnit\Framework\TestCase; +use Codeat3\InlineStyle\InlineStyle; -class InlineStyleTest extends \PHPUnit_Framework_TestCase + +class InlineStyleTest extends TestCase { /** - * @var \InlineStyle\InlineStyle + * @var \Codeat3\InlineStyle */ protected $object; protected $basedir; @@ -20,7 +22,7 @@ class InlineStyleTest extends \PHPUnit_Framework_TestCase * Sets up the fixture, for example, opens a network connection. * This method is called before a test is executed. */ - protected function setUp() + protected function setUp(): void { $this->basedir = __DIR__."/testfiles"; $this->object = new InlineStyle($this->basedir."/test.html"); @@ -30,11 +32,12 @@ protected function setUp() * Tears down the fixture, for example, closes a network connection. * This method is called after a test is executed. */ - protected function tearDown() + protected function tearDown(): void { unset($this->object); } + /* @test */ public function testGetHTML() { $this->assertEquals( @@ -71,7 +74,8 @@ public function testApplyExtractedStylesheet() $this->assertEquals( file_get_contents($this->basedir."/testApplyExtractedStylesheet.html"), - $this->object->getHTML()); + $this->object->getHTML() + ); } public function testParseStyleSheet() @@ -93,7 +97,15 @@ public function testParseStyleSheetWithComments() public function testIllegalXmlUtf8Chars() { // check an exception is not thrown when loading up illegal XML UTF8 chars - new InlineStyle("".chr(2).chr(3).chr(4).chr(5).""); + $htmldoc = new InlineStyle("".chr(2).chr(3).chr(4).chr(5).""); + $resultHtml = << + + +HTML; + // TODO: its a dummy test + $this->assertEquals($resultHtml, $htmldoc->getHTML()); + } public function testGetScoreForSelector() @@ -256,6 +268,38 @@ function testNonWorkingPseudoSelectors() } CSS ); + + $resultHtml = << + + +Example + + + + + + +

An example title

+

Paragraph 1

+

Paragraph 2

+
+ + + +HTML; + $this->assertEquals($resultHtml, $this->object->getHtml()); + } /** @@ -270,6 +314,36 @@ function testInvalidCssProperties() } CSS ); + $resultHtml = << + + +Example + + + + + + +

An example title

+

Paragraph 1

+

Paragraph 2

+
+ + + +HTML; + $this->assertEquals($resultHtml, $this->object->getHtml()); } function testRegression24() { @@ -298,7 +372,11 @@ function testMultipleStylesheets28() { $expected = << -Example + +Example + + +

Paragraph

Strong @@ -307,6 +385,7 @@ function testMultipleStylesheets28() { HTML; + $this->assertEquals($expected, $htmldoc->getHTML()); } @@ -316,8 +395,12 @@ function testMediaStylesheets31() { $expected = << -Example + +Example + + +