From d836b26c007dcb8ef6575751ee60c8688e672c7b Mon Sep 17 00:00:00 2001 From: Martijn van der Klis Date: Fri, 15 Dec 2023 12:31:24 +0100 Subject: [PATCH 1/4] Improves handling of   characters for position-based highlighing in general and the keyphrase density assessment in particular --- packages/js/src/watchers/elementorWatcher.js | 3 +- .../helpers/word/getWordsForHTMLParserSpec.js | 4 +-- .../researches/keywordCountSpec.js | 32 +++++++++++------ .../yoastseo/spec/parse/build/buildSpec.js | 36 +++++++++---------- packages/yoastseo/src/helpers/htmlEntities.js | 1 + .../helpers/word/getWordsForHTMLParser.js | 5 +-- 6 files changed, 47 insertions(+), 34 deletions(-) diff --git a/packages/js/src/watchers/elementorWatcher.js b/packages/js/src/watchers/elementorWatcher.js index 897d5726f4b..b86fae2c138 100644 --- a/packages/js/src/watchers/elementorWatcher.js +++ b/packages/js/src/watchers/elementorWatcher.js @@ -59,9 +59,8 @@ function getContent( editorDocument ) { editorDocument.$element.find( ".elementor-widget-container" ).each( ( index, element ) => { // We remove \n and \t from the HTML as Elementor formats the HTML after saving. // As this spacing is purely cosmetic, we can remove it for analysis purposes. - // We also convert   elements to regular spaces. // When we apply the marks, we do need to make the same amendments. - const rawHtml = element.innerHTML.replace( /[\n\t]/g, "" ).replace( / /g, " " ).trim(); + const rawHtml = element.innerHTML.replace( /[\n\t]/g, "" ).trim(); content.push( rawHtml ); } ); diff --git a/packages/yoastseo/spec/languageProcessing/helpers/word/getWordsForHTMLParserSpec.js b/packages/yoastseo/spec/languageProcessing/helpers/word/getWordsForHTMLParserSpec.js index 3898addf59c..0283560de88 100644 --- a/packages/yoastseo/spec/languageProcessing/helpers/word/getWordsForHTMLParserSpec.js +++ b/packages/yoastseo/spec/languageProcessing/helpers/word/getWordsForHTMLParserSpec.js @@ -54,8 +54,8 @@ const testCases = [ }, { description: "correctly tokenizes a phrase that is separated by non-breaking spaces", - text: "a\u00a0phrase\u00a0that\u00a0is\u00a0separated\u00a0by\u00a0non-breaking\u00a0spaces", - expectedResult: [ "a", "\u00a0", "phrase", "\u00a0", "that", "\u00a0", "is", "\u00a0", "separated", "\u00a0", "by", "\u00a0", + text: "a\u00a0phrase#nbsp;that\u00a0is#nbsp;separated\u00a0by#nbsp;non-breaking\u00a0spaces", + expectedResult: [ "a", "\u00a0", "phrase", "#nbsp;", "that", "\u00a0", "is", "#nbsp;", "separated", "\u00a0", "by", "#nbsp;", "non-breaking", "\u00a0", "spaces" ], }, { diff --git a/packages/yoastseo/spec/languageProcessing/researches/keywordCountSpec.js b/packages/yoastseo/spec/languageProcessing/researches/keywordCountSpec.js index 31614969e6e..4ec0f219240 100644 --- a/packages/yoastseo/spec/languageProcessing/researches/keywordCountSpec.js +++ b/packages/yoastseo/spec/languageProcessing/researches/keywordCountSpec.js @@ -543,13 +543,25 @@ const testCasesWithSpecialCharacters = [ keyphraseForms: [ [ "key" ], [ "word" ] ], expectedCount: 1, expectedMarkings: [ - new Mark( { marked: "a string with nbsp to match the key word.", - original: "a string with nbsp to match the key word.", + new Mark( { marked: "a string with nbsp to match the key word.", + original: "a string with nbsp to match the key word.", position: { startOffset: 35, - endOffset: 43, + endOffset: 38, startOffsetBlock: 32, - endOffsetBlock: 40, + endOffsetBlock: 35, + attributeId: "", + clientId: "", + isFirstSection: false, + }, + } ), + new Mark( { marked: "a string with nbsp to match the key word.", + original: "a string with nbsp to match the key word.", + position: { + startOffset: 44, + endOffset: 48, + startOffsetBlock: 41, + endOffsetBlock: 45, attributeId: "", clientId: "", isFirstSection: false, @@ -1064,10 +1076,10 @@ const testCasesWithSpecialCharacters = [ marked: " It's also about trying to make your brand synonymous with a certain product or service.", original: " It’s also about trying to make your brand synonymous with a certain product or service.", position: { - startOffset: 295, - endOffset: 305, - startOffsetBlock: 266, - endOffsetBlock: 276, + startOffset: 305, + endOffset: 315, + startOffsetBlock: 276, + endOffsetBlock: 286, attributeId: "", clientId: "", isFirstSection: false, @@ -1227,8 +1239,8 @@ const testCasesWithSpecialCharacters = [ expectedCount: 2, expectedMarkings: [ new Mark( { - marked: "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", - original: "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", + marked: "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", + original: "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", position: { startOffset: 149, endOffset: 154, diff --git a/packages/yoastseo/spec/parse/build/buildSpec.js b/packages/yoastseo/spec/parse/build/buildSpec.js index cad0ea56596..25cdfc4cf24 100644 --- a/packages/yoastseo/spec/parse/build/buildSpec.js +++ b/packages/yoastseo/spec/parse/build/buildSpec.js @@ -6,8 +6,8 @@ import memoizedSentenceTokenizer from "../../../src/languageProcessing/helpers/s import splitIntoTokensCustom from "../../../src/languageProcessing/languages/ja/helpers/splitIntoTokensCustom"; describe( "The parse function", () => { - it( "parses a basic HTML text with an HTML entity (&)", () => { - const html = "

Hello, world & beyond!

"; + it( "parses a basic HTML text with HTML entities (  and &)", () => { + const html = "

Hello, world & beyond!

"; const paper = new Paper( html ); const researcher = Factory.buildMockResearcher( {}, true, false, false, @@ -21,14 +21,14 @@ describe( "The parse function", () => { name: "div", sourceCodeLocation: { startOffset: 0, - endOffset: 58, + endOffset: 63, startTag: { startOffset: 0, endOffset: 5, }, endTag: { - startOffset: 52, - endOffset: 58, + startOffset: 57, + endOffset: 63, }, }, attributes: {}, @@ -40,34 +40,34 @@ describe( "The parse function", () => { }, sentences: [ { text: "Hello, world & beyond!", - sourceCodeRange: { startOffset: 22, endOffset: 48 }, + sourceCodeRange: { startOffset: 22, endOffset: 53 }, tokens: [ { text: "Hello", sourceCodeRange: { startOffset: 22, endOffset: 27 } }, { text: ",", sourceCodeRange: { startOffset: 27, endOffset: 28 } }, - { text: " ", sourceCodeRange: { startOffset: 28, endOffset: 29 } }, - { text: "world", sourceCodeRange: { startOffset: 29, endOffset: 34 } }, - { text: " ", sourceCodeRange: { startOffset: 34, endOffset: 35 } }, - { text: "&", sourceCodeRange: { startOffset: 35, endOffset: 40 } }, - { text: " ", sourceCodeRange: { startOffset: 40, endOffset: 41 } }, - { text: "beyond", sourceCodeRange: { startOffset: 41, endOffset: 47 } }, - { text: "!", sourceCodeRange: { startOffset: 47, endOffset: 48 } }, + { text: " ", sourceCodeRange: { startOffset: 28, endOffset: 34 } }, + { text: "world", sourceCodeRange: { startOffset: 34, endOffset: 39 } }, + { text: " ", sourceCodeRange: { startOffset: 39, endOffset: 40 } }, + { text: "&", sourceCodeRange: { startOffset: 40, endOffset: 45 } }, + { text: " ", sourceCodeRange: { startOffset: 45, endOffset: 46 } }, + { text: "beyond", sourceCodeRange: { startOffset: 46, endOffset: 52 } }, + { text: "!", sourceCodeRange: { startOffset: 52, endOffset: 53 } }, ], } ], childNodes: [ { name: "#text", - value: "Hello, world #amp; beyond!", - sourceCodeRange: { startOffset: 22, endOffset: 48 }, + value: "Hello,#nbsp;world #amp; beyond!", + sourceCodeRange: { startOffset: 22, endOffset: 53 }, } ], sourceCodeLocation: { startOffset: 5, - endOffset: 52, + endOffset: 57, startTag: { startOffset: 5, endOffset: 22, }, endTag: { - startOffset: 48, - endOffset: 52, + startOffset: 53, + endOffset: 57, }, }, } ], diff --git a/packages/yoastseo/src/helpers/htmlEntities.js b/packages/yoastseo/src/helpers/htmlEntities.js index c8980c09ce4..29ab727fdf9 100644 --- a/packages/yoastseo/src/helpers/htmlEntities.js +++ b/packages/yoastseo/src/helpers/htmlEntities.js @@ -18,6 +18,7 @@ const htmlEntities = new Map( [ [ "deg;", "°" ], [ "asymp;", "≈" ], [ "ne;", "≠" ], + [ "nbsp;", " " ], ] ); // Regex to find all HTML entities. diff --git a/packages/yoastseo/src/languageProcessing/helpers/word/getWordsForHTMLParser.js b/packages/yoastseo/src/languageProcessing/helpers/word/getWordsForHTMLParser.js index 47473262443..09130bba1cb 100644 --- a/packages/yoastseo/src/languageProcessing/helpers/word/getWordsForHTMLParser.js +++ b/packages/yoastseo/src/languageProcessing/helpers/word/getWordsForHTMLParser.js @@ -9,10 +9,11 @@ import { hashedHtmlEntitiesRegexEnd, hashedHtmlEntitiesRegexStart } from "../../ * The word separator explicitly only contains characters that split two words and not a word and a space. * - A space is a word separator because it separates two words if it occurs between two words. For example: "foo bar" * - A tab is a word separator because it separates two words if it occurs between two words. For example: "foo bar" - * - A non-breaking space is a word separator because it separates two words if it occurs between two words. For example: "foo\u00A0bar" + * - Non-breaking spaces are word separators because they separate two words if it occurs between two words. For example: "foo\u00A0bar". + * Note that   is portrayed here as #nbsp; -- we transformed the & to # to prevent parse5 from converting it to a space. * - Open and closing brackets are added to deal correctly with shortcodes downstream. */ -const wordSeparatorsRegex = /([\s\t\u00A0[\]])/; +const wordSeparatorsRegex = /([\s\t\u00A0[\]]|#nbsp;)/; /** * Tokenizes a text similar to getWords, but in a suitable way for the HTML parser. From 923c96fdabf5317a0a2802826f76cbcb5d0c3e37 Mon Sep 17 00:00:00 2001 From: Martijn van der Klis Date: Fri, 15 Dec 2023 14:37:32 +0100 Subject: [PATCH 2/4] Corrects the length of the block --- packages/js/tests/decorator/gutenberg.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/js/tests/decorator/gutenberg.test.js b/packages/js/tests/decorator/gutenberg.test.js index 10ceb722bce..0dd18121237 100644 --- a/packages/js/tests/decorator/gutenberg.test.js +++ b/packages/js/tests/decorator/gutenberg.test.js @@ -103,7 +103,7 @@ describe( "a test for retrieving annotations for blocks", () => { new Mark( { position: { startOffsetBlock: 0, - endOffsetBlock: 11, + endOffsetBlock: 16, clientId: "d126c9dc-c496-49c1-89db-1bdfbc9ef8ed", attributeId: "", isFirstSection: false, From 57c34dd9472d347a49af529ea8ab085da6c06f00 Mon Sep 17 00:00:00 2001 From: marinakoleva Date: Mon, 18 Dec 2023 13:54:21 +0200 Subject: [PATCH 3/4] small edits to comments --- .../researches/keywordCountSpec.js | 2 +- .../helpers/word/getWordsForHTMLParser.js | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/yoastseo/spec/languageProcessing/researches/keywordCountSpec.js b/packages/yoastseo/spec/languageProcessing/researches/keywordCountSpec.js index 4ec0f219240..b053cf212b2 100644 --- a/packages/yoastseo/spec/languageProcessing/researches/keywordCountSpec.js +++ b/packages/yoastseo/spec/languageProcessing/researches/keywordCountSpec.js @@ -538,7 +538,7 @@ describe.each( testCases )( "Test for counting the keyphrase in a text in englis const testCasesWithSpecialCharacters = [ { - description: "counts the keyphrase occurrence in the text with  ", + description: "counts the keyphrase occurrence in a text with  ", paper: new Paper( "

a string with nbsp to match the key word.

", { keyword: "key word" } ), keyphraseForms: [ [ "key" ], [ "word" ] ], expectedCount: 1, diff --git a/packages/yoastseo/src/languageProcessing/helpers/word/getWordsForHTMLParser.js b/packages/yoastseo/src/languageProcessing/helpers/word/getWordsForHTMLParser.js index 09130bba1cb..06d834f3d03 100644 --- a/packages/yoastseo/src/languageProcessing/helpers/word/getWordsForHTMLParser.js +++ b/packages/yoastseo/src/languageProcessing/helpers/word/getWordsForHTMLParser.js @@ -2,16 +2,15 @@ import { punctuationRegexEnd, punctuationRegexStart } from "../sanitize/removePu import { hashedHtmlEntitiesRegexEnd, hashedHtmlEntitiesRegexStart } from "../../../helpers/htmlEntities"; /* - * The following regex matches a word separator. A word separator is either a whitespace, a slash, a - * tab or a non-breaking space. Brackets are added to deal correctly with shortcodes downstream. - * The regex is used to split a text into tokens. + * The following regex is used to split a text into tokens. + * The regex matches word separators. A word separator is either a whitespace, a slash, a tab or a non-breaking space. + * Opening and closing square brackets are added to deal correctly with shortcodes downstream. * Do not add punctuation marks to this regex, as they are handled separately inside splitIntoTokens(). * The word separator explicitly only contains characters that split two words and not a word and a space. - * - A space is a word separator because it separates two words if it occurs between two words. For example: "foo bar" - * - A tab is a word separator because it separates two words if it occurs between two words. For example: "foo bar" - * - Non-breaking spaces are word separators because they separate two words if it occurs between two words. For example: "foo\u00A0bar". - * Note that   is portrayed here as #nbsp; -- we transformed the & to # to prevent parse5 from converting it to a space. - * - Open and closing brackets are added to deal correctly with shortcodes downstream. + * - A space is a word separator because it separates two words if it occurs between them. For example: "foo bar" + * - A tab is a word separator because it separates two words if it occurs between them. For example: "foo bar" + * - A non-breaking space is a word separator because it separates two words if it occurs between them. For example: "foo\u00A0bar". + * Note that   is added here as #nbsp; -- we transform the & to # to prevent parse5 from converting it to a space and messing up highlighting. */ const wordSeparatorsRegex = /([\s\t\u00A0[\]]|#nbsp;)/; From 198926e05c051aa4a1cfb28fc45fdd9f39c048c2 Mon Sep 17 00:00:00 2001 From: marinakoleva Date: Mon, 18 Dec 2023 15:46:05 +0200 Subject: [PATCH 4/4] editing specs in order to remove eslint max-len and make them more readable --- .../researches/keywordCountSpec.js | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/yoastseo/spec/languageProcessing/researches/keywordCountSpec.js b/packages/yoastseo/spec/languageProcessing/researches/keywordCountSpec.js index b053cf212b2..2f5ffbb20bf 100644 --- a/packages/yoastseo/spec/languageProcessing/researches/keywordCountSpec.js +++ b/packages/yoastseo/spec/languageProcessing/researches/keywordCountSpec.js @@ -1057,8 +1057,12 @@ const testCasesWithSpecialCharacters = [ wpBlocks: [ { attributes: { - // eslint-disable-next-line max-len - content: "Over the years, we’ve written quite a few articles about branding. Branding is about getting people to relate to your company and products. It’s also about trying to make your brand synonymous with a certain product or service. This can be a lengthy and hard project. It can potentially cost you all of your revenue. It’s no wonder that branding is often associated with investing lots of money in marketing and promotion. However, for a lot of small business owners, the investment in branding will have to be made with a relatively small budget. ", + content: "Over the years, we’ve written quite a few articles about branding. Branding is about getting people to relate to your company and products." + + " It’s also about trying to make your brand synonymous with a certain product or service. This can be a lengthy and" + + " hard project. It can potentially cost you all of your revenue. It’s no wonder that branding is often associated " + + "with investing lots of money in marketing and promotion. However, for a lot of small business owners, the " + + "investment in branding will have to be made with a relatively small budget. ", }, name: "core/paragraph", clientId: "6860403c-0b36-43b2-96fa-2d30c10cb44c", @@ -1072,8 +1076,8 @@ const testCasesWithSpecialCharacters = [ expectedMarkings: [ new Mark( { - // eslint-disable-next-line max-len - marked: " It's also about trying to make your brand synonymous with a certain product or service.", + marked: " It's also about trying to make your brand synonymous with a certain " + + "product or service.", original: " It’s also about trying to make your brand synonymous with a certain product or service.", position: { startOffset: 305, @@ -1103,8 +1107,10 @@ const testCasesWithSpecialCharacters = [ wpBlocks: [ { attributes: { - // eslint-disable-next-line max-len - content: "You might be a local bakery with 10 employees, or a local industrial company employing up to 500 people. These all can be qualified as ‘small business’. All have the same main goal when they start: the need to establish a name in their field of expertise. There are multiple ways to do this, without a huge budget. In this post, I’ll share my thoughts on how to go about your own low-budget branding.", + content: "You might be a local bakery with 10 employees, or a local industrial company employing up to 500 people. " + + "These all can be qualified as ‘small business’. All have the same main goal when they start: the need to establish" + + " a name in their field of expertise. There are multiple ways to do this, without a huge budget. In this post, I’ll" + + " share my thoughts on how to go about your own low-budget branding.", }, name: "core/paragraph", clientId: "65be5146-3395-4845-8c7c-4a79fd6e3611", @@ -1118,8 +1124,8 @@ const testCasesWithSpecialCharacters = [ expectedMarkings: [ new Mark( { - // eslint-disable-next-line max-len - marked: " All have the same main goal when they start: the need to establish a name in their field of expertise.", + marked: " All have the same main goal when they start: the need to establish a name in their field of " + + "expertise.", original: " All have the same main goal when they start: the need to establish a name in their field of expertise.", position: { startOffset: 282, @@ -1158,7 +1164,6 @@ const testCasesWithSpecialCharacters = [ keyphraseForms: [ [ "communicate" ] ], expectedCount: 1, expectedMarkings: [ - // eslint-disable-next-line max-len new Mark( { marked: "Define and communicate brand values", original: "Define and communicate brand values", @@ -1206,8 +1211,8 @@ const testCasesWithSpecialCharacters = [ innerBlocks: [ { attributes: { - // eslint-disable-next-line max-len - content: "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", + content: "Lorem Ipsum is simply dummy text of the printing and " + + "typesetting industry.", }, name: "core/paragraph", clientId: "3f0e68c1-287e-40ef-90c8-54b3ab61702a",