fix(forms): preserve token values during parser attribute insertion

This commit is contained in:
ldm0
2026-11-20 22:47:49 +08:00
parent 0f3ceecc0f
commit 6183ea062e
3 changed files with 83 additions and 12 deletions
+59 -9
View File
@@ -42,6 +42,12 @@ enum ElementCreationSource {
Parser,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AttributeMutationSource {
Script,
Parser,
}
fn initial_custom_element_state_for_identity(
namespace: &str,
local_name: &str,
@@ -1370,14 +1376,18 @@ impl Element {
attribute_name: &str,
attribute_value: Option<&str>,
previous_input_type: InputType,
source: AttributeMutationSource,
) {
if !attribute_namespace.is_empty() {
return;
}
let is_html_input =
self.namespace() == "http://www.w3.org/1999/xhtml" && self.local_name() == "input";
// Parser tokens define the initial attributes together. Only a script
// type change reflects an existing live value back into the attribute.
if is_html_input
&& attribute_name == "type"
&& source == AttributeMutationSource::Script
&& let Some(value) =
self.input_type_change_value_attribute(previous_input_type, self.input_type())
{
@@ -1451,19 +1461,51 @@ impl Element {
namespace: String,
prefix: Option<String>,
value: String,
) -> bool {
self.set_attribute_with_source(
local_name,
namespace,
prefix,
value,
AttributeMutationSource::Script,
)
}
pub(crate) fn set_attribute_from_parser(
&mut self,
local_name: String,
namespace: String,
prefix: Option<String>,
value: String,
) -> bool {
self.set_attribute_with_source(
local_name,
namespace,
prefix,
value,
AttributeMutationSource::Parser,
)
}
fn set_attribute_with_source(
&mut self,
local_name: String,
namespace: String,
prefix: Option<String>,
value: String,
source: AttributeMutationSource,
) -> bool {
let previous_input_type = self.input_type();
self.synchronize_element_reference_attribute(&namespace, &local_name);
let next_value = value.clone();
if let Some(index) = self
.attributes
.iter()
.position(|attribute| attribute.name_matches(&local_name))
{
// `set_attribute()` models qualified-name updates (`setAttribute()` and clone/parser
// insertion paths). Once an attribute is matched by its existing qualified name, we
// preserve its current namespace/prefix and only update the value. Callers that need
// namespace/local-name replacement semantics must go through `set_attribute_ns()`.
let index = self.attributes.iter().position(|attribute| match source {
AttributeMutationSource::Script => attribute.name_matches(&local_name),
AttributeMutationSource::Parser => {
attribute.namespace() == namespace && attribute.local_name() == local_name
}
});
if let Some(index) = index {
// An existing attribute retains its namespace and qualified name.
if self.attributes[index].value() == value
&& self
.rare_data
@@ -1482,6 +1524,7 @@ impl Element {
attribute_local_name.as_ref(),
Some(&next_value),
previous_input_type,
source,
);
return true;
}
@@ -1499,6 +1542,7 @@ impl Element {
attribute_local_name.as_ref(),
Some(&next_value),
previous_input_type,
source,
);
true
}
@@ -1539,6 +1583,7 @@ impl Element {
attribute_local_name.as_ref(),
Some(&next_value),
previous_input_type,
AttributeMutationSource::Script,
);
return true;
}
@@ -1562,6 +1607,7 @@ impl Element {
attribute_local_name.as_ref(),
Some(&next_value),
previous_input_type,
AttributeMutationSource::Script,
);
true
}
@@ -1619,6 +1665,7 @@ impl Element {
&local_name,
Some(&next_value),
previous_input_type,
AttributeMutationSource::Script,
);
return true;
}
@@ -1641,6 +1688,7 @@ impl Element {
attribute_local_name.as_ref(),
Some(&next_value),
previous_input_type,
AttributeMutationSource::Script,
);
true
}
@@ -1675,6 +1723,7 @@ impl Element {
removed.local_name.as_ref(),
None,
previous_input_type,
AttributeMutationSource::Script,
);
true
}
@@ -1695,6 +1744,7 @@ impl Element {
removed.local_name.as_ref(),
None,
previous_input_type,
AttributeMutationSource::Script,
);
true
}
+11 -2
View File
@@ -874,12 +874,21 @@ fn range_value_setters_use_element_bounds_and_step_authority() {
#[test]
fn parser_input_finalization_preserves_dirty_values_and_skips_other_elements() {
let mut input = Element::new_html("input");
input.set_attribute("type".into(), String::new(), None, "hidden".into());
input.set_attribute("type".into(), String::new(), None, "text".into());
input.set_attribute("value".into(), String::new(), None, "default".into());
input.set_input_value(" edited\nvalue ");
assert!(!input.resanitize_input_value_after_parser_attributes());
assert_eq!(input.input_value(), " edited\nvalue ");
assert_eq!(input.input_value(), " editedvalue ");
assert!(input.input_value_dirty());
assert_eq!(input.attribute("value"), Some("default"));
let mut hidden = Element::new_html("input");
hidden.set_attribute("type".into(), String::new(), None, "hidden".into());
hidden.set_input_value(" edited\nvalue ");
assert!(!hidden.input_value_dirty());
hidden.resanitize_input_value_after_parser_attributes();
assert_eq!(hidden.input_value(), " edited\nvalue ");
assert_eq!(hidden.attribute("value"), Some(" edited\nvalue "));
let mut div = Element::new_html("div");
assert!(!div.resanitize_input_value_after_parser_attributes());
assert!(!div.rare_data.is_materialized());
+13 -1
View File
@@ -198,7 +198,7 @@ impl DomHost {
if !already_exists {
named_index_changed |= attribute.local_name().eq_ignore_ascii_case("id")
|| attribute.local_name().eq_ignore_ascii_case("name");
let attribute_changed = element.set_attribute_ns(
let attribute_changed = element.set_attribute_from_parser(
attribute.local_name().to_owned(),
attribute.namespace().to_owned(),
attribute.prefix().map(str::to_owned),
@@ -365,6 +365,18 @@ mod tests {
);
assert!(!element.input_value_dirty());
assert_eq!(element.attribute("value"), Some(" first\nsecond "));
assert_eq!(
element
.attributes()
.iter()
.map(Attribute::local_name)
.collect::<Vec<_>>(),
if value_first {
vec!["value", "type"]
} else {
vec!["type", "value"]
}
);
}
}
}