Skip to content

change type ChildrenOnly to ChildrenOnly(Option<QualName>) - #277

Merged
bors-servo merged 1 commit into
servo:masterfrom
moonlightdrive:innerhtml-escape
Aug 23, 2017
Merged

bors-servo merged 1 commit into
servo:masterfrom
moonlightdrive:innerhtml-escape

Conversation

@moonlightdrive

@moonlightdrive moonlightdrive commented Jun 15, 2017 •

Copy link
Copy Markdown
Contributor

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 :)


This change is Reviewable

@Ygg01

Ygg01 commented Jun 23, 2017

Copy link
Copy Markdown
Contributor

Shouldn't there be test, to check in case this feature regresses in future?

@moonlightdrive

Copy link
Copy Markdown
Contributor Author

Sure thing. How would I add tests? By adding to html5ever/tests/serializer.rs? And how would I run them? cargo test from html5ever directory?

@Ygg01

Ygg01 commented Jun 27, 2017 •

Copy link
Copy Markdown
Contributor

A fair question. I think we put them in html5ever serializer tests.

Actually, if you can call cargo test -p html5ever, to run only h5e tests. See for examples workspace tests.

PS. I think it's safe to avoid --features "rustc-test/capture" . They only matter when running tests on CI IIUC.

Comment thread html5ever/src/serialize/mod.rs Outdated

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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: only indent these lines by four spaces past let.

Comment thread html5ever/src/serialize/mod.rs Outdated
}

fn ignore_children(name: &QualName) -> bool {
let ignore = name.ns == ns!(html) && match name.local {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for let ignore = or return ignore.

Comment thread html5ever/src/serialize/mod.rs Outdated
}

impl<Wr: Write> Serializer for HtmlSerializer<Wr> {
fn start_without_write(&mut self, name: QualName) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jdm

jdm commented Jul 5, 2017

Copy link
Copy Markdown
Member

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.

@jdm jdm self-assigned this Jul 5, 2017
@moonlightdrive

Copy link
Copy Markdown
Contributor Author

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

  1. Change the TraversalScope type to
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 QualName deriving Copy; QualName struct contains an Option<Prefix> field. Prefix, which does not derive the Copy trait, seems to be defined using the string-cache library. I'm stuck on using this to get Prefix to derive traits.

I think having Prefix derive Copy might be undesirable? My understanding is the purpose of a string interning library is to not have duplicate strings. If that's what's going on here? I don't understand the benefit of defining Prefix through this library.

  1. Change start_elem and end_elem to take a bool that determines whether the node should actually be serialized (popping/pushing the node regardless of the bool's value)
start_elem(&mut self, name: QualName, attrs:AtterIter, serialize_elem: bool) -> io::Result<()>

Instead of calling start_elem/end_elem from servo only when traversalScope == IncludeNode, we would always call start_elem/end_elem and pass true in the IncludeNode case, false otherwise.

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.

@jdm

jdm commented Jul 10, 2017

Copy link
Copy Markdown
Member

@moonlightdrive It sounds like the problem with my solution is that TraversalScope currently derives Copy, and since QualName does not (and cannot) implement Copy, this is an error. In that case, the answer is to remove Copy from the list of derived traits on TraversalScope.

@moonlightdrive

Copy link
Copy Markdown
Contributor Author

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 :)

@moonlightdrive moonlightdrive changed the title extend Serializer trait with additional functions change type ChildrenOnly to ChildrenOnly(Option<QualName>) Jul 23, 2017
@moonlightdrive

Copy link
Copy Markdown
Contributor Author

I seem to have broken some wpt tests related to this bit of code. innerHTML of <pre>\x0A</pre> should serialize to \n but after my change is serializing to '\n\n'. This seems to be the relevant bit of code

Spec says, when current node is an element

If current node is a pre, textarea, or listing element, append a U+000A LINE FEED (LF) character.)

Should this be done in start_elem instead of write_text?

@jdm

jdm commented Jul 25, 2017

Copy link
Copy Markdown
Member

@jrozner What are you referring to, exactly? This PR is addressing a problem with serializing fragments; it has nothing to do with parsing.

@jrozner

jrozner commented Jul 25, 2017 •

Copy link
Copy Markdown

@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.

@jrobsonchase

jrobsonchase commented Jul 25, 2017 •

Copy link
Copy Markdown
Contributor

@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.

@jdm

jdm commented Jul 25, 2017

Copy link
Copy Markdown
Member

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.

@jrobsonchase

jrobsonchase commented Jul 25, 2017 •

Copy link
Copy Markdown
Contributor

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:

This is necessary so that these nodes exist on the stack when we
serialize the children nodes and may need the parent for context.

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?

@jdm

jdm commented Jul 25, 2017

Copy link
Copy Markdown
Member

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.

@jdm

jdm commented Jul 25, 2017

Copy link
Copy Markdown
Member

(in a separate issue, please)

@jdm jdm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look great! Apologies for taking so long to review them!

Comment thread html5ever/src/serialize/mod.rs Outdated

impl<Wr: Write> HtmlSerializer<Wr> {
fn new(writer: Wr, opts: SerializeOpts) -> Self {
let qname = match &opts.traversal_scope {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The & in the match should not be necessary.

Comment thread html5ever/src/serialize/mod.rs Outdated
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())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do Some(tagname(n)) instead.

Comment thread markup5ever/rcdom.rs Outdated
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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do (&traversal_scope, &self.data) instead.

@jdm

jdm commented Aug 10, 2017

Copy link
Copy Markdown
Member

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.

@jdm

jdm commented Aug 10, 2017

Copy link
Copy Markdown
Member

In fact, further down the specification I see:

For historical reasons, this algorithm does not round-trip an initial U+000A LINE FEED (LF) character in pre, textarea, or listing elements, even though (in the first two cases) the markup being round-tripped can be conforming. The HTML parser will drop such a character during parsing, but this algorithm does not serialize an extra U+000A LINE FEED (LF) character.

I think this means we should remove that bit from the implementation entirely!

@bors-servo

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #293) made this pull request unmergeable. Please resolve the merge conflicts.

@moonlightdrive

Copy link
Copy Markdown
Contributor Author

Thanks for the review @jdm. I made the changes; is this looking better?

@jdm

jdm commented Aug 15, 2017

Copy link
Copy Markdown
Member

Some automated tests will need to be updated to account for the change:

...................thread 'listing_lf_2' panicked at 'assertion failed: `(left == right)`

  left: `"<listing>\n\nfoo bar</listing>"`,

 right: `"<listing>\nfoo bar</listing>"`', html5ever/tests/serializer.rs:176:0

note: Run with `RUST_BACKTRACE=1` for a backtrace.

F......thread 'pre_lf_2' panicked at 'assertion failed: `(left == right)`

  left: `"<pre>\n\nfoo bar</pre>"`,

 right: `"<pre>\nfoo bar</pre>"`', html5ever/tests/serializer.rs:168:0

F............thread 'textarea_lf_2' panicked at 'assertion failed: `(left == right)`

  left: `"<textarea>\n\nfoo bar</textarea>"`,

 right: `"<textarea>\nfoo bar</textarea>"`', html5ever/tests/serializer.rs:172:0

F.

failures:

failures:

    listing_lf_2

    pre_lf_2

    textarea_lf_2

@jdm

jdm commented Aug 15, 2017

Copy link
Copy Markdown
Member

Otherwise this looks good!

Comment thread html5ever/tests/serializer.rs Outdated
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>");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, you're right. I shouldn't have tried to do that from memory :)

@jdm

jdm commented Aug 23, 2017

Copy link
Copy Markdown
Member

@bors-servo: r+

@bors-servo

Copy link
Copy Markdown
Contributor

📌 Commit 015cfb0 has been approved by jdm

@bors-servo

Copy link
Copy Markdown
Contributor

⌛ Testing commit 015cfb0 with merge cbe87dc...

bors-servo pushed a commit that referenced this pull request Aug 23, 2017
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 -->
@bors-servo

Copy link
Copy Markdown
Contributor

☀️ Test successful - status-travis
Approved by: jdm
Pushing cbe87dc to master...

@bors-servo
bors-servo merged commit 015cfb0 into servo:master Aug 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants