|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 | 3 | // Wraps some elements in anchor tags referencing to the Symfony documentation
|
4 |
| -$(function() { |
5 |
| - var $modal = $('#sourceCodeModal'); |
6 |
| - var $controllerCode = $modal.find('code.php'); |
7 |
| - var $templateCode = $modal.find('code.twig'); |
| 4 | +document.addEventListener('DOMContentLoaded', function() { |
| 5 | + const modalElt = document.querySelector('#sourceCodeModal'); |
| 6 | + if (!modalElt) { |
| 7 | + return; |
| 8 | + } |
| 9 | + const controllerCode = modalElt.querySelector('code.php'); |
| 10 | + const templateCode = modalElt.querySelector('code.twig'); |
8 | 11 |
|
9 | 12 | function anchor(url, content) {
|
10 | 13 | return '<a class="doclink" target="_blank" href="' + url + '">' + content + '</a>';
|
11 |
| - }; |
| 14 | + } |
12 | 15 |
|
13 | 16 | function wrap(content, links) {
|
14 | 17 | return content.replace(
|
15 | 18 | new RegExp(Object.keys(links).join('|'), 'g'),
|
16 | 19 | token => anchor(links[token], token)
|
17 | 20 | );
|
18 |
| - }; |
| 21 | + } |
19 | 22 |
|
20 |
| - // Wraps links to the Symfony documentation |
21 |
| - $modal.find('.hljs-comment').each(function() { |
22 |
| - $(this).html($(this).html().replace(/https:\/\/symfony.com\/doc\/[\w/.#-]+/g, function(url) { |
23 |
| - return anchor(url, url); |
24 |
| - })); |
| 23 | + // Wrap Symfony Doc urls in comments |
| 24 | + [...modalElt.querySelectorAll('.hljs-comment')].forEach((commentElt) => { |
| 25 | + commentElt.innerHTML = commentElt.innerHTML.replace(/https:\/\/symfony.com\/[\w/.#-]+/g, (url) => anchor(url, url)); |
25 | 26 | });
|
26 | 27 |
|
27 |
| - // Wraps Symfony's attributes |
28 |
| - var attributes = { |
29 |
| - 'Cache': 'https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/cache.html', |
30 |
| - 'IsGranted': 'https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/security.html#isgranted', |
31 |
| - 'ParamConverter': 'https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html', |
32 |
| - 'Route': 'https://symfony.com/doc/current/routing.html#creating-routes-as-attributes-or-annotations', |
33 |
| - 'Security': 'https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/security.html#security' |
| 28 | + // Wraps Symfony PHP attributes in code |
| 29 | + const attributes = { |
| 30 | + 'Cache': 'https://symfony.com/doc/current/http_cache.html#http-cache-expiration-intro', |
| 31 | + 'Route': 'https://symfony.com/doc/current/routing.html#creating-routes-as-attributes', |
| 32 | + 'IsGranted': 'https://symfony.com/doc/current/security.html#security-securing-controller-annotations' |
34 | 33 | };
|
35 |
| - |
36 |
| - $controllerCode.find('.hljs-meta').each(function() { |
37 |
| - var src = $(this).text(); |
38 |
| - |
39 |
| - $(this).html(wrap(src, attributes)); |
| 34 | + [...controllerCode.querySelectorAll('.hljs-meta')].forEach((elt) => { |
| 35 | + elt.innerHTML = wrap(elt.textContent, attributes); |
40 | 36 | });
|
41 | 37 |
|
42 | 38 | // Wraps Twig's tags
|
43 |
| - $templateCode.find('.hljs-template-tag + .hljs-name').each(function() { |
44 |
| - var tag = $(this).text(); |
45 |
| - |
| 39 | + [...templateCode.querySelectorAll('.hljs-template-tag + .hljs-name')].forEach((elt) => { |
| 40 | + const tag = elt.textContent; |
46 | 41 | if ('else' === tag || tag.match(/^end/)) {
|
47 | 42 | return;
|
48 | 43 | }
|
49 |
| - |
50 |
| - var url = 'https://twig.symfony.com/doc/3.x/tags/' + tag + '.html#' + tag; |
51 |
| - |
52 |
| - $(this).html(anchor(url, tag)); |
| 44 | + const url = 'https://twig.symfony.com/doc/3.x/tags/' + tag + '.html#' + tag; |
| 45 | + elt.innerHTML = anchor(url, tag); |
53 | 46 | });
|
54 | 47 |
|
55 | 48 | // Wraps Twig's functions
|
56 |
| - $templateCode.find('.hljs-template-variable > .hljs-name').each(function() { |
57 |
| - var func = $(this).text(); |
58 |
| - |
59 |
| - var url = 'https://twig.symfony.com/doc/3.x/functions/' + func + '.html#' + func; |
60 |
| - |
61 |
| - $(this).html(anchor(url, func)); |
| 49 | + [...templateCode.querySelectorAll('.hljs-template-variable > .hljs-name')].forEach((elt) => { |
| 50 | + const func = elt.textContent; |
| 51 | + const url = 'https://twig.symfony.com/doc/3.x/functions/' + func + '.html#' + func; |
| 52 | + elt.innerHTML = anchor(url, func); |
62 | 53 | });
|
63 | 54 | });
|
0 commit comments