@@ -93,23 +93,11 @@ class Autolink
9393 protected $ url_base_cash = 'https://twitter.com/search?q=%24 ' ;
9494
9595 /**
96- * Whether to include the value 'nofollow' in the 'rel' attribute.
97- *
98- * @var bool
99- */
100- protected $ nofollow = true ;
101-
102- /**
103- * Whether to include the value 'external' in the 'rel' attribute.
96+ * the 'rel' attribute values.
10497 *
105- * Often this is used to be matched on in JavaScript for dynamically adding
106- * the 'target' attribute which is deprecated in HTML 4.01. In HTML 5 it has
107- * been undeprecated and thus the 'target' attribute can be used. If this is
108- * set to false then the 'target' attribute will be output.
109- *
110- * @var bool
98+ * @var array
11199 */
112- protected $ external = true ;
100+ protected $ rel = array ( ' external ' , ' nofollow ' ) ;
113101
114102 /**
115103 * The scope to open the link in.
@@ -209,6 +197,24 @@ public function __construct($tweet = null, $escape = true, $full_encode = false)
209197 $ this ->extractor = Extractor::create ();
210198 }
211199
200+ /**
201+ * Set CSS class to all link types.
202+ *
203+ * @param string $v CSS class for links.
204+ *
205+ * @return Autolink Fluid method chaining.
206+ */
207+ public function setToAllLinkClasses ($ v )
208+ {
209+ $ this ->setURLClass ($ v );
210+ $ this ->setUsernameClass ($ v );
211+ $ this ->setListClass ($ v );
212+ $ this ->setHashtagClass ($ v );
213+ $ this ->setCashtagClass ($ v );
214+
215+ return $ this ;
216+ }
217+
212218 /**
213219 * CSS class for auto-linked URLs.
214220 *
@@ -331,7 +337,7 @@ public function setCashtagClass($v)
331337 */
332338 public function getNoFollow ()
333339 {
334- return $ this ->nofollow ;
340+ return in_array ( ' nofollow ' , $ this ->rel , true ) ;
335341 }
336342
337343 /**
@@ -343,7 +349,15 @@ public function getNoFollow()
343349 */
344350 public function setNoFollow ($ v )
345351 {
346- $ this ->nofollow = $ v ;
352+ if ($ v && !$ this ->getNoFollow ()) {
353+ $ this ->setRel ('nofollow ' , true );
354+ }
355+ if (!$ v && $ this ->getNoFollow ()) {
356+ $ this ->rel = array_filter ($ this ->rel , function ($ r ) {
357+ return $ r !== 'nofollow ' ;
358+ });
359+ }
360+
347361 return $ this ;
348362 }
349363
@@ -359,7 +373,7 @@ public function setNoFollow($v)
359373 */
360374 public function getExternal ()
361375 {
362- return $ this ->external ;
376+ return in_array ( ' external ' , $ this ->rel , true ) ;
363377 }
364378
365379 /**
@@ -376,7 +390,15 @@ public function getExternal()
376390 */
377391 public function setExternal ($ v )
378392 {
379- $ this ->external = $ v ;
393+ if ($ v && !$ this ->getExternal ()) {
394+ $ this ->setRel ('external ' , true );
395+ }
396+ if (!$ v && $ this ->getExternal ()) {
397+ $ this ->rel = array_filter ($ this ->rel , function ($ r ) {
398+ return $ r !== 'external ' ;
399+ });
400+ }
401+
380402 return $ this ;
381403 }
382404
@@ -822,15 +844,9 @@ public function linkToCashtag($entity, $tweet = null)
822844 */
823845 public function linkToText (array $ entity , $ text , $ attributes = array ())
824846 {
825- $ rel = array ();
826- if ($ this ->external ) {
827- $ rel [] = 'external ' ;
828- }
829- if ($ this ->nofollow ) {
830- $ rel [] = 'nofollow ' ;
831- }
832- if (!empty ($ rel )) {
833- $ attributes ['rel ' ] = implode (' ' , $ rel );
847+ $ rel = $ this ->getRel ();
848+ if ($ rel !== '' ) {
849+ $ attributes ['rel ' ] = $ rel ;
834850 }
835851 if ($ this ->target ) {
836852 $ attributes ['target ' ] = $ this ->target ;
@@ -872,6 +888,39 @@ protected function linkToTextWithSymbol(array $entity, $symbol, $linkText, array
872888 return $ this ->linkToText ($ entity , $ linkText , $ attributes );
873889 }
874890
891+ /**
892+ * get rel attribute
893+ *
894+ * @return string
895+ */
896+ public function getRel ()
897+ {
898+ $ rel = $ this ->rel ;
899+ $ rel = array_unique ($ rel );
900+
901+ return implode (' ' , $ rel );
902+ }
903+
904+ /**
905+ * Set rel attribute.
906+ *
907+ * This method override setExternal/setNoFollow setting.
908+ *
909+ * @param string[]|string $rel the rel attribute
910+ * @param bool $merge if true, merge rel attributes instead replace.
911+ * @return $this
912+ */
913+ public function setRel ($ rel , $ merge = false )
914+ {
915+ if (is_string ($ rel )) {
916+ $ rel = explode (' ' , $ rel );
917+ }
918+
919+ $ this ->rel = $ merge ? array_unique (array_merge ($ this ->rel , $ rel )) : $ rel ;
920+
921+ return $ this ;
922+ }
923+
875924 /**
876925 * html escape
877926 *
0 commit comments