Bug 51164

Summary: execCommand('styleWithCSS') is ignored when selection isn't inside a contenteditable area
Product: WebKit Reporter: Ryosuke Niwa <rniwa>
Component: HTML EditingAssignee: Ryosuke Niwa <rniwa>
Status: RESOLVED FIXED    
Severity: Normal CC: abarth, darin, enrica, eric, jparent, justin.garcia, kalman, ojan, rolandsteiner, webkit.review.bot
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   
Attachments:
Description Flags
demo
none
fixes the bug none

Description Ryosuke Niwa 2010-12-15 22:36:55 PST
WebKit ignores document.execCommand('styleWithCSS', false, true/false) when the document's selection isn't inside a contenteditable area. Since styleWithCSS doesn't modify DOM, WebKit should respect calls even when the selection isn't in an editable region.
Comment 1 Ryosuke Niwa 2010-12-15 22:42:42 PST
Created attachment 76735 [details]
demo
Comment 2 Darin Adler 2010-12-16 08:24:16 PST
(In reply to comment #0)
> styleWithCSS doesn't modify DOM

I didn’t know that! I thought that it added style attributes to DOM elements.
Comment 3 Ryosuke Niwa 2010-12-16 10:01:26 PST
(In reply to comment #2)
> (In reply to comment #0)
> > styleWithCSS doesn't modify DOM
> 
> I didn’t know that! I thought that it added style attributes to DOM elements.

Subsequent execCommands DO modify DOM by inserting spans and adding style attributes but styleWithCSS itself doesn't.  It's simply a flag in Editor class:
http://trac.webkit.org/browser/trunk/WebCore/editing/EditorCommand.cpp?rev=73886#L982
http://trac.webkit.org/browser/trunk/WebCore/editing/Editor.h?rev=73886#L168

And it's not unusual for someone to call StyleWithCSS without putting caret inside an editable region.  For example, text editor can call it right up front to enforce the use of CSS.  We just had that bug in browser scope test (it was calling styleWithCSS before setting up selection).  And apparently, we're the only browser that requires caret/selection being inside an editable region for StyleWithCSS.
Comment 4 Darin Adler 2010-12-16 10:14:04 PST
I see. This command simply sets a flag. This should be very easy to fix!
Comment 5 Julie Parent 2010-12-16 10:45:30 PST
There is a little bit of subtlety here ... what happens if a document has 5 separate editable roots, the selection is in none of them, and you call execCommand('styleWithCSS')?  Does it effect all of them?
Comment 6 Julie Parent 2010-12-16 10:47:32 PST
Anyway, making this always enabled looks as easy as changing enabledInRichlyEditableText to enabled in

{ "StyleWithCSS", { executeStyleWithCSS, supported, enabledInRichlyEditableText, stateStyleWithCSS, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },

in WebCore/editing/EditorCommand.cpp
Comment 7 Darin Adler 2010-12-16 10:47:45 PST
(In reply to comment #5)
> There is a little bit of subtlety here ... what happens if a document has 5 separate editable roots, the selection is in none of them, and you call execCommand('styleWithCSS')?  Does it effect all of them?

We don’t have any per-editable-root settings. I presume this sets a per-document flag in WebKit now and should continue to do so. I’d imagine this is how ti works in other browsers as well.
Comment 8 Ryosuke Niwa 2010-12-16 11:02:12 PST
(In reply to comment #6)
> Anyway, making this always enabled looks as easy as changing enabledInRichlyEditableText to enabled in
> 
> { "StyleWithCSS", { executeStyleWithCSS, supported, enabledInRichlyEditableText, stateStyleWithCSS, valueNull, notTextInsertion, doNotAllowExecutionWhenDisabled } },

Indeed.  I'm going to post a patch in a minute.

(In reply to comment #7)
> (In reply to comment #5)
> > There is a little bit of subtlety here ... what happens if a document has 5 separate editable roots, the selection is in none of them, and you call execCommand('styleWithCSS')?  Does it effect all of them?
> 
> We don’t have any per-editable-root settings. I presume this sets a per-document flag in WebKit now and should continue to do so. I’d imagine this is how ti works in other browsers as well.

What Darin said. I think a lot of developers don't realize this because most of editing apps use an iframe + design mode to work-around Firefox's quirk to modify the editing host.
Comment 9 Ryosuke Niwa 2010-12-16 11:24:05 PST
Created attachment 76792 [details]
fixes the bug
Comment 10 Ryosuke Niwa 2010-12-16 11:59:11 PST
Thanks for the review, Darin. Landing now.
Comment 11 Ryosuke Niwa 2010-12-16 12:02:09 PST
Comment on attachment 76792 [details]
fixes the bug

Clearing flags on attachment: 76792

Committed r74204: <http://trac.webkit.org/changeset/74204>
Comment 12 Ryosuke Niwa 2010-12-16 12:02:14 PST
All reviewed patches have been landed.  Closing bug.
Comment 13 WebKit Review Bot 2010-12-16 15:39:52 PST
http://trac.webkit.org/changeset/74204 might have broken Leopard Intel Debug (Tests)