diff options
author | Smitty <me@iter.ca> | 2024-03-17 05:50:40 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-17 09:50:40 +0000 |
commit | d2dcb20beac29eabce02ea59b4944585d5b48a7c (patch) | |
tree | 82f2c9a162f6dfc03b511af834e3d897c1821735 /components/script/dom | |
parent | f98975bbbe7cd8cf55f172dae96f8cbc79c0e479 (diff) | |
download | servo-d2dcb20beac29eabce02ea59b4944585d5b48a7c.tar.gz servo-d2dcb20beac29eabce02ea59b4944585d5b48a7c.zip |
Implement console.count/countReset (#31635)
* Implement console.count/countReset
* Address review comment
Signed-off-by: syvb <me@iter.ca>
---------
Signed-off-by: syvb <me@iter.ca>
Diffstat (limited to 'components/script/dom')
-rw-r--r-- | components/script/dom/console.rs | 17 | ||||
-rw-r--r-- | components/script/dom/globalscope.rs | 25 | ||||
-rw-r--r-- | components/script/dom/webidls/Console.webidl | 4 |
3 files changed, 46 insertions, 0 deletions
diff --git a/components/script/dom/console.rs b/components/script/dom/console.rs index f8547c449ff..737bb35b926 100644 --- a/components/script/dom/console.rs +++ b/components/script/dom/console.rs @@ -296,4 +296,21 @@ impl Console { pub fn GroupEnd(global: &GlobalScope) { global.pop_console_group(); } + + /// <https://console.spec.whatwg.org/#count> + pub fn Count(global: &GlobalScope, label: DOMString) { + let count = global.increment_console_count(&label); + let message = DOMString::from(format!("{label}: {count}")); + console_message(global, message, LogLevel::Log); + } + + /// <https://console.spec.whatwg.org/#countreset> + pub fn CountReset(global: &GlobalScope, label: DOMString) { + if global.reset_console_count(&label).is_err() { + Self::internal_warn( + global, + DOMString::from(format!("Counter “{label}” doesn’t exist.")), + ) + } + } } diff --git a/components/script/dom/globalscope.rs b/components/script/dom/globalscope.rs index c9fd013318c..9140088f67d 100644 --- a/components/script/dom/globalscope.rs +++ b/components/script/dom/globalscope.rs @@ -329,6 +329,11 @@ pub struct GlobalScope { /// The stack of active group labels for the Console APIs. console_group_stack: DomRefCell<Vec<DOMString>>, + /// The count map for the Console APIs. + /// + /// <https://console.spec.whatwg.org/#count> + console_count_map: DomRefCell<HashMap<DOMString, usize>>, + /// List of ongoing dynamic module imports. dynamic_modules: DomRefCell<DynamicModuleList>, @@ -790,6 +795,7 @@ impl GlobalScope { frozen_supported_performance_entry_types: DomRefCell::new(Default::default()), https_state: Cell::new(HttpsState::None), console_group_stack: DomRefCell::new(Vec::new()), + console_count_map: Default::default(), dynamic_modules: DomRefCell::new(DynamicModuleList::new()), inherited_secure_context, } @@ -3255,6 +3261,25 @@ impl GlobalScope { let _ = self.console_group_stack.borrow_mut().pop(); } + pub(crate) fn increment_console_count(&self, label: &DOMString) -> usize { + *self + .console_count_map + .borrow_mut() + .entry(label.clone()) + .and_modify(|e| *e += 1) + .or_insert(1) + } + + pub(crate) fn reset_console_count(&self, label: &DOMString) -> Result<(), ()> { + match self.console_count_map.borrow_mut().get_mut(label) { + Some(value) => { + *value = 0; + Ok(()) + }, + None => Err(()), + } + } + pub(crate) fn dynamic_module_list(&self) -> RefMut<DynamicModuleList> { self.dynamic_modules.borrow_mut() } diff --git a/components/script/dom/webidls/Console.webidl b/components/script/dom/webidls/Console.webidl index ac6ffa27806..4cc02d80436 100644 --- a/components/script/dom/webidls/Console.webidl +++ b/components/script/dom/webidls/Console.webidl @@ -17,6 +17,10 @@ namespace console { undefined assert(boolean condition, optional any message); undefined clear(); + // Counting + undefined count(optional DOMString label = "default"); + undefined countReset(optional DOMString label = "default"); + // Grouping undefined group(any... data); undefined groupCollapsed(any... data); |