summaryrefslogtreecommitdiff
path: root/data/extensions/uBlock0@raymondhill.net/js/static-ext-filtering.js
blob: 8a33093222b6aea5a16b034eb28bb1430c60768b (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*******************************************************************************

    uBlock Origin - a comprehensive, efficient content blocker
    Copyright (C) 2017-present Raymond Hill

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see {http://www.gnu.org/licenses/}.

    Home: https://github.com/gorhill/uBlock
*/

import cosmeticFilteringEngine from './cosmetic-filtering.js';
import htmlFilteringEngine from './html-filtering.js';
import httpheaderFilteringEngine from './httpheader-filtering.js';
import logger from './logger.js';
import scriptletFilteringEngine from './scriptlet-filtering.js';

/*******************************************************************************

  All static extended filters are of the form:

  field 1: one hostname, or a list of comma-separated hostnames
  field 2: `##` or `#@#`
  field 3: selector

  The purpose of the static extended filtering engine is to coarse-parse and
  dispatch to appropriate specialized filtering engines. There are currently
  three specialized filtering engines:

  - cosmetic filtering (aka "element hiding" in Adblock Plus)
  - scriptlet injection: selector starts with `script:inject`
    - New shorter syntax (1.15.12): `example.com##+js(bab-defuser.js)`
  - html filtering: selector starts with `^`

  Depending on the specialized filtering engine, field 1 may or may not be
  optional.

  The static extended filtering engine also offers parsing capabilities which
  are available to all other specialized filtering engines. For example,
  cosmetic and html filtering can ask the extended filtering engine to
  compile/validate selectors.

**/

//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------

const staticExtFilteringEngine = {
    get acceptedCount() {
        return cosmeticFilteringEngine.acceptedCount +
               scriptletFilteringEngine.acceptedCount +
               httpheaderFilteringEngine.acceptedCount +
               htmlFilteringEngine.acceptedCount;
    },
    get discardedCount() {
        return cosmeticFilteringEngine.discardedCount +
               scriptletFilteringEngine.discardedCount +
               httpheaderFilteringEngine.discardedCount +
               htmlFilteringEngine.discardedCount;
    },
};

//--------------------------------------------------------------------------
// Public methods
//--------------------------------------------------------------------------

staticExtFilteringEngine.reset = function() {
    cosmeticFilteringEngine.reset();
    scriptletFilteringEngine.reset();
    httpheaderFilteringEngine.reset();
    htmlFilteringEngine.reset();
};

staticExtFilteringEngine.freeze = function() {
    cosmeticFilteringEngine.freeze();
    scriptletFilteringEngine.freeze();
    httpheaderFilteringEngine.freeze();
    htmlFilteringEngine.freeze();
};

staticExtFilteringEngine.compile = function(parser, writer) {
    if ( parser.isExtendedFilter() === false ) { return false; }

    if ( parser.hasError() ) {
        logger.writeOne({
            realm: 'message',
            type: 'error',
            text: `Invalid extended filter in ${writer.properties.get('name') || '?'}: ${parser.raw}`
        });
        return true;
    }

    // Scriptlet injection
    if ( parser.isScriptletFilter() ) {
        scriptletFilteringEngine.compile(parser, writer);
        return true;
    }

    // Response header filtering
    if ( parser.isResponseheaderFilter() ) {
        httpheaderFilteringEngine.compile(parser, writer);
        return true;
    }

    // HTML filtering
    // TODO: evaluate converting Adguard's `$$` syntax into uBO's HTML
    //       filtering syntax.
    if ( parser.isHtmlFilter() ) {
        htmlFilteringEngine.compile(parser, writer);
        return true;
    }

    // Cosmetic filtering
    if ( parser.isCosmeticFilter() ) {
        cosmeticFilteringEngine.compile(parser, writer);
        return true;
    }

    logger.writeOne({
        realm: 'message',
        type: 'error',
        text: `Unknown extended filter in ${writer.properties.get('name') || '?'}: ${parser.raw}`
    });
    return true;
};

staticExtFilteringEngine.fromCompiledContent = function(reader, options) {
    cosmeticFilteringEngine.fromCompiledContent(reader, options);
    scriptletFilteringEngine.fromCompiledContent(reader, options);
    httpheaderFilteringEngine.fromCompiledContent(reader, options);
    htmlFilteringEngine.fromCompiledContent(reader, options);
};

staticExtFilteringEngine.toSelfie = function() {
    return {
        cosmetic: cosmeticFilteringEngine.toSelfie(),
        scriptlets: scriptletFilteringEngine.toSelfie(),
        httpHeaders: httpheaderFilteringEngine.toSelfie(),
        html: htmlFilteringEngine.toSelfie(),
    };
};

staticExtFilteringEngine.fromSelfie = async function(selfie) {
    if ( typeof selfie !== 'object' || selfie === null ) { return false; }
    cosmeticFilteringEngine.fromSelfie(selfie.cosmetic);
    httpheaderFilteringEngine.fromSelfie(selfie.httpHeaders);
    htmlFilteringEngine.fromSelfie(selfie.html);
    if ( scriptletFilteringEngine.fromSelfie(selfie.scriptlets) === false ) {
        return false;
    }
    return true;
};

/******************************************************************************/

export default staticExtFilteringEngine;

/******************************************************************************/