Skip to content

Commit 69006b0

Browse files
authored
Merge pull request #19373 from Yoast/PC-1029-anchors-containing-square-brackets-in-content-lead-to-errors-with-classic-editor
PC-1029-anchors-containing-square-brackets-in-content-lead-to-errors-with-classic-editor
2 parents 89a5291 + 2bafe60 commit 69006b0

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

packages/yoastseo/spec/languageProcessing/helpers/word/markWordsInSentenceSpec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,34 @@ describe( "test the deconstructAnchor and reconstructAnchor helper", () => {
168168
const reconstructedAnchor = reConstructAnchor( deconstructedAnchor.openTag, deconstructedAnchor.content );
169169
expect( reconstructedAnchor ).toEqual( testAnchor );
170170
} );
171+
172+
it( "correctly deconstructs and reconstructs an anchor if does not contain content", () => {
173+
// Unrealistic Scenario. But protects against the bug that is solved in this PR:
174+
// https://github.com/Yoast/wordpress-seo/pull/19373
175+
const testAnchor = "<a href=\"https://yoast.com\"></a>";
176+
const deconstructedAnchor = deConstructAnchor( testAnchor );
177+
178+
expect( deconstructedAnchor ).toEqual( {
179+
openTag: "<a href=\"https://yoast.com\">",
180+
content: "",
181+
} );
182+
183+
const reconstructedAnchor = reConstructAnchor( deconstructedAnchor.openTag, deconstructedAnchor.content );
184+
expect( reconstructedAnchor ).toEqual( testAnchor );
185+
} );
186+
187+
it( "correctly deconstructs and reconstructs an anchor if content contains a newline", () => {
188+
// Unrealistic Scenario. But protects against the bug that is solved in this PR:
189+
// https://github.com/Yoast/wordpress-seo/pull/19373
190+
const testAnchor = "<a href=\"https://yoast.com\">This is a line.\nAnd this is a line.</a>";
191+
const deconstructedAnchor = deConstructAnchor( testAnchor );
192+
193+
expect( deconstructedAnchor ).toEqual( {
194+
openTag: "<a href=\"https://yoast.com\">",
195+
content: "This is a line.\nAnd this is a line.",
196+
} );
197+
198+
const reconstructedAnchor = reConstructAnchor( deconstructedAnchor.openTag, deconstructedAnchor.content );
199+
expect( reconstructedAnchor ).toEqual( testAnchor );
200+
} );
171201
} );

packages/yoastseo/src/languageProcessing/helpers/word/markWordsInSentences.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import matchWords from "../match/matchTextWithArray";
66
import arrayToRegex from "../regex/createRegexFromArray";
77

88
// Regex to deconstruct an anchor into open tag, content and close tag.
9-
const anchorDeconstructionRegex = /(<a[\s]+[^>]+>)(.+?)(<\/a>)/;
9+
// [^] matches any character, including newline
10+
const anchorDeconstructionRegex = /(<a[\s]+[^>]+>)([^]*?)(<\/a>)/;
1011

1112
/**
1213
* Deconstructs an anchor to the opening tag and the content. The content is the anchor text.
@@ -60,7 +61,6 @@ const getMarkedAnchors = function( sentence, wordsRegex ) {
6061
// Create a new anchor tag with a (marked) anchor text.
6162
return reConstructAnchor( openTag, markedAnchorText );
6263
} );
63-
6464
return { anchors, markedAnchors };
6565
};
6666

@@ -123,7 +123,6 @@ export function markWordsInSentences( wordsToMark, sentences, locale, matchWordC
123123

124124
sentences.forEach( function( sentence ) {
125125
wordsFoundInSentence = matchWords( sentence, wordsToMark, locale, matchWordCustomHelper ).matches;
126-
127126
if ( wordsFoundInSentence.length > 0 ) {
128127
markings = markings.concat( new Mark( {
129128
original: sentence,

0 commit comments

Comments
 (0)