Skip to content
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

Remove the glossary tooltip inside the HTML tags #1679

Merged
merged 12 commits into from
Sep 18, 2023

Conversation

amieiro
Copy link
Member

@amieiro amieiro commented Aug 24, 2023

Problem

When GlotPress has an HTML tag in the glossary, it shows the glossary tooltip in the HTML tag.

image

Fix #1382 and #1385.

Solution

This PR excludes the glossary tooltips inside the HTML tags, splitting the original strings (singular and plural) to avoid adding the glossary tooltips to the HTML tags.

image

Testing Instructions

This PR adds 2 tests:

  • test_map_glossary_entries_to_translation_originals_excluding_terms_in_html_tags.
  • test_map_glossary_entries_to_translation_originals_excluding_terms_in_html_tags_in_the_plural_origin.

To test it manually, you can:

  1. Add an HTML tag (e.g., strong) to the glossary.
  2. Add 2 new originals to be translated (singular and plural). E.g.:
  • Singular: This is s strong test <strong>strong</strong>. This is another<dd>strong</dd>, very strong test with<img src="strong.img" /> images <hr/>.
  • Plural: Plural. This is s strong test <strong>strong</strong>. This is another<dd>strong</dd>, very strong test with<img src="strong.img" /> images <hr/>.
  1. Check that the tooltip for the strongword only appears outside the HTML tags.

@amieiro amieiro requested review from akirk and trymebytes August 24, 2023 09:05
@amieiro amieiro requested a review from ocean90 August 25, 2023 09:43
@akirk
Copy link
Member

akirk commented Sep 6, 2023

Could we avoid adding multiple preg_splits by just checking the previous chunk like this?

foreach ( $singular_split as $k => $chunk ) {
	// [...]
	if ( $k > 0 && ( '<' === substr( $singular_split[ $k - 1 ], -1 ) || '</' === substr( $singular_split[ $k - 1 ], -2 ) ) ) {
		$singular_combined .= $escaped_chunk;
		continue;
	}

@akirk
Copy link
Member

akirk commented Sep 6, 2023

This patch also makes the test pass:

diff --git i/gp-templates/helper-functions.php w/gp-templates/helper-functions.php
index 2b43b50b..645a6295 100644
--- i/gp-templates/helper-functions.php
+++ w/gp-templates/helper-functions.php
@@ -230,10 +230,15 @@ function map_glossary_entries_to_translation_originals( $translation, $glossary
 	if ( is_array( $singular_split ) ) {
 		$singular_combined = '';
 
-		foreach ( $singular_split as $chunk ) {
+		foreach ( $singular_split as $k => $chunk ) {
 			// Create an escaped version for use later on.
 			$escaped_chunk = esc_translation( $chunk );
 
+			if ( $k > 0 && ( '<' === substr( $singular_split[ $k - 1 ], -1 ) || '"' === substr( $singular_split[ $k - 1 ], -1 ) || '</' === substr( $singular_split[ $k - 1 ], -2 ) ) ) {
+				$singular_combined .= $escaped_chunk;
+				continue;
+			}
+
 			// Create a lower case version to compare with the glossary terms.
 			$lower_chunk = strtolower( $chunk );
 
@@ -285,10 +290,16 @@ function map_glossary_entries_to_translation_originals( $translation, $glossary
 		if ( is_array( $plural_split ) ) {
 			$plural_combined = '';
 
-			foreach ( $plural_split as $chunk ) {
+			foreach ( $plural_split as $k => $chunk ) {
 				// Create an escaped version for use later on.
 				$escaped_chunk = esc_translation( $chunk );
 
+
+				if ( $k > 0 && ( '<' === substr( $plural_split[ $k - 1 ], -1 ) || '"' === substr( $plural_split[ $k - 1 ], -1 ) || '</' === substr( $plural_split[ $k - 1 ], -2 ) ) ) {
+					$plural_combined .= $escaped_chunk;
+					continue;
+				}
+
 				// Create a lower case version to compare with the glossary terms.
 				$lower_chunk = strtolower( $chunk );

@amieiro
Copy link
Member Author

amieiro commented Sep 6, 2023

@akirk with the current approach we don't show the glossary suggestions into some elements like title or alt elements.

image

I think the best approach will be to remove these checks:

'"' === substr( $singular_split[ $k - 1 ], -1 ) ||
 '"' === substr( $plural_split[ $k - 1 ], -1 ) ||

image

And then focus on special situations like src into the img tag, href into the a tag and another similar situations.

@akirk
Copy link
Member

akirk commented Sep 6, 2023

We can change it to something like ('"' === substr( $singular_split[ $k - 1 ], -1 ) && ! ( 'alt="' === substr( $singular_split[ $k - 1 ], -5 ) || 'title="' === substr( $singular_split[ $k - 1 ], -7 ) ) ) etc. Best to refactor it into a new function.

@amieiro
Copy link
Member Author

amieiro commented Sep 7, 2023

Now it maintains the glossary helper inside the HTML tags with the alt and title elements.

image

Copy link
Member

@akirk akirk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think making that function simpler would be good.

@amieiro
Copy link
Member Author

amieiro commented Sep 14, 2023

With the previous approach used in the should_skip_element function, we detect some glossary word that should be skipped:

image

With the new approach, we resolve these false positives:

image

I prefer to put this code in a new function because we use this code twice in the map_glossary_entries_to_translation_originals function, and to avoid increasing the size of this function (currently, it has 193 lines).

Copy link
Member

@akirk akirk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but I am still a bit unhappy about the complexity of the function. Check how it can be done with a state machine here: 29bc1c4

@amieiro
Copy link
Member Author

amieiro commented Sep 18, 2023

@akirk I review your commit 29bc1c4, and I think your approach is better, so, can you add this commit to this PR to close it?

@akirk
Copy link
Member

akirk commented Sep 18, 2023

Could you cherry-pick the commit?

@amieiro amieiro enabled auto-merge (squash) September 18, 2023 17:22
@amieiro amieiro merged commit aaa034e into GlotPress:develop Sep 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Glossary: Don't provide a tooltip for HTML tags
2 participants