Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/js/src/watchers/elementorWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
} );

Expand Down
2 changes: 1 addition & 1 deletion packages/js/tests/decorator/gutenberg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" ],
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,18 +538,30 @@ 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( "<p>a string with nbsp to match the key&nbsp;word.</p>", { keyword: "key word" } ),
keyphraseForms: [ [ "key" ], [ "word" ] ],
expectedCount: 1,
expectedMarkings: [
new Mark( { marked: "a string with nbsp to match the <yoastmark class='yoast-text-mark'>key word</yoastmark>.",
original: "a string with nbsp to match the key word.",
new Mark( { marked: "a string with nbsp to match the <yoastmark class='yoast-text-mark'>key word</yoastmark>.",
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 <yoastmark class='yoast-text-mark'>key word</yoastmark>.",
original: "a string with nbsp to match the key word.",
position: {
startOffset: 44,
endOffset: 48,
startOffsetBlock: 41,
endOffsetBlock: 45,
attributeId: "",
clientId: "",
isFirstSection: false,
Expand Down Expand Up @@ -1045,8 +1057,12 @@ const testCasesWithSpecialCharacters = [
wpBlocks: [
{
attributes: {
// eslint-disable-next-line max-len
content: "<strong>Over the years, we’ve written&nbsp;quite a few articles about&nbsp;</strong><a href=\"https://yoast.com/tag/branding/\">branding</a><strong>. 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&nbsp;be made with a&nbsp;relatively small budget.&nbsp;</strong>",
content: "<strong>Over the years, we’ve written&nbsp;quite a few articles about&nbsp;</strong><a href=\"https://yoast." +
"com/tag/branding/\">branding</a><strong>. 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&nbsp;be made with a&nbsp;relatively small budget.&nbsp;</strong>",
},
name: "core/paragraph",
clientId: "6860403c-0b36-43b2-96fa-2d30c10cb44c",
Expand All @@ -1060,14 +1076,14 @@ const testCasesWithSpecialCharacters = [
expectedMarkings: [

new Mark( {
// eslint-disable-next-line max-len
marked: " It's also about trying to make your brand <yoastmark class='yoast-text-mark'>synonymous</yoastmark> with a certain product or service.",
marked: " It's also about trying to make your brand <yoastmark class='yoast-text-mark'>synonymous</yoastmark> 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,
Expand All @@ -1091,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",
Expand All @@ -1106,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 <yoastmark class='yoast-text-mark'>expertise</yoastmark>.",
marked: " All have the same main goal when they start: the need to establish a name in their field of " +
"<yoastmark class='yoast-text-mark'>expertise</yoastmark>.",
original: " All have the same main goal when they start: the need to establish a name in their field of expertise.",
position: {
startOffset: 282,
Expand Down Expand Up @@ -1146,7 +1164,6 @@ const testCasesWithSpecialCharacters = [
keyphraseForms: [ [ "communicate" ] ],
expectedCount: 1,
expectedMarkings: [
// eslint-disable-next-line max-len
new Mark( {
marked: "Define and <yoastmark class='yoast-text-mark'>communicate</yoastmark> brand values",
original: "Define and communicate brand values",
Expand Down Expand Up @@ -1194,8 +1211,8 @@ const testCasesWithSpecialCharacters = [
innerBlocks: [
{
attributes: {
// eslint-disable-next-line max-len
content: "<strong>Lorem Ipsum</strong>&nbsp;is simply dummy text of the printing and typesetting industry.",
content: "<strong>Lorem Ipsum</strong>&nbsp;is simply dummy text of the printing and " +
"typesetting industry.",
},
name: "core/paragraph",
clientId: "3f0e68c1-287e-40ef-90c8-54b3ab61702a",
Expand Down Expand Up @@ -1227,8 +1244,8 @@ const testCasesWithSpecialCharacters = [
expectedCount: 2,
expectedMarkings: [
new Mark( {
marked: "Lorem <yoastmark class='yoast-text-mark'>Ipsum</yoastmark> 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 <yoastmark class='yoast-text-mark'>Ipsum</yoastmark> 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,
Expand Down
36 changes: 18 additions & 18 deletions packages/yoastseo/spec/parse/build/buildSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (&amp;)", () => {
const html = "<div><p class='yoast'>Hello, world &amp; beyond!</p></div>";
it( "parses a basic HTML text with HTML entities (&nbsp; and &amp;)", () => {
const html = "<div><p class='yoast'>Hello,&nbsp;world &amp; beyond!</p></div>";
const paper = new Paper( html );

const researcher = Factory.buildMockResearcher( {}, true, false, false,
Expand All @@ -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: {},
Expand All @@ -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,
},
},
} ],
Expand Down
1 change: 1 addition & 0 deletions packages/yoastseo/src/helpers/htmlEntities.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const htmlEntities = new Map( [
[ "deg;", "°" ],
[ "asymp;", "≈" ],
[ "ne;", "≠" ],
[ "nbsp;", " " ],
] );

// Regex to find all HTML entities.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ 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"
* - A non-breaking space is a word separator because it separates two words if it occurs between two words. For example: "foo\u00A0bar"
* - 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 &nbsp; 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[\]])/;
const wordSeparatorsRegex = /([\s\t\u00A0[\]]|#nbsp;)/;

/**
* Tokenizes a text similar to getWords, but in a suitable way for the HTML parser.
Expand Down