1/**
2 * This file is part of the @iconify/iconify package.
3 *
4 * (c) Vjacheslav Trushkin <cyberalien@gmail.com>
5 *
6 * For the full copyright and license information, please view the license.txt or license.gpl.txt
7 * files that were distributed with this source code.
8 *
9 * Licensed under Apache 2.0 or GPL 2.0 at your option.
10 * If derivative product is not compatible with one of licenses, you can pick one of licenses.
11 *
12 * @license Apache 2.0
13 * @license GPL 2.0
14 */
15
16/**
17 * Plugin for FontAwesome icons
18 */
19(function(Iconify) {
20    "use strict";
21
22    /**
23     * List of FontAwesome class names that do not represent icons
24     *
25     * @type {[string]}
26     */
27    var faReserved = ['fa-lg', 'fa-fw', 'fa-ul', 'fa-li', 'fa-border', 'fa-pull-left', 'fa-pull-right', 'fa-spin', 'fa-pulse', 'fa-rotate-90', 'fa-rotate-180', 'fa-rotate-270', 'fa-flip-horizontal', 'fa-flip-vertical', 'fa-stack', 'fa-stack-1x', 'fa-stack-2x', 'fa-inverse'],
28        rotateAttribute = Iconify.getConfig('_rotateAttribute'),
29        flipAttribute = Iconify.getConfig('_flipAttribute'),
30        inlineAttribute = Iconify.getConfig('_inlineModeAttribute'),
31        i;
32
33    /**
34     * Link to stylesheet
35     *
36     * @type {string}
37     */
38    var stylesheetCDN = DOKU_TPL + '/assets/iconify/plugins/fa.css';
39
40    /**
41     * True if stylesheet has been added
42     *
43     * @type {boolean}
44     */
45    var styleAdded = false;
46
47    /**
48     * Inserts short version of FontAwesome stylesheet into DOM
49     */
50    function insertStylesheet() {
51        var element = document.createElement('link');
52
53        styleAdded = true;
54
55        element.setAttribute('rel', 'stylesheet');
56        element.setAttribute('type', 'text/css');
57        element.setAttribute('href', stylesheetCDN);
58        document.head.appendChild(element);
59    }
60
61    /**
62     * Create finder object
63     *
64     * @param {string} selector
65     * @param {string} prefix
66     * @returns {object}
67     */
68    function finder(selector, prefix) {
69        return {
70            selector: selector,
71
72            /**
73             * Get icon name from element
74             *
75             * @param {Element} element
76             * @return {string}
77             */
78            icon: function(element) {
79                var item;
80
81                for (var i = 0; i < element.classList.length; i++) {
82                    item = element.classList[i];
83                    if (item.slice(0, 3) === 'fa-' && faReserved.indexOf(item) === -1) {
84                        return prefix + ':' + item.slice(3);
85                    }
86                }
87
88                return '';
89            },
90
91            /**
92             * Filter class names list, removing any FontAwesome specific classes
93             *
94             * @param {object} image
95             * @param {Array|DOMTokenList} list
96             * @return {Array}
97             */
98            filterClasses: function(image, list) {
99                var results = [],
100                    transform = {
101                        rotate: 0,
102                        hFlip: false,
103                        vFlip: false
104                    };
105
106                if (image.attributes === void 0) {
107                    image.attributes = Object.create(null);
108                }
109
110                for (var i = 0; i < list.length; i++) {
111                    switch (list[i]) {
112                        case 'fa-rotate-90':
113                            transform.rotate = 1;
114                            break;
115
116                        case 'fa-rotate-180':
117                            transform.rotate = 2;
118                            break;
119
120                        case 'fa-rotate-270':
121                            transform.rotate = 3;
122                            break;
123
124                        case 'fa-flip-horizontal':
125                            transform.hFlip = true;
126                            break;
127
128                        case 'fa-flip-vertical':
129                            transform.vFlip = true;
130                            break;
131
132                        default:
133                            results.push(list[i]);
134                    }
135                }
136
137                if (image.attributes[inlineAttribute] === void 0) {
138                    image.attributes[inlineAttribute] = true;
139                }
140
141                // Add transformation as attributes
142                if (transform.rotate) {
143                    image.attributes[rotateAttribute] = transform.rotate;
144                }
145                if (transform.hFlip || transform.vFlip) {
146                    image.attributes[flipAttribute] = (transform.hFlip && transform.vFlip) ? 'horizontal vertical' : (
147                        transform.hFlip ? 'horizontal' : 'vertical'
148                    );
149                }
150
151                // Insert short version of FontAwesome stylesheet into DOM
152                if (!styleAdded) {
153                    insertStylesheet();
154                }
155
156                return results;
157            }
158        };
159    }
160
161    /**
162     * Add more reserved keywords
163     */
164    for (i = 2; i < 11; i ++) {
165        faReserved.push('fa-' + i + 'x');
166    }
167
168    /**
169     * Add finder to list of finders
170     */
171    Iconify.addFinder('fa', finder('.fa', 'fa'));
172    Iconify.addFinder('fas', finder('.fas', 'fa-solid'));
173    Iconify.addFinder('far', finder('.far', 'fa-regular'));
174    Iconify.addFinder('fal', finder('.fal', 'fa-light')); // pro only
175    Iconify.addFinder('fab', finder('.fab', 'fa-brands'));
176
177})(Iconify);
178