mirror of
https://github.com/lexmount/moli.git
synced 2026-11-24 16:02:53 +00:00
fix(dom): preserve live boundaries during normalization
This commit is contained in:
@@ -39,8 +39,8 @@ pub use self::mutation::{
|
||||
DomMutationRecord, DomMutationRecordBatch, DomMutationRecordKind, DomScriptMutationEffects,
|
||||
DomSlotAssignmentChange, DomSlotMutationEffects, DomStyleInvalidationInputs,
|
||||
DomStylesheetOwnerChange, DomStylesheetOwnerChangeKind, DomStylesheetOwnerTransitions,
|
||||
DomStylesheetOwnerTreeScopes, DomTextareaValueChange, DomTreeMutationEffects,
|
||||
ScriptPrepareTrigger, ScriptPrepareTriggerKind,
|
||||
DomStylesheetOwnerTreeScopes, DomTextNormalizationStep, DomTextareaValueChange,
|
||||
DomTreeMutationEffects, ScriptPrepareTrigger, ScriptPrepareTriggerKind,
|
||||
};
|
||||
|
||||
use self::query_index::ElementQueryIndex;
|
||||
|
||||
@@ -2,11 +2,14 @@ use super::*;
|
||||
|
||||
mod attributes;
|
||||
mod effects;
|
||||
mod normalization;
|
||||
mod owner_lifecycle;
|
||||
mod state;
|
||||
mod text_control;
|
||||
mod tree;
|
||||
|
||||
pub use normalization::DomTextNormalizationStep;
|
||||
|
||||
pub use effects::{
|
||||
DomAttributeMutation, DomAttributeMutationOutcome, DomChildListMutation, DomMutationEffects,
|
||||
DomMutationRecord, DomMutationRecordBatch, DomMutationRecordKind, DomScriptMutationEffects,
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
use super::*;
|
||||
|
||||
/// One text node removed by normalization, optionally merged into an earlier
|
||||
/// nonempty Text sibling. Consumers apply the steps in document order.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct DomTextNormalizationStep {
|
||||
pub parent: DomHandle,
|
||||
pub text: DomHandle,
|
||||
pub merge_into: Option<DomHandle>,
|
||||
}
|
||||
|
||||
impl DomHost {
|
||||
/// Plan only ordinary descendants: CDATA, template contents and shadow
|
||||
/// trees are not contiguous exclusive Text nodes in this subtree.
|
||||
pub fn text_normalization_steps(&self, root: DomHandle) -> Vec<DomTextNormalizationStep> {
|
||||
let mut steps = Vec::new();
|
||||
let mut stack = self.child_handles_reversed(root).collect::<Vec<_>>();
|
||||
let mut previous_text = None;
|
||||
while let Some(handle) = stack.pop() {
|
||||
let Some(node) = self.node(handle) else {
|
||||
continue;
|
||||
};
|
||||
if node.node_type() != NodeType::Text {
|
||||
previous_text = None;
|
||||
stack.extend(self.child_handles_reversed(handle));
|
||||
continue;
|
||||
}
|
||||
let Some(parent) = node.parent_node() else {
|
||||
continue;
|
||||
};
|
||||
let merge_into = previous_text
|
||||
.filter(|(previous_parent, _)| *previous_parent == parent)
|
||||
.map(|(_, text)| text);
|
||||
if merge_into.is_some() || node.character_data_value().is_some_and(|v| v.is_empty()) {
|
||||
steps.push(DomTextNormalizationStep {
|
||||
parent,
|
||||
text: handle,
|
||||
merge_into,
|
||||
});
|
||||
} else {
|
||||
previous_text = Some((parent, handle));
|
||||
}
|
||||
}
|
||||
steps
|
||||
}
|
||||
|
||||
pub fn normalize_effects(&mut self, handle: DomHandle) -> DomMutationEffects {
|
||||
let mut effects = DomMutationEffects::default();
|
||||
for step in self.text_normalization_steps(handle) {
|
||||
if let Some(target) = step.merge_into {
|
||||
let source = self
|
||||
.node(step.text)
|
||||
.and_then(Node::character_data_value)
|
||||
.cloned();
|
||||
let value = self
|
||||
.node(target)
|
||||
.and_then(Node::character_data_value)
|
||||
.cloned();
|
||||
if let (Some(source), Some(mut value)) = (source, value)
|
||||
&& !source.is_empty()
|
||||
{
|
||||
value.append(&source);
|
||||
effects.merge(self.set_character_data_value_effects(target, value, false));
|
||||
}
|
||||
}
|
||||
effects.merge(self.remove_child_effects(step.parent, step.text));
|
||||
}
|
||||
effects
|
||||
}
|
||||
}
|
||||
@@ -817,91 +817,6 @@ impl DomHost {
|
||||
effects
|
||||
}
|
||||
|
||||
pub fn normalize_effects(&mut self, handle: DomHandle) -> DomMutationEffects {
|
||||
if self.node(handle).is_none() {
|
||||
return DomMutationEffects::default();
|
||||
}
|
||||
|
||||
let mut containers = Vec::new();
|
||||
let mut stack = vec![handle];
|
||||
while let Some(container) = stack.pop() {
|
||||
let Some(node) = self.node(container) else {
|
||||
continue;
|
||||
};
|
||||
if !node.can_have_children() {
|
||||
continue;
|
||||
}
|
||||
containers.push(container);
|
||||
stack.extend(
|
||||
self.child_handles_reversed(container)
|
||||
.filter(|child| self.node(*child).is_some_and(Node::can_have_children)),
|
||||
);
|
||||
}
|
||||
|
||||
let mut effects = DomMutationEffects::default();
|
||||
for container in containers.into_iter().rev() {
|
||||
effects.merge(self.normalize_direct_child_text_effects(container));
|
||||
}
|
||||
effects
|
||||
}
|
||||
|
||||
fn normalize_direct_child_text_effects(&mut self, handle: DomHandle) -> DomMutationEffects {
|
||||
let mut effects = DomMutationEffects::default();
|
||||
let mut child = self.node(handle).and_then(Node::first_child);
|
||||
while let Some(child_handle) = child {
|
||||
let next = self.node(child_handle).and_then(Node::next_sibling);
|
||||
let Some(child_type) = self.node(child_handle).map(Node::node_type) else {
|
||||
child = next;
|
||||
continue;
|
||||
};
|
||||
|
||||
if child_type == NodeType::Text {
|
||||
let current = self
|
||||
.node(child_handle)
|
||||
.and_then(Node::character_data_value)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
if current.is_empty() {
|
||||
effects.merge(self.remove_child_effects(handle, child_handle));
|
||||
child = next;
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut merged = current.clone();
|
||||
let mut adjacent = next;
|
||||
while let Some(sibling_handle) = adjacent {
|
||||
let is_text = self
|
||||
.node(sibling_handle)
|
||||
.is_some_and(|node| node.node_type() == NodeType::Text);
|
||||
if !is_text {
|
||||
break;
|
||||
}
|
||||
let sibling_data = self
|
||||
.node(sibling_handle)
|
||||
.and_then(Node::character_data_value)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
if !sibling_data.is_empty() {
|
||||
merged.append(&sibling_data);
|
||||
effects.merge(self.set_character_data_value_effects(
|
||||
child_handle,
|
||||
merged.clone(),
|
||||
false,
|
||||
));
|
||||
}
|
||||
effects.merge(self.remove_child_effects(handle, sibling_handle));
|
||||
adjacent = self.node(child_handle).and_then(Node::next_sibling);
|
||||
}
|
||||
child = adjacent;
|
||||
continue;
|
||||
}
|
||||
|
||||
child = next;
|
||||
}
|
||||
|
||||
effects
|
||||
}
|
||||
|
||||
pub fn connected_script_handles(&self, root: DomHandle) -> Vec<DomHandle> {
|
||||
let mut handles = Vec::new();
|
||||
self.collect_script_handles_in_shadow_including_subtree(root, true, &mut handles);
|
||||
|
||||
@@ -31,8 +31,8 @@ pub use host::{
|
||||
DomMutationEffects, DomMutationRecord, DomMutationRecordBatch, DomMutationRecordKind,
|
||||
DomScriptMutationEffects, DomSlotAssignmentChange, DomSlotMutationEffects,
|
||||
DomStyleInvalidationInputs, DomStylesheetOwnerChange, DomStylesheetOwnerChangeKind,
|
||||
DomStylesheetOwnerTransitions, DomStylesheetOwnerTreeScopes, DomTextareaValueChange,
|
||||
DomTreeMutationEffects, ScriptPrepareTrigger, ScriptPrepareTriggerKind,
|
||||
DomStylesheetOwnerTransitions, DomStylesheetOwnerTreeScopes, DomTextNormalizationStep,
|
||||
DomTextareaValueChange, DomTreeMutationEffects, ScriptPrepareTrigger, ScriptPrepareTriggerKind,
|
||||
StylesheetCandidateTreeScopeSnapshots,
|
||||
};
|
||||
pub use host::{
|
||||
|
||||
@@ -431,139 +431,6 @@ impl DocumentRuntime {
|
||||
Some(new_text)
|
||||
}
|
||||
|
||||
pub(crate) fn normalize(
|
||||
&mut self,
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
host_ptr: *mut PageScriptHost,
|
||||
handle: DomHandle,
|
||||
) -> bool {
|
||||
let (effects, prepublished_removals) =
|
||||
if unsafe { &*host_ptr }.has_dom_debugger_dom_breakpoints() {
|
||||
self.normalize_effects_with_dom_debugger(host_ptr, handle)
|
||||
} else {
|
||||
(self.dom_host.normalize_effects(handle), Vec::new())
|
||||
};
|
||||
self.apply_runtime_mutation_effects_with_prepublished_removals(
|
||||
scope,
|
||||
host_ptr,
|
||||
effects,
|
||||
RuntimeMutationOptions::js_dom_api(),
|
||||
prepublished_removals,
|
||||
)
|
||||
}
|
||||
|
||||
fn normalize_effects_with_dom_debugger(
|
||||
&mut self,
|
||||
host_ptr: *mut PageScriptHost,
|
||||
handle: DomHandle,
|
||||
) -> (
|
||||
DomMutationEffects,
|
||||
Vec<devtools_mutations::DevToolsDomPrepublishedRemoval>,
|
||||
) {
|
||||
let mut containers = Vec::new();
|
||||
let mut stack = vec![handle];
|
||||
while let Some(container) = stack.pop() {
|
||||
let Some(node) = self.dom_host.node(container) else {
|
||||
continue;
|
||||
};
|
||||
if !node.can_have_children() {
|
||||
continue;
|
||||
}
|
||||
containers.push(container);
|
||||
stack.extend(
|
||||
self.dom_host
|
||||
.child_handles_reversed(container)
|
||||
.filter(|child| {
|
||||
self.dom_host
|
||||
.node(*child)
|
||||
.is_some_and(Node::can_have_children)
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
let mut effects = DomMutationEffects::default();
|
||||
let mut prepublished_removals = Vec::new();
|
||||
for container in containers.into_iter().rev() {
|
||||
let mut child = self.dom_host.node(container).and_then(Node::first_child);
|
||||
while let Some(child_handle) = child {
|
||||
let next = self
|
||||
.dom_host
|
||||
.node(child_handle)
|
||||
.and_then(Node::next_sibling);
|
||||
if self.dom_host.node(child_handle).map(Node::node_type) != Some(NodeType::Text) {
|
||||
child = next;
|
||||
continue;
|
||||
}
|
||||
|
||||
let current = self
|
||||
.dom_host
|
||||
.node(child_handle)
|
||||
.and_then(Node::character_data_value)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
if current.is_empty() {
|
||||
prepublished_removals.extend(self.break_on_dom_debugger_before_tree_removal(
|
||||
host_ptr,
|
||||
container,
|
||||
child_handle,
|
||||
));
|
||||
effects.merge(self.dom_host.remove_child_effects(container, child_handle));
|
||||
child = next;
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut adjacent = next;
|
||||
while let Some(sibling_handle) = adjacent {
|
||||
if self.dom_host.node(sibling_handle).map(Node::node_type)
|
||||
!= Some(NodeType::Text)
|
||||
{
|
||||
break;
|
||||
}
|
||||
let sibling_value = self
|
||||
.dom_host
|
||||
.node(sibling_handle)
|
||||
.and_then(Node::character_data_value)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
if !sibling_value.is_empty() {
|
||||
let mut merged = self
|
||||
.dom_host
|
||||
.node(child_handle)
|
||||
.and_then(Node::character_data_value)
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
merged.append(&sibling_value);
|
||||
let character_effects = self.dom_host.set_character_data_value_effects(
|
||||
child_handle,
|
||||
merged,
|
||||
false,
|
||||
);
|
||||
if character_effects.did_change() {
|
||||
unsafe { &mut *host_ptr }
|
||||
.break_on_dom_debugger_character_data_modified(child_handle);
|
||||
}
|
||||
effects.merge(character_effects);
|
||||
}
|
||||
prepublished_removals.extend(self.break_on_dom_debugger_before_tree_removal(
|
||||
host_ptr,
|
||||
container,
|
||||
sibling_handle,
|
||||
));
|
||||
effects.merge(
|
||||
self.dom_host
|
||||
.remove_child_effects(container, sibling_handle),
|
||||
);
|
||||
adjacent = self
|
||||
.dom_host
|
||||
.node(child_handle)
|
||||
.and_then(Node::next_sibling);
|
||||
}
|
||||
child = adjacent;
|
||||
}
|
||||
}
|
||||
(effects, prepublished_removals)
|
||||
}
|
||||
|
||||
pub(crate) fn set_attribute(
|
||||
&mut self,
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
|
||||
@@ -9,6 +9,7 @@ mod insertion_followups;
|
||||
mod insertion_plan;
|
||||
mod live_ranges;
|
||||
mod node_iterators;
|
||||
mod normalization;
|
||||
mod parser;
|
||||
mod parser_post_step;
|
||||
mod policy;
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
use crate::{
|
||||
custom_elements,
|
||||
document_runtime::{DocumentRuntime, DomHandle},
|
||||
dom::native::Node,
|
||||
native_bridge::PageScriptHost,
|
||||
};
|
||||
|
||||
impl DocumentRuntime {
|
||||
pub(crate) fn normalize(
|
||||
&mut self,
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
host_ptr: *mut PageScriptHost,
|
||||
handle: DomHandle,
|
||||
) -> bool {
|
||||
let steps = self.dom_host.text_normalization_steps(handle);
|
||||
custom_elements::with_custom_element_reaction_scope(scope, host_ptr, |scope| {
|
||||
let mut changed = false;
|
||||
for step in steps {
|
||||
if let Some(target) = step.merge_into {
|
||||
let Some(mut value) = self
|
||||
.dom_host
|
||||
.node(target)
|
||||
.and_then(Node::character_data_value)
|
||||
.cloned()
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let Some(source) = self
|
||||
.dom_host
|
||||
.node(step.text)
|
||||
.and_then(Node::character_data_value)
|
||||
.cloned()
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let offset = value.utf16_units().len() as u32;
|
||||
if !source.is_empty() {
|
||||
value.append(&source);
|
||||
changed |= self.set_character_data_value_in_current_reaction_queue(
|
||||
scope, host_ptr, target, value, false,
|
||||
);
|
||||
}
|
||||
// Range endpoints move into the retained text before ordinary
|
||||
// removal can rehome them to the parent. Empty siblings count.
|
||||
unsafe { &mut *host_ptr }
|
||||
.update_live_range_records_for_text_merge(scope, target, step.text, offset);
|
||||
}
|
||||
changed |= self.remove_child_appending_to_current_reaction_queue(
|
||||
scope,
|
||||
host_ptr,
|
||||
step.parent,
|
||||
step.text,
|
||||
);
|
||||
}
|
||||
changed
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,22 @@ impl PageScriptHost {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn update_sequential_focus_starting_points_for_text_merge(
|
||||
&mut self,
|
||||
target: DomHandle,
|
||||
removed_text: DomHandle,
|
||||
offset: u32,
|
||||
) {
|
||||
let dom = unsafe { &*self.document_runtime }.dom_host();
|
||||
for state in self.document_focus_changes.values_mut() {
|
||||
if let Some(SequentialFocusStartingPoint::Position(point)) =
|
||||
&mut state.sequential_starting_point
|
||||
{
|
||||
point.update_for_text_merge(dom, target, removed_text, offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn update_sequential_focus_starting_points_for_child_removal(
|
||||
&mut self,
|
||||
parent: DomHandle,
|
||||
|
||||
@@ -169,7 +169,7 @@ impl RangeRecordRegistry {
|
||||
ids.into_iter().collect()
|
||||
}
|
||||
|
||||
fn live_record_ids_for_text_split(
|
||||
fn live_record_ids_for_text_boundary_change(
|
||||
&mut self,
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
dom_host: &crate::dom::native::DomHost,
|
||||
@@ -438,6 +438,27 @@ impl RangeRecordRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
fn update_for_text_merge(
|
||||
&mut self,
|
||||
dom_host: &crate::dom::native::DomHost,
|
||||
ids: &[RangeRecordId],
|
||||
target: DomHandle,
|
||||
removed_text: DomHandle,
|
||||
offset: u32,
|
||||
) {
|
||||
for id in ids {
|
||||
let Some(record) = self.records.get_mut(id) else {
|
||||
continue;
|
||||
};
|
||||
let before = record.boundary_index_keys();
|
||||
record.for_each_boundary_mut(|boundary| {
|
||||
boundary.update_for_text_merge(dom_host, target, removed_text, offset);
|
||||
});
|
||||
let after = record.boundary_index_keys();
|
||||
self.reindex_record_boundaries_if_changed(*id, before, after);
|
||||
}
|
||||
}
|
||||
|
||||
fn is_collapsed(
|
||||
&mut self,
|
||||
dom_host: &crate::dom::native::DomHost,
|
||||
@@ -785,7 +806,7 @@ impl PageScriptHost {
|
||||
.update_composed_for_text_split(dom_host, original, new_text, offset);
|
||||
let ids = self
|
||||
.range_record_registry
|
||||
.live_record_ids_for_text_split(scope, dom_host, original);
|
||||
.live_record_ids_for_text_boundary_change(scope, dom_host, original);
|
||||
if ids.is_empty() {
|
||||
return;
|
||||
}
|
||||
@@ -798,6 +819,37 @@ impl PageScriptHost {
|
||||
.sync_linked_range_boundaries(&self.range_record_registry, selection_links);
|
||||
}
|
||||
|
||||
pub(crate) fn update_live_range_records_for_text_merge(
|
||||
&mut self,
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
target: DomHandle,
|
||||
removed_text: DomHandle,
|
||||
offset: u32,
|
||||
) {
|
||||
self.update_sequential_focus_starting_points_for_text_merge(target, removed_text, offset);
|
||||
let dom_host = unsafe { &*self.document_runtime }.dom_host();
|
||||
self.selection_record_registry
|
||||
.update_composed_for_text_merge(dom_host, target, removed_text, offset);
|
||||
let ids = self
|
||||
.range_record_registry
|
||||
.live_record_ids_for_text_boundary_change(scope, dom_host, removed_text);
|
||||
if ids.is_empty() {
|
||||
return;
|
||||
}
|
||||
let selection_links = self
|
||||
.selection_record_registry
|
||||
.linked_range_boundaries(dom_host, &self.range_record_registry);
|
||||
self.range_record_registry.update_for_text_merge(
|
||||
dom_host,
|
||||
&ids,
|
||||
target,
|
||||
removed_text,
|
||||
offset,
|
||||
);
|
||||
self.selection_record_registry
|
||||
.sync_linked_range_boundaries(&self.range_record_registry, selection_links);
|
||||
}
|
||||
|
||||
pub(crate) fn range_record_is_collapsed(&mut self, handle: RangeRecordHandle) -> Option<bool> {
|
||||
let runtime = self.document_runtime;
|
||||
let dom_host = unsafe { &*runtime }.dom_host();
|
||||
|
||||
@@ -255,6 +255,25 @@ impl SelectionRecordRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn update_composed_for_text_merge(
|
||||
&mut self,
|
||||
dom: &DomHost,
|
||||
target: DomHandle,
|
||||
removed_text: DomHandle,
|
||||
offset: u32,
|
||||
) {
|
||||
for record in self.records.values_mut() {
|
||||
for point in [&mut record.composed_start, &mut record.composed_end]
|
||||
.into_iter()
|
||||
.flatten()
|
||||
{
|
||||
point.update_text_boundary(dom, |boundary| {
|
||||
boundary.update_for_text_merge(dom, target, removed_text, offset);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn update_for_child_removal(
|
||||
&mut self,
|
||||
dom: &DomHost,
|
||||
|
||||
@@ -129,6 +129,30 @@ impl RangeBoundaryPoint {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn update_for_text_merge(
|
||||
&mut self,
|
||||
dom_host: &DomHost,
|
||||
target: DomHandle,
|
||||
removed_text: DomHandle,
|
||||
target_length: u32,
|
||||
) {
|
||||
let offset = if self.container == removed_text {
|
||||
let Some(offset) = self.offset(dom_host) else {
|
||||
return;
|
||||
};
|
||||
target_length.saturating_add(offset)
|
||||
} else if dom_host.parent_node(removed_text) == Some(self.container)
|
||||
&& self.child_before_boundary == dom_host.previous_sibling(removed_text)
|
||||
{
|
||||
target_length
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
if let Some(point) = Self::new(dom_host, target, offset) {
|
||||
*self = point;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_child_before_boundary(
|
||||
&mut self,
|
||||
dom_host: &DomHost,
|
||||
|
||||
@@ -2426,6 +2426,7 @@ mod native_node_event_outcomes;
|
||||
mod navigation_timing_inheritance;
|
||||
mod no_cors_header_fill;
|
||||
mod node_event_worlds;
|
||||
mod node_normalization;
|
||||
mod node_root_dictionary;
|
||||
|
||||
mod document_traversal_factory_realms;
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
(() => {
|
||||
const child = document.getElementById('child').contentWindow;
|
||||
const documents = [
|
||||
['main', document], ['iframe', child.document],
|
||||
['windowless', document.implementation.createHTMLDocument('')],
|
||||
['xml', document.implementation.createDocument(null, 'root')]
|
||||
];
|
||||
const failures = [];
|
||||
let checks = 0;
|
||||
const check = (name, condition, details) => {
|
||||
checks++;
|
||||
if (!condition) failures.push({name, details});
|
||||
};
|
||||
for (const [context, doc] of documents) {
|
||||
for (const [entry, normalize] of [
|
||||
['own', node => node.normalize()],
|
||||
['top', node => Node.prototype.normalize.call(node)],
|
||||
['iframe', node => child.Node.prototype.normalize.call(node)]
|
||||
]) {
|
||||
for (const values of [
|
||||
['ab', 'cd', 'ef'], ['', 'a', '', 'b', ''], ['', '', ''],
|
||||
['😀', '\uD800', '\uDC00', 'z'], ['only'], []
|
||||
]) {
|
||||
const parent = doc.createElement('container');
|
||||
const nodes = values.map(value => parent.appendChild(doc.createTextNode(value)));
|
||||
const first = values.findIndex(value => value.length > 0);
|
||||
const prefixLength = index => values.slice(first, index).join('').length;
|
||||
const ranges = [];
|
||||
const addRange = (node, offset, expectedNode, expectedOffset) => {
|
||||
const range = doc.createRange();
|
||||
range.setStart(node, offset);
|
||||
range.collapse(true);
|
||||
ranges.push({range, expectedNode, expectedOffset});
|
||||
};
|
||||
for (let index = 0; index < nodes.length; index++) {
|
||||
for (let offset = 0; offset <= values[index].length; offset++) {
|
||||
addRange(nodes[index], offset,
|
||||
first < 0 || index < first ? parent : nodes[first],
|
||||
first < 0 || index < first ? 0 : prefixLength(index) + offset);
|
||||
}
|
||||
}
|
||||
for (let index = 0; index <= nodes.length; index++) {
|
||||
const atParent = first < 0 || index <= first || index === nodes.length;
|
||||
const expectedOffset = first < 0 || index <= first ? 0
|
||||
: index === nodes.length ? 1 : prefixLength(index);
|
||||
addRange(parent, index, atParent ? parent : nodes[first], expectedOffset);
|
||||
}
|
||||
normalize(parent);
|
||||
const label = `${context}/${entry}/${JSON.stringify(values)}`;
|
||||
check(`${label}/contents`, parent.textContent === values.join('') &&
|
||||
parent.childNodes.length === (first < 0 ? 0 : 1));
|
||||
const validateRanges = phase => {
|
||||
for (const [index, {range, expectedNode, expectedOffset}] of ranges.entries()) {
|
||||
check(`${label}/${phase}/${index}`,
|
||||
range.startContainer === expectedNode && range.endContainer === expectedNode &&
|
||||
range.startOffset === expectedOffset && range.endOffset === expectedOffset,
|
||||
[range.startOffset, range.endOffset, expectedOffset]);
|
||||
}
|
||||
};
|
||||
validateRanges('normalized');
|
||||
for (let index = 0; index < nodes.length; index++) {
|
||||
if (index !== first) nodes[index].data = 'removed';
|
||||
}
|
||||
validateRanges('after-removed-node-edit');
|
||||
}
|
||||
for (const empty of [false, true]) {
|
||||
for (const before of [false, true]) {
|
||||
const parent = doc.createElement('container');
|
||||
const first = parent.appendChild(doc.createTextNode(empty ? '' : 'ab'));
|
||||
const second = parent.appendChild(doc.createTextNode(empty ? '' : 'cd'));
|
||||
const tail = parent.appendChild(doc.createElement('tail'));
|
||||
const iterator = doc.createNodeIterator(parent);
|
||||
while (iterator.nextNode() !== second) {}
|
||||
if (before) { iterator.nextNode(); iterator.previousNode(); iterator.previousNode(); }
|
||||
normalize(parent);
|
||||
const reference = before ? tail : empty ? parent : first;
|
||||
check(`${context}/${entry}/iterator/${empty}/${before}`,
|
||||
iterator.referenceNode === reference && iterator.pointerBeforeReferenceNode === before);
|
||||
check(`${context}/${entry}/iterator-next/${empty}/${before}`, iterator.nextNode() === tail);
|
||||
}
|
||||
}
|
||||
const container = doc.createElement('container');
|
||||
const left = container.appendChild(doc.createTextNode('a'));
|
||||
const comment = container.appendChild(doc.createComment(''));
|
||||
const nested = container.appendChild(doc.createElement('nested'));
|
||||
nested.append(doc.createTextNode('x'), doc.createTextNode('y'));
|
||||
const right = container.appendChild(doc.createTextNode('b'));
|
||||
normalize(container);
|
||||
check(`${context}/${entry}/separate-runs`, container.childNodes.length === 4 &&
|
||||
left.data === 'a' && right.data === 'b' && comment.parentNode === container &&
|
||||
nested.childNodes.length === 1 && nested.textContent === 'xy');
|
||||
}
|
||||
if (context === 'xml') {
|
||||
const parent = doc.createElement('container');
|
||||
parent.append(doc.createTextNode('a'), doc.createCDATASection(''), doc.createTextNode('b'));
|
||||
parent.normalize();
|
||||
check('xml/cdata-separates-runs', parent.childNodes.length === 3);
|
||||
}
|
||||
}
|
||||
for (const realm of [window, child]) {
|
||||
const doc = realm.document;
|
||||
for (const shadow of [false, true]) {
|
||||
const host = doc.body.appendChild(doc.createElement('div'));
|
||||
const parent = shadow ? host.attachShadow({mode: 'open'}) : host;
|
||||
const first = parent.appendChild(doc.createTextNode('ab'));
|
||||
const second = parent.appendChild(doc.createTextNode('cd'));
|
||||
const selection = realm.getSelection();
|
||||
selection.setBaseAndExtent(second, 2, second, 1);
|
||||
if (shadow) {
|
||||
host.normalize();
|
||||
check(`${realm === window}/shadow-not-traversed`, parent.childNodes.length === 2);
|
||||
}
|
||||
parent.normalize();
|
||||
check(`${realm === window}/selection/${shadow}`,
|
||||
selection.anchorNode === first && selection.anchorOffset === 4 &&
|
||||
selection.focusNode === first && selection.focusOffset === 3);
|
||||
const [composed] = selection.getComposedRanges({shadowRoots: shadow ? [parent] : []});
|
||||
check(`${realm === window}/composed/${shadow}`, composed.startContainer === first &&
|
||||
composed.startOffset === 3 && composed.endContainer === first && composed.endOffset === 4);
|
||||
second.data = 'removed';
|
||||
check(`${realm === window}/selection-stable/${shadow}`, selection.anchorNode === first &&
|
||||
selection.anchorOffset === 4 && selection.focusNode === first && selection.focusOffset === 3);
|
||||
selection.removeAllRanges();
|
||||
host.remove();
|
||||
}
|
||||
}
|
||||
globalThis.__nodeNormalizationFailures = failures;
|
||||
globalThis.__nodeNormalizationCheckCount = checks;
|
||||
})()
|
||||
@@ -0,0 +1,15 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn node_normalization_updates_ranges_iterators_and_selection_across_documents() {
|
||||
let mut vm = new_parsed_test_vm(
|
||||
"https://node-normalization.test/",
|
||||
"<!doctype html><body><iframe id=child></iframe></body>",
|
||||
);
|
||||
vm.eval(include_str!("node_normalization.js")).unwrap();
|
||||
assert_eq!(
|
||||
vm.eval("JSON.stringify(globalThis.__nodeNormalizationFailures)")
|
||||
.unwrap(),
|
||||
"[]"
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user