Repository navigation
change type ChildrenOnly to ChildrenOnly(Option<QualName>) - #277
Conversation
|
Shouldn't there be test, to check in case this feature regresses in future? |
|
Sure thing. How would I add tests? By adding to |
|
A fair question. I think we put them in html5ever serializer tests. Actually, if you can call PS. I think it's safe to avoid |
|
|
||
| fn ignore_children(name: &QualName) -> bool { | ||
| let ignore = name.ns == ns!(html) && match name.local { | ||
| local_name!("area") | local_name!("base") | local_name!("basefont") | local_name!("bgsound") | local_name!("br") |
There was a problem hiding this comment.
nit: only indent these lines by four spaces past let.
| } | ||
|
|
||
| fn ignore_children(name: &QualName) -> bool { | ||
| let ignore = name.ns == ns!(html) && match name.local { |
There was a problem hiding this comment.
No need for let ignore = or return ignore.
| } | ||
|
|
||
| impl<Wr: Write> Serializer for HtmlSerializer<Wr> { | ||
| fn start_without_write(&mut self, name: QualName) { |
There was a problem hiding this comment.
I'm not sure I understand why this is an improvement over my suggested solution in servo/servo#14975 (comment) . Am I right that the code that uses the serializer would choose to call start_without_write or start_elem with the parent node, based on whether ChildrenOnly is selected? This seems like it makes the serializer interface more complicated than necessary to me.
|
Sorry for the long delay in providing feedback. Could you explain why you ended up choosing this implementation? There's a gap between my proposed solution and your comment suggesting this alternative one which doesn't make clear why it was necessary. |
|
No worries about the wait! Apologies for the incoming wall of text. I actually don't think this is the right solution/implementation anymore. (Definitely opened this PR prematurely.) I think there are two solutions
pub enum TraversalScope {
IncludeNode,
ChildrenOnly(QualName)
}This way the parent node is accessible from Serializer::new, or anywhere else in html5ever, and can be pushed to the stack as we discussed. But I haven't gotten this to work due to I think having
start_elem(&mut self, name: QualName, attrs:AtterIter, serialize_elem: bool) -> io::Result<()>Instead of calling I do have this implementation working locally. It's different than what we discussed & I'm not sure if it's logically the right thing to do. |
|
@moonlightdrive It sounds like the problem with my solution is that TraversalScope currently derives Copy, and since |
|
I assumed that the trait was derived for a particular reason and that removing it would be undesirable. Cursory google search has now led me to believe that it's standard Rust practice to derive Copy unless there's a reason not to? So I will go ahead and remove Copy and see where that leads me. Thanks @jdm for your response :) |
29ae0a7 to
9c0a2b0
Compare
|
I seem to have broken some wpt tests related to this bit of code. innerHTML of Spec says, when current node is an element
Should this be done in start_elem instead of |
|
@jrozner What are you referring to, exactly? This PR is addressing a problem with serializing fragments; it has nothing to do with parsing. |
|
@jdm An earlier commit we had been working with used the start_without_write methods introduced earlier to deal with serializing out invalid html without fixing it after running this parser. It isn't a parsing issue but an issue with the serializer not being able to handle malformed html which is what we need in our case. We are doing the fixup ourself without running the parser just by referencing the stack. It looks like the original commit that introduced the changes has been force pushed and removed so I'm not sure this PR is necessarily relevant to our needs anymore but we did have a use for the old one. |
|
@jdm Just to further clarify what @jrozner's saying, our flow looks like this: Potentially malformed hmtl input -> html5ever lexer -> process token stream -> serialize with the html5ever serializer We'd like to be able to run the serializer without having to fabricate start tokens. The previous implementation that simply added them to the internal serializer stack without actually writing them to the output stream was exactly what we needed, but I'm not quite seeing how to do it with the new implementation. |
|
Ok, that helps me understand a bit better. This is currently waiting on me to review the most recent changes; the recent lack of activity was waiting on addressing previous review comments. The goal is to address the underlying issue for servo/servo#14975; it's not clear to me if that's the same problem that you're using these changes to solve. |
|
No, not quite the same problem that we were using this change to solve (though @jrozner informs me that we see that issue as well), but the stated rationale:
was exactly what I was searching for when I came across this. If the future of this PR doesn't quite align with our goals, maybe a different one would be needed? Really, some sort of api to manipulate the stack would be ideal for us. Could that work here as well? |
|
I'm open to other changes to address your use case, but a unit test demonstrating your issue would go a long way to clearing up what we're actually discussing. |
|
(in a separate issue, please) |
jdm
left a comment
There was a problem hiding this comment.
These changes look great! Apologies for taking so long to review them!
|
|
||
| impl<Wr: Write> HtmlSerializer<Wr> { | ||
| fn new(writer: Wr, opts: SerializeOpts) -> Self { | ||
| let qname = match &opts.traversal_scope { |
There was a problem hiding this comment.
The & in the match should not be necessary.
| fn new(writer: Wr, opts: SerializeOpts) -> Self { | ||
| let qname = match &opts.traversal_scope { | ||
| &TraversalScope::IncludeNode | &TraversalScope::ChildrenOnly(None) => None, | ||
| &TraversalScope::ChildrenOnly(Some(ref n)) => Some(n.clone()) |
| fn serialize<S>(&self, serializer: &mut S, traversal_scope: TraversalScope) -> io::Result<()> | ||
| where S: Serializer { | ||
| match (traversal_scope, &self.data) { | ||
| match (traversal_scope.clone(), &self.data) { |
There was a problem hiding this comment.
Let's do (&traversal_scope, &self.data) instead.
|
Whoops, I totally missed #277 (comment) . That's an interesting result! The specification we should refer to is https://html.spec.whatwg.org/multipage/parsing.html#serialising-html-fragments ; interestingly, I do not see any equivalent for "If current node is a pre, textarea, or listing element, append a U+000A LINE FEED (LF) character." in that specification. |
|
In fact, further down the specification I see: I think this means we should remove that bit from the implementation entirely! |
|
☔ The latest upstream changes (presumably #293) made this pull request unmergeable. Please resolve the merge conflicts. |
9c0a2b0 to
22f4f4f
Compare
|
Thanks for the review @jdm. I made the changes; is this looking better? |
|
Some automated tests will need to be updated to account for the change: |
|
Otherwise this looks good! |
22f4f4f to
eaa15f2
Compare
| test!(pre_lf_0, "<pre>foo bar</pre>"); | ||
| test!(pre_lf_1, "<pre>\nfoo bar</pre>", "<pre>foo bar</pre>"); | ||
| test!(pre_lf_2, "<pre>\n\nfoo bar</pre>"); | ||
| test!(pre_lf_2, "<pre>foo bar</pre>"); |
There was a problem hiding this comment.
As written, this is identical to pre_lf_0. Instead, we should do test!(pre_lf_2, "<pre>\n\nfoo bar</pre>", "<pre>\n\nfoo bar</pre>"); (and same for the other modified tests).
There was a problem hiding this comment.
oh whoops.. my mistake.. not sure what was going on in my mind :|
Should it actually be test!(pre_lf_2, "<pre>\n\nfoo bar</pre>", "<pre>\nfoo bar</pre>");?
For example, consider the following markup:
<pre> Hello.</pre>When this document is first parsed, the pre element's child text content starts with a single newline character. After a serialize-reparse roundtrip, the pre element's child text content is simply "Hello.".
I'm don't understand what's meant by a serialize roundtrip.. but test!(pre_lf_2, "<pre>\n\nfoo bar</pre>", "<pre>foo bar</pre>"); doesn't seem to be right
There was a problem hiding this comment.
Ah, yes, you're right. I shouldn't have tried to do that from memory :)
eaa15f2 to
015cfb0
Compare
|
@bors-servo: r+ |
|
📌 Commit 015cfb0 has been approved by |
change type ChildrenOnly to ChildrenOnly(Option<QualName>) This is necessary so that these nodes exist on the stack when we serialize the children nodes and may need the parent for context. Addresses servo/servo#14975 --- Opening this PR for feedback/review ~~This does work and solves the above bug. I'm not sure if this is the best implementation, as there's a bit of duplicated code. I thought about adding a boolean flag to `start_elem` that would determine whether the writes should be performed. But then I thought maybe when not writing, it's best not to call a function returning `io::Result<()>`?~~ Also not 100% sure if the xml implementation is the right thing to do Anyway, please let me know of any improvements/changes to make. Thanks :) <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/html5ever/277) <!-- Reviewable:end -->
|
☀️ Test successful - status-travis |
This is necessary so that these nodes exist on the stack when we
serialize the children nodes and may need the parent for context.
Addresses servo/servo#14975
Opening this PR for feedback/review
This does work and solves the above bug. I'm not sure if this is the best implementation, as there's a bit of duplicated code. I thought about adding a boolean flag tostart_elemthat would determine whether the writes should be performed. But then I thought maybe when not writing, it's best not to call a function returningio::Result<()>?Also not 100% sure if the xml implementation is the right thing to do
Anyway, please let me know of any improvements/changes to make. Thanks :)
This change is