Commit graph

88 commits

Author SHA1 Message Date
Sam Atkins
ffc81cbfad LibWeb: Use Selectors instead of a String for :not() selectors
Rather than parsing the selector every time we want to check it, we
now parse it once at the beginning.

A bonus effect of this is that we now support a selector list in
:not(), instead of just a single selector, though only when using
the new parser.
2021-07-14 13:31:00 +02:00
Sam Atkins
776b1f4548 LibWeb: Make CSS::Selector reference counted
The end goal is to make the PseudoClass::not_selector be a Selector
instead of a String that is repeatedly re-parsed. But since Selector
contains a Vector of ComplexSelectors, which each have a Vector of
SimpleSelectors, it's probably a good idea to not be passing them
around by value anyway. :^)
2021-07-14 13:31:00 +02:00
Sam Atkins
8cae79cc8d LibWeb: Add 'PseudoElement' as a CSS SimpleSelector::Type
Same reasoning again! This is the last one.

While I was at it, I added the two remaining CSS2.2 pseudo-elements,
::first-line and ::first-letter. All 4 are handled in the new CSS
parser, including with the compatibility single-colon syntax. I have
not added support to the old parser.
2021-07-14 13:31:00 +02:00
Sam Atkins
4af7d41879 LibWeb: Add 'PseudoClass' as a CSS SimpleSelector::Type
Same reasoning as the previous commit.
2021-07-14 13:31:00 +02:00
Sam Atkins
96b2356cbb LibWeb: Add 'Attribute' as a CSS SimpleSelector::Type
Previously, SimpleSelectors optionally had Attribute-selector data
as well as their main type. Now, they're either one or the other,
which better matches the spec, and makes parsing and matching more
straightforward.
2021-07-14 13:31:00 +02:00
Sam Atkins
86994336a7 LibWeb: Correct parsing invalid list of declarations
We were only discarding at most one token when a declaration is
invalid, when we should discard all until we see a ; or EOF.
2021-07-11 23:19:56 +02:00
Sam Atkins
c249fbd17c LibWeb: Correct escape handling in CSS Tokenizer
Calling is_valid_escape_sequence() with no arguments hides what it
is operating on, so I have removed that, so that you must explicitly
tell it what you are testing.

The call from consume_a_token() was using the wrong tokens, so it
returned false incorrectly. This was resulting in corrupted output
when faced with this code from Acid2. (Abbreviated)

```css
.parser { error: \}; }
.parser { }
```
2021-07-11 23:19:56 +02:00
Sam Atkins
e381ca258f LibWeb: Add more logging to CSS parser 2021-07-11 23:19:56 +02:00
Sam Atkins
9cfbc07c24 LibWeb: Implement CSS::Parse::parse_nth_child_pattern()
This is a modified copy of the code from Selector.cpp, to work on a
TokenStream instead of a String.
2021-07-11 23:19:56 +02:00
Sam Atkins
cf333574ac LibWeb: Convert StyleFunctionRule.m_values to ComponentValues
The input is ComponentValues, and the output is too, so storing as
a String in the middle was inefficient and unnecessary.
2021-07-11 23:19:56 +02:00
Sam Atkins
fabc09a593 LibWeb: Increase clarity with CSS token debug logging
Had to adjust some places that were using Token.to_string() for
non-debug-logging purposes. Changed its name to to_debug_string()
to make the usage clearer.
2021-07-11 23:19:56 +02:00
Sam Atkins
e5ac5e1fab LibWeb: Fix CSS attribute and ID selector parsing
There were several crashes here from out-of-bounds memory access.
2021-07-11 23:19:56 +02:00
Sam Atkins
78d191554a LibWeb: Remove non-compliant whitespace stripping in CSS Parser 2021-07-11 23:19:56 +02:00
Sam Atkins
7e4f75c78c LibWeb: Fix whitespace handling in CSS selectors
Whitespace marks the end of a compound-selector, no matter where
it occurs. `check_for_eof_or_whitespace()` reconsumes the whitespace
token for convenience.
2021-07-11 23:19:56 +02:00
Sam Atkins
57f6d86996 LibWeb: Implement remaining CSS parse_as...() entry points 2021-07-11 23:19:56 +02:00
Sam Atkins
65fe3895e6 LibWeb: Enabling @import in new CSS parser 2021-07-11 23:19:56 +02:00
Sam Atkins
6b6bf4a0aa LibWeb: Implement CSS::parse_css_value()
A lot of this is not spec-compliant and copied from the old parser.
In future PRs, we can revise it.
2021-07-11 23:19:56 +02:00
Sam Atkins
c91c00f587 LibWeb: Remove unnecessary CSS:: namespace from CSS Parser code
It's feeling unwieldy having it everywhere.
2021-07-11 23:19:56 +02:00
Sam Atkins
28e8bb9b1d LibWeb: Convert style declarations and at rules into CSSRules 2021-07-11 23:19:56 +02:00
Sam Atkins
2c84379846 LibWeb: Add accessors to CSS::DeclarationOrAtRule 2021-07-11 23:19:56 +02:00
Sam Atkins
6d14791c40 LibWeb: Use TokenStream in CSS Parser
Each method can either be called with a TokenStream, or with no
arguments to replicate the previous behaviour.
2021-07-11 23:19:56 +02:00
Sam Atkins
b7116711bf LibWeb: Add TokenStream class to CSS Parser
The entry points for CSS parsing in the spec are defined as accepting
any of a stream of Tokens, or a stream of ComponentValues, or a String.
TokenStream is an attempt to reduce the duplication of code for that.
2021-07-11 23:19:56 +02:00
Sam Atkins
6c03123b2d LibWeb: Give CSS Token and StyleComponentValueRule matching is() funcs
The end goal here is to make the two classes mostly interchangeable, as
the CSS spec requires that the various parser algorithms can take a
stream of either class, and we want to have that functionality without
needing to duplicate all of the code.
2021-07-11 23:19:56 +02:00
Sam Atkins
9c14504bbb LibWeb: Rename CSS::Token::TokenType -> Type 2021-07-11 23:19:56 +02:00
Sam Atkins
8671d79ba4 LibWeb: Add direct StyleComponentValueRule constructors
Rather than passing a ComponentType, and then manually modifying the
data fields, we now create them initialized.

The constructor that takes a Token is intentionally left implicit,
so that we can automatically convert when using the TokenStream later.
2021-07-11 23:19:56 +02:00
Sam Atkins
82d12b170a LibWeb: Merge CSS Parser's QualifiedStyleRule and AtStyleRule
AtStyleRule being a subclass of QualifiedStyleRule was causing
problems when trying to distinguish between them. Combining them
and then distinguishing between them with a Type enum makes that
check simpler, and is in line with how similar checks are done
elsewhere in the parser.
2021-07-11 23:19:56 +02:00
Sam Atkins
a6085e19ae LibWeb: Implement CSS selector parsing entry points
They're still using the same parsing code, so there's a lot of room
for improvement, but it's good for now.
2021-07-11 23:19:56 +02:00
Sam Atkins
004ae453d1 LibWeb: Add context to new CSS parser, and deprecate the old one
The new one is the same as the old one, just in the new Parser's
source files. This isn't the most elegant solution but it seemed
like the best option. And it's all temporary, after all.
2021-07-11 23:19:56 +02:00
Sam Atkins
390cc30a97 LibWeb: Bring CSS::Parser::parse_as_comma_separated_list...() to spec
Previous implementation was returning everything in a single Vector,
when what we really want is a Vector of Vectors, one for each comma-
separated part of the list.
2021-07-11 23:19:56 +02:00
Sam Atkins
985ed47a38 LibWeb: Use EOF code point instead of Optional in CSS Tokenizer
Optional seems like a good idea, but in many places we were not
checking if it had a value, which was causing crashes when the
Tokenizer was given malformed input. Using an EOF value along with
is_eof() makes things a lot simpler.
2021-07-11 23:19:56 +02:00
Sam Atkins
caff7fff94 LibWeb: Convert CSS::Parser methods to return desired types
This is very much stubbed out for now. Most notably is
Parser::convert_rule() where most of the conversion will happen
from the parser's internal rule classes to CSSRule and its children.
2021-07-11 23:19:56 +02:00
Sam Atkins
f9ffa34622 LibWeb: Remove broken CSS:Parser::is_combinator()
A single DELIM token is only one character long, so the check for
a "||" DELIM didn't work. We now just do the check inline.
2021-07-11 23:19:56 +02:00
Sam Atkins
9115c23bd5 LibWeb: Fix greedy CSS Tokenizer whitespace parsing
Whitespace parsing was too greedy, consuming the first non-
whitespace character after it.
2021-07-11 23:19:56 +02:00
Sam Atkins
06fc099310 LibWeb: Fix Off-by-one error in CSS::Parser::next_token() 2021-07-11 23:19:56 +02:00
Sam Atkins
06cd418770 LibWeb: Convert QualifiedStyleRule to a RefPtr type in new Parser 2021-07-11 23:19:56 +02:00
Sam Atkins
f690259a42 LibWeb: Add new CSS attribute match types to new parser 2021-07-11 23:19:56 +02:00
Sam Atkins
7fefe34797 LibWeb: Add remaining CSS AttributeMatchTypes
This adds:
- ContainsString     [att*=val]
- StartsWithSegment  [att|=val]
- StartsWithString   [att^=val]
- EndsWithString     [att$=val]

Renamed AttributeMatchType::Contains to ::ContainsWord for clarity.
2021-07-11 23:19:56 +02:00
Sam Atkins
29d78bba4b LibWeb: Use StyleComponentValueRules for StyleBlockRule's values
Noticed while doing this that attribute selectors have two different
ways of saying "starts with", and so AttributeMatchType::StartsWith
needs a better name. But I'll change that when I add the missing
types.

These class names are a mouthful to fit in a commit message. :^)
2021-07-11 23:19:56 +02:00
Sam Atkins
89bfde29dc LibWeb: Convert some CSS parser *Rule classes to using pointers
Previously these were all passed around by value, but some of them
(StyleComponentValueRule and StyleBlockRule) want to include each
other as fields, so this had to change.
2021-07-11 23:19:56 +02:00
Sam Atkins
a558916e1f LibWeb: Make CSS selector parsing use StyleComponentValueRules
Also added some pseudo-classes that were handled in the deprecated
parser:
- :disabled
- :enabled
- :checked
- :nth-child
- :nth-last-child
- :not
2021-07-11 23:19:56 +02:00
Sam Atkins
f7c79de0c5 LibWeb: Add convenience methods to CSS::Parser::Token
Some of these will be removed later, when we move to using is()
exclusively.
2021-07-11 23:19:56 +02:00
Sam Atkins
d6b4022b58 LibWeb: Make CSS::QualifiedStyleRule's prelude StyleComponentValueRule 2021-07-11 23:19:56 +02:00
Max Wipfli
9cc35d1ba3 AK: Implement String::find_any_of() and StringView::find_any_of()
This implements StringUtils::find_any_of() and uses it in
String::find_any_of() and StringView::find_any_of(). All uses of
find_{first,last}_of have been replaced with find_any_of(), find() or
find_last(). find_{first,last}_of have subsequently been removed.
2021-07-02 21:54:21 +02:00
Paul Irwin
5eb65286b6 LibWeb: Support :active pseudo-class for hyperlinks, :focus possibly
Adds support for the :active pseudo-class for hyperlinks (<a> tags
only).

Also, since it was very similar to :focus and an element having a
focused state was already implemented, I went ahead and implemented
that pseudo-class too, although I cannot come up with a working
example to validate it.
2021-06-25 01:02:29 +02:00
Tobias Christiansen
6d3361f077 LibWeb: Fix logic issue when parsing CSS custom properties
With best wishes from De Morgan, this is the correct way to check
whether the string isn't of the form "var(...)".
2021-06-09 21:50:22 +02:00
Tobias Christiansen
af4d80af4d LibWeb: Add parsing for NumericStyleValue
This StyleValue can hold an arbitrary float value.
2021-06-06 01:46:06 +04:30
Max Wipfli
bc8d16ad28 Everywhere: Replace ctype.h to avoid narrowing conversions
This replaces ctype.h with CharacterType.h everywhere I could find
issues with narrowing conversions. While using it will probably make
sense almost everywhere in the future, the most critical places should
have been addressed.
2021-06-03 13:31:46 +02:00
Andreas Kling
12a42edd13 Everywhere: codepoint => code point 2021-06-01 10:01:11 +02:00
Andreas Kling
407d6cd9e4 AK: Rename Utf8CodepointIterator => Utf8CodePointIterator 2021-06-01 09:45:52 +02:00
Tobias Christiansen
3ede1d08f5 LibWeb: Avoid unnecessary String copy in parsing CSS custom properties 2021-05-29 00:58:07 +04:30