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

Replace tokens by splitting text runs/paragraphs instead of replacing #62

Merged
merged 9 commits into from
Jul 19, 2024
349 changes: 60 additions & 289 deletions .editorconfig

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion CRM/Civioffice/DocumentRendererType/LocalUnoconv.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,4 @@ public static function defaultConfiguration(): array
'phpword_tokens' => false,
];
}
}
}

Large diffs are not rendered by default.

95 changes: 95 additions & 0 deletions Civi/Civioffice/PhpWord/StyleMerger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/*
* Copyright (C) 2024 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types = 1);

namespace Civi\Civioffice\PhpWord;

final class StyleMerger {

private \DOMElement $styleElement;

/**
* @phpstan-var array<string, \DOMElement>
*/
private array $elements = [];

public static function mergeStyles(string $style, string ...$styles): string {
$styleMerger = new self($style);
foreach ($styles as $styleToMerge) {
$styleMerger->merge($styleToMerge);
}

return $styleMerger->getStyleString();
}

public function __construct(string $style) {
$this->styleElement = $this->createStyleElement($style);
foreach ($this->styleElement->childNodes as $node) {
if ($node instanceof \DOMElement) {
$this->elements[$node->tagName] = $node;
}
}
}

public function merge(string $style): self {
$styleElement = $this->createStyleElement($style);
foreach ($styleElement->childNodes as $node) {
if ($node instanceof \DOMElement) {
// @todo Do we need recursive merging for some elements?
if (!isset($this->elements[$node->tagName])) {
// @phpstan-ignore-next-line
$importedNode = $this->styleElement->ownerDocument->importNode($node, TRUE);
if (!$importedNode instanceof \DOMElement) {
throw new \RuntimeException('Importing node failed');
}

$this->styleElement->appendChild($importedNode);
$this->elements[$node->tagName] = $importedNode;
}
}
}

return $this;
}

public function getStyleString(): string {
// @phpstan-ignore-next-line
return $this->styleElement->ownerDocument->saveXML($this->styleElement);
}

private function createStyleElement(string $style): \DOMElement {
if (NULL === $style = preg_replace('/>\s+</', '><', $style)) {
throw new \RuntimeException('Error processing style');
}

$doc = new \DOMDocument();
$doc->loadXML(
'<root xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">' . $style . '</root>'
);

// @phpstan-ignore-next-line
foreach ($doc->documentElement->childNodes as $node) {
if ($node instanceof \DOMElement) {
return $node;
}
}

throw new \RuntimeException('Could not create style element');
}

}
6 changes: 6 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<config name="ignore_warnings_on_exit" value="true"/>

<rule ref="tools/phpcs/vendor/drupal/coder/coder_sniffer/Drupal">
<!-- Conflicts with PHPStan type hints -->
<exclude name="Drupal.Commenting.VariableComment.IncorrectVarType"/>
<exclude name="Drupal.Commenting.FunctionComment.ParamTypeSpaces"/>
<exclude name="Drupal.Commenting.FunctionComment.ReturnTypeSpaces"/>
<exclude name="Drupal.Commenting.VariableComment.MissingVar"/>

<!-- Conflicts with union type hints -->
<exclude name="Drupal.Commenting.VariableComment.IncorrectVarType"/>

Expand Down
5 changes: 2 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
<php>
<ini name="error_reporting" value="-1" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[direct]=0&amp;baselineFile=./tests/ignored-deprecations.json"/>
<!-- With nikic/php-parser v4.18.0 and v5.0.0 code coverage fails -->
<!-- See https://github.com/symfony/symfony/issues/53459 -->
<env name="SYMFONY_PHPUNIT_REQUIRE" value="nikic/php-parser:4.17" />
<!-- Version 5 conflicts with drush <=12 which as version 4 as indirect dependency. -->
<env name="SYMFONY_PHPUNIT_REQUIRE" value="nikic/php-parser:^4" />
</php>

<testsuites>
Expand Down
Loading
Loading