Bug 47749

Summary: serializeNodesWithNamespaces should be in MarkupAccumulator
Product: WebKit Reporter: Ryosuke Niwa <rniwa>
Component: HTML EditingAssignee: Ryosuke Niwa <rniwa>
Status: RESOLVED FIXED    
Severity: Enhancement CC: abarth, eric, tony
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   
Bug Depends on:    
Bug Blocks: 47748    
Attachments:
Description Flags
cleanup
none
fixed per Adam's & Tony's comments tony: review+

Description Ryosuke Niwa 2010-10-15 16:04:42 PDT
serializeNodesWithNamespaces should be a member function of MarkupAccumulator, and serializeNodes should be a member function of StyledMarkupAccumulator.
Comment 1 Ryosuke Niwa 2010-10-15 17:04:54 PDT
Created attachment 70925 [details]
cleanup
Comment 2 Adam Barth 2010-10-15 17:11:07 PDT
Comment on attachment 70925 [details]
cleanup

View in context: https://bugs.webkit.org/attachment.cgi?id=70925&action=review

I think there are more webkit data structures we can use here to help, but this is clearly an improvement.

> WebCore/editing/MarkupAccumulator.cpp:133
> +    size_t vectorSize = strings.size();
> +    for (size_t i = 0; i < vectorSize; ++i)

No need to make a temporary for strings.size()

> WebCore/editing/MarkupAccumulator.cpp:141
> -String MarkupAccumulator::takeResults()
> +void MarkupAccumulator::concatenateMarkups(Vector<UChar>& out)

concatenateMarkups => concatenateMarkup ?

> WebCore/editing/MarkupAccumulator.h:78
> +        out.reserveInitialCapacity(length());
> +        concatenateMarkups(out);
> +        return String::adopt(out);

Why not String::createUninitialized?
Comment 3 Tony Chang 2010-10-15 17:20:20 PDT
Comment on attachment 70925 [details]
cleanup

View in context: https://bugs.webkit.org/attachment.cgi?id=70925&action=review

> WebCore/editing/MarkupAccumulator.cpp:129
> +size_t MarkupAccumulator::sumOfStringLengths(const Vector<String>& strings) const

This method doesn't need to be part of MarkupAccumulator.  You can make it a static function (outside the class).

> WebCore/editing/MarkupAccumulator.h:114
> +    void serializeNodes(Node*, Node* nodeToSkip, EChildrenOnly, const Namespaces*);

Shouldn't this return a String like your ChangeLog says?

> WebCore/editing/markup.cpp:734
> +    return accumulator.serializeNodes(const_cast<Node*>(node), deleteButtonContainerElement, childrenOnly);

Doesn't serializeNodes return void and we want to return a string?
Comment 4 Ryosuke Niwa 2010-10-15 17:27:25 PDT
(In reply to comment #3)
> > WebCore/editing/MarkupAccumulator.cpp:129
> > +size_t MarkupAccumulator::sumOfStringLengths(const Vector<String>& strings) const
> 
> This method doesn't need to be part of MarkupAccumulator.  You can make it a static function (outside the class).

It's used in StyledMarkupAccumulator so it needs to be a protected member.

> > WebCore/editing/MarkupAccumulator.h:114
> > +    void serializeNodes(Node*, Node* nodeToSkip, EChildrenOnly, const Namespaces*);
> 
> Shouldn't this return a String like your ChangeLog says?

Not this one. This is a helper for the public version of serializeNodes.  Let me rename that to something else so that there's no confusion.

> > WebCore/editing/markup.cpp:734
> > +    return accumulator.serializeNodes(const_cast<Node*>(node), deleteButtonContainerElement, childrenOnly);
> 
> Doesn't serializeNodes return void and we want to return a string?

There's a version of serializeNodes that returns String in MarkupAccumulator.h.  But I agree that it's quite confusing.
Comment 5 Tony Chang 2010-10-15 17:31:18 PDT
(In reply to comment #4)
> (In reply to comment #3)
> > > WebCore/editing/MarkupAccumulator.cpp:129
> > > +size_t MarkupAccumulator::sumOfStringLengths(const Vector<String>& strings) const
> > 
> > This method doesn't need to be part of MarkupAccumulator.  You can make it a static function (outside the class).
> 
> It's used in StyledMarkupAccumulator so it needs to be a protected member.

I see, can it be a static method on the class?
Comment 6 Ryosuke Niwa 2010-10-15 17:32:34 PDT
(In reply to comment #5)
> (In reply to comment #4)
> > (In reply to comment #3)
> > > > WebCore/editing/MarkupAccumulator.cpp:129
> > > > +size_t MarkupAccumulator::sumOfStringLengths(const Vector<String>& strings) const
> > > 
> > > This method doesn't need to be part of MarkupAccumulator.  You can make it a static function (outside the class).
> > 
> > It's used in StyledMarkupAccumulator so it needs to be a protected member.
> 
> I see, can it be a static method on the class?

Mn... if I do that then I have to make a bunch of protected methods public and I don't really want to do that.
Comment 7 Ryosuke Niwa 2010-10-15 17:33:07 PDT
Created attachment 70930 [details]
fixed per Adam's & Tony's comments
Comment 8 Tony Chang 2010-10-15 17:49:21 PDT
Comment on attachment 70930 [details]
fixed per Adam's & Tony's comments

View in context: https://bugs.webkit.org/attachment.cgi?id=70930&action=review

FWIW, you can call protected static methods on a parent class.

Adam asked the following:
> WebCore/editing/MarkupAccumulator.h:78
> +        out.reserveInitialCapacity(length());
> +        concatenateMarkups(out);
> +        return String::adopt(out);

Why not String::createUninitialized?

> WebCore/ChangeLog:22
> +        (WebCore::MarkupAccumulator::length): Added; calls sumOfStringLengths.

Nit: calls totalLength

> WebCore/ChangeLog:26
> +        (WebCore::StyledMarkupAccumulator::takeResults): Calls length, sumOfStringLengths, and concatenateMarkups.

Nit: totalLength, and concatenateMarkup
Comment 9 Adam Barth 2010-10-15 18:04:12 PDT
(In reply to comment #8)
> (From update of attachment 70930 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=70930&action=review
> 
> FWIW, you can call protected static methods on a parent class.
> 
> Adam asked the following:
> > WebCore/editing/MarkupAccumulator.h:78
> > +        out.reserveInitialCapacity(length());
> > +        concatenateMarkups(out);
> > +        return String::adopt(out);
> 
> Why not String::createUninitialized?

rniwa and I talked about that.  I think we're going to do that in a subsequent patch.
Comment 10 Ryosuke Niwa 2010-10-15 20:13:39 PDT
Thanks for the review!

(In reply to comment #6)
> (In reply to comment #5)
> > (In reply to comment #4)
> > > (In reply to comment #3)
> > > > > WebCore/editing/MarkupAccumulator.cpp:129
> > > > > +size_t MarkupAccumulator::sumOfStringLengths(const Vector<String>& strings) const
> > > > 
> > > > This method doesn't need to be part of MarkupAccumulator.  You can make it a static function (outside the class).
> > > 
> > > It's used in StyledMarkupAccumulator so it needs to be a protected member.
> > 
> > I see, can it be a static method on the class?
> 
> Mn... if I do that then I have to make a bunch of protected methods public and I don't really want to do that.

Oops, this comment is totally bogus.  I thought you meant the old serializeNodes (now serializeNodesWithNamespaces).  Will make it static.

(In reply to comment #8)
> Adam asked the following:
> > WebCore/editing/MarkupAccumulator.h:78
> > +        out.reserveInitialCapacity(length());
> > +        concatenateMarkups(out);
> > +        return String::adopt(out);
> 
> Why not String::createUninitialized?

That's because concatenateMarkups is also used in StyledMarkupAccumulator, and I didn't want to create two strings there.  We need to figure out a cleaner way of doing this after this patch is landed.

> > WebCore/ChangeLog:22
> > +        (WebCore::MarkupAccumulator::length): Added; calls sumOfStringLengths.
> 
> Nit: calls totalLength
> 
> > WebCore/ChangeLog:26
> > +        (WebCore::StyledMarkupAccumulator::takeResults): Calls length, sumOfStringLengths, and concatenateMarkups.
> 
> Nit: totalLength, and concatenateMarkup

Thanks for noticing those.  Fixed.
Comment 11 Ryosuke Niwa 2010-10-15 20:16:31 PDT
Committed r69909: <http://trac.webkit.org/changeset/69909>