OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 10 matching lines...) Expand all Loading... |
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "config.h" | 26 #include "config.h" |
27 #if ENABLE(DIALOG_ELEMENT) | 27 #if ENABLE(DIALOG_ELEMENT) |
28 #include "HTMLDialogElement.h" | 28 #include "HTMLDialogElement.h" |
29 | 29 |
30 #include "ExceptionCode.h" | 30 #include "ExceptionCode.h" |
31 #include "RenderDialog.h" | 31 #include "FrameView.h" |
| 32 #include "RenderBlock.h" |
| 33 #include "RenderStyle.h" |
| 34 #include "StyleResolver.h" |
32 | 35 |
33 namespace WebCore { | 36 namespace WebCore { |
34 | 37 |
35 using namespace HTMLNames; | 38 using namespace HTMLNames; |
36 | 39 |
37 HTMLDialogElement::HTMLDialogElement(const QualifiedName& tagName, Document* doc
ument) | 40 HTMLDialogElement::HTMLDialogElement(const QualifiedName& tagName, Document* doc
ument) |
38 : HTMLElement(tagName, document) | 41 : HTMLElement(tagName, document) |
| 42 , m_topIsValid(false) |
| 43 , m_top(0) |
39 { | 44 { |
40 ASSERT(hasTagName(dialogTag)); | 45 ASSERT(hasTagName(dialogTag)); |
| 46 setHasCustomStyleCallbacks(); |
41 ScriptWrappable::init(this); | 47 ScriptWrappable::init(this); |
42 } | 48 } |
43 | 49 |
44 PassRefPtr<HTMLDialogElement> HTMLDialogElement::create(const QualifiedName& tag
Name, Document* document) | 50 PassRefPtr<HTMLDialogElement> HTMLDialogElement::create(const QualifiedName& tag
Name, Document* document) |
45 { | 51 { |
46 return adoptRef(new HTMLDialogElement(tagName, document)); | 52 return adoptRef(new HTMLDialogElement(tagName, document)); |
47 } | 53 } |
48 | 54 |
49 void HTMLDialogElement::close(ExceptionCode& ec) | 55 void HTMLDialogElement::close(ExceptionCode& ec) |
50 { | 56 { |
51 if (!fastHasAttribute(openAttr)) { | 57 if (!fastHasAttribute(openAttr)) { |
52 ec = INVALID_STATE_ERR; | 58 ec = INVALID_STATE_ERR; |
53 return; | 59 return; |
54 } | 60 } |
55 setBooleanAttribute(openAttr, false); | 61 setBooleanAttribute(openAttr, false); |
56 document()->removeFromTopLayer(this); | 62 document()->removeFromTopLayer(this); |
| 63 m_topIsValid = false; |
| 64 } |
| 65 |
| 66 static bool needsCenteredPositioning(const RenderStyle* style) |
| 67 { |
| 68 return style->position() == AbsolutePosition && style->hasAutoTopAndBottom()
; |
| 69 } |
| 70 |
| 71 PassRefPtr<RenderStyle> HTMLDialogElement::customStyleForRenderer() |
| 72 { |
| 73 RefPtr<RenderStyle> originalStyle = document()->styleResolver()->styleForEle
ment(this); |
| 74 RefPtr<RenderStyle> style = RenderStyle::clone(originalStyle.get()); |
| 75 |
| 76 // Override top to remain centered after style recalcs. |
| 77 if (needsCenteredPositioning(style.get()) && m_topIsValid) |
| 78 style->setTop(Length(m_top.toInt(), WebCore::Fixed)); |
| 79 |
| 80 return style.release(); |
| 81 } |
| 82 |
| 83 void HTMLDialogElement::positionAndReattach() |
| 84 { |
| 85 // Layout because we need to know our ancestors' positions and our own heigh
t. |
| 86 document()->updateLayoutIgnorePendingStylesheets(); |
| 87 |
| 88 RenderBox* box = renderBox(); |
| 89 if (!box || !needsCenteredPositioning(box->style())) |
| 90 return; |
| 91 |
| 92 // Set up dialog's position to be safe-centered in the viewport. |
| 93 // FIXME: Figure out what to do in vertical writing mode. |
| 94 FrameView* frameView = document()->view(); |
| 95 int scrollTop = frameView->scrollOffset().height(); |
| 96 FloatPoint absolutePoint(0, scrollTop); |
| 97 int visibleHeight = frameView->visibleContentRect(ScrollableArea::IncludeScr
ollbars).height(); |
| 98 if (box->height() < visibleHeight) |
| 99 absolutePoint.move(0, (visibleHeight - box->height()) / 2); |
| 100 FloatPoint localPoint = box->containingBlock()->absoluteToLocal(absolutePoin
t); |
| 101 |
| 102 m_top = localPoint.y(); |
| 103 m_topIsValid = true; |
| 104 |
| 105 // FIXME: It's inefficient to reattach here. We could do better by mutating
style directly and forcing another layout. |
| 106 reattach(); |
57 } | 107 } |
58 | 108 |
59 void HTMLDialogElement::show() | 109 void HTMLDialogElement::show() |
60 { | 110 { |
61 if (fastHasAttribute(openAttr)) | 111 if (fastHasAttribute(openAttr)) |
62 return; | 112 return; |
63 setBooleanAttribute(openAttr, true); | 113 setBooleanAttribute(openAttr, true); |
| 114 positionAndReattach(); |
64 } | 115 } |
65 | 116 |
66 void HTMLDialogElement::showModal(ExceptionCode& ec) | 117 void HTMLDialogElement::showModal(ExceptionCode& ec) |
67 { | 118 { |
68 if (fastHasAttribute(openAttr) || !inDocument()) { | 119 if (fastHasAttribute(openAttr) || !inDocument()) { |
69 ec = INVALID_STATE_ERR; | 120 ec = INVALID_STATE_ERR; |
70 return; | 121 return; |
71 } | 122 } |
| 123 document()->addToTopLayer(this); |
72 setBooleanAttribute(openAttr, true); | 124 setBooleanAttribute(openAttr, true); |
73 document()->addToTopLayer(this); | 125 positionAndReattach(); |
74 } | 126 } |
75 | 127 |
76 bool HTMLDialogElement::isPresentationAttribute(const QualifiedName& name) const | 128 bool HTMLDialogElement::isPresentationAttribute(const QualifiedName& name) const |
77 { | 129 { |
78 // FIXME: Workaround for <https://bugs.webkit.org/show_bug.cgi?id=91058>: mo
difying an attribute for which there is an attribute selector | 130 // FIXME: Workaround for <https://bugs.webkit.org/show_bug.cgi?id=91058>: mo
difying an attribute for which there is an attribute selector |
79 // in html.css sometimes does not trigger a style recalc. | 131 // in html.css sometimes does not trigger a style recalc. |
80 if (name == openAttr) | 132 if (name == openAttr) |
81 return true; | 133 return true; |
82 | 134 |
83 return HTMLElement::isPresentationAttribute(name); | 135 return HTMLElement::isPresentationAttribute(name); |
84 } | 136 } |
85 | 137 |
86 RenderObject* HTMLDialogElement::createRenderer(RenderArena* arena, RenderStyle*
) | |
87 { | |
88 return new (arena) RenderDialog(this); | |
89 } | |
90 | |
91 } | 138 } |
92 | 139 |
93 #endif | 140 #endif |
OLD | NEW |