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