Skip to content

Commit

Permalink
Include requires_extension from spec in AMP_Allowed_Tags_Generated
Browse files Browse the repository at this point in the history
* Use spec to determine which script components get enqueued
* Use the latest component instead of 0.1
* Reduce duplicated logic by defining get_scripts() method on base sanitizer
* Add get_allowed_tag_data method on AMP_Allowed_Tags_Generated to reduce size of array passing.
* Define sanitized_tag class variable on sanitizer classes
  • Loading branch information
westonruter committed Jan 19, 2018
1 parent 2447165 commit 26c53c1
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 175 deletions.
72 changes: 55 additions & 17 deletions bin/amphtml-update.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Follow the steps below to generate a new version of the allowed tags class:
- Download a copy of the latet AMPHTML repository from github:
git clone [email protected]:ampproject/amphtml.git
- Copy this file into the repo's validator subdirectory:
Expand Down Expand Up @@ -149,6 +149,8 @@ def GenerateHeaderPHP(out):
out.append(' * Note: This file only contains tags that are relevant to the `body` of')
out.append(' * an AMP page. To include additional elements modify the variable')
out.append(' * `mandatory_parent_blacklist` in the amp_wp_build.py script.')
out.append(' *')
out.append(' * phpcs:ignoreFile')
out.append(' */')
out.append('class AMP_Allowed_Tags_Generated {')
out.append('')
Expand Down Expand Up @@ -223,15 +225,15 @@ def GenerateAttributesPHP(out, attributes, indent_level = 4):
indent = ''
for i in range(0,indent_level):
indent += '\t'

sorted_attributes = sorted(attributes.items())
for (attribute, values) in collections.OrderedDict(sorted_attributes).iteritems():
logging.info('generating php for attribute: %s...' % attribute.lower())
out.append('%s\'%s\' => array(' % (indent, attribute.lower()))
GeneratePropertiesPHP(out, values)
out.append('%s),' % indent)
logging.info('...done with: %s' % attribute.lower())

out.append('')
logging.info('... done')

Expand Down Expand Up @@ -305,26 +307,56 @@ def GenerateFooterPHP(out):
logging.info('entering ...')

# Output the footer.
out.append('\tpublic static function get_allowed_tags() {')
out.append('\t\treturn self::$allowed_tags;')
out.append('\t}')
out.append('')
out.append('''
/**
* Get allowed tags.
*
* @since 0.5
* @return array Allowed tags.
*/
public static function get_allowed_tags() {
return self::$allowed_tags;
}
/**
* Get allowed tag data.
*
* @since 0.7
* @param string $tag_name Tag name.
* @return array|null Allowed tag data.
*/
public static function get_allowed_tag_data( $tag_name ) {
if ( isset( self::$allowed_tags[ $tag_name ] ) ) {
return self::$allowed_tags[ $tag_name ];
}
return null;
}
/**
* Get list of globally-allowed attributes.
*
* @since 0.5
* @return array Allowed tag.
*/
public static function get_allowed_attributes() {
return self::$globally_allowed_attrs;
}
/**
* Get layout attributes.
*
* @since 0.5
* @return array Allowed tag.
*/
public static function get_layout_attributes() {
return self::$layout_allowed_attrs;
}''')

out.append('\tpublic static function get_allowed_attributes() {')
out.append('\t\treturn self::$globally_allowed_attrs;')
out.append('\t}')
out.append('')

out.append('\tpublic static function get_layout_attributes() {')
out.append('\t\treturn self::$layout_allowed_attrs;')
out.append('\t}')
out.append('')

out.append('}')
out.append('')

out.append('?>')
out.append('')
logging.info('... done')


Expand Down Expand Up @@ -434,6 +466,12 @@ def GetTagRules(tag_spec):
also_requires_tag_list.append(UnicodeEscape(also_requires_tag))
tag_rules['also_requires_tag'] = {'also_requires_tag': also_requires_tag_list}

if hasattr(tag_spec, 'requires_extension') and len( tag_spec.requires_extension ) != 0:
requires_extension_list = []
for requires_extension in tag_spec.requires_extension:
requires_extension_list.append(requires_extension)
tag_rules['requires_extension'] = {'requires_extension': requires_extension_list}

if tag_spec.disallowed_ancestor:
disallowed_ancestor_list = []
for disallowed_ancestor in tag_spec.disallowed_ancestor:
Expand Down
3 changes: 2 additions & 1 deletion bin/amphtml-update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ fi

# Run script.
python amphtml-update.py
cp amp_wp/class-amp-allowed-tags-generated.php ../../../includes/sanitizers/
mv amp_wp/class-amp-allowed-tags-generated.php ../../../includes/sanitizers/
rm -r amp_wp
Loading

0 comments on commit 26c53c1

Please sign in to comment.