Commit Graph

135 Commits

Author SHA1 Message Date
Andreas Kling
475c6d7112 LibHTML: Use ResourceLoader in HTMLLinkElement
This makes loading external stylesheets from http:// URLs work. :^)
2019-10-09 10:39:28 +02:00
Andreas Kling
a1c8c754eb LibHTML: Fire the file:// load completion callback asynchronously
This makes it consistent with how http:// callbacks are fired.
It would probably be fine to have file:// be synchronous, but at the
same time it's nice to have consistency.
2019-10-08 19:40:48 +02:00
Andreas Kling
3be6d1aff0 LibHTML: Add ResourceLoader to support protocol-agnostic URL loading
We now support loading both file:// and http:// URLs. Feel free to
visit http://www.serenityos.org/ and enjoy the fancy good times. :^)
2019-10-08 19:37:15 +02:00
Andreas Kling
31ac19543a LibHTML: Use an enum for CSS property ID's
Instead of using string everywhere, have the CSS parser produce enum
values, since they are a lot nicer to work with.

In the future we should generate most of this code based on a list of
supported CSS properties.
2019-10-08 15:35:05 +02:00
Andreas Kling
19dbfc3153 LibHTML: Move selector matching into a SelectorEngine namespace 2019-10-08 15:35:05 +02:00
Andreas Kling
6287c2b270 LibHTML: Don't crash when calling set_document(nullptr) 2019-10-07 19:33:06 +02:00
Andreas Kling
edbf09ea29 LibHTML: Make the CSS and HTML parsers take StringViews
This allows us to avoid unnecessary making unnecessary String copies of
all the source text.
2019-10-07 19:11:33 +02:00
Andreas Kling
71e8ddcd1c LibHTML: Start adding support for <link rel="stylesheet">
This patch adds basic support for external stylesheets. It currently
only works with file:// URLs.

We do a synchronous full relayout after loading a stylesheet, which is
definitely on the aggressive side, but it gives us something to work
on improving. :^)
2019-10-07 19:06:47 +02:00
Andreas Kling
749e3f0f30 LibHTML: Add LayoutNodeWithStyle class, make LayoutText style-less
Since LayoutText always inherits style, it shouldn't store any style of
its own. This patch adds a LayoutNodeWithStyle class to sit between
LayoutNode and everyone who wants to inherit from LayoutNode except
LayoutText :^)

Since LayoutText can never have children, we also know that the parent
of any LayoutNode is always going to be a LayoutNodeWithStyle.
So this patch makes LayoutNode::parent() return LayoutNodeWithStyle*.
2019-10-07 10:56:44 +02:00
Andreas Kling
15f3e64862 LibHTML: Rename "style_properties" to "style" everywhere 2019-10-07 10:56:44 +02:00
Andreas Kling
66caa7af2b LibHTML: Optionally pass document URL to the HTML parser
This makes the document URL available to all the parse_attributes()
callbacks, in case they need it for anything.
2019-10-06 21:13:24 +02:00
Andreas Kling
a239ef34d5 LibHTML: Add Node::first_ancestor_of_type<T>()
Here's another helper for finding the first ancestor of a Node of a
specific type.
2019-10-06 21:07:36 +02:00
Andreas Kling
3bee9d3d3c LibHTML: Templatize Node::first_child_of_type<T>()
This is a lot nicer than first_child_with_tag_name(...).

The is<T>(Node) functions are obviously unoptimized at the moment,
and this is about establishing pleasant patterns right now. :^)
2019-10-06 20:47:57 +02:00
Andreas Kling
f52f2736e1 LibHTML: Add is<ElementType> and to<ElementType> helper functions
These will help us write node-type-aware template functions.
2019-10-06 20:38:26 +02:00
Andreas Kling
bedb00603c LibHTML: Add adjacent (+) and general (~) sibling combinators
This patch implements two more selector features:

- "div + p" matches the <p> sibling immediately after a <div>.
- "div ~ p" matches all <p> siblings after a <div>.
2019-10-06 19:59:07 +02:00
Andreas Kling
5a6c36dc91 LibHTML: Add Node::{next,previous}_element_sibling()
These helpers return the next/previous sibling Node that's actually an
element. This will be useful in the CSS engine since CSS doesn't care
about text nodes.
2019-10-06 19:54:50 +02:00
Andreas Kling
cad326f323 LibHTML: Implement immediate-child selectors (#foo > #bar) 2019-10-06 15:41:16 +02:00
Andreas Kling
156b35742a LibHTML: Implement matching for descendant selectors
The CSS engine now correctly matches selectors like "#foo #bar #baz".
2019-10-06 15:34:42 +02:00
Andreas Kling
b587eb2f4d LibHTML: Fall back to the system default font if we can't find a font
Instead of crashing when we don't have some font the CSS wanted,
just go with Font::default_font().
2019-10-06 14:57:49 +02:00
Andreas Kling
769f86ff9c LibHTML: Add a little bit of margin around <hr> elements 2019-10-06 14:44:18 +02:00
Conrad Pankoff
2cf5d69a67 LibHTML: Use current style's text colour for alt text on images 2019-10-06 14:33:00 +02:00
Conrad Pankoff
451730000c LibHTML: Trim image alt text to the right if necessary 2019-10-06 14:33:00 +02:00
Conrad Pankoff
b240500107 LibHTML: Support width/height attributes on images 2019-10-06 14:33:00 +02:00
Conrad Pankoff
088237a25f LibHTML: Draw image alt text as black rather than white 2019-10-06 14:33:00 +02:00
Conrad Pankoff
5f6903a714 LibHTML: Use image URL as alt text if alt is missing 2019-10-06 14:33:00 +02:00
Conrad Pankoff
1ef53be2f3 LibHTML: Load image resource when src attribute is set 2019-10-06 14:33:00 +02:00
Conrad Pankoff
ef8b754a46 LibHTML: Add function for invalidating the document layout
This allows any external actor to signal that the document layout may be
stale. This can be used when loading resources, changing the size or
placement of an element, adding/removing nodes, or really any time.
2019-10-06 14:33:00 +02:00
Andreas Kling
fabc717b39 LibHTML: Make the text-decoration inheritance quirk actually work
Things work better if you spell things correctly, oops!

This fixes an issue in Help where parts of links were missing their
underline decoration.
2019-10-06 11:48:53 +02:00
Andreas Kling
ef0613ea4c LibHTML: Unbreak parsing of standalone declarations
Oops, I forgot to update CSSParser::parse_standalone_declaration()
after making parse_property() return a property (or {} for failure.)
2019-10-06 11:43:38 +02:00
Andreas Kling
b9557bf876 LibHTML: Move font loading from LayoutText to StyleProperties
Since LayoutText inherits all of its style information from its parent
Element anyway, it makes more sense to load the font at a higher level.

And since the font depends only on the style and nothing else, this
patch moves font loading (and caching) into StyleProperties. This could
be made a lot smarter to avoid loading the same font many times, etc.
2019-10-06 11:26:34 +02:00
Andreas Kling
c8e5039418 LibHTML: LayoutInline can only be constructed with an Element
Since LayoutText no longer inherits from LayoutInline, all of the
DOM nodes with a LayoutInline are going to be Elements.
2019-10-06 11:26:34 +02:00
Andreas Kling
1b7aa00768 LibHTML: LayoutText should inherit from LayoutNode directly
There's no need for LayoutText to inherit from LayoutInline.
I had the wrong idea here: I was thinking that everything that can be
laid out inline should inherit from LayoutInline, but that's clearly
not sufficient for something like LayoutReplaced which can be laid out
in either way.
2019-10-06 11:26:31 +02:00
Andreas Kling
2a266db05b LibHTML: Parse descendant relations in CSS selectors
"div p" now generates a Selector with two components where the second
component is a type=TagName, value="p", relation=Descendant.

We still don't handle matching of these, but at least we parse them.
2019-10-06 11:07:57 +02:00
Andreas Kling
847072c2b1 LibHTML: Respect the link color set via <body link>
The default style for "a" tags now has { color: -libhtml-link; }.
We implement this vendor-specific property by querying the containing
document for the appropriate link color.

Currently we only use the basic link color, but in the future this can
be extended to remember visited links, etc.
2019-10-06 10:25:08 +02:00
Andreas Kling
83a6474d82 LibHTML: Parse link, alink and vlink in <body> and pass to Document
This patch adds HTMLBodyElement::parse_attribute() where we extract the
link colors and stash them away on Document.
2019-10-06 10:11:54 +02:00
Andreas Kling
772718b8d8 LibHTML: Add a per-attribute callback for Elements to parse attributes
Elements now have a virtual parse_attribute(name, value) callback that
gets invoked for each one of its attributes.
2019-10-06 10:09:55 +02:00
Andreas Kling
306b9dfb51 LibHTML: Various little improvements to the CSS parser
The parser now kinda recognizes immediate child selectors, !important,
and various other little things.

It's still extremely far from robust and/or correct. :^)
2019-10-06 09:28:58 +02:00
Andreas Kling
48f43a7429 LibHTML: Anonymous blocks *should* inherit some properties
Okay, I got that a bit wrong. Here's what CSS 2.1 says:

"The properties of anonymous boxes are inherited from the enclosing
non-anonymous box. Non-inherited properties have their initial value."

This patch implements a better behavior by only copying the inherited
properties from the parent style.
2019-10-05 23:47:06 +02:00
Andreas Kling
958b395418 LibHTML: Support rendering <img src> with file:// URLs
We can now show images loaded from local file:// URLs. Pretty neat :^)
2019-10-05 23:41:14 +02:00
Andreas Kling
9858e2632c LibHTML: Let's not have anonymous blocks inherit their parent's style
This was introducing a bunch of unwanted margins.
2019-10-05 23:29:01 +02:00
Andreas Kling
ee567cdc3d LibHTML: Implement basic layout for inline <img alt>
LayoutReplaced objects can now participate in inline layout.

It's very hackish, but basically LayoutReplaced will just add itself to
the last line in the containing block.

This patch gets rid of the idea that only LayoutInline subclasses can
be split into lines, by moving the split_into_lines() virtual up to
LayoutNode and overriding it in LayoutReplaced.
2019-10-05 23:29:01 +02:00
Andreas Kling
f150134de9 LibHTML: Make Node::create_layout_node() virtual
Instead of branching on the Node type, let subclasses decide how their
layout nodes get constructed.

This will allow elements to create custom layout nodes if they want.
2019-10-05 23:29:01 +02:00
Andreas Kling
09dccb3224 LibHTML: Flesh out <img> element with LayoutImage and LayoutReplaced
This patch adds parsing of <img> into HTMLImageElement objects.
It also adds LayoutImage and its parent class LayoutReplaced, which is
going to represent CSS "replaced elements."
2019-10-05 23:29:01 +02:00
Andreas Kling
56d1ae98bc LibHTML: Always fill the entire paint event rect with background
And do the filling before translating the painter.

This fixes a bug where scrolling down in the Help app would render the
bottom part of the page with a different background color.
2019-10-05 21:53:09 +02:00
Andreas Kling
a8297657ab LibHTML: Fire the HtmlView::on_title_change hook after a load finishes 2019-10-05 10:30:14 +02:00
Andreas Kling
319f0d2d18 LibHTML: Render list markers in the same color as text 2019-10-05 10:27:59 +02:00
Andreas Kling
78d65c1c64 LibHTML: Add load(URL) and reload() functions to HtmlView
You can now pass a file:/// URL to HtmlView and it will take care of
the loading logic for you. Each Document remembers the URL it was
loaded from, which allows us to also have reload().

This patch also adds a very simple function for resolving relative
URL's into absolute ones.
2019-10-05 10:17:33 +02:00
Andreas Kling
ee64d99a96 LibHTML: Make StyleResolver responsible for loading the default style
Instead of HtmlView clients having to worry about parsing and loading
the default CSS, just take care of it inside StyleResolver.

The default style is automatically inserted into the stylesheet list,
at the very start, so everyone else gets a chance to override it.
2019-10-05 09:02:29 +02:00
Andreas Kling
77218b1c2a LibHTML: Make CSS inheritance slightly less hacky
Add a simple is_inherited_property(name) lookup function and use that
to implement CSS property inheritance.

It's a bug that "text-decoration" is inherited, but we currently rely
on this in order to propagate e.g underline into linkified line boxes.
2019-10-04 22:52:49 +02:00
Andreas Kling
8fb979148d LibHTML: Add support for <font color>
This was pleasantly trivial to implement with the new support for
presentational hints. :^)
2019-10-04 21:14:59 +02:00