Skip to content

Commit

Permalink
classes with style fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafał Pośpiech committed Apr 25, 2019
1 parent b35e4dd commit 23de27b
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 95 deletions.
67 changes: 67 additions & 0 deletions examples/TableClassNames.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<style>
.table {
border: 1px solid blue;
border-collapse: collapse;
}
.thead {
background: black;
color: yellowgreen;
font-family: 'DejaVu Sans';
font-weight: bold;
}
.theadcol {
background: yellow;
color: black;
border: 1px solid black;
}
.red {
background: red;
}
.tbodyrow {
background: olive;
color: white;
font-family: 'DejaVu Sans';
font-size: 12px;
}
.green {
background: green;
color: yellow;
font-family: 'DejaVu Sans';
font-size: 18px;
}
</style>
DIV<br />
<div class="table">
<div class="thead">
<div class="theadrow">
<div class="theadcol">.theadcol</div>
<div class="theadcol red">.theadcol .red</div>
<div class="theadcol red" style="font-weight:normal;">.theadcol .red inline-normal</div>
</div>
</div>
<div class="tbody">
<div class="tbodyrow">
<div class="tbodycol">.tbodycol</div>
<div class="tbodycol green">.tbodycol .green</div>
<div class="tbodycol green" style="font-weight:bold;">.tbodycol .green inline-bold</div>
</div>
</div>
</div>
<br />
TABLE<br />
<table class="table">
<thead class="thead">
<tr class="theadrow">
<th class="theadcol">.theadcol</th>
<th class="theadcol red">.theadcol .red</th>
<th class="theadcol red" style="font-weight:400;">.theadcol .red inline-normal</th>
</tr>
</thead>
<tbody class="tbody">
<tr class="tbodyrow">
<td class="tbodycol">.tbodycol</td>
<td class="tbodycol green">.tbodycol .green</td>
<td class="tbodycol green" style="font-weight: bold;">.tbodycol .green inline-bold</td>
</tr>
</tbody>
</table>
Binary file added examples/TableClassNames.pdf
Binary file not shown.
9 changes: 9 additions & 0 deletions examples/TableClassNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$loader = require '../vendor/autoload.php';
$document = (new YetiForcePDF\Document())->init();
$document->loadHtml(file_get_contents('TableClassNames.html'));
$time = microtime(true);
$pdfFile = $document->render();
echo microtime(true) - $time;
file_put_contents('TableClassNames.pdf', $pdfFile);
36 changes: 28 additions & 8 deletions lib/Html/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class Element extends \YetiForcePDF\Base
*/
protected $domElement;
/**
* Class names for element
* Class names for element.
*
* @var string
* @var string
*/
protected $classNames = '';

Expand All @@ -58,7 +58,7 @@ public function init()
if (isset($this->domElement->tagName)) {
$this->name = $this->domElement->tagName;
} else {
$this->ame = $this->domElement->nodeName;
$this->name = $this->domElement->nodeName;
}
return $this;
}
Expand Down Expand Up @@ -133,6 +133,25 @@ public function getElementId(): string
return $this->elementId;
}

/**
* Attach classes.
*
* @return self
*/
public function attachClasses()
{
if ($this->domElement instanceof \DOMElement && $this->domElement->hasAttribute('class')) {
$classNames = [];
foreach (explode(' ', $this->domElement->getAttribute('class')) as $className) {
if (trim($className)) {
$classNames[] = '.' . $className;
}
}
$this->setClassNames(implode(' ', $classNames));
}
return $this;
}

/**
* Parse element style.
*
Expand All @@ -144,6 +163,7 @@ public function parseStyle(): \YetiForcePDF\Style\Style
if ($this->domElement instanceof \DOMElement && $this->domElement->hasAttribute('style')) {
$styleStr = $this->domElement->getAttribute('style');
}
$this->attachClasses();
$style = (new \YetiForcePDF\Style\Style())
->setDocument($this->document)
->setElement($this)
Expand Down Expand Up @@ -178,11 +198,11 @@ public function isTextNode()
}

/**
* Set class names for element
* Set class names for element.
*
* @param string $classNames Class names for element
* @param string $classNames Class names for element
*
* @return self
* @return self
*/
public function setClassNames(string $classNames)
{
Expand All @@ -192,9 +212,9 @@ public function setClassNames(string $classNames)
}

/**
* Get class names for element
* Get class names for element.
*
* @return string[]
* @return string[]
*/
public function getClassNames()
{
Expand Down
9 changes: 1 addition & 8 deletions lib/Layout/ElementBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,8 @@ public function buildTree($parentBlock = null)
$style->setContent($childDomElement->getAttribute('style'));
} elseif ($childDomElement->nodeName === 'style') {
$style->parseCss($childDomElement->nodeValue);
}elseif ($childDomElement->hasAttribute('class')) {
$classNames = [];
foreach (explode(' ', $childDomElement->getAttribute('class')) as $className) {
if (trim($className)) {
$classNames[] = '.' . $className;
}
}
$element->setClassNames(implode(' ', $classNames));
}
$element->attachClasses();
}
$style = $style->parseInline();
$display = $style->getRules('display');
Expand Down
30 changes: 30 additions & 0 deletions lib/Style/Normalizer/Background.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);
/**
* Background class.
*
* @package YetiForcePDF\Style\Normalizer
*
* @copyright YetiForce Sp. z o.o
* @license YetiForce Public License v3
* @author Rafal Pospiech <r.pospiech@yetiforce.com>
*/

namespace YetiForcePDF\Style\Normalizer;

/**
* Class Background.
*/
class Background extends Normalizer
{
public function normalize($ruleValue, string $ruleName = ''): array
{
$normalizerName = \YetiForcePDF\Style\Normalizer\Normalizer::getNormalizerClassName('background-color');
return (new $normalizerName())
->setDocument($this->document)
->setStyle($this->style)
->init()
->normalize($ruleValue, $ruleName);
}
}

0 comments on commit 23de27b

Please sign in to comment.