UNPKG

3.42 kBJavaScriptView Raw
1(function () {
2
3 if (typeof Prism === 'undefined' || typeof document === 'undefined') {
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 attr = script.getAttribute('data-filter-selector');
98 if (attr) {
99 config.addSelector(attr);
100 }
101 attr = script.getAttribute('data-reject-selector');
102 if (attr) {
103 config.reject.addSelector(attr);
104 }
105 }
106
107 /**
108 * Applies all filters to the given element and returns true if and only if every filter returned true on the
109 * given element.
110 *
111 * @param {HTMLElement} element
112 * @returns {boolean}
113 */
114 function combinedFilter(element) {
115 for (var i = 0, l = filters.length; i < l; i++) {
116 if (!filters[i](element)) {
117 return false;
118 }
119 }
120 return true;
121 }
122
123 Prism.hooks.add('before-all-elements-highlight', function (env) {
124 env.elements = env.elements.filter(combinedFilter);
125 });
126
127}());