aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Functions/extractTextFromHTML.cpp
blob: 4eefeaa9f86c8e1047465284132f8b51e97cd5dd (plain) (blame)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <Columns/ColumnString.h>
#include <Functions/FunctionFactory.h>
#include <Functions/FunctionHelpers.h>
#include <Functions/IFunction.h>
#include <base/find_symbols.h>
#include <Common/StringUtils/StringUtils.h>


/** A function to extract text from HTML or XHTML.
  * It does not necessarily 100% conforms to any of the HTML, XML or XHTML standards,
  * but the implementation is reasonably accurate and it is fast.
  *
  * The rules are the following:
  *
  * 1. Comments are skipped. Example: <!-- test -->
  * Comment must end with -->. Nested comments are not possible.
  * Note: constructions like <!--> <!---> are not valid comments in HTML but will be skipped by other rules.
  *
  * 2. CDATA is pasted verbatim.
  * Note: CDATA is XML/XHTML specific. But we still process it for "best-effort" approach.
  *
  * 3. 'script' and 'style' elements are removed with all their content.
  * Note: it's assumed that closing tag cannot appear inside content.
  * For example, in JS string literal is has to be escaped as "<\/script>".
  * Note: comments and CDATA is possible inside script or style - then closing tags are not searched inside CDATA.
  * Example: <script><![CDATA[</script>]]></script>
  * But still searched inside comments. Sometimes it becomes complicated:
  * <script>var x = "<!--"; </script> var y = "-->"; alert(x + y);</script>
  * Note: script and style can be the names of XML namespaces - then they are not treat like usual script or style.
  * Example: <script:a>Hello</script:a>.
  * Note: whitespaces are possible after closing tag name: </script > but not before: < / script>.
  *
  * 4. Other tags or tag-like elements are skipped without inner content.
  * Example: <a>.</a>
  * Note: it's expected that this HTML is illegal: <a test=">"></a>
  * Note: it will also skip something like tags: <>, <!>, etc.
  * Note: tag without end will be skipped to the end of input: <hello
  * >
  * 5. HTML and XML entities are not decoded.
  * It should be processed by separate function.
  *
  * 6. Whitespaces in text are collapsed or inserted by specific rules.
  * Whitespaces at beginning and at the end are removed.
  * Consecutive whitespaces are collapsed.
  * But if text is separated by other elements and there is no whitespace, it is inserted.
  * It may be unnatural, examples: Hello<b>world</b>, Hello<!-- -->world
  * - in HTML there will be no whitespace, but the function will insert it.
  * But also consider: Hello<p>world</p>, Hello<br>world.
  * This behaviour is reasonable for data analysis, e.g. convert HTML to a bag of words.
  *
  * 7. Also note that correct handling of whitespaces would require
  * support of <pre></pre> and CSS display and white-space properties.
  *
  * Usage example:
  *
  * SELECT extractTextFromHTML(html) FROM url('https://github.com/ClickHouse/ClickHouse', RawBLOB, 'html String')
  *
  * - ClickHouse has embedded web browser.
  */

namespace DB
{

namespace ErrorCodes
{
    extern const int ILLEGAL_COLUMN;
    extern const int ILLEGAL_TYPE_OF_ARGUMENT;
}

namespace
{

inline bool startsWith(const char * s, const char * end, const std::string_view prefix)
{
    return s + prefix.length() < end && 0 == memcmp(s, prefix.data(), prefix.length());
}

inline bool checkAndSkip(const char * __restrict & s, const char * end, const std::string_view prefix)
{
    if (startsWith(s, end, prefix))
    {
        s += prefix.length();
        return true;
    }
    return false;
}

bool processComment(const char * __restrict & src, const char * end)
{
    if (!checkAndSkip(src, end, "<!--"))
        return false;

    while (true)
    {
        const char * gt = find_first_symbols<'>'>(src, end);
        if (gt >= end)
            break;

        if (gt > src + strlen("--") && gt[-1] == '-' && gt[-2] == '-')
        {
            src = gt + 1;
            break;
        }

        src = gt + 1;
    }

    return true;
}

bool processCDATA(const char * __restrict & src, const char * end, char * __restrict & dst)
{
    if (!checkAndSkip(src, end, "<![CDATA["))
        return false;

    const char * gt = src;
    while (true)
    {
        gt = find_first_symbols<'>'>(gt, end);
        if (gt >= end)
            break;

        if (gt[-1] == ']' && gt[-2] == ']')
        {
            if (dst)
            {
                size_t bytes_to_copy = gt - src - strlen("]]");
                memcpy(dst, src, bytes_to_copy);
                dst += bytes_to_copy;
            }
            src = gt + 1;
            break;
        }

        ++gt;
    }

    return true;
}

bool processElementAndSkipContent(const char * __restrict & src, const char * end, const std::string_view tag_name)
{
    const auto * old_src = src;

    if (!(src < end && *src == '<'))
        return false;
    ++src;

    if (!checkAndSkip(src, end, tag_name))
    {
        src = old_src;
        return false;
    }

    if (src >= end)
        return false;

    if (!(isWhitespaceASCII(*src) || *src == '>'))
    {
        src = old_src;
        return false;
    }

    const char * gt = find_first_symbols<'>'>(src, end);
    if (gt >= end)
        return false;

    src = gt + 1;

    while (true)
    {
        const char * lt = find_first_symbols<'<'>(src, end);
        src = lt;
        if (src + 1 >= end)
            break;

        ++src;

        /// Skip CDATA
        if (*src == '!')
        {
            --src;
            char * dst = nullptr;
            if (processCDATA(src, end, dst))
                continue;
            ++src;
        }

        if (*src != '/')
            continue;
        ++src;

        if (checkAndSkip(src, end, tag_name))
        {
            while (src < end && isWhitespaceASCII(*src))
                ++src;

            if (src >= end)
                break;

            if (*src == '>')
            {
                ++src;
                break;
            }
        }
    }

    return true;
}

bool skipTag(const char * __restrict & src, const char * end)
{
    if (src < end && *src == '<')
    {
        src = find_first_symbols<'>'>(src, end);
        if (src < end)
            ++src;

        return true;
    }

    return false;
}

void copyText(const char * __restrict & src, const char * end, char * __restrict & dst, bool needs_whitespace)
{
    while (src < end && isWhitespaceASCII(*src))
        ++src;

    const char * lt = find_first_symbols<'<'>(src, end);

    if (needs_whitespace && src < lt)
    {
        *dst = ' ';
        ++dst;
    }

    while (true)
    {
        const char * ws = find_first_symbols<' ', '\t', '\n', '\r', '\f', '\v'>(src, lt);
        size_t bytes_to_copy = ws - src;
        memcpy(dst, src, bytes_to_copy);
        dst += bytes_to_copy;

        src = ws;
        while (src < lt && isWhitespaceASCII(*src))
            ++src;

        if (src < lt)
        {
            *dst = ' ';
            ++dst;
        }
        else
        {
            break;
        }
    }

    src = lt;
}

size_t extract(const char * __restrict src, size_t size, char * __restrict dst)
{
    /** There are the following rules:
      * - comments are removed with all their content;
      * - elements 'script' and 'style' are removed with all their content;
      * - for other elements tags are removed but content is processed as text;
      * - CDATA should be copied verbatim;
      */

    const char * end = src + size;
    char * dst_begin = dst;

    while (src < end)
    {
        bool needs_whitespace = dst != dst_begin && dst[-1] != ' ';
        copyText(src, end, dst, needs_whitespace);

        processComment(src, end)
            || processCDATA(src, end, dst)
            || processElementAndSkipContent(src, end, "script")
            || processElementAndSkipContent(src, end, "style")
            || skipTag(src, end);
    }

    return dst - dst_begin;
}

}


class FunctionExtractTextFromHTML : public IFunction
{
public:
    static constexpr auto name = "extractTextFromHTML";

    static FunctionPtr create(ContextPtr) { return std::make_shared<FunctionExtractTextFromHTML>(); }
    String getName() const override { return name; }
    size_t getNumberOfArguments() const override { return 1; }
    bool useDefaultImplementationForConstants() const override { return true; }
    bool isSuitableForShortCircuitArgumentsExecution(const DataTypesWithConstInfo & /*arguments*/) const override { return true; }

    DataTypePtr getReturnTypeImpl(const DataTypes & arguments) const override
    {
        if (!isString(arguments[0]))
            throw Exception(ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT, "Illegal type {} of argument of function {}",
                arguments[0]->getName(), getName());
        return arguments[0];
    }

    ColumnPtr executeImpl(const ColumnsWithTypeAndName & arguments, const DataTypePtr &, size_t rows) const override
    {
        const ColumnString * src = checkAndGetColumn<ColumnString>(arguments[0].column.get());
        if (!src)
             throw Exception(ErrorCodes::ILLEGAL_COLUMN, "First argument for function {} must be string.", getName());

        const ColumnString::Chars & src_chars = src->getChars();
        const ColumnString::Offsets & src_offsets = src->getOffsets();

        auto res = ColumnString::create();

        ColumnString::Chars & res_chars = res->getChars();
        ColumnString::Offsets & res_offsets = res->getOffsets();

        res_chars.resize(src_chars.size());
        res_offsets.resize(src_offsets.size());

        ColumnString::Offset src_offset = 0;
        ColumnString::Offset res_offset = 0;

        for (size_t i = 0; i < rows; ++i)
        {
            auto next_src_offset = src_offsets[i];

            res_offset += extract(
                reinterpret_cast<const char *>(&src_chars[src_offset]),
                next_src_offset - src_offset - 1,
                reinterpret_cast<char *>(&res_chars[res_offset]));

            res_chars[res_offset] = 0;
            ++res_offset;
            res_offsets[i] = res_offset;

            src_offset = next_src_offset;
        }

        res_chars.resize(res_offset);
        return res;
    }
};

REGISTER_FUNCTION(ExtractTextFromHTML)
{
    factory.registerFunction<FunctionExtractTextFromHTML>();
}

}