Skip to content

Commit 8bb3f11

Browse files
committed
Replace is_mathml_annotation_xml_integration_point with attr lookup.
1 parent 25a626c commit 8bb3f11

File tree

6 files changed

+43
-34
lines changed

6 files changed

+43
-34
lines changed

html5ever/examples/noop-tree-builder.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ impl TreeSink for Sink {
6161
self.names.get(target).expect("not an element").expanded()
6262
}
6363

64+
fn elem_any_attr<P>(&self, _target: &usize, _predicate: P) -> bool
65+
where P: FnMut(ExpandedName, &str) -> bool {
66+
false
67+
}
68+
6469
fn create_element(&mut self, name: QualName, _attrs: Vec<Attribute>) -> usize {
6570
let id = self.get_id();
6671
self.names.insert(id, name);

html5ever/examples/print-tree-actions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ impl TreeSink for Sink {
6565
self.names.get(target).expect("not an element").expanded()
6666
}
6767

68+
fn elem_any_attr<P>(&self, _target: &usize, _predicate: P) -> bool
69+
where P: FnMut(ExpandedName, &str) -> bool {
70+
false
71+
}
6872

6973
fn create_element(&mut self, name: QualName, _attrs: Vec<Attribute>) -> usize {
7074
let id = self.get_id();

html5ever/src/tree_builder/actions.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,16 @@ impl<Handle, Sink> TreeBuilderActions<Handle>
953953
TagToken(Tag { kind: StartTag, name: local_name!("svg"), .. }) => return false,
954954
CharacterTokens(..) | NullCharacterToken |
955955
TagToken(Tag { kind: StartTag, .. }) => {
956-
return !self.sink.is_mathml_annotation_xml_integration_point(self.adjusted_current_node())
956+
let integration_point = self.sink.elem_any_attr(
957+
self.adjusted_current_node(),
958+
|attr_name, attr_value| {
959+
attr_name == expanded_name!("", "encoding") && (
960+
attr_value.eq_ignore_ascii_case("text/html") ||
961+
attr_value.eq_ignore_ascii_case("application/xhtml+xml")
962+
)
963+
}
964+
);
965+
return !integration_point
957966
}
958967
_ => {}
959968
};

html5ever/src/tree_builder/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ mod test {
569569
self.rcdom.elem_name(target)
570570
}
571571

572+
fn elem_any_attr<P>(&self, target: &Self::Handle, predicate: P) -> bool
573+
where P: FnMut(ExpandedName, &str) -> bool {
574+
self.rcdom.elem_any_attr(target, predicate)
575+
}
576+
572577
fn create_element(&mut self, name: QualName, attrs: Vec<Attribute>) -> Handle {
573578
self.line_vec.push((name.clone(), self.current_line));
574579
self.rcdom.create_element(name, attrs)
@@ -619,10 +624,6 @@ mod test {
619624
self.rcdom.mark_script_already_started(target);
620625
}
621626

622-
fn is_mathml_annotation_xml_integration_point(&self, handle: &Handle) -> bool {
623-
self.rcdom.is_mathml_annotation_xml_integration_point(handle)
624-
}
625-
626627
fn set_current_line(&mut self, line_number: u64) {
627628
self.current_line = line_number;
628629
}

markup5ever/interface/tree_builder.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ pub trait TreeSink {
7979
/// feel free to `panic!`.
8080
fn elem_name<'a>(&'a self, target: &'a Self::Handle) -> ExpandedName<'a>;
8181

82+
/// Does this element have an attribute matching the given predicate?
83+
///
84+
/// Should never be called on a non-element node;
85+
/// feel free to `panic!`.
86+
fn elem_any_attr<P>(&self, target: &Self::Handle, predicate: P) -> bool
87+
where P: FnMut(ExpandedName, &str) -> bool;
88+
8289
/// Create an element.
8390
///
8491
/// When creating a template element (`name.ns.expanded() == expanded_name!(html "template")`),
@@ -108,7 +115,7 @@ pub trait TreeSink {
108115
system_id: StrTendril);
109116

110117
/// Mark a HTML `<script>` as "already started".
111-
fn mark_script_already_started(&mut self, node: &Self::Handle);
118+
fn mark_script_already_started(&mut self, _node: &Self::Handle) {}
112119

113120
/// Indicate that a node was popped off the stack of open elements.
114121
fn pop(&mut self, _node: &Self::Handle) {}
@@ -157,12 +164,6 @@ pub trait TreeSink {
157164
/// Remove all the children from node and append them to new_parent.
158165
fn reparent_children(&mut self, node: &Self::Handle, new_parent: &Self::Handle);
159166

160-
/// Returns true if the adjusted current node is an HTML integration point
161-
/// and the token is a start tag.
162-
fn is_mathml_annotation_xml_integration_point(&self, _handle: &Self::Handle) -> bool {
163-
false
164-
}
165-
166167
/// Called whenever the line number changes.
167168
fn set_current_line(&mut self, _line_number: u64) {}
168169

markup5ever/rcdom.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//! This is sufficient as a static parse tree, but don't build a
1313
//! web browser using it. :)
1414
15-
use std::ascii::AsciiExt;
1615
use std::cell::{RefCell, Cell};
1716
use std::collections::HashSet;
1817
use std::default::Default;
@@ -33,7 +32,7 @@ use serialize::{Serialize, Serializer};
3332
use serialize::TraversalScope;
3433
use serialize::TraversalScope::{IncludeNode, ChildrenOnly};
3534

36-
pub use self::ElementEnum::{AnnotationXml, Normal, Script, Template};
35+
pub use self::ElementEnum::{Normal, Script, Template};
3736
pub use self::NodeEnum::{Document, Doctype, Text, Comment, Element, PI};
3837

3938
/// The different kinds of elements in the DOM.
@@ -45,11 +44,6 @@ pub enum ElementEnum {
4544
/// A template element and its template contents.
4645
/// https://html.spec.whatwg.org/multipage/#template-contents
4746
Template(Handle),
48-
/// An annotation-xml element in the MathML namespace whose start tag token had an attribute
49-
/// with the name "encoding" whose value was an ASCII case-insensitive match for the string
50-
/// "text/html" or "application/xhtml+xml"
51-
/// https://html.spec.whatwg.org/multipage/embedded-content.html#math:annotation-xml
52-
AnnotationXml(bool),
5347
}
5448

5549
/// The different kinds of nodes in the DOM.
@@ -196,18 +190,20 @@ impl TreeSink for RcDom {
196190
};
197191
}
198192

193+
fn elem_any_attr<P>(&self, target: &Self::Handle, mut predicate: P) -> bool
194+
where P: FnMut(ExpandedName, &str) -> bool {
195+
return match target.node {
196+
Element(_, _, ref attrs) => {
197+
attrs.borrow().iter().any(|a| predicate(a.name.expanded(), &*a.value))
198+
}
199+
_ => panic!("not an element!"),
200+
};
201+
}
202+
199203
fn create_element(&mut self, name: QualName, attrs: Vec<Attribute>) -> Handle {
200204
let info = match name.expanded() {
201205
expanded_name!(html "script") => Script(Cell::new(false)),
202206
expanded_name!(html "template") => Template(new_node(Document)),
203-
expanded_name!(mathml "annotation-xml") => {
204-
AnnotationXml(attrs.iter()
205-
.find(|attr| attr.name.expanded() == expanded_name!("", "encoding"))
206-
.map_or(false, |attr| {
207-
attr.value.eq_ignore_ascii_case("text/html") ||
208-
attr.value.eq_ignore_ascii_case("application/xhtml+xml")
209-
}))
210-
},
211207
_ => Normal,
212208
};
213209
new_node(Element(name, info, RefCell::new(attrs)))
@@ -319,13 +315,6 @@ impl TreeSink for RcDom {
319315
panic!("not a script element!");
320316
}
321317
}
322-
323-
fn is_mathml_annotation_xml_integration_point(&self, handle: &Handle) -> bool {
324-
match handle.node {
325-
Element(_, AnnotationXml(ret), _) => ret,
326-
_ => unreachable!(),
327-
}
328-
}
329318
}
330319

331320
impl Default for RcDom {

0 commit comments

Comments
 (0)