-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#804 Update Allowed Tags And Attributes #823
Conversation
Generate class-amp-allowed-tags-generated.php, Using amp_wp_build.py. Followed the instructions at the top of the file. This is spec file revision 527.
Before, there was an error in: tag_spec.also_requires_tag This didn't always have the property. Also, update the directory of the extensions. The .protoascii file is no longer in the directory '0.1.'
Since the allowed tags and attributes are updated, Several assertions failed. Update them to reflect the new AMP spec. Since there is no longer a protocol whitelist, Remove the assertions for protocols.
This file was generated from a build script, In the repo https://github.com/ampproject/amphtml The spec file seems to require an empty value: extensions/amp-ad/validator-amp-ad.protoascii But examples of <amp-ad> seem to have the value. Like <amp-ad data-multi-size=320x50. @see examples/a4a-multisize.amp.html. So remove the line in the PHP file that requires it to be empty.
Having generated a new file for the allowed tags and attributes, Add these to the data for test_sanitizer(). Also, add unit test for is_missing_mandatory_attribute(). And rename that function from: is_mandatory_attribute_missing().
Before, this was inside an 'else' block. And it should be outside of this, So it applies more broadly. If there is a list of attributes, and one is missing, Remove the node. This was the previous behavior.
This was assigned a value that's used later. There's no need to store it in a variable.
There was some missing documentation. Add this for test_sanitizer and validate_tag_spec_for_node.
This has hundreds of errors and warnings. It's generated from a Python script.
In response to the warnings in Travis. And align an equal sign in test_sanitizer().
Before, there was a failed Travis build. This used boolval(), which caused a fatal error. Instead, simply compare the value to true.
In response to a failed Travis build. Fix an error in an array not being aligned. An move an = sign 2 spaces to the left.
There was an error in the Travis build. A function accessed a DOMElement property as an array. Instead, use the setAttribute() method.
$attr_spec_list = array_merge( $attr_spec_list, $rule_spec_list_to_validate[ $id ][AMP_Rule_Spec::ATTR_SPEC_LIST] ); | ||
foreach ( $spec_ids_sorted as $id ) { | ||
$spec_list = isset( $rule_spec_list_to_validate[ $id ][ AMP_Rule_Spec::ATTR_SPEC_LIST ] ) ? $rule_spec_list_to_validate[ $id ][ AMP_Rule_Spec::ATTR_SPEC_LIST ] : null; | ||
if ( ! $this->is_missing_mandatory_attribute( $spec_list, $node ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This addresses an edge case where there are more than one spec lists, and one of them requires a value. If one of them doesn't require a value, use that instead.
This detects that with $this->is_missing_mandatory_attribute()
. If the node satisfies one of the specs, this doesn't remove it.
if ( $is_mandatory && ! $attribute_exists ) { | ||
$this->remove_node( $node ); | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is mainly decomposed into the function below, is_missing_mandatory_attribute()
.
@@ -430,8 +449,7 @@ private function _sanitize_disallowed_attribute_values_in_node( $node, $attr_spe | |||
foreach ( $attrs_to_remove as $attr_name ) { | |||
if ( isset( $attr_spec_list[$attr_name][AMP_Rule_Spec::ALLOW_EMPTY] ) && | |||
( true == $attr_spec_list[$attr_name][AMP_Rule_Spec::ALLOW_EMPTY] ) ) { | |||
$attr = $node->attributes; | |||
$attr[ $attr_name ]->value = ''; | |||
$node->setAttribute( $attr_name, '' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This raised an error, so it uses the setAttribute()
method instead.
@@ -19,4 +19,5 @@ | |||
<exclude-pattern>*/dev-lib/*</exclude-pattern> | |||
<exclude-pattern>*/node_modules/*</exclude-pattern> | |||
<exclude-pattern>*/vendor/*</exclude-pattern> | |||
<exclude-pattern>*/includes/sanitizers/class-amp-allowed-tags-generated.php</exclude-pattern> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is generated by the Python file, and created hundreds of warnings and errors in PHPCS.
@@ -246,136 +246,6 @@ public function get_attr_spec_rule_data() { | |||
), | |||
'expected' => AMP_Rule_Spec::NOT_APPLICABLE, | |||
), | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The allowed protocol values are removed from the spec file, so this removes the tests for them.
Double-arrow PHPCS errors are suppressed for now via 2b1443e. |
@kienstra I think the next thing here is to look over gitHub issues that seem to be related to the spec being out of date, and confirm that the issues are now resolved with your latest changes. Thanks, @westonruter. I'll look at the other issues surrounding the specification needing to be updated, and confirm if this pull request resolves them. -Ryan |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job on the PR @kienstra. Below is a bash script draft which is still in WIP.
@@ -0,0 +1,40 @@ | |||
#!/bin/bash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is a draft the bash script to update AMP HTML. This script is meant to run in Linux environment (ex. VVV). To run the script, simply cd
to the plugin root and run bash bin/amphtml-update.sh
.
bin/amphtml-update.sh
Outdated
# Go to the right location. | ||
if [[ '.' != $(dirname "$0") ]]; then | ||
cd bin | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I usually do instead of this conditional is:
cd "$(dirname "$0")"
bin/amphtml-update.sh
Outdated
sudo apt-get install git | ||
sudo apt-get install python | ||
sudo apt-get install protobuf-compiler | ||
sudo apt-get install python-protobuf |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can combine all of the dependencies into one call:
sudo apt-get install git python protobuf-compiler python-protobuf
bin/amphtml-update.sh
Outdated
# Clone amphtml repo. | ||
if [[ ! -e $VENDOR_PATH/amphtml ]]; then | ||
git clone https://github.com/ampproject/amphtml amphtml | ||
fi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add an else
here which goes into $VENDOR_PATH/amphtml
and does git pull
?
bin/amphtml-update.sh
Outdated
|
||
# Install dependencies. | ||
sudo apt-get install git | ||
sudo apt-get install python |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should make git
and python
just dependencies that we assume are installed, and exit if the command doesn't exist?
if ! command -v git >/dev/null 2>&1; then
echo "Git is required"
exit 1
fi
if ! command -v python >/dev/null 2>&1; then
echo "Python is required"
exit 1
fi
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are ok with it, I think it is fine to automatically install git
or python
if not installed, the check is pretty quick if it is already installed.
bin/amphtml-update.sh
Outdated
PROJECT_PATH=$(dirname $PWD) | ||
VENDOR_PATH=$PROJECT_PATH/vendor | ||
|
||
if ! apt-get > /dev/null; then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to check if the command exists via: if ! command -v apt-get >/dev/null 2>&1; then
bin/amphtml-update.sh
Outdated
|
||
if ! apt-get > /dev/null; then | ||
echo -e "The AMP HTML uses the apt-get, make sure to run this script in a Linux environment" | ||
exit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do exit 1
so a failure error code is sent. An exit
means just exit 0
which means success.
bin/amphtml-update.sh
Outdated
VENDOR_PATH=$PROJECT_PATH/vendor | ||
|
||
if ! apt-get > /dev/null; then | ||
echo -e "The AMP HTML uses the apt-get, make sure to run this script in a Linux environment" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for -e
@kienstra now that the bash script to update the HTML AMP tags is completed, you may update the PR description with the steps to run the script:
Note that the Checklist
|
@kienstra This PR will close PR #749 as obsolete, correct? Thanks, @westonruter. You're right, PR #749 is obsolete now. -Ryan |
This describes how to use the new script amphtml-update.sh. This is mainly modified from another open-source file. It could probably use more information. Including the workflow for submitting pull requests.
Created Hi @westonruter and @ThierryA, Still, I need to look at the remaining issues for AMP tag support, and see if this PR addresses them. |
As harrisdavid mentioned, before this PR, the 'src' was mandatory. But with the updated AMP spec, it's not.
Before, there was an issue in the file that created: class-amp-allowed-tags-generated.php. amphtml-update.sh had a conditional: if isinstance(value, list) But the allowed_protocol attribute has a type of: google.protobuf.internal.containers.RepeatedScalarFieldContainer So that conditional failed. And allowed_protocol wasn't added. So instead, make that an 'else' statement. That change only resulted in 'allowed_tags' being added.
@@ -0,0 +1,30 @@ | |||
# AMP Contributing Guide | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps add a welcoming sentence (ex. "Thanks for taking the time to contribute!")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @ThierryA. This commit adds your sentence to contributing.md:
Thanks for taking the time to contribute!
2. run `bash bin/amphtml-update.sh` | ||
That script is intended for a Linux environment like [VVV](https://github.com/Varying-Vagrant-Vagrants/VVV). | ||
|
||
### PHPUnit Testing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way the dev lib runs PHPUnit (run_phpunit_local
) needs to run in an environment which has WordPress Unit Test installed, for example VVV. I would advise to add that as a note.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this came up in #828,.along with questions about how to get pre-commit hook working from wp-dev-lib.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @ThierryA. This commit adds your point to contributing.md:
Please run these tests in an environment with WordPress unit tests installed, like [VVV](https://github.com/Varying-Vagrant-Vagrants/VVV).
@kienstra as you go through the PRs and issues that this PR address, could you add the |
Sure, @ThierryA. I'll add |
Add a point that this requires WP unit tests. Also, begin with the welcome that Thierry suggested. Props @ThierryA.
…tic/amp-wp into feature/804-allowed-tags
Added Issues That Are Fixed Hi @ThierryA and @westonruter, #689 (Only the requirement "Minor: The script should do the git clone...") The following issues also look to have been resolved, though not with this PR. I made comments to that effect on the tickets, requesting that the openers verify them: |
In phpcs.xml, retain edits from both branches. There were several conflicts in: class-amp-tag-and-attribute-sanitizer.php These were mainly conflicts with 03c12. Some were only DocBlocks, but some were logic. I mainly resovled them in favor of this branch: feature/804-allowed-tags.
When this has a type of 'application/json,' There is a required ancestor. So create an ancestor of <amp-analytics>.
In response to a failed Travis build. Also, change == to ===.
Resolved Merge Conflicts Hi @westonruter, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Impressive work here! You wrestled with dragons. Let's get these changes in wider testing.
This PR fixes and improves the AMP HTML tag and attributes updater, refer to the ACs for more info...
Steps To Manually Run Script
In order for
amp_wp_build.py
to generateclass-amp-allowed-tags-generated.php
, run these commands, using a Linux environment like VVV:cd /path/to/your/vvv/wp-content/plugins/amp-wp
bash bin/amphtml-update.sh
Note: these steps were edited, as most of these steps are now in the file
bin/amphtml-update.sh
. That file also includes the step:7.
git apply
this commit to re-addvalidator_gen_md.py
, which this commit deletedSee #689 (Only the requirement "Minor: The script should do the git clone...")
Fixes #761
Fixes #772
Fixes #804
Closes #749
Closes #805