Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #349 from facebook/fix/transformation-settings
Browse files Browse the repository at this point in the history
Fix #346 - Transformer's transform method does not account for settings
  • Loading branch information
diegoquinteiro authored Mar 21, 2019
2 parents 8f152d1 + bd2c556 commit acefb2a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
8 changes: 8 additions & 0 deletions src/Facebook/InstantArticles/Elements/InstantArticle.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,14 @@ public function addMetaProperty($property_name, $property_content)
return $this;
}

public function getMetaProperty($property_name)
{
if (array_key_exists($property_name, $this->metaProperties)) {
return $this->metaProperties[$property_name];
}
return null;
}

public function render($doctype = '<!doctype html>', $format = false, $validate = true)
{
$doctype = is_null($doctype) ? '<!doctype html>' : $doctype;
Expand Down
12 changes: 7 additions & 5 deletions src/Facebook/InstantArticles/Transformer/Transformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,6 @@ public function transformString($context, $content, $encoding = "utf-8")
libxml_clear_errors();
libxml_use_internal_errors($libxml_previous_state);
$result = $this->transform($context, $document);
if (Type::is($result, InstantArticle::getClassName())) {
$result = $this->handleTransformationSettings($result);
}
$totalTime = round(microtime(true) - $start, 3)*1000;
$totalWarnings = count($this->getWarnings());
$this->addLog(
Expand All @@ -278,15 +275,16 @@ public function transformString($context, $content, $encoding = "utf-8")
/**
* @param InstantArticle $context
* @param \DOMNode $node
* @deprecated Use @see Transformer::transformString instead.
*
* @return mixed
*/
public function transform($context, $node)
{
if (Type::is($context, InstantArticle::getClassName())) {
$is_first_run = false;
if (Type::is($context, InstantArticle::getClassName()) && $context->getMetaProperty('op:generator:transformer') === null) {
$context->addMetaProperty('op:generator:transformer', 'facebook-instant-articles-sdk-php');
$context->addMetaProperty('op:generator:transformer:version', InstantArticle::CURRENT_VERSION);
$is_first_run = true;
$this->instantArticle = $context;
}

Expand Down Expand Up @@ -375,6 +373,10 @@ public function transform($context, $node)
}
}

if ($is_first_run) {
$context = $this->handleTransformationSettings($context);
}

return $context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ public function testSelfTransformerContent()

$html_file = file_get_contents(__DIR__ . '/simple.html');

$transformer->transformString($instant_article, $html_file);
libxml_use_internal_errors(true);
$document = new \DOMDocument();
$document->loadHTML($html_file);
libxml_use_internal_errors(false);

$transformer->transform($instant_article, $document);
$instant_article->addMetaProperty('op:generator:version', '1.0.0');
$instant_article->addMetaProperty('op:generator:transformer:version', '1.0.0');
$result = $instant_article->render('', true)."\n";
Expand Down

0 comments on commit acefb2a

Please sign in to comment.