Commit Graph

281 Commits

Author SHA1 Message Date
Tom
75f61fe3d9 AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.

Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.

In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-11-10 19:11:52 +01:00
Brendan Coles
3482b9b937 LibWeb: Enforce Same-Origin Policy (SOP) for XMLHttpRequest requests
`DOM::XMLHttpRequest` now checks if the requested URL has the same
`Origin` as the requesting `Document`. If the requested URL is in
violation of SOP the request is rejected and an "error" `DOM::Event`
is dispatched.
2020-11-01 10:23:08 +01:00
Andreas Kling
46c15276e9 LibWeb: Fix Document construction mishap in <template> element
Ref-counted objects must not be stack allocated. Make DOM::Document's
constructor private to avoid this issue. (I wish we could mark classes
as heap-only..)
2020-10-23 08:33:16 +02:00
Andreas Kling
f79e28bd65 LibWeb: Break reference cycles so DOM::Document actually gets deleted
When a document reaches ref_count==0, we will now remove all of the
descendant nodes from the document, and also break all the explicit
links (such as the currently hovered element.)

Basically, DOM nodes will keep the document alive even after the
document reaches ref_count==0. This allows JS wrappers to stay alive
and keep the document alive as well. This matches the behavior of
at least some other browsers.

This patch also adds a bunch of sanity checking assertions around
DOM teardown, to help catch mistakes in the future.

Fixes #3771.
2020-10-22 23:41:32 +02:00
Andreas Kling
018b458962 LibWeb: Make sure nodes are adopted when moving between documents
Otherwise, the "referencing node count" accounting won't be accurate,
and anything that accesses the document will be confused.
2020-10-22 23:37:17 +02:00
Andreas Kling
385d744989 LibWeb: Use smart pointers between DOM and Layout tree
DOM::Node now points to its LayoutNode with a WeakPtr.
LayoutNode points to its DOM::Node and DOM::Document with RefPtrs.

Layout trees come and go in response to various events, so the DOM tree
already has to deal with that. The DOM should always live at least as
long as the layout tree, so this patch enforces that assumption by
making layout nodes keep their corresponding DOM objects alive.

This may not be optimal, but it removes a lot of ambiguous raw pointer
action which is not worth accomodating.
2020-10-22 20:26:32 +02:00
Andreas Kling
5fdc8c14a6 LibWeb: Make Nodes actually ref/unref their Document
Oops, it seems like I implemented all of the "nodes keep the document
alive" mechanism except the part where the functions are actually
called. :^)

Fixes #3811.
2020-10-22 18:48:01 +02:00
Luke
e8a9e8aed5 LibWeb: Add namespace to Element 2020-10-22 15:24:42 +02:00
Luke
efaf03e986 LibWeb: Use modern namespaces and fix clang-format comments in tag names 2020-10-22 15:24:42 +02:00
Andreas Kling
0af2795662 LibWeb: Tear down layout trees properly
Instead of just ripping out the root of the layout tree from its RefPtr
in Document, actually go through the DOM and gather up all the layout
nodes. Then destroy them all in one swoop.

Also, make sure to do this when detaching Document from Frame,
to enforce the invariant that layout only occurs in framed documents.
2020-10-20 18:08:37 +02:00
Andreas Kling
24162127ba LibWeb: Dispatch "load" on document and window
These happen right after "DOMContentLoaded" for now, which is incorrect
since they should really wait until subresources have loaded.
However, this makes a bunch of things work already so let's do it.
2020-10-18 13:45:28 +02:00
Andreas Kling
b92bc9c6e5 LibWeb: Make DOM::Window into an EventTarget
This will allow us to dispatch window events.
2020-10-18 13:43:44 +02:00
Linus Groh
ac98a48177 LibWeb: Fix EventDispatcher::dispatch()
We were never wrapping and using the actual DOM::Event but instead
wrapped the *target* twice and passed it to the event listener callback,
as this value and as argument.

This unbreaks "fun demo" and "canvas path quadratic curve test" - and
event dispatching in general, of course :^)

Fixes #3721.
2020-10-15 20:40:35 +02:00
Andreas Kling
f68ed6d25b LibWeb: Make DOM Nodes keep their Document alive
In addition to being reference-counted, all nodes that are part of a
document must also keep the document alive.

This is achieved by adding a second ref-count to the Document object
and incrementing/decrementing it whenever a node is created/destroyed
in that document.

This brings us much closer to a proper DOM lifetime model, although
the JS bindings still need more work.
2020-10-11 21:52:59 +02:00
Andreas Kling
99acbbe86b LibWeb: Remove unused Document::fixup()
This was some naive fixup mechanism we used before implementing a spec
compliant HTML parser.
2020-10-11 21:24:14 +02:00
Andreas Kling
51dbea3a0e LibWeb: Use RefPtrs more in getElementById() and getElementsByName()
Passing around Vector<Element*> is not a great idea long-term.
2020-10-07 12:47:17 +02:00
Andreas Kling
97d0acc5b6 LibWeb: Implement performance.now()
This patch introduces the HighResolutionTime namespace which is home to
the Performance object (exposed via window.performance)

performance.now() is currently the only function, and it returns the
number of milliseconds since the window object was constructed. :^)
2020-09-29 18:19:18 +02:00
Andreas Kling
8cb789d061 LibWeb: Remove a bunch of unnecessary <LibJS/Interpreter.h> includes 2020-09-29 17:04:16 +02:00
Andreas Kling
700cbc02ec LibWeb: Use JS::VM::call() in timer and RAF callback invocation
This removes assumptions about having an Interpreter, and also unbreaks
requestAnimationFrame which was asserting.
2020-09-27 20:31:13 +02:00
Andreas Kling
2bc5bc64fb LibJS: Remove a whole bunch of includes of <LibJS/Interpreter.h> 2020-09-27 20:26:58 +02:00
Andreas Kling
861815596f LibWeb: Bypass the JS::Interpreter when invoking JS event callbacks
Use VM::call() instead of Interpreter::call().
2020-09-27 20:26:58 +02:00
Ben Wiederhake
08f9bc26a6 Meta+LibHTTP through LibWeb: Make clang-format-10 clean 2020-09-25 21:18:17 +02:00
Andreas Kling
4a8bfcdd1c LibJS: Move the current exception from Interpreter to VM
This will allow us to throw exceptions even when there is no active
interpreter in the VM.
2020-09-22 20:10:20 +02:00
Andreas Kling
1c43442be4 LibJS+Clients: Add JS::VM object, separate Heap from Interpreter
Taking a big step towards a world of multiple global object, this patch
adds a new JS::VM object that houses the JS::Heap.

This means that the Heap moves out of Interpreter, and the same Heap
can now be used by multiple Interpreters, and can also outlive them.

The VM keeps a stack of Interpreter pointers. We push/pop on this
stack when entering/exiting execution with a given Interpreter.
This allows us to make this change without disturbing too much of
the existing code.

There is still a 1-to-1 relationship between Interpreter and the
global object. This will change in the future.

Ultimately, the goal here is to make Interpreter a transient object
that only needs to exist while you execute some code. Getting there
will take a lot more work though. :^)

Note that in LibWeb, the global JS::VM is called main_thread_vm(),
to distinguish it from future worker VM's.
2020-09-20 19:24:44 +02:00
Andreas Kling
c6ae0c41d9 LibWeb: Add Bindings::ScriptExecutionContext
This will be inherited by documents and workers, to provide a common
abstraction for script execution. (We don't have workers yet, but we
might as well make this little space for them now to simplify things
down the road.)
2020-09-20 19:22:44 +02:00
Andreas Kling
d9e39cb82d LibWeb: Support window.alert() in multi-process context
Alerts are now delegated to the embedding GUI process.
2020-09-12 14:49:29 +02:00
Andreas Kling
8a6a9a8fb6 LibWeb: Move DOM event dispatch to its own class
For now, the new DOM::EventDispatcher is very simple, it just iterates
over the set of listeners on an EventTarget and invokes the callbacks
as it goes.

This simplifies EventTarget subclasses since they no longer have to
implement the callback mechanism themselves.
2020-09-06 14:48:14 +02:00
Luke
124c52b3b5 LibWeb: Implement document ready state 2020-08-31 23:05:51 +02:00
AnotherTest
394e4c04cd LibJS: Add a helper for calling JS::Function's with arguments
The fact that a `MarkedValueList` had to be created was just annoying,
so here's an alternative.
This patchset also removes some (now) unneeded MarkedValueList.h includes.
2020-08-26 08:45:01 +02:00
Luke
7902d215b3 LibWeb: Implement <template> parsing
Note that there is currently no way to display them as we can't
currently clone nodes.

Adds special case for templates for dumping to console.
Doesn't add it to the DOM inspector as I'm not sure how to do it.
2020-08-21 11:57:11 +02:00
Nico Weber
2460980d2c LibWeb: Implement Element.innerText
Reading the property has a few warts (see FIXMEs in the included
tests), but with this the timestamps on http://45.33.8.238/
get localized :^)

Since the Date() constructor currently ignores all arguments,
they don't get localized correctly but are all set to the current
time, but hey, it's still progress from a certain point of view.
2020-08-18 16:58:46 +02:00
Nico Weber
2f85af2a20 LibWeb: Simplify Node::text_content() 2020-08-18 16:58:46 +02:00
Luke
c2a2552e46 LibWeb: Add more document tests, add comment, text and mixin tests
Also adds a TypeScript definition file for the test runner object.
2020-08-17 22:57:05 +02:00
Luke
8b807e65d7 LibWeb: Add Comment and DocumentFragment bindings, move querySelector...
...{All} to ParentNode. Exposes createDocumentFragment and
createComment on Document. Stubs out the document.body setter. 

Also adds ParentNode back :^).
2020-08-17 22:57:05 +02:00
Nico Weber
e9b56b5b9c LibWeb: Add Node.textContent
This requires moving remove_all_children() from ParentNode to
Node, which makes ParentNode.cpp empty, so remove it.

It also co-opts the existing Node::text_content() method and
tweaks it slightly to fit the semantics of Node.textContent.
2020-08-17 21:23:11 +02:00
Andreas Kling
56c3748dcc LibWeb: Rename PageView => InProcessWebView 2020-08-17 18:05:35 +02:00
Andreas Kling
01022eb5d6 LibWeb: Allow focusing individual (focusable) elements with Tab key
You can now cycle through focusable elements (currently only hyperlinks
are focusable) with the Tab key.

The focus outline is rendered in a new FocusOutline paint phase.
2020-08-15 00:05:45 +02:00
Andreas Kling
7698feb8ce LibWeb: Add NonDocumentTypeChildNode::next_element_in_pre_order()
This is handy for traversing only the elements in a document.
2020-08-15 00:05:45 +02:00
Andreas Kling
bbe2d4a2d9 LibJS+LibWeb: Clear exceptions after call'ing JavaScript functions
Decorated Interpreter::call() with [[nodiscard]] to provoke thinking
about the returned value at each call site. This is definitely not
perfect and we should really start thinking about slimming down the
public-facing LibJS interpreter API.

Fixes #3136.
2020-08-14 17:31:07 +02:00
Linus Groh
7390098adc LibWeb: Fix #include <LibWeb/{DOM => HTML}/AttributeNames.h>
This file has been moved from DOM/ to HTML/ in
a784090b91.
2020-08-12 15:37:42 +02:00
Andreas Kling
a784090b91 LibWeb: Move HTML::AttributeNames file into HTML/ directory 2020-08-12 13:22:59 +02:00
Andreas Kling
40f4ccc3ea LibWeb: Initialize tag/attribute name globals in init-time constructors 2020-08-12 11:27:44 +02:00
Linus Groh
1d728af5c4 LibWeb: Clear exceptions in each Document::run_javascript() call
We don't want to carry over exceptions across multiple
Document::run_javascript() calls as Interpreter::run() and every of its
exception checks will get confused - in this case there would be an
exception, but not because a certain action failed.

Real-life example:

<script>var a = {}; a.test()</script>
<script>alert("It worked!")</script>

The above HTML will invoke Document::run_javascript() twice, the first
call will result in a TypeError, which is still stored during the second
call. The interpreter will eventually call the following functions (in
order) for the alert() invocation:

- Identifier::execute()
- Interpreter::get_variable()
- Object::get() (on the global object)

That last Object::get() call has an exception check which is triggered
as we still carry around the exception from earlier - and eventually
returns an empty value.

Long story short, the second script will wrongly fail with
"ReferenceError, 'alert' is not defined".

Fixes #3091.
2020-08-11 21:08:30 +02:00
Andreas Kling
eaf7e68408 LibWeb: Move tree iteration helpers from Node/LayoutNode to TreeNode
Since these are generally useful in our trees, let's just keep them
in TreeNode instead of duplicating the helpers in subclasses.
2020-08-10 15:21:23 +02:00
Luke
5724ac8e72 LibWeb: Add HTML elements to factories, add missing tags and attributes
This is mostly to get the grunt work of the way. This is split up into
multiple commits to hopefully make it more manageable to review.

Note that these are not full implementations, and the bindings mostly
get the low hanging fruit.

Also implements some attributes that I kept out because they had
dashes in them. Therefore, this closes #2905.
2020-08-09 21:14:51 +02:00
Luke
567845c480 LibWeb: Make sure that head and body always get the HTML element
Now that document element returns a generic DOM element, we need to
make sure head and body get a html element. 

The spec just says to check if the document element is a html element,
so let's do that.
2020-08-04 11:05:14 +02:00
Andreas Kling
73645e11c4 LibWeb: Add CharacterData and Text IDL interfaces 2020-08-03 20:50:45 +02:00
Andreas Kling
2dc08d259e LibWeb: Add Element.{next,previous}ElementSibling IDL attributes 2020-08-03 20:32:06 +02:00
Andreas Kling
d367044e67 LibWeb: Move "element sibling" getters to NonDocumentTypeChildNode
Here's another CRTP mixin since that's the best we can do with C++.
This prepares exposing these via IDL on Element and CharacterData.
2020-08-03 20:30:02 +02:00
Andreas Kling
e27726dc92 LibWeb: Add the Document.documentElement API
Also change DOM::Document::document_element() to return an Element*
and not an HTML::HTMLHtmlElement since that's not the only kind of
documentElement we might encounter.
2020-08-03 13:30:18 +02:00