MCPcopy
hub / github.com/django/django / handle_word

Method handle_word

django/utils/html.py:342–410  ·  view source on GitHub ↗
(
        self,
        word,
        *,
        safe_input,
        trim_url_limit=None,
        nofollow=False,
        autoescape=False,
    )

Source from the content-addressed store, hash-verified

340 return "".join(urlized_words)
341
342 def handle_word(
343 self,
344 word,
345 *,
346 safe_input,
347 trim_url_limit=None,
348 nofollow=False,
349 autoescape=False,
350 ):
351 if "." in word or "@" in word or ":" in word:
352 # lead: Punctuation trimmed from the beginning of the word.
353 # middle: State of the word.
354 # trail: Punctuation trimmed from the end of the word.
355 lead, middle, trail = self.trim_punctuation(word)
356 # Make URL we want to point to.
357 url = None
358 nofollow_attr = ' rel="nofollow"' if nofollow else ""
359 if len(middle) <= MAX_URL_LENGTH and self.simple_url_re.match(middle):
360 url = smart_urlquote(html.unescape(middle))
361 elif len(middle) <= MAX_URL_LENGTH and self.simple_url_2_re.match(middle):
362 unescaped_middle = html.unescape(middle)
363 # RemovedInDjango70Warning: When the deprecation ends, replace
364 # with:
365 # url = smart_urlquote(f"https://{unescaped_middle}")
366 protocol = (
367 "https"
368 if getattr(settings, "URLIZE_ASSUME_HTTPS", False)
369 else "http"
370 )
371 if not settings.URLIZE_ASSUME_HTTPS:
372 warnings.warn(
373 "The default protocol will be changed from HTTP to "
374 "HTTPS in Django 7.0. Set the URLIZE_ASSUME_HTTPS "
375 "transitional setting to True to opt into using HTTPS as the "
376 "new default protocol.",
377 RemovedInDjango70Warning,
378 stacklevel=2,
379 )
380 url = smart_urlquote(f"{protocol}://{unescaped_middle}")
381 elif ":" not in middle and self.is_email_simple(middle):
382 local, domain = middle.rsplit("@", 1)
383 # Encode per RFC 6068 Section 2 (items 1, 4, 5). Defer any IDNA
384 # to the user agent. See #36013.
385 local = quote(local, safe="")
386 domain = quote(domain, safe="")
387 url = self.mailto_template.format(local=local, domain=domain)
388 nofollow_attr = ""
389 # Make link.
390 if url:
391 trimmed = self.trim_url(middle, limit=trim_url_limit)
392 if autoescape and not safe_input:
393 lead, trail = escape(lead), escape(trail)
394 trimmed = escape(trimmed)
395 middle = self.url_template.format(
396 href=escape(url),
397 attrs=nofollow_attr,
398 url=trimmed,
399 )

Callers 1

__call__Method · 0.95

Calls 10

trim_punctuationMethod · 0.95
is_email_simpleMethod · 0.95
trim_urlMethod · 0.95
SafeStringClass · 0.90
mark_safeFunction · 0.90
smart_urlquoteFunction · 0.85
quoteFunction · 0.85
escapeFunction · 0.85
matchMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected