Skip to content

Commit 2d7effd

Browse files
committed
Implement Console grouping APIs.
1 parent e0e976a commit 2d7effd

File tree

4 files changed

+76
-59
lines changed

4 files changed

+76
-59
lines changed

components/script/dom/console.rs

Lines changed: 46 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -52,88 +52,83 @@ where
5252
f()
5353
}
5454

55+
fn console_messages(global: &GlobalScope, messages: &[DOMString], level: LogLevel) {
56+
console_message(global, DOMString::from(messages.join(" ")), level)
57+
}
58+
59+
fn console_message(global: &GlobalScope, message: DOMString, level: LogLevel) {
60+
with_stderr_lock(move || {
61+
let prefix = global.current_group_label().unwrap_or_default();
62+
let message = DOMString::from(format!("{}{}", prefix, message));
63+
println!("{}", message);
64+
Console::send_to_devtools(global, level, message);
65+
})
66+
}
67+
5568
#[allow(non_snake_case)]
5669
impl Console {
5770
// https://developer.mozilla.org/en-US/docs/Web/API/Console/log
5871
pub fn Log(global: &GlobalScope, messages: Vec<DOMString>) {
59-
with_stderr_lock(move || {
60-
for message in messages {
61-
println!("{}", message);
62-
Self::send_to_devtools(global, LogLevel::Log, message);
63-
}
64-
})
72+
console_messages(global, &messages, LogLevel::Log)
6573
}
6674

6775
// https://developer.mozilla.org/en-US/docs/Web/API/Console
6876
pub fn Debug(global: &GlobalScope, messages: Vec<DOMString>) {
69-
with_stderr_lock(move || {
70-
for message in messages {
71-
println!("{}", message);
72-
Self::send_to_devtools(global, LogLevel::Debug, message);
73-
}
74-
})
77+
console_messages(global, &messages, LogLevel::Debug)
7578
}
7679

7780
// https://developer.mozilla.org/en-US/docs/Web/API/Console/info
7881
pub fn Info(global: &GlobalScope, messages: Vec<DOMString>) {
79-
with_stderr_lock(move || {
80-
for message in messages {
81-
println!("{}", message);
82-
Self::send_to_devtools(global, LogLevel::Info, message);
83-
}
84-
})
82+
console_messages(global, &messages, LogLevel::Info)
8583
}
8684

8785
// https://developer.mozilla.org/en-US/docs/Web/API/Console/warn
8886
pub fn Warn(global: &GlobalScope, messages: Vec<DOMString>) {
89-
with_stderr_lock(move || {
90-
for message in messages {
91-
println!("{}", message);
92-
Self::send_to_devtools(global, LogLevel::Warn, message);
93-
}
94-
})
87+
console_messages(global, &messages, LogLevel::Warn)
9588
}
9689

9790
// https://developer.mozilla.org/en-US/docs/Web/API/Console/error
9891
pub fn Error(global: &GlobalScope, messages: Vec<DOMString>) {
99-
with_stderr_lock(move || {
100-
for message in messages {
101-
println!("{}", message);
102-
Self::send_to_devtools(global, LogLevel::Error, message);
103-
}
104-
})
92+
console_messages(global, &messages, LogLevel::Error)
10593
}
10694

10795
// https://developer.mozilla.org/en-US/docs/Web/API/Console/assert
10896
pub fn Assert(global: &GlobalScope, condition: bool, message: Option<DOMString>) {
109-
with_stderr_lock(move || {
110-
if !condition {
111-
let message = message.unwrap_or_else(|| DOMString::from("no message"));
112-
println!("Assertion failed: {}", message);
113-
Self::send_to_devtools(global, LogLevel::Error, message);
114-
}
115-
})
97+
if !condition {
98+
let message = message.unwrap_or_else(|| DOMString::from("no message"));
99+
let message = DOMString::from(format!("Assertion failed: {}", message));
100+
console_message(global, message, LogLevel::Error)
101+
};
116102
}
117103

118104
// https://developer.mozilla.org/en-US/docs/Web/API/Console/time
119105
pub fn Time(global: &GlobalScope, label: DOMString) {
120-
with_stderr_lock(move || {
121-
if let Ok(()) = global.time(label.clone()) {
122-
let message = DOMString::from(format!("{}: timer started", label));
123-
println!("{}", message);
124-
Self::send_to_devtools(global, LogLevel::Log, message);
125-
}
126-
})
106+
if let Ok(()) = global.time(label.clone()) {
107+
let message = DOMString::from(format!("{}: timer started", label));
108+
console_message(global, message, LogLevel::Log);
109+
}
127110
}
128111

129112
// https://developer.mozilla.org/en-US/docs/Web/API/Console/timeEnd
130113
pub fn TimeEnd(global: &GlobalScope, label: DOMString) {
131-
with_stderr_lock(move || {
132-
if let Ok(delta) = global.time_end(&label) {
133-
let message = DOMString::from(format!("{}: {}ms", label, delta));
134-
println!("{}", message);
135-
Self::send_to_devtools(global, LogLevel::Log, message);
136-
};
137-
})
114+
if let Ok(delta) = global.time_end(&label) {
115+
let message = DOMString::from(format!("{}: {}ms", label, delta));
116+
console_message(global, message, LogLevel::Log);
117+
}
118+
}
119+
120+
// https://console.spec.whatwg.org/#group
121+
pub fn Group(global: &GlobalScope, messages: Vec<DOMString>) {
122+
global.push_console_group(DOMString::from(messages.join(" ")));
123+
}
124+
125+
// https://console.spec.whatwg.org/#groupcollapsed
126+
pub fn GroupCollapsed(global: &GlobalScope, messages: Vec<DOMString>) {
127+
global.push_console_group(DOMString::from(messages.join(" ")));
128+
}
129+
130+
// https://console.spec.whatwg.org/#groupend
131+
pub fn GroupEnd(global: &GlobalScope) {
132+
global.pop_console_group();
138133
}
139134
}

components/script/dom/globalscope.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ pub struct GlobalScope {
289289

290290
/// currect https state (from previous request)
291291
https_state: Cell<HttpsState>,
292+
293+
/// The stack of active group labels for the Console APIs.
294+
console_group_stack: DomRefCell<Vec<DOMString>>,
292295
}
293296

294297
/// A wrapper for glue-code between the ipc router and the event-loop.
@@ -738,6 +741,7 @@ impl GlobalScope {
738741
gpu_id_hub,
739742
frozen_supported_performance_entry_types: DomRefCell::new(Default::default()),
740743
https_state: Cell::new(HttpsState::None),
744+
console_group_stack: DomRefCell::new(Vec::new()),
741745
}
742746
}
743747

@@ -2908,6 +2912,21 @@ impl GlobalScope {
29082912
pub fn wgpu_id_hub(&self) -> Arc<Mutex<Identities>> {
29092913
self.gpu_id_hub.clone()
29102914
}
2915+
2916+
pub(crate) fn current_group_label(&self) -> Option<DOMString> {
2917+
self.console_group_stack
2918+
.borrow()
2919+
.last()
2920+
.map(|label| DOMString::from(format!("[{}]", label)))
2921+
}
2922+
2923+
pub(crate) fn push_console_group(&self, group: DOMString) {
2924+
self.console_group_stack.borrow_mut().push(group);
2925+
}
2926+
2927+
pub(crate) fn pop_console_group(&self) {
2928+
let _ = self.console_group_stack.borrow_mut().pop();
2929+
}
29112930
}
29122931

29132932
fn timestamp_in_ms(time: Timespec) -> u64 {
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
/* This Source Code Form is subject to the terms of the Mozilla Public
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4-
/*
5-
* References:
6-
* MDN Docs - https://developer.mozilla.org/en-US/docs/Web/API/console
7-
* Draft Spec - https://sideshowbarker.github.io/console-spec/
8-
*
9-
* © Copyright 2014 Mozilla Foundation.
10-
*/
4+
5+
// https://console.spec.whatwg.org/
116

127
[ClassString="Console",
138
Exposed=(Window,Worker,Worklet),
149
ProtoObjectHack]
1510
namespace console {
16-
// These should be DOMString message, DOMString message2, ...
11+
// Logging
1712
void log(DOMString... messages);
1813
void debug(DOMString... messages);
1914
void info(DOMString... messages);
2015
void warn(DOMString... messages);
2116
void error(DOMString... messages);
2217
void assert(boolean condition, optional DOMString message);
18+
19+
// Grouping
20+
void group(DOMString... data);
21+
void groupCollapsed(DOMString... data);
22+
void groupEnd();
23+
24+
// Timing
2325
void time(DOMString message);
2426
void timeEnd(DOMString message);
2527
};

python/tidy/servo_tidy/tidy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def wpt_path(*args):
9090
b"//drafts.csswg.org",
9191
b"//drafts.css-houdini.org",
9292
b"//drafts.fxtf.org",
93+
b"//console.spec.whatwg.org",
9394
b"//encoding.spec.whatwg.org",
9495
b"//fetch.spec.whatwg.org",
9596
b"//html.spec.whatwg.org",

0 commit comments

Comments
 (0)