@@ -26,7 +26,8 @@ class Autolink
26
26
'strip_scheme ' => false ,
27
27
'text_limit ' => false ,
28
28
'auto_title ' => false ,
29
- 'escape ' => true
29
+ 'escape ' => true ,
30
+ 'link_no_scheme ' => false
30
31
);
31
32
32
33
/**
@@ -72,19 +73,32 @@ public function __construct($options = array(), $schemes = array())
72
73
public function convert ($ text , $ attribs = array ())
73
74
{
74
75
$ self = $ this ;
76
+ $ linkNoScheme = $ this ->linkNoScheme ();
75
77
76
- $ regex = "/(([a-zA-Z]*= \")*( " . $ this ->getSchemes (true ) . ")\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}([\/a-zA-Z0-9\-._~:?#\[\]@!$&'()*+,;=% \">]*)?)/ " ;
78
+ if ($ linkNoScheme ) {
79
+ $ schemeRegex = "[(%s)\:\/\/]* " ;
80
+ } else {
81
+ $ schemeRegex = "(%s)\:\/\/ " ;
82
+ }
83
+
84
+ $ schemeRegex = sprintf ($ schemeRegex , $ this ->getSchemes (true ));
85
+
86
+ $ regex = '/(([a-zA-Z]*=")* ' . $ schemeRegex . "[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}([\/a-zA-Z0-9\-._~:?#\[\]@!$&'()*+,;=% \">]*)?)/ " ;
77
87
78
88
return preg_replace_callback (
79
89
$ regex ,
80
- function ($ matches ) use ($ self , $ attribs ) {
90
+ function ($ matches ) use ($ self , $ attribs, $ linkNoScheme ) {
81
91
preg_match ('/[a-zA-Z]*\=\"(.*)/ ' , $ matches [0 ], $ inElements );
82
92
83
- if (! $ inElements ) {
84
- return $ self -> link ( $ matches [0 ], $ attribs ) ;
93
+ if ($ inElements ) {
94
+ return $ matches [0 ];
85
95
}
86
96
87
- return $ matches [0 ];
97
+ if ($ linkNoScheme && strpos ($ matches [0 ], ':// ' ) === 0 ) {
98
+ return $ matches [0 ];
99
+ }
100
+
101
+ return $ self ->link ($ matches [0 ], $ attribs );
88
102
},
89
103
$ text
90
104
);
@@ -151,6 +165,12 @@ public function link($url, $attribs = array())
151
165
152
166
$ attribs ['href ' ] = $ this ->autoEscape () ? htmlspecialchars ($ url ) : $ url ;
153
167
168
+ if (($ scheme = $ this ->linkNoScheme ()) && strpos ($ attribs ['href ' ], ':// ' ) === false ) {
169
+ $ scheme = is_string ($ scheme ) ? $ scheme : 'http ' ;
170
+
171
+ $ attribs ['href ' ] = $ scheme . ':// ' . $ attribs ['href ' ];
172
+ }
173
+
154
174
if ($ this ->autoTitle ()) {
155
175
$ attribs ['title ' ] = htmlspecialchars ($ url );
156
176
}
@@ -246,6 +266,18 @@ public function autoTitle($value = null)
246
266
return $ this ->optionAccess ('auto_title ' , $ value );
247
267
}
248
268
269
+ /**
270
+ * linkNoScheme
271
+ *
272
+ * @param mixed $value
273
+ *
274
+ * @return mixed|static
275
+ */
276
+ public function linkNoScheme ($ value = null )
277
+ {
278
+ return $ this ->optionAccess ('link_no_scheme ' , $ value );
279
+ }
280
+
249
281
/**
250
282
* optionAccess
251
283
*
0 commit comments