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
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,34 @@ describe( "test the deconstructAnchor and reconstructAnchor helper", () => {
const reconstructedAnchor = reConstructAnchor( deconstructedAnchor.openTag, deconstructedAnchor.content );
expect( reconstructedAnchor ).toEqual( testAnchor );
} );

it( "correctly deconstructs and reconstructs an anchor if does not contain content", () => {
// Unrealistic Scenario. But protects against the bug that is solved in this PR:
// https://github.com/Yoast/wordpress-seo/pull/19373
const testAnchor = "<a href=\"https://yoast.com\"></a>";
const deconstructedAnchor = deConstructAnchor( testAnchor );

expect( deconstructedAnchor ).toEqual( {
openTag: "<a href=\"https://yoast.com\">",
content: "",
} );

const reconstructedAnchor = reConstructAnchor( deconstructedAnchor.openTag, deconstructedAnchor.content );
expect( reconstructedAnchor ).toEqual( testAnchor );
} );

it( "correctly deconstructs and reconstructs an anchor if content contains a newline", () => {
// Unrealistic Scenario. But protects against the bug that is solved in this PR:
// https://github.com/Yoast/wordpress-seo/pull/19373
const testAnchor = "<a href=\"https://yoast.com\">This is a line.\nAnd this is a line.</a>";
const deconstructedAnchor = deConstructAnchor( testAnchor );

expect( deconstructedAnchor ).toEqual( {
openTag: "<a href=\"https://yoast.com\">",
content: "This is a line.\nAnd this is a line.",
} );

const reconstructedAnchor = reConstructAnchor( deconstructedAnchor.openTag, deconstructedAnchor.content );
expect( reconstructedAnchor ).toEqual( testAnchor );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import matchWords from "../match/matchTextWithArray";
import arrayToRegex from "../regex/createRegexFromArray";

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

/**
* Deconstructs an anchor to the opening tag and the content. The content is the anchor text.
Expand Down Expand Up @@ -60,7 +61,6 @@ const getMarkedAnchors = function( sentence, wordsRegex ) {
// Create a new anchor tag with a (marked) anchor text.
return reConstructAnchor( openTag, markedAnchorText );
} );

return { anchors, markedAnchors };
};

Expand Down Expand Up @@ -123,7 +123,6 @@ export function markWordsInSentences( wordsToMark, sentences, locale, matchWordC

sentences.forEach( function( sentence ) {
wordsFoundInSentence = matchWords( sentence, wordsToMark, locale, matchWordCustomHelper ).matches;

if ( wordsFoundInSentence.length > 0 ) {
markings = markings.concat( new Mark( {
original: sentence,
Expand Down