• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 4c5f9780cef7c22b7f9bfbace943d71badc638ed

08 Oct 2024 09:39AM UTC coverage: 54.494% (-0.04%) from 54.538%
4c5f9780cef7c22b7f9bfbace943d71badc638ed

push

github

YoastBot
Bump version to 23.6 on free

7502 of 13498 branches covered (55.58%)

Branch coverage included in aggregate %.

29530 of 54458 relevant lines covered (54.23%)

41951.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.87
/packages/yoastseo/src/values/Paper.js
1
import { defaults, isEmpty, isEqual, isNil } from "lodash";
2

3
/**
4
 * Default attributes to be used by the Paper if they are left undefined.
5
 * @type {{keyword: string, synonyms: string, description: string, title: string, titleWidth: number,
6
 *                    slug: string, locale: string, permalink: string, date: string, customData: object, textTitle: string, writingDirection: "LTR" }}
7
 */
8
const defaultAttributes = {
588✔
9
        keyword: "",
10
        synonyms: "",
11
        description: "",
12
        title: "",
13
        titleWidth: 0,
14
        slug: "",
15
        locale: "en_US",
16
        permalink: "",
17
        date: "",
18
        customData: {},
19
        textTitle: "",
20
        writingDirection: "LTR",
21
        wpBlocks: [],
22
};
23

24
/**
25
 * Represents an object where the analysis data is stored.
26
 */
27
class Paper {
28
        /**
29
         * Constructs the Paper object and sets its attributes.
30
         *
31
         * @param {string}  text                            The text to use in the analysis.
32
         * @param {object}  [attributes]                    The object containing all attributes.
33
         * @param {string}  [attributes.keyword]            The main keyword or keyphrase of the text.
34
         * @param {string}  [attributes.synonyms]           The synonyms of the main keyword or keyphrase. It should be separated by commas if multiple synonyms are added.
35
         * @param {string}  [attributes.description]        The SEO meta description.
36
         * @param {string}  [attributes.title]              The SEO title.
37
         * @param {number}  [attributes.titleWidth=0]         The width of the title in pixels.
38
         * @param {string}  [attributes.slug]               The slug.
39
         * @param {string}  [attributes.locale=en_US]             The locale.
40
         * @param {string}  [attributes.permalink]          The full URL for any given post, page, or other pieces of content on a site.
41
         * @param {string}  [attributes.date]               The date.
42
         * @param {Object[]}  [attributes.wpBlocks]           The array of texts, encoded in WordPress block editor blocks.
43
         * @param {Object}  [attributes.customData]         Custom data.
44
         * @param {string}  [attributes.textTitle]          The title of the text.
45
         * @param {string}  [attributes.writingDirection=LTR]   The writing direction of the paper. Defaults to left to right (LTR).
46
         */
47
        constructor( text, attributes ) {
48
                this._text = text || "";
4,972✔
49

50
                this._tree = null;
4,972✔
51

52
                attributes = attributes || {};
4,972✔
53
                defaults( attributes, defaultAttributes );
4,972✔
54

55
                if ( attributes.locale === "" ) {
4,972✔
56
                        attributes.locale = defaultAttributes.locale;
2✔
57
                }
58

59
                if ( attributes.hasOwnProperty( "url" ) ) {
4,972✔
60
                        // The 'url' attribute has been deprecated since version 1.19.1, refer to hasUrl and getUrl below.
61
                        console.warn( "The 'url' attribute is deprecated, use 'slug' instead." );
4✔
62
                        attributes.slug = attributes.url || attributes.slug;
4✔
63
                }
64

65
                const onlyLetters = attributes.keyword.replace( /[‘’“”"'.?!:;,¿¡«»&*@#±^%|~`[\](){}⟨⟩<>/\\–\-\u2014\u00d7\u002b\s]/g, "" );
4,972✔
66

67
                if ( isEmpty( onlyLetters ) ) {
4,972✔
68
                        attributes.keyword = defaultAttributes.keyword;
3,264✔
69
                }
70

71
                this._attributes = attributes;
4,972✔
72
        }
73

74

75
        /**
76
         * Checks whether a keyword is available.
77
         * @returns {boolean} Returns true if the Paper has a keyword.
78
         */
79
        hasKeyword() {
80
                return this._attributes.keyword !== "";
2,856✔
81
        }
82

83
        /**
84
         * Returns the associated keyword or an empty string if no keyword is available.
85
         * @returns {string} Returns Keyword
86
         */
87
        getKeyword() {
88
                return this._attributes.keyword;
4,874✔
89
        }
90

91
        /**
92
         * Checks whether synonyms are available.
93
         * @returns {boolean} Returns true if the Paper has synonyms.
94
         */
95
        hasSynonyms() {
96
                return this._attributes.synonyms !== "";
4✔
97
        }
98

99
        /**
100
         * Returns the associated synonyms or an empty string if no synonyms is available.
101
         * @returns {string} Returns synonyms.
102
         */
103
        getSynonyms() {
104
                return this._attributes.synonyms;
2,668✔
105
        }
106

107
        /**
108
         * Checks whether the text is available.
109
         * @returns {boolean} Returns true if the paper has a text.
110
         */
111
        hasText() {
112
                return this._text !== "";
2,598✔
113
        }
114

115
        /**
116
         * Returns the associated text or an empty string if no text is available.
117
         * @returns {string} Returns the text.
118
         */
119
        getText() {
120
                return this._text;
18,702✔
121
        }
122

123
        /**
124
         * Sets the tree.
125
         *
126
         * @param {Node} tree The tree to set.
127
         *
128
         * @returns {void}
129
         */
130
        setTree( tree ) {
131
                this._tree = tree;
1,126✔
132
        }
133

134
        /**
135
         * Returns the tree.
136
         *
137
         * @returns {Node} The tree.
138
         */
139
        getTree() {
140
                return this._tree;
19,958✔
141
        }
142

143
        /**
144
         * Checks whether a description is available.
145
         * @returns {boolean} Returns true if the paper has a description.
146
         */
147
        hasDescription() {
148
                return this._attributes.description !== "";
338✔
149
        }
150

151
        /**
152
         * Returns the description or an empty string if no description is available.
153
         * @returns {string} Returns the description.
154
         */
155
        getDescription() {
156
                return this._attributes.description;
2,744✔
157
        }
158

159
        /**
160
         * Checks whether an SEO title is available
161
         * @returns {boolean} Returns true if the Paper has an SEO title.
162
         */
163
        hasTitle() {
164
                return this._attributes.title !== "";
570✔
165
        }
166

167
        /**
168
         * Returns the SEO title, or an empty string if no title is available.
169
         * @returns {string} Returns the SEO title.
170
         */
171
        getTitle() {
172
                return this._attributes.title;
2,594✔
173
        }
174

175
        /**
176
         * Checks whether an SEO title width in pixels is available.
177
         * @returns {boolean} Returns true if the Paper's SEO title is wider than 0 pixels.
178
         */
179
        hasTitleWidth() {
180
                return this._attributes.titleWidth !== 0;
6✔
181
        }
182

183
        /**
184
         * Gets the SEO title width in pixels, or an empty string of no title width in pixels is available.
185
         * @returns {number} Returns the SEO title width in pixels.
186
         */
187
        getTitleWidth() {
188
                return this._attributes.titleWidth;
94✔
189
        }
190

191
        /**
192
         * Checks whether a slug is available.
193
         * @returns {boolean} Returns true if the Paper has a slug.
194
         */
195
        hasSlug() {
196
                return this._attributes.slug !== "";
244✔
197
        }
198

199
        /**
200
         * Gets the paper's slug, or an empty string if no slug is available.
201
         * @returns {string} Returns the slug.
202
         */
203
        getSlug() {
204
                return this._attributes.slug;
4,564✔
205
        }
206

207
        /**
208
         * Checks whether an url is available
209
         * @deprecated Since version 1.19.1. Use hasSlug instead.
210
         * @returns {boolean} Returns true if the Paper has a slug.
211
         */
212
        hasUrl() {
213
                console.warn( "This function is deprecated, use hasSlug instead" );
×
214
                return this.hasSlug();
×
215
        }
216

217
        /**
218
         * Returns the url, or an empty string if no url is available.
219
         * @deprecated Since version 1.19.1. Use getSlug instead.
220
         * @returns {string} Returns the url
221
         */
222
        getUrl() {
223
                console.warn( "This function is deprecated, use getSlug instead" );
×
224
                return this.getSlug();
×
225
        }
226

227
        /**
228
         * Checks whether a locale is available.
229
         * @returns {boolean} Returns true if the paper has a locale.
230
         */
231
        hasLocale() {
232
                return this._attributes.locale !== "";
4✔
233
        }
234

235
        /**
236
         * Returns the locale or an empty string if no locale is available
237
         * @returns {string} Returns the locale.
238
         */
239
        getLocale() {
240
                return this._attributes.locale;
3,508✔
241
        }
242

243
        /**
244
         * Gets the information of the writing direction of the paper.
245
         * It returns "LTR" (left to right) if this attribute is not provided.
246
         *
247
         * @returns {string} Returns the information of the writing direction of the paper.
248
         */
249
        getWritingDirection() {
250
                return this._attributes.writingDirection;
22✔
251
        }
252

253
        /**
254
         * Checks whether a permalink is available.
255
         * @returns {boolean} Returns true if the Paper has a permalink.
256
         */
257
        hasPermalink() {
258
                return this._attributes.permalink !== "";
8✔
259
        }
260

261
        /**
262
         * Returns the permalink, or an empty string if no permalink is available.
263
         * @returns {string} Returns the permalink.
264
         */
265
        getPermalink() {
266
                return this._attributes.permalink;
590✔
267
        }
268

269
        /**
270
         * Checks whether a date is available.
271
         * @returns {boolean} Returns true if the Paper has a date.
272
         */
273
        hasDate() {
274
                return this._attributes.date !== "";
2✔
275
        }
276

277
        /**
278
         * Returns the date, or an empty string if no date is available.
279
         * @returns {string} Returns the date.
280
         */
281
        getDate() {
282
                return this._attributes.date;
352✔
283
        }
284

285
        /**
286
         * Checks whether custom data is available.
287
         * @returns {boolean} Returns true if the Paper has custom data.
288
         */
289
        hasCustomData() {
290
                return ! isEmpty( this._attributes.customData );
6✔
291
        }
292

293
        /**
294
         * Returns the custom data, or an empty object if no data is available.
295
         * @returns {Object} Returns the custom data.
296
         */
297
        getCustomData() {
298
                return this._attributes.customData;
320✔
299
        }
300

301
        /**
302
         * Checks whether a text title is available.
303
         * @returns {boolean} Returns true if the Paper has a text title.
304
         */
305
        hasTextTitle() {
306
                return this._attributes.textTitle !== "" && ! isNil( this._attributes.textTitle );
2✔
307
        }
308

309
        /**
310
         * Returns the text title, or an empty string if no data is available.
311
         * @returns {string} Returns the text title.
312
         */
313
        getTextTitle() {
314
                return this._attributes.textTitle;
8,464✔
315
        }
316

317
        /**
318
         * Serializes the Paper instance to an object.
319
         *
320
         * @returns {Object} The serialized Paper.
321
         */
322
        serialize() {
323
                return {
50✔
324
                        _parseClass: "Paper",
325
                        text: this._text,
326
                        ...this._attributes,
327
                };
328
        }
329

330
        /**
331
         * Checks whether the given paper has the same properties as this instance.
332
         *
333
         * @param {Paper} paper The paper to compare to.
334
         *
335
         * @returns {boolean} Whether the given paper is identical or not.
336
         */
337
        equals( paper ) {
338
                return this._text === paper.getText() && isEqual( this._attributes, paper._attributes );
10✔
339
        }
340

341
        /**
342
         * Parses the object to a Paper.
343
         *
344
         * @param {Object|Paper} serialized The serialized object or Paper instance.
345
         *
346
         * @returns {Paper} The parsed Paper.
347
         */
348
        static parse( serialized ) {
349
                // For ease of use, check if it is not already a Paper instance.
350
                if ( serialized instanceof Paper ) {
56✔
351
                        return serialized;
2✔
352
                }
353

354
                // _parseClass is taken here, so it doesn't end up in the attributes.
355
                // eslint-disable-next-line no-unused-vars
356
                const { text, _parseClass, ...attributes } = serialized;
54✔
357

358
                return new Paper( text, attributes );
54✔
359
        }
360
}
361

362
export default Paper;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc