From 954908b547c38d12ee75d636c64b41b74330f7fc Mon Sep 17 00:00:00 2001 From: Swapnil Sarwe Date: Mon, 6 Sep 2021 12:41:12 +0530 Subject: [PATCH 1/6] exposed one method getDomObject --- InlineStyle/InlineStyle.php | 20 +++++++++++++++++++- composer.json | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/InlineStyle/InlineStyle.php b/InlineStyle/InlineStyle.php index ed3c6f4..1a92ae1 100644 --- a/InlineStyle/InlineStyle.php +++ b/InlineStyle/InlineStyle.php @@ -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 * @@ -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/composer.json b/composer.json index 155edd3..fe0550e 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"], From b0f6e6f169d938e6f4c7b902b6f1c7e931e1867b Mon Sep 17 00:00:00 2001 From: Swapnil Sarwe Date: Thu, 3 Feb 2022 09:19:45 +0530 Subject: [PATCH 2/6] restructure & retest --- .github/workflows/tests.yml | 34 ++++++ composer.json | 15 ++- phpunit.xml.dist | 2 +- {InlineStyle => src}/InlineStyle.php | 2 +- .../Tests => tests}/InlineStyleTest.php | 104 ++++++++++++++++-- .../Tests => tests}/testfiles/external.css | 0 .../Tests => tests}/testfiles/external2.css | 0 .../Tests => tests}/testfiles/test.html | 0 .../testApplyExtractedStylesheet.html | 2 + .../testfiles/testApplyStylesheet.html | 0 .../testApplyStylesheetObeysSpecificity.html | 0 .../testfiles/testExtractStylesheets.php | 0 .../testfiles/testGetHTML.html | 0 .../testLinkedMediaStylesheets31.html | 0 .../testfiles/testMediaStylesheets31.html | 0 .../testfiles/testMultipleStylesheets.html | 0 16 files changed, 144 insertions(+), 15 deletions(-) create mode 100644 .github/workflows/tests.yml rename {InlineStyle => src}/InlineStyle.php (99%) rename {InlineStyle/Tests => tests}/InlineStyleTest.php (80%) rename {InlineStyle/Tests => tests}/testfiles/external.css (100%) rename {InlineStyle/Tests => tests}/testfiles/external2.css (100%) rename {InlineStyle/Tests => tests}/testfiles/test.html (100%) rename {InlineStyle/Tests => tests}/testfiles/testApplyExtractedStylesheet.html (99%) rename {InlineStyle/Tests => tests}/testfiles/testApplyStylesheet.html (100%) rename {InlineStyle/Tests => tests}/testfiles/testApplyStylesheetObeysSpecificity.html (100%) rename {InlineStyle/Tests => tests}/testfiles/testExtractStylesheets.php (100%) rename {InlineStyle/Tests => tests}/testfiles/testGetHTML.html (100%) rename {InlineStyle/Tests => tests}/testfiles/testLinkedMediaStylesheets31.html (100%) rename {InlineStyle/Tests => tests}/testfiles/testMediaStylesheets31.html (100%) rename {InlineStyle/Tests => tests}/testfiles/testMultipleStylesheets.html (100%) 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/composer.json b/composer.json index fe0550e..1ef08a6 100644 --- a/composer.json +++ b/composer.json @@ -13,13 +13,20 @@ } ], "require": { - "php": ">=5.3.3", - "symfony/css-selector": ">=2.1" + "php": "^7.4|^8.0", + "symfony/css-selector": "^6.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 99% rename from InlineStyle/InlineStyle.php rename to src/InlineStyle.php index 1a92ae1..e47fece 100644 --- a/InlineStyle/InlineStyle.php +++ b/src/InlineStyle.php @@ -1,5 +1,5 @@ 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 + + +