Skip to content

Commit

Permalink
Use XPath instead of getElementsByTagName
Browse files Browse the repository at this point in the history
Bug: T336917
  • Loading branch information
tstarling authored May 23, 2023
1 parent eb9d5a2 commit 3987b63
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/Model/Svg/SvgFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use DOMDocument;
use DOMElement;
use DOMNode;
use DOMNodeList;
use DOMXpath;
use Krinkle\Intuition\Intuition;
use Psr\Log\LoggerAwareTrait;
Expand Down Expand Up @@ -158,15 +159,15 @@ protected function makeTranslationReady(): void
}

// Check that there is something to translate.
$texts = $this->document->getElementsByTagName('text');
$texts = $this->getElementsByTagName('text');
$textLength = $texts->length;
if (0 === $textLength) {
// Nothing to translate. Given special handling in TranslateController.
$this->logFileProblem('File {file} has nothing to translate');
return;
}

$styles = $this->document->getElementsByTagName('style');
$styles = $this->getElementsByTagName('style');
$styleLength = $styles->length;
for ($i = 0; $i < $styleLength; $i++) {
$style = $styles->item($i);
Expand All @@ -187,16 +188,16 @@ protected function makeTranslationReady(): void
}

// tref tags are not supported.
$trefs = $this->document->getElementsByTagName('tref');
$trefs = $this->getElementsByTagName('tref');
if (0 !== $trefs->length) {
throw new SvgStructureException('structure-error-contains-tref', $trefs->item(0));
}

// Strip empty tspans, texts, fill $idsInUse
$idsInUse = [ 0 ];
$translatableNodes = [];
$tspans = $this->document->getElementsByTagName('tspan');
$texts = $this->document->getElementsByTagName('text');
$tspans = $this->getElementsByTagName('tspan');
$texts = $this->getElementsByTagName('text');
foreach ($tspans as $tspan) {
if ($tspan->childNodes->length > 1
|| ( 1 == $tspan->childNodes->length && XML_TEXT_NODE !== $tspan->childNodes->item(0)->nodeType )
Expand Down Expand Up @@ -256,8 +257,8 @@ protected function makeTranslationReady(): void

// Reset $translatableNodes
$translatableNodes = [];
$tspans = $this->document->getElementsByTagName('tspan');
$texts = $this->document->getElementsByTagName('text');
$tspans = $this->getElementsByTagName('tspan');
$texts = $this->getElementsByTagName('text');
foreach ($tspans as $tspan) {
array_push($translatableNodes, $tspan);
}
Expand All @@ -274,7 +275,7 @@ protected function makeTranslationReady(): void
}
}

$texts = $this->document->getElementsByTagName('text');
$texts = $this->getElementsByTagName('text');
$textLength = $texts->length;
for ($i = 0; $i < $textLength; $i++) {
/** @var DOMElement $text */
Expand Down Expand Up @@ -322,9 +323,9 @@ protected function makeTranslationReady(): void
}
}

$switchLength = $this->document->getElementsByTagName('switch')->length;
$switchLength = $this->getElementsByTagName('switch')->length;
for ($i = 0; $i < $switchLength; $i++) {
$switch = $this->document->getElementsByTagName('switch')->item($i);
$switch = $this->getElementsByTagName('switch')->item($i);
$siblings = $switch->childNodes;
$existingLangs = [];
foreach ($siblings as $sibling) {
Expand Down Expand Up @@ -396,7 +397,7 @@ protected function makeTranslationReady(): void
*/
protected function analyse(): void
{
$switches = $this->document->getElementsByTagName('switch');
$switches = $this->getElementsByTagName('switch');
$number = $switches->length;
$translations = [];
$this->filteredTextNodes = []; // Reset
Expand Down Expand Up @@ -470,6 +471,17 @@ protected function analyse(): void
$this->savedLanguages = array_unique($this->savedLanguages);
}

/**
* Workaround for O(N^2) performance of DOMDocument::getElementsByTagName() T336917
*
* @param string $name
* @return DOMNodeList
*/
private function getElementsByTagName(string $name): DOMNodeList
{
return $this->xpath->query("//*[local-name()=\"$name\"]");
}

/**
* Returns a list of translations present in the loaded file, in the following format:
*
Expand Down Expand Up @@ -561,7 +573,7 @@ public function switchToTranslationSet(array $translations): array
$currentLanguages = $this->getSavedLanguages();
$expanded = $started = [];

$switches = $this->document->getElementsByTagName('switch');
$switches = $this->getElementsByTagName('switch');
$number = $switches->length;
for ($i = 0; $i < $number; $i++) {
$switch = $switches->item($i);
Expand Down

0 comments on commit 3987b63

Please sign in to comment.