Serenity Operating System
1/*
2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibWeb/HTML/Parser/HTMLToken.h>
8
9namespace Web::HTML {
10
11DeprecatedString HTMLToken::to_deprecated_string() const
12{
13 StringBuilder builder;
14
15 switch (type()) {
16 case HTMLToken::Type::DOCTYPE:
17 builder.append("DOCTYPE"sv);
18 builder.append(" { name: '"sv);
19 builder.append(doctype_data().name);
20 builder.append("' }"sv);
21 break;
22 case HTMLToken::Type::StartTag:
23 builder.append("StartTag"sv);
24 break;
25 case HTMLToken::Type::EndTag:
26 builder.append("EndTag"sv);
27 break;
28 case HTMLToken::Type::Comment:
29 builder.append("Comment"sv);
30 break;
31 case HTMLToken::Type::Character:
32 builder.append("Character"sv);
33 break;
34 case HTMLToken::Type::EndOfFile:
35 builder.append("EndOfFile"sv);
36 break;
37 case HTMLToken::Type::Invalid:
38 VERIFY_NOT_REACHED();
39 }
40
41 if (type() == HTMLToken::Type::StartTag || type() == HTMLToken::Type::EndTag) {
42 builder.append(" { name: '"sv);
43 builder.append(tag_name());
44 builder.append("', { "sv);
45 for_each_attribute([&](auto& attribute) {
46 builder.append(attribute.local_name);
47 builder.append("=\""sv);
48 builder.append(attribute.value);
49 builder.append("\" "sv);
50 return IterationDecision::Continue;
51 });
52 builder.append("} }"sv);
53 }
54
55 if (is_comment()) {
56 builder.append(" { data: '"sv);
57 builder.append(comment());
58 builder.append("' }"sv);
59 }
60
61 if (is_character()) {
62 builder.append(" { data: '"sv);
63 builder.append_code_point(code_point());
64 builder.append("' }"sv);
65 }
66
67 if (type() == HTMLToken::Type::Character) {
68 builder.appendff("@{}:{}", m_start_position.line, m_start_position.column);
69 } else {
70 builder.appendff("@{}:{}-{}:{}", m_start_position.line, m_start_position.column, m_end_position.line, m_end_position.column);
71 }
72
73 return builder.to_deprecated_string();
74}
75
76}