Skip to content

Commit

Permalink
Auto merge of #17501 - jyc:Text-children_changed, r=<try>
Browse files Browse the repository at this point in the history
Have CharacterData call children_changed on its parent when data is set.

**Can't run WPT on my computer, so pushing here to run tests 馃槩**

Have CharacterData.SetData call children_changed on its parent when
data is set (if it is a Text node) so that HTMLStyleElement parents can
re-parse. Add variant ChildrenMutation::Text for it to use as the
mutation.

This fixes an issue where an empty <style> element's data is set but the
style is not updated. An HTMLStyleElement parent re-parses in its
children_changed implementation.

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes are part of a series to fix #17182 (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is鈥俒<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/17501)
<!-- Reviewable:end -->
  • Loading branch information
bors-servo committed Jun 24, 2017
2 parents bc3ec0c + 6c3fbac commit b0e07ef
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
18 changes: 15 additions & 3 deletions components/script/dom/characterdata.rs
Expand Up @@ -6,6 +6,7 @@

use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::CharacterDataBinding::CharacterDataMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeBinding::NodeMethods;
use dom::bindings::codegen::Bindings::ProcessingInstructionBinding::ProcessingInstructionMethods;
use dom::bindings::codegen::InheritTypes::{CharacterDataTypeId, NodeTypeId};
use dom::bindings::codegen::UnionTypes::NodeOrString;
Expand All @@ -16,9 +17,10 @@ use dom::bindings::str::DOMString;
use dom::comment::Comment;
use dom::document::Document;
use dom::element::Element;
use dom::node::{Node, NodeDamage};
use dom::node::{ChildrenMutation, Node, NodeDamage};
use dom::processinginstruction::ProcessingInstruction;
use dom::text::Text;
use dom::virtualmethods::vtable_for;
use dom_struct::dom_struct;
use servo_config::opts;
use std::cell::Ref;
Expand Down Expand Up @@ -83,8 +85,18 @@ impl CharacterDataMethods for CharacterData {
let new_length = data.encode_utf16().count() as u32;
*self.data.borrow_mut() = data;
self.content_changed();
let node = self.upcast::<Node>();
node.ranges().replace_code_units(node, 0, old_length, new_length);

// If this is a Text node, we might need to re-parse (say, if our parent
// is a <style> element.) We don't need to if this is a Comment or
// ProcessingInstruction.
if let Some(_) = self.downcast::<Text>() {
let node = self.upcast::<Node>();
node.ranges().replace_code_units(node, 0, old_length, new_length);
if let Some(parent_node) = node.GetParentNode() {
let mutation = ChildrenMutation::Text;
vtable_for(&parent_node).children_changed(&mutation);
}
}
}

// https://dom.spec.whatwg.org/#dom-characterdata-length
Expand Down
10 changes: 10 additions & 0 deletions components/script/dom/node.rs
Expand Up @@ -2530,6 +2530,11 @@ pub enum ChildrenMutation<'a> {
next: Option<&'a Node>,
},
ReplaceAll { removed: &'a [&'a Node], added: &'a [&'a Node] },
/// Mutation for when a Text node's data is modified.
/// This doesn't change the structure of the list, which is what the other
/// variants' fields are stored for at the moment, so this can just have no
/// arguments.
Text,
}

impl<'a> ChildrenMutation<'a> {
Expand Down Expand Up @@ -2581,13 +2586,17 @@ impl<'a> ChildrenMutation<'a> {
}

/// Get the child that follows the added or removed children.
/// Currently only used when this mutation might force us to
/// restyle later children (see HAS_SLOW_SELECTOR_LATER_SIBLINGS and
/// Element's implementation of VirtualMethods::children_changed).
pub fn next_child(&self) -> Option<&Node> {
match *self {
ChildrenMutation::Append { .. } => None,
ChildrenMutation::Insert { next, .. } => Some(next),
ChildrenMutation::Prepend { next, .. } => Some(next),
ChildrenMutation::Replace { next, .. } => next,
ChildrenMutation::ReplaceAll { .. } => None,
ChildrenMutation::Text => None,
}
}

Expand Down Expand Up @@ -2625,6 +2634,7 @@ impl<'a> ChildrenMutation<'a> {

ChildrenMutation::Replace { prev: None, next: None, .. } => unreachable!(),
ChildrenMutation::ReplaceAll { .. } => None,
ChildrenMutation::Text => None,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions components/script/dom/nodelist.rs
Expand Up @@ -279,6 +279,7 @@ impl ChildrenList {
self.last_index.set(middle as u32);
}
},
ChildrenMutation::Text => {},
}
}

Expand Down

0 comments on commit b0e07ef

Please sign in to comment.