-
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
Don't tree-shake allowed child classes of amp-date-picker
if it's in the DOM
#4339
Changes from all commits
8100ff6
28d037e
bd9313d
686e742
36cba7d
afab46f
5bffd65
eea565f
01a8e8f
d83354e
b6f1a23
19507af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -661,6 +661,12 @@ private function has_used_class_name( $class_names ) { | |
} | ||
continue 2; | ||
} | ||
} elseif ( ctype_upper( $class_name[0] ) && $this->has_used_tag_names( [ 'amp-date-picker' ] ) && $this->is_class_allowed_in_amp_date_picker( $class_name ) ) { | ||
// If the document has an amp-date-picker tag, check if this class is an allowed child of it. | ||
// That component's child classes won't be present yet in the document, so prevent tree-shaking valid classes. | ||
// The ctype_upper() check is an optimization since we know up front that all class names in React Dates are | ||
// in CamelCase form, thus we can short-circut if the first character of the class name is not upper-case. | ||
continue; | ||
} | ||
|
||
if ( ! isset( $this->used_class_names[ $class_name ] ) ) { | ||
|
@@ -756,6 +762,32 @@ private function has_used_attributes( $attribute_names ) { | |
return true; | ||
} | ||
|
||
/** | ||
* Whether a given class is allowed to be styled in <amp-date-picker>. | ||
* | ||
* That component has child classes that won't be present in the document yet. | ||
* So get whether a class is an allowed child. | ||
* | ||
* @since 1.5.0 | ||
* @link https://github.com/airbnb/react-dates/tree/05356/src/components | ||
* | ||
* @param string $class The name of the class to evaluate. | ||
* @return bool Whether the class is allowed as a child of <amp-date-picker>. | ||
*/ | ||
private function is_class_allowed_in_amp_date_picker( $class ) { | ||
static $class_prefixes = [ | ||
'CalendarDay', | ||
'CalendarMonth', | ||
'CalendarMonthGrid', | ||
'DayPicker', | ||
kienstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'DayPickerKeyboardShortcuts', | ||
'DayPickerNavigation', | ||
'KeyboardShortcutRow', | ||
Comment on lines
+779
to
+785
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. I'm using this function in the console: function gatherClassNames( elements ) {
const classNames = new Set();
const add = ( className ) => {
if ( !/^i-amphtml-/.test( className ) ) {
classNames.add( className.replace( /_.+/, '' ) );
}
};
for ( const element of elements ) {
element.classList.forEach( add );
gatherClassNames( element.children ).forEach( add );
}
return classNames;
} And I'm invoking it via: gatherClassNames(document.querySelectorAll('amp-date-picker')) I'm not able to find any other class names in use when looking at the 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. Thanks, great function to get the class names. 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. The |
||
]; | ||
|
||
return in_array( strtok( $class, '_' ), $class_prefixes, true ); | ||
} | ||
|
||
/** | ||
* Run logic before any sanitizers are run. | ||
* | ||
|
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 might be missing some classes, though I tried to be thorough.
I looked at the components that have styles.
This doesn't include components that are just an
.svg
, like CalendarIcon.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 any of these component names get carried over to be class names in the DOM? If so, they should be included in this list.
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, all of these appear as names in the DOM as classes, except for
DateInput
,DateRangePicker
,DateRangePicker
:For those 3 that didn't appear, I couldn't find them, when using most of the snippets in the amp-date-picker documentation. I should probably remove those, though maybe I just missed a case where they are used.
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 3 that I couldn't find in the DOM as class names are removed in 5bffd65