Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Add options bag parameter to goog.string.linkify.linkifyPlainTextAsHtml
Browse files Browse the repository at this point in the history
RELNOTES: goog.string.linkify.linkifyPlainTextAsHtml now accepts an options bag as its second parameter.

PiperOrigin-RevId: 419914003
Change-Id: I54949a6e228e8be712f98087b7d5d4c0b518fe80
  • Loading branch information
shicks authored and copybara-github committed Jan 5, 2022
1 parent 000682f commit f4e2047
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
47 changes: 43 additions & 4 deletions closure/goog/string/linkify.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,43 @@ goog.require('goog.string');
goog.require('goog.string.Const');


/**
* Options bag for linkifyPlainTextAsHtml's second parameter.
* @private
* @record
*/
goog.string.linkify.LinkifyOptions_ = class {
constructor() {
/**
* HTML attributes to add to all links created. Default are `rel=nofollow`
* and `target=_blank`. To clear these defaults attributes, set them
* explicitly to '', i.e. `{rel: '', target: ''}`.
* @const {!Object<string, ?goog.html.SafeHtml.AttributeValue>|undefined}
*/
this.attributes;
/**
* Whether to preserve newlines with &lt;br&gt;.
* @const {boolean|undefined}
*/
this.preserveNewlines;
/**
* Whether to preserve spaces with non-breaking spaces and tabs with
* &lt;span style="white-space:pre"&gt;
* @const {boolean|undefined}
*/
this.preserveSpacesAndTabs;
}
};


/**
* Takes a string of plain text and linkifies URLs and email addresses. For a
* URL (unless opt_attributes is specified), the target of the link will be
* _blank and it will have a rel=nofollow attribute applied to it so that links
* created by linkify will not be of interest to search engines.
* @param {string} text Plain text.
* @param {!Object<string, ?goog.html.SafeHtml.AttributeValue>=} opt_attributes
* @param {!goog.string.linkify.LinkifyOptions_|
* !Object<string, ?goog.html.SafeHtml.AttributeValue>=} opt_attributes
* Attributes to add to all links created. Default are rel=nofollow and
* target=_blank. To clear those default attributes set rel='' and
* target=''.
Expand All @@ -36,6 +66,15 @@ goog.require('goog.string.Const');
goog.string.linkify.linkifyPlainTextAsHtml = function(
text, opt_attributes, opt_preserveNewlines, opt_preserveSpacesAndTabs) {
'use strict';
if (opt_attributes &&
(opt_attributes.attributes || opt_attributes.preserveNewlines ||
opt_attributes.preserveSpacesAndTabs)) {
opt_preserveNewlines = opt_attributes.preserveNewlines;
opt_preserveSpacesAndTabs = opt_attributes.preserveSpacesAndTabs;
opt_attributes = opt_attributes.attributes;
}
const /** !Object<?goog.html.SafeHtml.AttributeValue> */ attributes =
opt_attributes || {};

/**
* @param {string} plainText
Expand Down Expand Up @@ -78,12 +117,12 @@ goog.string.linkify.linkifyPlainTextAsHtml = function(
}

const attributesMap = {};
for (let key in opt_attributes) {
if (!opt_attributes[key]) {
for (let key in attributes) {
if (!attributes[key]) {
// Our API allows '' to omit the attribute, SafeHtml requires null.
attributesMap[key] = null;
} else {
attributesMap[key] = opt_attributes[key];
attributesMap[key] = attributes[key];
}
}
// Set default options if they haven't been explicitly set.
Expand Down
13 changes: 7 additions & 6 deletions closure/goog/string/linkify_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ function assertLinkify(
comment, input, expected, preserveNewlines = undefined,
preserveSpacesAndTabs = undefined) {
assertEquals(
comment, expected,
SafeHtml.unwrap(linkify.linkifyPlainTextAsHtml(
input, {rel: '', target: ''}, preserveNewlines,
preserveSpacesAndTabs)));
comment, expected, SafeHtml.unwrap(linkify.linkifyPlainTextAsHtml(input, {
attributes: {rel: '', target: ''},
preserveNewlines,
preserveSpacesAndTabs,
})));
}

testSuite({
Expand Down Expand Up @@ -334,7 +335,7 @@ testSuite({
linkify.linkifyPlainTextAsHtml(
'The link for www.google.com is located somewhere in ' +
'https://www.google.fr/?hl=en, you should find it easily.',
{rel: '', target: ''}));
{attributes: {rel: '', target: ''}}));
testingDom.assertHtmlContentsMatch(
'The link for <a href="http://www.google.com">www.google.com<\/a> is ' +
'located somewhere in ' +
Expand All @@ -348,7 +349,7 @@ testSuite({
div,
linkify.linkifyPlainTextAsHtml(
'Attribute with <class> name www.w3c.org.',
{'class': 'link-added'}));
{attributes: {'class': 'link-added'}}));
testingDom.assertHtmlContentsMatch(
'Attribute with &lt;class&gt; name <a href="http://www.w3c.org" ' +
'target="_blank" rel="nofollow" class="link-added">www.w3c.org<\/a>.',
Expand Down

0 comments on commit f4e2047

Please # to comment.