mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:37:44 +00:00
Libraries: Create top level directory for libraries.
Things were getting a little crowded in the project root, so this patch moves the Lib*/ directories into Libraries/.
This commit is contained in:
parent
63814ffebf
commit
04b9dc2d30
328 changed files with 36 additions and 36 deletions
151
Libraries/LibHTML/Parser/CSSParser.cpp
Normal file
151
Libraries/LibHTML/Parser/CSSParser.cpp
Normal file
|
@ -0,0 +1,151 @@
|
|||
#include <LibHTML/CSS/StyleSheet.h>
|
||||
#include <LibHTML/Parser/CSSParser.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
NonnullRefPtr<StyleValue> parse_css_value(const StringView& view)
|
||||
{
|
||||
String string(view);
|
||||
bool ok;
|
||||
int as_int = string.to_int(ok);
|
||||
if (ok)
|
||||
return LengthStyleValue::create(Length(as_int, Length::Type::Absolute));
|
||||
unsigned as_uint = string.to_uint(ok);
|
||||
if (ok)
|
||||
return LengthStyleValue::create(Length(as_uint, Length::Type::Absolute));
|
||||
if (string == "auto")
|
||||
return LengthStyleValue::create(Length());
|
||||
return StringStyleValue::create(string);
|
||||
}
|
||||
|
||||
NonnullRefPtr<StyleSheet> parse_css(const String& css)
|
||||
{
|
||||
NonnullRefPtrVector<StyleRule> rules;
|
||||
|
||||
enum class State {
|
||||
Free,
|
||||
InSelectorComponent,
|
||||
InPropertyName,
|
||||
InPropertyValue,
|
||||
};
|
||||
|
||||
struct CurrentRule {
|
||||
Vector<Selector> selectors;
|
||||
NonnullRefPtrVector<StyleDeclaration> declarations;
|
||||
};
|
||||
|
||||
CurrentRule current_rule;
|
||||
Vector<char> buffer;
|
||||
|
||||
int index = 0;
|
||||
|
||||
auto peek = [&]() -> char {
|
||||
if (index < css.length())
|
||||
return css[index];
|
||||
return 0;
|
||||
};
|
||||
|
||||
auto consume_specific = [&](char ch) {
|
||||
ASSERT(peek() == ch);
|
||||
++index;
|
||||
return ch;
|
||||
};
|
||||
|
||||
auto consume_one = [&]() -> char {
|
||||
return css[index++];
|
||||
};
|
||||
|
||||
auto consume_whitespace = [&] {
|
||||
while (isspace(peek()))
|
||||
++index;
|
||||
};
|
||||
|
||||
auto is_valid_selector_char = [](char ch) {
|
||||
return isalnum(ch) || ch == '-' || ch == '_';
|
||||
};
|
||||
|
||||
auto parse_selector = [&] {
|
||||
consume_whitespace();
|
||||
Selector::Component::Type type;
|
||||
if (peek() == '.')
|
||||
type = Selector::Component::Type::Class;
|
||||
else if (peek() == '#')
|
||||
type = Selector::Component::Type::Id;
|
||||
else
|
||||
type = Selector::Component::Type::TagName;
|
||||
|
||||
while (is_valid_selector_char(peek()))
|
||||
buffer.append(consume_one());
|
||||
|
||||
ASSERT(!buffer.is_null());
|
||||
|
||||
auto component_string = String::copy(buffer);
|
||||
|
||||
Vector<Selector::Component> components;
|
||||
components.append({ type, component_string });
|
||||
buffer.clear();
|
||||
current_rule.selectors.append(Selector(move(components)));
|
||||
};
|
||||
|
||||
auto parse_selector_list = [&] {
|
||||
for (;;) {
|
||||
parse_selector();
|
||||
consume_whitespace();
|
||||
if (peek() == ',') {
|
||||
consume_one();
|
||||
continue;
|
||||
}
|
||||
if (peek() == '{')
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
auto is_valid_property_name_char = [](char ch) {
|
||||
return !isspace(ch) && ch != ':';
|
||||
};
|
||||
|
||||
auto is_valid_property_value_char = [](char ch) {
|
||||
return !isspace(ch) && ch != ';';
|
||||
};
|
||||
|
||||
auto parse_declaration = [&] {
|
||||
consume_whitespace();
|
||||
buffer.clear();
|
||||
while (is_valid_property_name_char(peek()))
|
||||
buffer.append(consume_one());
|
||||
auto property_name = String::copy(buffer);
|
||||
buffer.clear();
|
||||
consume_whitespace();
|
||||
consume_specific(':');
|
||||
consume_whitespace();
|
||||
while (is_valid_property_value_char(peek()))
|
||||
buffer.append(consume_one());
|
||||
auto property_value = String::copy(buffer);
|
||||
buffer.clear();
|
||||
consume_specific(';');
|
||||
current_rule.declarations.append(StyleDeclaration::create(property_name, parse_css_value(property_value)));
|
||||
};
|
||||
|
||||
auto parse_declarations = [&] {
|
||||
for (;;) {
|
||||
parse_declaration();
|
||||
consume_whitespace();
|
||||
if (peek() == '}')
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
auto parse_rule = [&] {
|
||||
parse_selector_list();
|
||||
consume_specific('{');
|
||||
parse_declarations();
|
||||
consume_specific('}');
|
||||
rules.append(StyleRule::create(move(current_rule.selectors), move(current_rule.declarations)));
|
||||
};
|
||||
|
||||
while (index < css.length()) {
|
||||
parse_rule();
|
||||
}
|
||||
|
||||
return StyleSheet::create(move(rules));
|
||||
}
|
7
Libraries/LibHTML/Parser/CSSParser.h
Normal file
7
Libraries/LibHTML/Parser/CSSParser.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <LibHTML/CSS/StyleSheet.h>
|
||||
|
||||
NonnullRefPtr<StyleSheet> parse_css(const String&);
|
||||
|
236
Libraries/LibHTML/Parser/HTMLParser.cpp
Normal file
236
Libraries/LibHTML/Parser/HTMLParser.cpp
Normal file
|
@ -0,0 +1,236 @@
|
|||
#include <AK/NonnullRefPtrVector.h>
|
||||
#include <LibHTML/DOM/Element.h>
|
||||
#include <LibHTML/DOM/Text.h>
|
||||
#include <LibHTML/Parser/HTMLParser.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static NonnullRefPtr<Element> create_element(const String& tag_name)
|
||||
{
|
||||
return adopt(*new Element(tag_name));
|
||||
}
|
||||
|
||||
static bool is_valid_in_attribute_name(char ch)
|
||||
{
|
||||
return isalnum(ch) || ch == '_' || ch == '-';
|
||||
}
|
||||
|
||||
static bool is_self_closing_tag(const String& tag_name)
|
||||
{
|
||||
return tag_name == "area"
|
||||
|| tag_name == "base"
|
||||
|| tag_name == "br"
|
||||
|| tag_name == "col"
|
||||
|| tag_name == "embed"
|
||||
|| tag_name == "hr"
|
||||
|| tag_name == "img"
|
||||
|| tag_name == "input"
|
||||
|| tag_name == "link"
|
||||
|| tag_name == "meta"
|
||||
|| tag_name == "param"
|
||||
|| tag_name == "source"
|
||||
|| tag_name == "track"
|
||||
|| tag_name == "wbr";
|
||||
}
|
||||
|
||||
NonnullRefPtr<Document> parse_html(const String& html)
|
||||
{
|
||||
NonnullRefPtrVector<ParentNode> node_stack;
|
||||
|
||||
auto doc = adopt(*new Document);
|
||||
node_stack.append(doc);
|
||||
|
||||
enum class State {
|
||||
Free = 0,
|
||||
BeforeTagName,
|
||||
InTagName,
|
||||
InAttributeList,
|
||||
InAttributeName,
|
||||
BeforeAttributeValue,
|
||||
InAttributeValueNoQuote,
|
||||
InAttributeValueSingleQuote,
|
||||
InAttributeValueDoubleQuote,
|
||||
};
|
||||
|
||||
auto state = State::Free;
|
||||
|
||||
Vector<char, 256> text_buffer;
|
||||
|
||||
Vector<char, 32> tag_name_buffer;
|
||||
|
||||
Vector<Attribute> attributes;
|
||||
Vector<char, 256> attribute_name_buffer;
|
||||
Vector<char, 256> attribute_value_buffer;
|
||||
|
||||
bool is_slash_tag = false;
|
||||
|
||||
auto move_to_state = [&](State new_state) {
|
||||
if (new_state == State::BeforeTagName) {
|
||||
is_slash_tag = false;
|
||||
tag_name_buffer.clear();
|
||||
attributes.clear();
|
||||
}
|
||||
if (new_state == State::InAttributeName)
|
||||
attribute_name_buffer.clear();
|
||||
if (new_state == State::BeforeAttributeValue)
|
||||
attribute_value_buffer.clear();
|
||||
if (state == State::Free && !text_buffer.is_empty()) {
|
||||
auto text_node = adopt(*new Text(String::copy(text_buffer)));
|
||||
text_buffer.clear();
|
||||
node_stack.last().append_child(text_node);
|
||||
}
|
||||
state = new_state;
|
||||
text_buffer.clear();
|
||||
};
|
||||
|
||||
auto close_tag = [&] {
|
||||
if (node_stack.size() > 1)
|
||||
node_stack.take_last();
|
||||
};
|
||||
|
||||
auto open_tag = [&] {
|
||||
auto new_element = create_element(String::copy(tag_name_buffer));
|
||||
tag_name_buffer.clear();
|
||||
new_element->set_attributes(move(attributes));
|
||||
node_stack.append(new_element);
|
||||
if (node_stack.size() != 1)
|
||||
node_stack[node_stack.size() - 2].append_child(new_element);
|
||||
|
||||
if (is_self_closing_tag(new_element->tag_name()))
|
||||
close_tag();
|
||||
};
|
||||
|
||||
auto commit_tag = [&] {
|
||||
if (is_slash_tag)
|
||||
close_tag();
|
||||
else
|
||||
open_tag();
|
||||
};
|
||||
|
||||
auto commit_attribute = [&] {
|
||||
attributes.append({ String::copy(attribute_name_buffer), String::copy(attribute_value_buffer) });
|
||||
};
|
||||
|
||||
for (int i = 0; i < html.length(); ++i) {
|
||||
char ch = html[i];
|
||||
switch (state) {
|
||||
case State::Free:
|
||||
if (ch == '<') {
|
||||
is_slash_tag = false;
|
||||
move_to_state(State::BeforeTagName);
|
||||
break;
|
||||
}
|
||||
text_buffer.append(ch);
|
||||
break;
|
||||
case State::BeforeTagName:
|
||||
if (ch == '/') {
|
||||
is_slash_tag = true;
|
||||
break;
|
||||
}
|
||||
if (ch == '>') {
|
||||
move_to_state(State::Free);
|
||||
break;
|
||||
}
|
||||
if (!isalpha(ch))
|
||||
break;
|
||||
move_to_state(State::InTagName);
|
||||
[[fallthrough]];
|
||||
case State::InTagName:
|
||||
if (isspace(ch)) {
|
||||
move_to_state(State::InAttributeList);
|
||||
break;
|
||||
}
|
||||
if (ch == '>') {
|
||||
commit_tag();
|
||||
move_to_state(State::Free);
|
||||
break;
|
||||
}
|
||||
tag_name_buffer.append(ch);
|
||||
break;
|
||||
case State::InAttributeList:
|
||||
if (ch == '>') {
|
||||
commit_tag();
|
||||
move_to_state(State::Free);
|
||||
break;
|
||||
}
|
||||
if (!isalpha(ch))
|
||||
break;
|
||||
move_to_state(State::InAttributeName);
|
||||
[[fallthrough]];
|
||||
case State::InAttributeName:
|
||||
if (is_valid_in_attribute_name(ch)) {
|
||||
attribute_name_buffer.append(ch);
|
||||
break;
|
||||
}
|
||||
if (isspace(ch)) {
|
||||
commit_attribute();
|
||||
break;
|
||||
}
|
||||
|
||||
if (ch == '>') {
|
||||
commit_tag();
|
||||
move_to_state(State::Free);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ch == '=') {
|
||||
move_to_state(State::BeforeAttributeValue);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case State::BeforeAttributeValue:
|
||||
if (ch == '\'') {
|
||||
move_to_state(State::InAttributeValueSingleQuote);
|
||||
break;
|
||||
}
|
||||
if (ch == '"') {
|
||||
move_to_state(State::InAttributeValueDoubleQuote);
|
||||
break;
|
||||
}
|
||||
if (ch == '>') {
|
||||
commit_tag();
|
||||
move_to_state(State::Free);
|
||||
break;
|
||||
}
|
||||
if (isspace(ch)) {
|
||||
commit_attribute();
|
||||
move_to_state(State::InAttributeList);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case State::InAttributeValueSingleQuote:
|
||||
if (ch == '\'') {
|
||||
commit_attribute();
|
||||
move_to_state(State::InAttributeList);
|
||||
break;
|
||||
}
|
||||
attribute_value_buffer.append(ch);
|
||||
break;
|
||||
case State::InAttributeValueDoubleQuote:
|
||||
if (ch == '"') {
|
||||
commit_attribute();
|
||||
move_to_state(State::InAttributeList);
|
||||
break;
|
||||
}
|
||||
attribute_value_buffer.append(ch);
|
||||
break;
|
||||
case State::InAttributeValueNoQuote:
|
||||
if (isspace(ch)) {
|
||||
commit_attribute();
|
||||
move_to_state(State::InAttributeList);
|
||||
break;
|
||||
}
|
||||
if (ch == '>') {
|
||||
commit_tag();
|
||||
move_to_state(State::Free);
|
||||
break;
|
||||
}
|
||||
attribute_value_buffer.append(ch);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Unhandled state %d\n", (int)state);
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
return doc;
|
||||
}
|
7
Libraries/LibHTML/Parser/HTMLParser.h
Normal file
7
Libraries/LibHTML/Parser/HTMLParser.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <LibHTML/DOM/Document.h>
|
||||
|
||||
NonnullRefPtr<Document> parse_html(const String&);
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue