-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from Lullabot/62-amp-audio-conversion
62 amp audio conversion
- Loading branch information
Showing
10 changed files
with
169 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
/* | ||
* Copyright 2016 Google | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
namespace Lullabot\AMP\Pass; | ||
|
||
use QueryPath\DOMQuery; | ||
use Lullabot\AMP\Utility\ActionTakenLine; | ||
use Lullabot\AMP\Utility\ActionTakenType; | ||
|
||
/** | ||
* Class AudioTagTransformPass | ||
* @package Lullabot\AMP\Pass | ||
* | ||
* @see https://github.com/ampproject/amphtml/blob/master/extensions/amp-audio/amp-audio.md | ||
* @see https://developer.mozilla.org/en/docs/Web/HTML/Element/audio | ||
*/ | ||
class AudioTagTransformPass extends BasePass | ||
{ | ||
function pass() | ||
{ | ||
$audio_tags = $this->q->find('audio:not(noscript audio)'); | ||
/** @var DOMQuery $el */ | ||
foreach ($audio_tags as $el) { | ||
/** @var \DOMElement $dom_el */ | ||
$dom_el = $el->get(0); | ||
|
||
$lineno = $this->getLineNo($dom_el); | ||
$context_string = $this->getContextString($dom_el); | ||
|
||
$new_dom_el = $this->cloneAndRenameDomElement($dom_el, 'amp-audio'); | ||
$new_el = $el->prev(); | ||
$el->remove(); | ||
|
||
$this->addFallbackAndPlaceholder($new_el); | ||
$this->addActionTaken(new ActionTakenLine('audio', ActionTakenType::AUDIO_CONVERTED, $lineno, $context_string)); | ||
$this->context->addLineAssociation($new_dom_el, $lineno); | ||
} | ||
|
||
return $this->transformations; | ||
} | ||
|
||
/** | ||
* @param DOMQuery $el | ||
*/ | ||
function addFallbackAndPlaceholder(DOMQuery $el) | ||
{ | ||
/** @var DOMQuery $wrap_this */ | ||
$wrap_this = $el->children()->not('source')->not('track'); | ||
|
||
if ($wrap_this->count()) { | ||
$wrap_this->wrapAll('<div fallback=""></div>'); | ||
} | ||
|
||
if (isset($this->options['audio_placeholder_html'])) { | ||
$el->prepend('<div placeholder="">' . $this->options['audio_placeholder_html'] . '</div>'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
tests/test-data/fragment-html/audio-to-amp-audio-conversion-fragment.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<audio controls autoplay> | ||
<div>Your browser does not support the <strong>audio</strong> tag</div> | ||
<p>Try using recent versions of Google Chrome or Firefox</p> | ||
<source src="//example.dd:8088/sites/default/files/drums.mp3" type="audio/mpeg"> | ||
<source src="http://example.dd:8088/sites/default/files/drums.ogg" type="audio/ogg"> | ||
<source src="https://example.dd:8088/sites/default/files/drums.wav" type="audio/wav"> | ||
<track kind="captions" src="//example.dd:8088/sites/default/files/drums.en.vtt" srclang="en" label="English"> | ||
<track kind="captions" src="https://example.dd:8088/sites/default/files/drums.hi.vtt" srclang="hi" label="Hindi"> | ||
</audio> |
50 changes: 50 additions & 0 deletions
50
tests/test-data/fragment-html/audio-to-amp-audio-conversion-fragment.html.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<amp-audio controls autoplay> | ||
<div fallback><div>Your browser does not support the <strong>audio</strong> tag</div><p>Try using recent versions of Google Chrome or Firefox</p></div> | ||
|
||
<source src="//example.dd:8088/sites/default/files/drums.mp3" type="audio/mpeg"> | ||
<source type="audio/ogg"> | ||
<source src="https://example.dd:8088/sites/default/files/drums.wav" type="audio/wav"> | ||
<track kind="captions" src="//example.dd:8088/sites/default/files/drums.en.vtt" srclang="en" label="English"> | ||
<track kind="captions" src="https://example.dd:8088/sites/default/files/drums.hi.vtt" srclang="hi" label="Hindi"> | ||
</amp-audio> | ||
|
||
|
||
ORIGINAL HTML | ||
--------------- | ||
Line 1: <audio controls autoplay> | ||
Line 2: <div>Your browser does not support the <strong>audio</strong> tag</div> | ||
Line 3: <p>Try using recent versions of Google Chrome or Firefox</p> | ||
Line 4: <source src="//example.dd:8088/sites/default/files/drums.mp3" type="audio/mpeg"> | ||
Line 5: <source src="http://example.dd:8088/sites/default/files/drums.ogg" type="audio/ogg"> | ||
Line 6: <source src="https://example.dd:8088/sites/default/files/drums.wav" type="audio/wav"> | ||
Line 7: <track kind="captions" src="//example.dd:8088/sites/default/files/drums.en.vtt" srclang="en" label="English"> | ||
Line 8: <track kind="captions" src="https://example.dd:8088/sites/default/files/drums.hi.vtt" srclang="hi" label="Hindi"> | ||
Line 9: </audio> | ||
Line 10: | ||
|
||
|
||
Transformations made from HTML tags to AMP custom tags | ||
------------------------------------------------------- | ||
|
||
<audio controls autoplay> at line 1 | ||
ACTION TAKEN: audio tag was converted to the amp-audio tag. | ||
|
||
|
||
AMP-HTML Validation Issues and Fixes | ||
------------------------------------- | ||
FAIL | ||
|
||
<source src="http://example.dd:8088/sites/default/files/drums.ogg" type="audio/ogg"> on line 5 | ||
- Invalid URL protocol 'http:' for attribute 'src' in tag 'amp-audio > source'. | ||
[code: INVALID_URL_PROTOCOL category: AMP_TAG_PROBLEM see: https://www.ampproject.org/docs/reference/extended/amp-audio.html] | ||
ACTION TAKEN: source.src attribute was removed due to validation issues. | ||
|
||
<track kind="captions" src="//example.dd:8088/sites/default/files/drums.en.vtt" srclang="en" label="English"> on line 7 | ||
- The relative URL '//example.dd:8088/sites/default/files/drums.en.vtt' for attribute 'src' in tag 'amp-audio > track' is disallowed. | ||
[code: DISALLOWED_RELATIVE_URL category: AMP_TAG_PROBLEM] | ||
|
||
|
||
COMPONENT NAMES WITH JS PATH | ||
------------------------------ | ||
'amp-audio', include path 'https://cdn.ampproject.org/v0/amp-audio-0.1.js' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters