Skip to content

Commit

Permalink
Merge pull request #1883 from WordPress/feature/fix-up-i18n-sniff
Browse files Browse the repository at this point in the history
Clean up the I18n sniff
  • Loading branch information
GaryJones committed Apr 11, 2020
2 parents 02af9f6 + 04fc205 commit 3ee6c4f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions WordPress/Sniffs/WP/I18nSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,8 @@ protected function check_text( $context ) {
*
* Strip placeholders and surrounding quotes.
*/
$content_without_surrounding_quotes = trim( $this->strip_quotes( $content ) );
$non_placeholder_content = preg_replace( self::SPRINTF_PLACEHOLDER_REGEX, '', $content_without_surrounding_quotes );
$content_without_quotes = trim( $this->strip_quotes( $content ) );
$non_placeholder_content = preg_replace( self::SPRINTF_PLACEHOLDER_REGEX, '', $content_without_quotes );

if ( '' === $non_placeholder_content ) {
$this->phpcsFile->addError( 'Strings should have translatable content', $stack_ptr, 'NoEmptyStrings' );
Expand All @@ -644,7 +644,7 @@ protected function check_text( $context ) {
* Strip surrounding quotes.
*/
$reader = new \XMLReader();
$reader->XML( $content_without_surrounding_quotes, 'UTF-8', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING );
$reader->XML( $content_without_quotes, 'UTF-8', LIBXML_NOERROR | LIBXML_ERR_NONE | LIBXML_NOWARNING );

// Is the first node an HTML element?
if ( ! $reader->read() || \XMLReader::ELEMENT !== $reader->nodeType ) {
Expand All @@ -653,10 +653,13 @@ protected function check_text( $context ) {

// If the opening HTML element includes placeholders in its attributes, we don't warn.
// E.g. '<option id="%1$s" value="%2$s">Translatable option name</option>'.
for ( $i = 0; $attr = $reader->getAttributeNo( $i ); $i++ ) {
if ( preg_match( self::SPRINTF_PLACEHOLDER_REGEX, $attr ) ) {
$i = 0;
while ( $attr = $reader->getAttributeNo( $i ) ) {
if ( preg_match( self::SPRINTF_PLACEHOLDER_REGEX, $attr ) === 1 ) {
return;
}

++$i;
}

// We don't flag strings wrapped in `<a href="...">...</a>`, as the link target might actually need localization.
Expand All @@ -665,7 +668,7 @@ protected function check_text( $context ) {
}

// Does the entire string only consist of this HTML node?
if ( $reader->readOuterXml() === $content_without_surrounding_quotes ) {
if ( $reader->readOuterXml() === $content_without_quotes ) {
$this->phpcsFile->addWarning( 'Strings should not be wrapped in HTML', $stack_ptr, 'NoHtmlWrappedStrings' );
}
}
Expand Down

0 comments on commit 3ee6c4f

Please sign in to comment.