PHPackages                             iidestiny/amp - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. iidestiny/amp

ActiveLibrary

iidestiny/amp
=============

A set of useful classes and utilities to convert html to AMP html (See https://www.ampproject.org/)

3.2(1y ago)0346↓100%Apache-2.0PHPPHP &gt;=5.6

Since Mar 15Pushed 1y agoCompare

[ Source](https://github.com/iiDestiny/amp-library)[ Packagist](https://packagist.org/packages/iidestiny/amp)[ Docs](https://github.com/Lullabot/amp-library)[ RSS](/packages/iidestiny-amp/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (8)Versions (5)Used By (0)

[![Build Status](https://camo.githubusercontent.com/97bfed8463c390b4f01a30fbc0e44188786424124952959bcb6dc5dcf11be86c/68747470733a2f2f7472617669732d63692e6f72672f4c756c6c61626f742f616d702d6c6962726172792e7376673f6272616e63683d6d61696e)](https://travis-ci.org/Lullabot/amp-library)

AMP PHP Library
===============

[](#amp-php-library)

An open source PHP library and console utility to convert HTML to [AMP HTML](https://www.ampproject.org/) and report HTML compliance with the AMP HTML specification.

### What is the AMP PHP Library?

[](#what-is-the-amp-php-library)

The AMP PHP Library is an open source and pure PHP Library that:

- Works with whole or partial HTML documents (or strings). Specifically, the AMP PHP Library:
- Reports compliance of a whole/partial HTML document with the [AMP HTML specification](https://www.ampproject.org/). We implement an AMP HTML validator in pure PHP to report compliance of an arbitrary HTML document / HTML fragment with the AMP HTML standard. This validator is a ported subset of the [canonical validator](https://github.com/ampproject/amphtml/tree/main/validator) that is implemented in JavaScript
    - Specifically, the PHP validator supports tag specification validation, attribute specification validation, CDATA validation, CSS validation, layout validation, template validation and attribute property-value pair validation. It will report tags and attributes that are missing, illegal, mandatory according to spec but not present, unique according to spec but multiply present, having wrong parents or ancestors or children and so forth.
    - *Note*: while the AMP PHP library (already) supports many of the features and capabilities of the canonical validator, it is not intended to achieve parity in *every* respect with the canonical validator. Even *within* the features we support (e.g. CSS validation) there may be certain validation issues that we don't flag but the canonical validator does.
- Using the feedback given by the in-house PHP validator, the AMP PHP library tries to "correct" some issues found in the HTML to make it more AMP HTML compliant. This would, for example, involve:
    - Removing illegal attributes e.g. `style` attribute within `` tag
    - Removing all kinds of illegal tags e.g. `` within `` tag, a tag with a disallowed ancestor, a duplicate unique tag etc.
    - Removing illegal property value pairs e.g. removing `minimum-scale=hello` from ``
    - Adding or correcting the tags necessary for a minimally valid AMP document:
        - ``, ``, `meta viewport`, `meta charset`, `` and `` tags
        - The `link rel=canonical` tag if you let the library know the canonical path of the document
        - Javascript `` tags for the various AMP components and generic AMP Javascript `` tag
        - Boilerplate CSS
    - If there are mutually exclusive attributes for a tag, removing all but one of them
    - Fixing issues with `amp-img` tags that have problems like inconsistent units, invalid attributes, missing mandatory attributes, invalid implied or specified layouts.
    - *Notes*:
        - The library does a decent job of *removing* bad things and in a few cases makes some corrections/additions to the HTML. As the library cannot understand the true *intention* of the user, a lot the validation problems in the HTML may eventually need to be fixed manually by the human.
        - In general, the library will try to fix validation errors in `` and if its not successful in doing so, *remove* those tags from ``. Within `` the AMP PHP library is less aggressive and in most cases will *not* remove the tag from the document if the tag does not validate after it attempts any fixes on it.
        - The library needs to be provided with well formed HTML / HTML5. Please don't give it faulty, incorrect html (e.g. non closed `` tags etc). The correction it does is related to AMP HTML standard issues only. Use a HTML tidying library if you expect your HTML to be malformed.
- Converts some non-amp elements to their AMP equivalents automatically
    - A `` tag is converted to an `` tag
    - A `` tag is converted to an `` tag
    - A [``](https://github.com/Lullabot/amp-library/blob/main/tests/test-data/fragment-html/audio-to-amp-audio-conversion-fragment.html) tag is converted to an `` tag
    - A [``](https://github.com/Lullabot/amp-library/blob/main/tests/test-data/fragment-html/video-fragment-and-placeholder-test.html) tag is converted to an `` tag
    - [Twitter embed code](https://github.com/Lullabot/amp-library/blob/main/tests/test-data/fragment-html/twitter-fragment.html) for tweets is converted to an `` tag.
    - [Instagram embed code](https://github.com/Lullabot/amp-library/blob/main/tests/test-data/fragment-html/instagram-fragment.html) for instagrams is converted to an `` tag.
    - [Youtube embed code](https://github.com/Lullabot/amp-library/blob/main/tests/test-data/fragment-html/youtube-fragment.html) for videos is converted to an `` tag
    - [Dailymotion embed code](https://github.com/Lullabot/amp-library/blob/main/tests/test-data/fragment-html/dailymotion-fragment.html) for videos is converted to an `` tag
    - [Pinterest embed code](https://github.com/Lullabot/amp-library/blob/main/tests/test-data/fragment-html/pinterest-fragment.html) for pins is converted to an `` tag
    - [Soundcloud embed code](https://github.com/Lullabot/amp-library/blob/main/tests/test-data/fragment-html/soundcloud-fragment.html) for audio music is converted to an `` tag
    - [Vimeo embed code](https://github.com/Lullabot/amp-library/blob/main/tests/test-data/fragment-html/vimeo-fragment.html) for videos is converted to an `` tag
    - Facebook [iframe](https://github.com/Lullabot/amp-library/blob/main/tests/test-data/fragment-html/facebook-iframe-fragment.html) and [Javascript SDK](https://github.com/Lullabot/amp-library/blob/main/tests/test-data/fragment-html/facebook-non-iframe-fragment.html) embed code for posts and videos is converted to an `` tag
    - *Notes*:
        - Some of these embed code conversions may not have the advanced features you may require. File an issue if you need enhancements to the functionality already provided or new embed code conversions
        - Some of the embed codes have an associated `` tag. These conversions will work even if no `` tag was added to your HTML document. The AMP library will add the appropriate AMP component `` tag to the `` if it is provided a full html document.
        - You may experiment with the command line utility `amp-console` on the above HTML fragments to see how the converted HTML looks
- Provides both a console and programmatic interface with which to call the library. It works like this: the developer first provides some HTML. After processing it, the library returns:
    - The AMPized HTML
    - A list of validation errors in the HTML provided
    - A description of fixes and embed code conversions made to the HTML

### Use Cases

[](#use-cases)

- Currently the AMP PHP Library is used by the [Drupal AMP Module](https://www.drupal.org/project/amp) to report issues with user entered, arbitrary HTML (originating from Rich Text Editors) and converting the HTML to AMPized HTML (as much as possible)
- The AMP PHP Library command line validator can be used for experimentation and to do HTML to AMP HTML conversion of HTML files. While the [canonical validator](https://github.com/ampproject/amphtml/tree/main/validator) only validates, our library tries to make corrections too. As noted above, our validator is a subset of the canonical validator but already covers a lot of cases
- The AMP PHP Library can be used in any other PHP project to "convert" HTML to AMP HTML and report validation issues. It does not have any non-PHP dependencies and will work in PHP 5.5 and higher. It will also work in recent versions of [HHVM](http://hhvm.com/).

### Setup

[](#setup)

The project uses a [composer](https://getcomposer.org/) workflow. If you're not familiar with composer then please read up on it before trying to set this up.

Using this in Drupal requires some specific steps. Please refer to the [Drupal AMP Module](https://www.drupal.org/project/amp) documentation.

For all other scenarios, continue reading.

#### Setup for command line console

[](#setup-for-command-line-console)

`git clone` this repo, `cd` into it and type in `$ composer install` at the command prompt to get all the dependencies of the library. Now you'll be able to use the command line AMP html converter `amp-console` (or equivalently `amp-console.php`

##### Running phpunit tests

[](#running-phpunit-tests)

After doing a `$ composer install` for setting up the command line console, you can run some [phpunit](https://phpunit.de/) tests

```
$ vendor/bin/phpunit tests
```

##### Looking at test coverage

[](#looking-at-test-coverage)

To see test coverage data first ensure you have the xdebug extenstion installed in your PHP installation.

```
$ php -m | grep xdebug # should output xdebug
$ vendor/bin/phpunit tests --coverage-html=coverage-data
$ cd coverage-data
$ firefox index.html
```

#### Setup for your composer based PHP project

[](#setup-for-your-composer-based-php-project)

To use this in your composer based PHP project, refer to [composer docs here](https://getcomposer.org/doc/05-repositories.md#loading-a-package-from-a-vcs-repository) to make changes to your `composer.json`

Or you can simply do `$ composer require lullabot/amp:"^1.0.0"` to fetch the library from [here](https://packagist.org/packages/lullabot/amp) and automatically update your `composer.json`

##### Advanced

[](#advanced)

Should you wish to follow the bleeding edge you can do `$ composer require lullabot/amp:"dev-main"`. Note that this will create a `.git` folder in `vendor/lullabot/amp`. If you want to avoid that, do `$ composer require lullabot/amp:"dev-master" --prefer-dist`

### Using the command line `amp-console`

[](#using-the-command-line-amp-console)

```
$ cd
# Do this if you haven't already
$ composer install
$ ./amp-console amp:convert --help
$ ./amp-console amp:convert
```

Please note that the `--help` command line option is your friend. Use that when confused!

A few example HTML files are available in the test-html folder for you to test drive so that you can get a flavor of the AMP PHP library.

```
$ ./amp-console amp:convert sample-html/sample-html-fragment.html
$ ./amp-console amp:convert sample-html/several_errors.html --full-document
```

Note that you need to provide `--full-document` if you're providing a full html document file for conversion.

Lets see the output of the first example command above. The first few lines is the AMPized HTML provided by our library. The rest of the headings are self explanatory.

```
$ cd
$ ./amp-console amp:convert sample-html/sample-html-fragment.html
Line  1: Run
Line  2: CNN
Line  3:
Line  4: BBC
Line  5:
Line  6: This is a sample sample paragraph
Line  7:
Line  8:
Line  9:
Line 10:

ORIGINAL HTML
---------------
Line  1: Run
Line  2: CNN
Line  3:
Line  4: BBC
Line  5:
Line  6: This is a sample sample paragraph
Line  7:
Line  8:
Line  9:
Line 10:

Transformations made from HTML tags to AMP custom tags
-------------------------------------------------------

 at line 3
 ACTION TAKEN: img tag was converted to the amp-img tag.

 at line 7
 ACTION TAKEN: iframe tag was converted to the amp-iframe tag.

AMP-HTML Validation Issues and Fixes
-------------------------------------
FAIL

 on line 1
- The attribute 'style' may not appear in tag 'a'.
   [code: DISALLOWED_ATTR  category: DISALLOWED_HTML]
   ACTION TAKEN: a.style attribute was removed due to validation issues.
- Invalid URL protocol 'javascript:' for attribute 'href' in tag 'a'.
   [code: INVALID_URL_PROTOCOL  category: DISALLOWED_HTML]
   ACTION TAKEN: a.href attribute was removed due to validation issues.

 on line 2
- The attribute 'style' may not appear in tag 'a'.
   [code: DISALLOWED_ATTR  category: DISALLOWED_HTML]
   ACTION TAKEN: a.style attribute was removed due to validation issues.
- The attribute 'target' in tag 'a' is set to the invalid value '_parent'.
   [code: INVALID_ATTR_VALUE  category: DISALLOWED_HTML]
   ACTION TAKEN: a.target attribute was removed due to validation issues.

 on line 5
- The tag 'input' is disallowed.
   [code: DISALLOWED_TAG  category: DISALLOWED_HTML]
   ACTION TAKEN: input tag was removed due to validation issues.

 on line 6
- The attribute 'onmouseover' may not appear in tag 'div'.
   [code: DISALLOWED_ATTR  category: DISALLOWED_HTML]
   ACTION TAKEN: div.onmouseover attribute was removed due to validation issues.

 on line 8
- The tag 'script' is disallowed except in specific forms.
   [code: GENERAL_DISALLOWED_TAG  category: CUSTOM_JAVASCRIPT_DISALLOWED]
   ACTION TAKEN: script tag was removed due to validation issues.

 on line 9
- The parent tag of tag 'style' is 'body', but it can only be 'head'.
   [code: WRONG_PARENT_TAG  category: DISALLOWED_HTML see: https://www.ampproject.org/docs/reference/spec.html#required-markup]
   ACTION TAKEN: style tag was removed due to validation issues.
```

### Using the library in a composer based PHP project

[](#using-the-library-in-a-composer-based-php-project)

First, follow the setup steps above if you're using this in a composer based project.

Sample code to get started:

```
