From d2dcb20beac29eabce02ea59b4944585d5b48a7c Mon Sep 17 00:00:00 2001 From: Smitty Date: Sun, 17 Mar 2024 05:50:40 -0400 Subject: [PATCH] Implement console.count/countReset (#31635) * Implement console.count/countReset * Address review comment Signed-off-by: syvb --------- Signed-off-by: syvb --- components/script/dom/console.rs | 17 +++++++++++++ components/script/dom/globalscope.rs | 25 +++++++++++++++++++ components/script/dom/webidls/Console.webidl | 4 +++ .../console-label-conversion.any.js.ini | 24 ------------------ .../console/idlharness.any.js.ini | 12 --------- .../console-label-conversion.any.js.ini | 24 ------------------ tests/wpt/meta/console/idlharness.any.js.ini | 12 --------- 7 files changed, 46 insertions(+), 72 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(); } + + /// + 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); + } + + /// + 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>, + /// The count map for the Console APIs. + /// + /// + console_count_map: DomRefCell>, + /// List of ongoing dynamic module imports. dynamic_modules: DomRefCell, @@ -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 { 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); diff --git a/tests/wpt/meta-legacy-layout/console/console-label-conversion.any.js.ini b/tests/wpt/meta-legacy-layout/console/console-label-conversion.any.js.ini index a27c3c58e21..172f06032c8 100644 --- a/tests/wpt/meta-legacy-layout/console/console-label-conversion.any.js.ini +++ b/tests/wpt/meta-legacy-layout/console/console-label-conversion.any.js.ini @@ -1,16 +1,4 @@ [console-label-conversion.any.html] - [console.count()'s label gets converted to string via label.toString() when label is an object] - expected: FAIL - - [console.count() throws exceptions generated by erroneous label.toString() conversion] - expected: FAIL - - [console.countReset()'s label gets converted to string via label.toString() when label is an object] - expected: FAIL - - [console.countReset() throws exceptions generated by erroneous label.toString() conversion] - expected: FAIL - [console.timeLog()'s label gets converted to string via label.toString() when label is an object] expected: FAIL @@ -19,18 +7,6 @@ [console-label-conversion.any.worker.html] - [console.count()'s label gets converted to string via label.toString() when label is an object] - expected: FAIL - - [console.count() throws exceptions generated by erroneous label.toString() conversion] - expected: FAIL - - [console.countReset()'s label gets converted to string via label.toString() when label is an object] - expected: FAIL - - [console.countReset() throws exceptions generated by erroneous label.toString() conversion] - expected: FAIL - [console.timeLog()'s label gets converted to string via label.toString() when label is an object] expected: FAIL diff --git a/tests/wpt/meta-legacy-layout/console/idlharness.any.js.ini b/tests/wpt/meta-legacy-layout/console/idlharness.any.js.ini index 56286ceece8..2de2e1b05fd 100644 --- a/tests/wpt/meta-legacy-layout/console/idlharness.any.js.ini +++ b/tests/wpt/meta-legacy-layout/console/idlharness.any.js.ini @@ -17,12 +17,6 @@ [console namespace: operation dirxml(any...)] expected: FAIL - [console namespace: operation count(optional DOMString)] - expected: FAIL - - [console namespace: operation countReset(optional DOMString)] - expected: FAIL - [console namespace: operation time(optional DOMString)] expected: FAIL @@ -52,12 +46,6 @@ [console namespace: operation dirxml(any...)] expected: FAIL - [console namespace: operation count(optional DOMString)] - expected: FAIL - - [console namespace: operation countReset(optional DOMString)] - expected: FAIL - [console namespace: operation time(optional DOMString)] expected: FAIL diff --git a/tests/wpt/meta/console/console-label-conversion.any.js.ini b/tests/wpt/meta/console/console-label-conversion.any.js.ini index a27c3c58e21..172f06032c8 100644 --- a/tests/wpt/meta/console/console-label-conversion.any.js.ini +++ b/tests/wpt/meta/console/console-label-conversion.any.js.ini @@ -1,16 +1,4 @@ [console-label-conversion.any.html] - [console.count()'s label gets converted to string via label.toString() when label is an object] - expected: FAIL - - [console.count() throws exceptions generated by erroneous label.toString() conversion] - expected: FAIL - - [console.countReset()'s label gets converted to string via label.toString() when label is an object] - expected: FAIL - - [console.countReset() throws exceptions generated by erroneous label.toString() conversion] - expected: FAIL - [console.timeLog()'s label gets converted to string via label.toString() when label is an object] expected: FAIL @@ -19,18 +7,6 @@ [console-label-conversion.any.worker.html] - [console.count()'s label gets converted to string via label.toString() when label is an object] - expected: FAIL - - [console.count() throws exceptions generated by erroneous label.toString() conversion] - expected: FAIL - - [console.countReset()'s label gets converted to string via label.toString() when label is an object] - expected: FAIL - - [console.countReset() throws exceptions generated by erroneous label.toString() conversion] - expected: FAIL - [console.timeLog()'s label gets converted to string via label.toString() when label is an object] expected: FAIL diff --git a/tests/wpt/meta/console/idlharness.any.js.ini b/tests/wpt/meta/console/idlharness.any.js.ini index 8881965fd27..a1cbfd2aab1 100644 --- a/tests/wpt/meta/console/idlharness.any.js.ini +++ b/tests/wpt/meta/console/idlharness.any.js.ini @@ -17,12 +17,6 @@ [console namespace: operation dirxml(any...)] expected: FAIL - [console namespace: operation count(optional DOMString)] - expected: FAIL - - [console namespace: operation countReset(optional DOMString)] - expected: FAIL - [console namespace: operation time(optional DOMString)] expected: FAIL @@ -52,12 +46,6 @@ [console namespace: operation dirxml(any...)] expected: FAIL - [console namespace: operation count(optional DOMString)] - expected: FAIL - - [console namespace: operation countReset(optional DOMString)] - expected: FAIL - [console namespace: operation time(optional DOMString)] expected: FAIL