UNPKG

3.38 kBJavaScriptView Raw
1(function () {
2
3 if (typeof self !== 'undefined' && !self.Prism) {
4 return;
5 }
6
7 // https://developer.mozilla.org/en-US/docs/Web/API/Element/matches#Polyfill
8 if (!Element.prototype.matches) {
9 Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector;
10 }
11
12 var script = Prism.util.currentScript();
13
14
15 /**
16 * @type {Array<(element: HTMLElement) => boolean>}
17 */
18 var filters = [];
19
20 var config = Prism.plugins.filterHighlightAll = {
21
22 /**
23 * Adds a new filter for the elements of `highlightAll` and `highlightAllUnder` such that only elements for
24 * which the given function returns `true` will be highlighted.
25 *
26 * @param {(value: { element: HTMLElement, language: string }) => boolean} condition
27 */
28 add: function (condition) {
29 filters.push(function (element) {
30 return condition({
31 element: element,
32 language: Prism.util.getLanguage(element)
33 });
34 });
35 },
36
37 /**
38 * Adds a new filter for the elements of `highlightAll` and `highlightAllUnder` such that only elements that
39 * match the given CSS selection will be highlighted.
40 *
41 * @param {string} selector
42 */
43 addSelector: function (selector) {
44 filters.push(function (element) {
45 return element.matches(selector);
46 });
47 },
48
49 reject: {
50
51 /**
52 * Adds a new filter for the elements of `highlightAll` and `highlightAllUnder` such that only elements for
53 * which the given function returns `false` will be highlighted.
54 *
55 * @param {(value: { element: HTMLElement, language: string }) => boolean} condition
56 */
57 add: function (condition) {
58 filters.push(function (element) {
59 return !condition({
60 element: element,
61 language: Prism.util.getLanguage(element)
62 });
63 });
64 },
65
66 /**
67 * Adds a new filter for the elements of `highlightAll` and `highlightAllUnder` such that only elements that do
68 * not match the given CSS selection will be highlighted.
69 *
70 * @param {string} selector
71 */
72 addSelector: function (selector) {
73 filters.push(function (element) {
74 return !element.matches(selector);
75 });
76 },
77
78 },
79
80 /**
81 * Filters the elements of `highlightAll` and `highlightAllUnder` such that only elements with a known language
82 * will be highlighted. All elements with an unset or unknown language will be ignored.
83 *
84 * __Note:__ This will effectively disable the AutoLoader plugin.
85 *
86 * @type {boolean}
87 */
88 filterKnown: !!script && script.hasAttribute('data-filter-known')
89 };
90
91 config.add(function filterKnown(env) {
92 return !config.filterKnown || typeof Prism.languages[env.language] === 'object';
93 });
94
95 if (script) {
96 var attr;
97 if (attr = script.getAttribute('data-filter-selector')) {
98 config.addSelector(attr);
99 }
100 if (attr = script.getAttribute('data-reject-selector')) {
101 config.reject.addSelector(attr);
102 }
103 }
104
105 /**
106 * Applies all filters to the given element and returns true if and only if every filter returned true on the
107 * given element.
108 *
109 * @param {HTMLElement} element
110 * @returns {boolean}
111 */
112 function combinedFilter(element) {
113 for (var i = 0, l = filters.length; i < l; i++) {
114 if (!filters[i](element)) {
115 return false;
116 }
117 }
118 return true;
119 }
120
121 Prism.hooks.add('before-all-elements-highlight', function (env) {
122 env.elements = env.elements.filter(combinedFilter);
123 });
124
125}());