Skip to content

Commit

Permalink
Merge pull request #114 from Lullabot/better-correction
Browse files Browse the repository at this point in the history
Better correction
  • Loading branch information
sidkshatriya authored Jul 4, 2016
2 parents 0090544 + b2fe303 commit ec72088
Show file tree
Hide file tree
Showing 34 changed files with 859 additions and 98 deletions.
15 changes: 11 additions & 4 deletions src/AMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Lullabot\AMP\Spec\ValidationErrorCode;
use Lullabot\AMP\Utility\AMPHTML5;
use Lullabot\AMP\Validate\GroupedValidationResult;
use QueryPath;
use SebastianBergmann\Diff\Differ;
use Lullabot\AMP\Pass\BasePass;
Expand All @@ -42,6 +43,7 @@
class AMP
{
const AMP_LINENUM_ATTRIBUTE = 'data-amp-library-linenum';
const AMP_GLOBAL_WARNING = 'GLOBAL WARNING';

// The StandardScanPass should be first after all transform passes
// The StandardFixPass should be after StandardScanPass
Expand All @@ -63,6 +65,8 @@ class AMP
'Lullabot\AMP\Pass\TwitterTransformPass',
'Lullabot\AMP\Pass\StandardScanPass',
'Lullabot\AMP\Pass\StandardFixPass',
'Lullabot\AMP\Pass\StandardFixPassTwo',
'Lullabot\AMP\Pass\MinimumValidFixPass',
'Lullabot\AMP\Pass\StatisticsPass'
];

Expand All @@ -79,7 +83,9 @@ class AMP
/** @var Context */
protected $context = null;
/** @var SValidationResult */
protected $validation_result = null;
protected $validation_result;
/** @var GroupedValidationResult */
protected $grouped_validation_result;
/** @var string */
protected $scope = Scope::BODY_SCOPE;
/** @var string[] */
Expand Down Expand Up @@ -221,6 +227,7 @@ public function clear()
$this->component_js = [];
$this->validation_result = new SValidationResult();
$this->validation_result->status = ValidationResultStatus::UNKNOWN;
$this->grouped_validation_result = new GroupedValidationResult();
$this->context = null;
$this->scope = Scope::BODY_SCOPE;
}
Expand Down Expand Up @@ -416,14 +423,14 @@ public function convertToAmpHtml()

// Each of the $qp objects are pointing to the same DOMDocument
/** @var BasePass $pass */
$pass = (new $pass_name($qp_branch, $this->context, $this->validation_result, $this->parsed_rules, $this->options));
$pass = (new $pass_name($qp_branch, $this->context, $this->validation_result, $this->grouped_validation_result, $this->parsed_rules, $this->options));

// Run the pass
$pass->pass();
$this->action_taken = array_merge($this->action_taken, $pass->getWarnings());
$this->component_js = array_merge($this->component_js, $pass->getComponentJs());
}

$this->component_js = $this->context->getComponentJs();
$this->sortActionTakeByLineno();
$temp_amp_html = $this->getOutputHTML($qp);
$this->amp_html = $this->addStatisticsIfEnabled($temp_amp_html);
Expand Down Expand Up @@ -504,7 +511,7 @@ protected function getValidationWarnings()
/** @var RenderValidationResult $render_validation_result */
$render_validation_result = new RenderValidationResult($this->parsed_rules->format_by_code);
$filename = !empty($this->options['filename']) ? $this->options['filename'] : '';
return $render_validation_result->renderValidationResult($this->validation_result, $filename);
return $render_validation_result->renderValidationResult($this->grouped_validation_result, $filename);
}

/**
Expand Down
51 changes: 9 additions & 42 deletions src/Pass/BasePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Lullabot\AMP\Spec\AmpLayoutLayout;
use Lullabot\AMP\Utility\ParseUrl;
use Lullabot\AMP\Validate\GroupedValidationResult;
use Lullabot\AMP\Validate\ParsedTagSpec;
use Lullabot\AMP\Validate\ParsedValidatorRules;
use Lullabot\AMP\Validate\Scope;
Expand All @@ -43,32 +44,8 @@ abstract class BasePass
protected $context;
/** @var SValidationResult */
protected $validation_result;

protected static $component_mappings = [
'amp-analytics' => 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js',
'amp-anim' => 'https://cdn.ampproject.org/v0/amp-anim-0.1.js',
'amp-audio' => 'https://cdn.ampproject.org/v0/amp-audio-0.1.js',
'amp-brightcove' => 'https://cdn.ampproject.org/v0/amp-brightcove-0.1.js',
'amp-carousel' => 'https://cdn.ampproject.org/v0/amp-carousel-0.1.js',
'amp-dailymotion' => 'https://cdn.ampproject.org/v0/amp-dailymotion-0.1.js',
'amp-facebook' => 'https://cdn.ampproject.org/v0/amp-facebook-0.1.js',
'amp-fit-text' => 'https://cdn.ampproject.org/v0/amp-fit-text-0.1.js',
'amp-font' => 'https://cdn.ampproject.org/v0/amp-font-0.1.js',
'amp-iframe' => 'https://cdn.ampproject.org/v0/amp-iframe-0.1.js',
'amp-instagram' => 'https://cdn.ampproject.org/v0/amp-instagram-0.1.js',
'amp-install-serviceworker' => 'https://cdn.ampproject.org/v0/amp-install-serviceworker-0.1.js',
'amp-image-lightbox' => 'https://cdn.ampproject.org/v0/amp-image-lightbox-0.1.js',
'amp-lightbox' => 'https://cdn.ampproject.org/v0/amp-lightbox-0.1.js',
'amp-list' => 'https://cdn.ampproject.org/v0/amp-list-0.1.js',
'amp-pinterest' => 'https://cdn.ampproject.org/v0/amp-pinterest-0.1.js',
'amp-soundcloud' => 'https://cdn.ampproject.org/v0/amp-soundcloud-0.1.js',
'amp-twitter' => 'https://cdn.ampproject.org/v0/amp-twitter-0.1.js',
'amp-user-notification' => 'https://cdn.ampproject.org/v0/amp-user-notification-0.1.js',
'amp-vine' => 'https://cdn.ampproject.org/v0/amp-vine-0.1.js',
'amp-vimeo' => 'https://cdn.ampproject.org/v0/amp-vimeo-0.1.js',
'amp-youtube' => 'https://cdn.ampproject.org/v0/amp-youtube-0.1.js',
'template' => 'https://cdn.ampproject.org/v0/amp-mustache-0.1.js'
];
/** @var GroupedValidationResult */
protected $grouped_validation_result;

/**
* FixBasePass constructor.
Expand All @@ -78,32 +55,21 @@ abstract class BasePass
* @param ParsedValidatorRules $parsed_rules
* @param array $options
*/
function __construct(DOMQuery $q, Context $context, SValidationResult $validation_result, ParsedValidatorRules $parsed_rules, $options = [])
function __construct(DOMQuery $q, Context $context, SValidationResult $validation_result, GroupedValidationResult $grouped_validation_result, ParsedValidatorRules $parsed_rules, $options = [])
{
$this->q = $q;
$this->parsed_rules = $parsed_rules;
$this->options = $options;
$this->context = $context;
$this->validation_result = $validation_result;
$this->grouped_validation_result = $grouped_validation_result;
}

function getWarnings()
{
return $this->transformations;
}

function getComponentJs()
{
return $this->component_js;
}

function addComponent($component_name)
{
if (isset(self::$component_mappings[$component_name])) {
$this->component_js[$component_name] = self::$component_mappings[$component_name];
}
}

abstract function pass();

protected function addActionTaken(ActionTakenLine $w)
Expand Down Expand Up @@ -169,7 +135,7 @@ protected function encounteredAttributes(\DOMElement $el)
*/
protected function addComponentJsToHead($component)
{
if ($this->options['scope'] != Scope::HTML_SCOPE || !isset(self::$component_mappings[$component])) {
if ($this->options['scope'] != Scope::HTML_SCOPE || !isset(Context::$component_mappings[$component])) {
return false;
}

Expand All @@ -179,10 +145,11 @@ protected function addComponentJsToHead($component)
return false;
}

$new_script = $head->append('<script></script>')->lastChild();
// Add new line to just make it look good in the html
$new_script = $head->append(' <script></script>' . PHP_EOL)->lastChild();
$new_script->attr('async', '');
$new_script->attr('custom-element', $component);
$new_script->attr('src', self::$component_mappings[$component]);
$new_script->attr('src', Context::$component_mappings[$component]);
return true;
}

Expand Down
35 changes: 32 additions & 3 deletions src/Pass/IframeTagTransformPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace Lullabot\AMP\Pass;

use Lullabot\AMP\Utility\ParseUrl;
use QueryPath\DOMQuery;

use Lullabot\AMP\Utility\ActionTakenLine;
Expand Down Expand Up @@ -51,7 +52,13 @@ function pass()

$lineno = $this->getLineNo($dom_el);
$context_string = $this->getContextString($dom_el);
$src = $this->getIframeSrc($el);
list($src, $converted_to_https) = $this->getIframeSrc($el);

// Don't try to convert if a proper url was not found
if ($src === false) {
continue;
}

if ($el->hasAttr('class')) {
$class_attr = $el->attr('class');
}
Expand All @@ -68,15 +75,37 @@ function pass()

// Remove the iframe and its children
$el->removeChildren()->remove();
$this->addActionTaken(new ActionTakenLine('iframe', ActionTakenType::IFRAME_CONVERTED, $lineno, $context_string));
if ($converted_to_https) {
$this->addActionTaken(new ActionTakenLine('iframe', ActionTakenType::IFRAME_CONVERTED_AND_HTTPS, $lineno, $context_string));
} else {
$this->addActionTaken(new ActionTakenLine('iframe', ActionTakenType::IFRAME_CONVERTED, $lineno, $context_string));
}
$this->context->addLineAssociation($new_dom_el, $lineno);
}

return $this->transformations;
}

/**
* @param DOMQuery $el
* @return array
*/
protected function getIframeSrc(DOMQuery $el)
{
return $el->attr('src');
$src = trim($el->attr('src'));
if (empty($src)) {
return [false, false];
}

$parsed_url = ParseUrl::parse_url($src);
if ($parsed_url === false) {
return [false, false];
}

if ($parsed_url['scheme'] == 'http') {
return [preg_replace('/(*UTF8)^http:/i', 'https:', $src), true];
} else {
return [$src, false];
}
}
}
5 changes: 3 additions & 2 deletions src/Pass/ImgTagTransformPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Lullabot\AMP\Pass;

use Lullabot\AMP\Utility\ParseUrl;
use Lullabot\AMP\Validate\GroupedValidationResult;
use Lullabot\AMP\Validate\Scope;
use Lullabot\AMP\Utility\ActionTakenLine;
use Lullabot\AMP\Utility\ActionTakenType;
Expand Down Expand Up @@ -46,10 +47,10 @@ class ImgTagTransformPass extends BasePass
*/
protected $fastimage;

function __construct(DOMQuery $q, Context $context, SValidationResult $validation_result, ParsedValidatorRules $parsed_rules, array $options)
function __construct(DOMQuery $q, Context $context, SValidationResult $validation_result, GroupedValidationResult $grouped_validation_result, ParsedValidatorRules $parsed_rules, array $options)
{
$this->fastimage = new FastImageSize();
parent::__construct($q, $context, $validation_result, $parsed_rules, $options);
parent::__construct($q, $context, $validation_result, $grouped_validation_result, $parsed_rules, $options);
}

function pass()
Expand Down
Loading

0 comments on commit ec72088

Please sign in to comment.