-
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
0b987a6
804 : Update allowed AMP tags in sanitizer file.
30064ca
804 : Prevent errors on amp_wp_build.py, update extensions dir.
ba459a2
Create is_mandatory_attribute_missing method
8cff768
804 : Modify PHPUnit tests for tag and attribute validation.
dd807a8
804 : Remove the requirement that 'data-multi-size' be empty.
a698eea
804 : PHPUnit tests for newly-allowed tags.
a066117
804 : Move logic for missing attributes.
cfcf730
804 : Remove the unused $spec_value variable.
8f0a925
804 : Improve variable alignment and add PHPDoc tags.
a400fa5
804 : Exclude the generated allowed tags file from PHPCS checks.
cfc0247
804 : Align 'array' keywords vertically.
7f9a2db
804 : Change the conditional to simply compare to true.
b4bfd81
804 : Array and equal sign alignment fixes.
6f5e39d
804 : Prevent accessing an object as an array.
2b1443e
Suppress DoubleError phpcs errors in test-tag-and-attribute-sanitizer…
westonruter c73f92b
[WIP] AMP HTML bash updater draft
ThierryA f67041d
[WIP] AMP HTML bash updater improvements
ThierryA 236968b
Wrap up AMP HTML bash updater
ThierryA eab6207
804 : Add a contributing.md file, with steps to use build script.
eef4b8b
804 : Test that 'amp-playbuzz' is sanitized properly.
ae92a58
804 : Add allowed protocols to sanitization file.
4597be5
Fix WPCS warning
ThierryA 44554c5
804 : Add to contributing.md, including unit test information.
ed3c93d
Merge branch 'feature/804-allowed-tags' of https://github.com/Automat…
c3b28c0
804 : Merge in develop, resolve conflicts.
4b3ec53
804 : Correct PHPUnit test for <script> validation.
af7b830
804 : Add spaces to correct PHPCS errors.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.DS_Store | ||
vendor |
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,221 @@ | ||
diff --git a/validator/validator_gen_md.py b/validator/validator_gen_md.py | ||
new file mode 100644 | ||
index 000000000..80909fd4c | ||
--- /dev/null | ||
+++ b/validator/validator_gen_md.py | ||
@@ -0,0 +1,215 @@ | ||
+# | ||
+# Copyright 2015 The AMP HTML Authors. All Rights Reserved. | ||
+# | ||
+# 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. | ||
+# | ||
+"""Generates validator-generated.md""" | ||
+ | ||
+import os | ||
+ | ||
+ | ||
+def GenerateValidatorGeneratedMd(specfile, validator_pb2, text_format, out): | ||
+ """Main method for the markdown generator. | ||
+ | ||
+ This method reads the specfile and emits Markdown to sys.stdout. | ||
+ | ||
+ Args: | ||
+ specfile: Path to validator.protoascii. | ||
+ validator_pb2: The proto2 Python module generated from validator.proto. | ||
+ text_format: The text_format module from the protobuf package, e.g. | ||
+ google.protobuf.text_format. | ||
+ out: a list of lines to output (without the newline characters), to | ||
+ which this function will append. | ||
+ """ | ||
+ out.append('<!-- Generated by %s - do not edit. -->' % | ||
+ os.path.basename(__file__)) | ||
+ out.append( | ||
+ '<!-- WARNING: This does not fully reflect validator.protoascii yet. -->') | ||
+ out.append('') | ||
+ out.append('[Accelerated Mobile Pages Project](https://www.ampproject.org)') | ||
+ out.append('') | ||
+ | ||
+ out.append('# validator.protoascii') | ||
+ out.append('') | ||
+ | ||
+ rules = validator_pb2.ValidatorRules() | ||
+ text_format.Merge(open(specfile).read(), rules) | ||
+ if rules.HasField('spec_file_revision'): | ||
+ out.append('* spec file revision: %d' % rules.spec_file_revision) | ||
+ if rules.HasField('min_validator_revision_required'): | ||
+ out.append('* minimum validator revision required: %d' % | ||
+ rules.min_validator_revision_required) | ||
+ out.append('') | ||
+ out.append('Allowed Tags') | ||
+ out.append('') | ||
+ out.append('[TOC]') | ||
+ out.append('') | ||
+ for (field_desc, field_val) in rules.ListFields(): | ||
+ if field_desc.name == 'tags': | ||
+ for tag_spec in field_val: | ||
+ PrintTagSpec(validator_pb2, tag_spec, out) | ||
+ | ||
+ | ||
+def GetLayout(validator_pb2, layout_index): | ||
+ """Helper function that returns the AmpLayout.Layout name for a given index. | ||
+ | ||
+ See amp.validator.AmpLayout.Layout in validator.proto for details. | ||
+ | ||
+ Args: | ||
+ validator_pb2: The proto2 Python module generated from validator.proto. | ||
+ layout_index: integer representing a particular AmpLayout.Layout | ||
+ Returns: | ||
+ A string which represents the name for this supported layout. | ||
+ """ | ||
+ amp_layout = validator_pb2.DESCRIPTOR.message_types_by_name['AmpLayout'] | ||
+ layouts = amp_layout.fields_by_name['supported_layouts'].enum_type.values | ||
+ return layouts[layout_index].name | ||
+ | ||
+ | ||
+def PrintAmpLayout(validator_pb2, amp_layout, out): | ||
+ """Prints a Markdown version of the given proto message (AmpLayout). | ||
+ | ||
+ See amp.validator.AmpLayout in validator.proto for details of proto message. | ||
+ | ||
+ Args: | ||
+ validator_pb2: The proto2 Python module generated from validator.proto. | ||
+ amp_layout: The AmpLayout message. | ||
+ out: A list of lines to output (without newline characters), to which this | ||
+ function will append. | ||
+ """ | ||
+ for layout in amp_layout.supported_layouts: | ||
+ out.append('* %s' % UnicodeEscape(GetLayout(validator_pb2, layout))) | ||
+ if amp_layout.defines_default_width: | ||
+ out.append('* Defines Default Width') | ||
+ if amp_layout.defines_default_height: | ||
+ out.append('* Defines Default Height') | ||
+ | ||
+ | ||
+def PrintAttrSpec(attr_spec, out): | ||
+ """Prints a Markdown version of the given proto message (AttrSpec). | ||
+ | ||
+ See amp.validator.AttrSpec in validator.proto for details of proto message. | ||
+ | ||
+ Args: | ||
+ attr_spec: The AttrSpec message. | ||
+ out: A list of lines to output (without newline characters), to which this | ||
+ function will append. | ||
+ """ | ||
+ out.append('* %s' % UnicodeEscape(attr_spec.name)) | ||
+ if attr_spec.alternative_names: | ||
+ out.append(' * Alternative Names: %s' % | ||
+ RepeatedFieldToString(attr_spec.alternative_names)) | ||
+ if attr_spec.mandatory: | ||
+ out.append(' * Mandatory') | ||
+ if attr_spec.mandatory_oneof: | ||
+ out.append(' * Mandatory One of: %s' % attr_spec.mandatory_oneof) | ||
+ if attr_spec.value: | ||
+ out.append(' * Required Value: %s' % attr_spec.value) | ||
+ | ||
+ | ||
+def PrintTagSpec(validator_pb2, tag_spec, out): | ||
+ """Prints a Markdown version of the given proto message (TagSpec). | ||
+ | ||
+ See amp.validator.TagSpec in validator.proto for details of proto message. | ||
+ | ||
+ Args: | ||
+ validator_pb2: The proto2 Python module generated from validator.proto. | ||
+ tag_spec: The TagSpec message. | ||
+ out: A list of lines to output (without newline characters), to which this | ||
+ function will append. | ||
+ """ | ||
+ header = '## %s' % UnicodeEscape(tag_spec.tag_name) | ||
+ if tag_spec.spec_name and (tag_spec.tag_name != tag_spec.spec_name): | ||
+ header += ': %s' % UnicodeEscape(tag_spec.spec_name) | ||
+ if tag_spec.deprecation: | ||
+ header += ' (DEPRECATED)' | ||
+ header += '{#%s_%s}' % (UnicodeEscape(tag_spec.tag_name).replace(' ', '_'), | ||
+ UnicodeEscape(tag_spec.spec_name).replace(' ', '_')) | ||
+ out.append('') | ||
+ out.append(header) | ||
+ out.append('') | ||
+ if tag_spec.deprecation: | ||
+ out.append('This is deprecated.') | ||
+ out.append('Please see [%s](%s)' % | ||
+ (UnicodeEscape(tag_spec.deprecation), tag_spec.deprecation_url)) | ||
+ out.append('') | ||
+ if tag_spec.mandatory: | ||
+ out.append('* Mandatory') | ||
+ if tag_spec.mandatory_alternatives: | ||
+ out.append('* Mandatory Alternatives: %s' % | ||
+ UnicodeEscape(tag_spec.mandatory_alternatives)) | ||
+ if tag_spec.unique: | ||
+ out.append('* Unique') | ||
+ if tag_spec.mandatory_parent: | ||
+ out.append('* Mandatory Parent: %s' % | ||
+ UnicodeEscape(tag_spec.mandatory_parent)) | ||
+ if tag_spec.mandatory_ancestor: | ||
+ out.append('* Mandatory Ancestor: %s' % | ||
+ UnicodeEscape(tag_spec.mandatory_ancestor)) | ||
+ if tag_spec.mandatory_ancestor_suggested_alternative: | ||
+ out.append('* Mandatory Ancestor Alternative: %s' % | ||
+ UnicodeEscape(tag_spec.mandatory_ancestor_suggested_alternative)) | ||
+ if tag_spec.disallowed_ancestor: | ||
+ out.append('* Disallowed Ancestor: %s' % | ||
+ RepeatedFieldToString(tag_spec.disallowed_ancestor)) | ||
+ if tag_spec.attrs: | ||
+ out.append('') | ||
+ out.append('Allowed Attributes:') | ||
+ out.append('') | ||
+ for attr_spec in tag_spec.attrs: | ||
+ PrintAttrSpec(attr_spec, out) | ||
+ if (tag_spec.amp_layout.supported_layouts or | ||
+ tag_spec.amp_layout.defines_default_width or | ||
+ tag_spec.amp_layout.defines_default_height): | ||
+ out.append('') | ||
+ out.append('Allowed Layouts:') | ||
+ out.append('') | ||
+ PrintAmpLayout(validator_pb2, tag_spec.amp_layout, out) | ||
+ if tag_spec.spec_url: | ||
+ out.append('') | ||
+ out.append('[Spec](%s)' % tag_spec.spec_url) | ||
+ out.append('') | ||
+ | ||
+ | ||
+def RepeatedFieldToString(field): | ||
+ """Helper function which converts a list into an escaped string. | ||
+ | ||
+ Args: | ||
+ field: A list of strings. | ||
+ Returns: | ||
+ A string, segmented by commas. | ||
+ """ | ||
+ return ', '.join([UnicodeEscape(s) for s in field]) | ||
+ | ||
+ | ||
+def UnderscoreToTitleCase(under_score): | ||
+ """Helper function which converts under_score names to TitleCase. | ||
+ | ||
+ Args: | ||
+ under_score: A name, segmented by under_scores. | ||
+ Returns: | ||
+ A name, segmented as TitleCase. | ||
+ """ | ||
+ segments = under_score.split('_') | ||
+ return ' '.join([s.title() for s in segments]) | ||
+ | ||
+ | ||
+def UnicodeEscape(string): | ||
+ """Helper function which escapes unicode characters. | ||
+ | ||
+ Args: | ||
+ string: A string which may contain unicode characters. | ||
+ Returns: | ||
+ An escaped string. | ||
+ """ | ||
+ return ('' + string).encode('unicode-escape') |
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,40 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
# 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 commentThe reason will be displayed to describe this comment to others. Learn more. What I usually do instead of this conditional is:
|
||
|
||
BIN_PATH="$(pwd)" | ||
PROJECT_PATH=$(dirname $PWD) | ||
VENDOR_PATH=$PROJECT_PATH/vendor | ||
|
||
# Create and go to vendor. | ||
if [[ ! -e $VENDOR_PATH ]]; then | ||
mkdir $VENDOR_PATH | ||
fi | ||
cd $VENDOR_PATH | ||
|
||
# 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 commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add an |
||
|
||
# Copy script to location and go there. | ||
cp $BIN_PATH/amphtml-update.py $VENDOR_PATH/amphtml/validator | ||
cd $VENDOR_PATH/amphtml/validator | ||
|
||
# Temporary fix until https://github.com/ampproject/amphtml/issues/12371 is addressed. | ||
if [ ! -f $VENDOR_PATH/amphtml/validator/validator_gen_md.py ]; then | ||
git apply $BIN_PATH/amphtml-fix.diff | ||
fi | ||
|
||
# Install dependencies. | ||
sudo apt-get install python | ||
sudo apt-get install protobuf-compiler | ||
sudo apt-get install python-protobuf | ||
|
||
# Run script. | ||
python amphtml-update.py | ||
cp amp_wp/class-amp-allowed-tags-generated.php ../../../includes/sanitizers/ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 runbash bin/amphtml-update.sh
.