Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "mozilla/dom/Element.h" |
michael@0 | 6 | #include "nsAString.h" |
michael@0 | 7 | #include "nsCOMPtr.h" |
michael@0 | 8 | #include "nsDebug.h" |
michael@0 | 9 | #include "nsError.h" |
michael@0 | 10 | #include "nsHTMLEditUtils.h" |
michael@0 | 11 | #include "nsHTMLEditor.h" |
michael@0 | 12 | #include "nsIContent.h" |
michael@0 | 13 | #include "nsIDOMElement.h" |
michael@0 | 14 | #include "nsIDOMEventTarget.h" |
michael@0 | 15 | #include "nsIDOMHTMLElement.h" |
michael@0 | 16 | #include "nsIDOMNode.h" |
michael@0 | 17 | #include "nsIHTMLEditor.h" |
michael@0 | 18 | #include "nsIHTMLObjectResizer.h" |
michael@0 | 19 | #include "nsIPresShell.h" |
michael@0 | 20 | #include "nsLiteralString.h" |
michael@0 | 21 | #include "nsReadableUtils.h" |
michael@0 | 22 | #include "nsString.h" |
michael@0 | 23 | #include "nscore.h" |
michael@0 | 24 | |
michael@0 | 25 | // Uncomment the following line if you want to disable |
michael@0 | 26 | // table deletion when the only column/row is removed |
michael@0 | 27 | // #define DISABLE_TABLE_DELETION 1 |
michael@0 | 28 | |
michael@0 | 29 | NS_IMETHODIMP |
michael@0 | 30 | nsHTMLEditor::SetInlineTableEditingEnabled(bool aIsEnabled) |
michael@0 | 31 | { |
michael@0 | 32 | mIsInlineTableEditingEnabled = aIsEnabled; |
michael@0 | 33 | return NS_OK; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | NS_IMETHODIMP |
michael@0 | 37 | nsHTMLEditor::GetInlineTableEditingEnabled(bool * aIsEnabled) |
michael@0 | 38 | { |
michael@0 | 39 | *aIsEnabled = mIsInlineTableEditingEnabled; |
michael@0 | 40 | return NS_OK; |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | NS_IMETHODIMP |
michael@0 | 44 | nsHTMLEditor::ShowInlineTableEditingUI(nsIDOMElement * aCell) |
michael@0 | 45 | { |
michael@0 | 46 | NS_ENSURE_ARG_POINTER(aCell); |
michael@0 | 47 | |
michael@0 | 48 | // do nothing if aCell is not a table cell... |
michael@0 | 49 | if (!nsHTMLEditUtils::IsTableCell(aCell)) |
michael@0 | 50 | return NS_OK; |
michael@0 | 51 | |
michael@0 | 52 | if (mInlineEditedCell) { |
michael@0 | 53 | NS_ERROR("call HideInlineTableEditingUI first"); |
michael@0 | 54 | return NS_ERROR_UNEXPECTED; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | // the resizers and the shadow will be anonymous children of the body |
michael@0 | 58 | nsCOMPtr<nsIDOMElement> bodyElement = do_QueryInterface(GetRoot()); |
michael@0 | 59 | NS_ENSURE_TRUE(bodyElement, NS_ERROR_NULL_POINTER); |
michael@0 | 60 | |
michael@0 | 61 | CreateAnonymousElement(NS_LITERAL_STRING("a"), bodyElement, |
michael@0 | 62 | NS_LITERAL_STRING("mozTableAddColumnBefore"), |
michael@0 | 63 | false, getter_AddRefs(mAddColumnBeforeButton)); |
michael@0 | 64 | CreateAnonymousElement(NS_LITERAL_STRING("a"), bodyElement, |
michael@0 | 65 | NS_LITERAL_STRING("mozTableRemoveColumn"), |
michael@0 | 66 | false, getter_AddRefs(mRemoveColumnButton)); |
michael@0 | 67 | CreateAnonymousElement(NS_LITERAL_STRING("a"), bodyElement, |
michael@0 | 68 | NS_LITERAL_STRING("mozTableAddColumnAfter"), |
michael@0 | 69 | false, getter_AddRefs(mAddColumnAfterButton)); |
michael@0 | 70 | |
michael@0 | 71 | CreateAnonymousElement(NS_LITERAL_STRING("a"), bodyElement, |
michael@0 | 72 | NS_LITERAL_STRING("mozTableAddRowBefore"), |
michael@0 | 73 | false, getter_AddRefs(mAddRowBeforeButton)); |
michael@0 | 74 | CreateAnonymousElement(NS_LITERAL_STRING("a"), bodyElement, |
michael@0 | 75 | NS_LITERAL_STRING("mozTableRemoveRow"), |
michael@0 | 76 | false, getter_AddRefs(mRemoveRowButton)); |
michael@0 | 77 | CreateAnonymousElement(NS_LITERAL_STRING("a"), bodyElement, |
michael@0 | 78 | NS_LITERAL_STRING("mozTableAddRowAfter"), |
michael@0 | 79 | false, getter_AddRefs(mAddRowAfterButton)); |
michael@0 | 80 | |
michael@0 | 81 | AddMouseClickListener(mAddColumnBeforeButton); |
michael@0 | 82 | AddMouseClickListener(mRemoveColumnButton); |
michael@0 | 83 | AddMouseClickListener(mAddColumnAfterButton); |
michael@0 | 84 | AddMouseClickListener(mAddRowBeforeButton); |
michael@0 | 85 | AddMouseClickListener(mRemoveRowButton); |
michael@0 | 86 | AddMouseClickListener(mAddRowAfterButton); |
michael@0 | 87 | |
michael@0 | 88 | mInlineEditedCell = aCell; |
michael@0 | 89 | return RefreshInlineTableEditingUI(); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | NS_IMETHODIMP |
michael@0 | 93 | nsHTMLEditor::HideInlineTableEditingUI() |
michael@0 | 94 | { |
michael@0 | 95 | mInlineEditedCell = nullptr; |
michael@0 | 96 | |
michael@0 | 97 | RemoveMouseClickListener(mAddColumnBeforeButton); |
michael@0 | 98 | RemoveMouseClickListener(mRemoveColumnButton); |
michael@0 | 99 | RemoveMouseClickListener(mAddColumnAfterButton); |
michael@0 | 100 | RemoveMouseClickListener(mAddRowBeforeButton); |
michael@0 | 101 | RemoveMouseClickListener(mRemoveRowButton); |
michael@0 | 102 | RemoveMouseClickListener(mAddRowAfterButton); |
michael@0 | 103 | |
michael@0 | 104 | // get the presshell's document observer interface. |
michael@0 | 105 | nsCOMPtr<nsIPresShell> ps = GetPresShell(); |
michael@0 | 106 | // We allow the pres shell to be null; when it is, we presume there |
michael@0 | 107 | // are no document observers to notify, but we still want to |
michael@0 | 108 | // UnbindFromTree. |
michael@0 | 109 | |
michael@0 | 110 | // get the root content node. |
michael@0 | 111 | nsCOMPtr<nsIContent> bodyContent = GetRoot(); |
michael@0 | 112 | NS_ENSURE_TRUE(bodyContent, NS_ERROR_FAILURE); |
michael@0 | 113 | |
michael@0 | 114 | DeleteRefToAnonymousNode(mAddColumnBeforeButton, bodyContent, ps); |
michael@0 | 115 | mAddColumnBeforeButton = nullptr; |
michael@0 | 116 | DeleteRefToAnonymousNode(mRemoveColumnButton, bodyContent, ps); |
michael@0 | 117 | mRemoveColumnButton = nullptr; |
michael@0 | 118 | DeleteRefToAnonymousNode(mAddColumnAfterButton, bodyContent, ps); |
michael@0 | 119 | mAddColumnAfterButton = nullptr; |
michael@0 | 120 | DeleteRefToAnonymousNode(mAddRowBeforeButton, bodyContent, ps); |
michael@0 | 121 | mAddRowBeforeButton = nullptr; |
michael@0 | 122 | DeleteRefToAnonymousNode(mRemoveRowButton, bodyContent, ps); |
michael@0 | 123 | mRemoveRowButton = nullptr; |
michael@0 | 124 | DeleteRefToAnonymousNode(mAddRowAfterButton, bodyContent, ps); |
michael@0 | 125 | mAddRowAfterButton = nullptr; |
michael@0 | 126 | |
michael@0 | 127 | return NS_OK; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | NS_IMETHODIMP |
michael@0 | 131 | nsHTMLEditor::DoInlineTableEditingAction(nsIDOMElement * aElement) |
michael@0 | 132 | { |
michael@0 | 133 | NS_ENSURE_ARG_POINTER(aElement); |
michael@0 | 134 | bool anonElement = false; |
michael@0 | 135 | if (aElement && |
michael@0 | 136 | NS_SUCCEEDED(aElement->HasAttribute(NS_LITERAL_STRING("_moz_anonclass"), &anonElement)) && |
michael@0 | 137 | anonElement) { |
michael@0 | 138 | nsAutoString anonclass; |
michael@0 | 139 | nsresult res = aElement->GetAttribute(NS_LITERAL_STRING("_moz_anonclass"), anonclass); |
michael@0 | 140 | NS_ENSURE_SUCCESS(res, res); |
michael@0 | 141 | |
michael@0 | 142 | if (!StringBeginsWith(anonclass, NS_LITERAL_STRING("mozTable"))) |
michael@0 | 143 | return NS_OK; |
michael@0 | 144 | |
michael@0 | 145 | nsCOMPtr<nsIDOMNode> tableNode = GetEnclosingTable(mInlineEditedCell); |
michael@0 | 146 | nsCOMPtr<nsIDOMElement> tableElement = do_QueryInterface(tableNode); |
michael@0 | 147 | int32_t rowCount, colCount; |
michael@0 | 148 | res = GetTableSize(tableElement, &rowCount, &colCount); |
michael@0 | 149 | NS_ENSURE_SUCCESS(res, res); |
michael@0 | 150 | |
michael@0 | 151 | bool hideUI = false; |
michael@0 | 152 | bool hideResizersWithInlineTableUI = (mResizedObject == tableElement); |
michael@0 | 153 | |
michael@0 | 154 | if (anonclass.EqualsLiteral("mozTableAddColumnBefore")) |
michael@0 | 155 | InsertTableColumn(1, false); |
michael@0 | 156 | else if (anonclass.EqualsLiteral("mozTableAddColumnAfter")) |
michael@0 | 157 | InsertTableColumn(1, true); |
michael@0 | 158 | else if (anonclass.EqualsLiteral("mozTableAddRowBefore")) |
michael@0 | 159 | InsertTableRow(1, false); |
michael@0 | 160 | else if (anonclass.EqualsLiteral("mozTableAddRowAfter")) |
michael@0 | 161 | InsertTableRow(1, true); |
michael@0 | 162 | else if (anonclass.EqualsLiteral("mozTableRemoveColumn")) { |
michael@0 | 163 | DeleteTableColumn(1); |
michael@0 | 164 | #ifndef DISABLE_TABLE_DELETION |
michael@0 | 165 | hideUI = (colCount == 1); |
michael@0 | 166 | #endif |
michael@0 | 167 | } |
michael@0 | 168 | else if (anonclass.EqualsLiteral("mozTableRemoveRow")) { |
michael@0 | 169 | DeleteTableRow(1); |
michael@0 | 170 | #ifndef DISABLE_TABLE_DELETION |
michael@0 | 171 | hideUI = (rowCount == 1); |
michael@0 | 172 | #endif |
michael@0 | 173 | } |
michael@0 | 174 | else |
michael@0 | 175 | return NS_OK; |
michael@0 | 176 | |
michael@0 | 177 | if (hideUI) { |
michael@0 | 178 | HideInlineTableEditingUI(); |
michael@0 | 179 | if (hideResizersWithInlineTableUI) |
michael@0 | 180 | HideResizers(); |
michael@0 | 181 | } |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | return NS_OK; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | void |
michael@0 | 188 | nsHTMLEditor::AddMouseClickListener(nsIDOMElement * aElement) |
michael@0 | 189 | { |
michael@0 | 190 | nsCOMPtr<nsIDOMEventTarget> evtTarget(do_QueryInterface(aElement)); |
michael@0 | 191 | if (evtTarget) { |
michael@0 | 192 | evtTarget->AddEventListener(NS_LITERAL_STRING("click"), |
michael@0 | 193 | mEventListener, true); |
michael@0 | 194 | } |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | void |
michael@0 | 198 | nsHTMLEditor::RemoveMouseClickListener(nsIDOMElement * aElement) |
michael@0 | 199 | { |
michael@0 | 200 | nsCOMPtr<nsIDOMEventTarget> evtTarget(do_QueryInterface(aElement)); |
michael@0 | 201 | if (evtTarget) { |
michael@0 | 202 | evtTarget->RemoveEventListener(NS_LITERAL_STRING("click"), |
michael@0 | 203 | mEventListener, true); |
michael@0 | 204 | } |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | NS_IMETHODIMP |
michael@0 | 208 | nsHTMLEditor::RefreshInlineTableEditingUI() |
michael@0 | 209 | { |
michael@0 | 210 | nsCOMPtr<nsIDOMHTMLElement> htmlElement = do_QueryInterface(mInlineEditedCell); |
michael@0 | 211 | if (!htmlElement) { |
michael@0 | 212 | return NS_ERROR_NULL_POINTER; |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | int32_t xCell, yCell, wCell, hCell; |
michael@0 | 216 | GetElementOrigin(mInlineEditedCell, xCell, yCell); |
michael@0 | 217 | |
michael@0 | 218 | nsresult res = htmlElement->GetOffsetWidth(&wCell); |
michael@0 | 219 | NS_ENSURE_SUCCESS(res, res); |
michael@0 | 220 | res = htmlElement->GetOffsetHeight(&hCell); |
michael@0 | 221 | NS_ENSURE_SUCCESS(res, res); |
michael@0 | 222 | |
michael@0 | 223 | int32_t xHoriz = xCell + wCell/2; |
michael@0 | 224 | int32_t yVert = yCell + hCell/2; |
michael@0 | 225 | |
michael@0 | 226 | nsCOMPtr<nsIDOMNode> tableNode = GetEnclosingTable(mInlineEditedCell); |
michael@0 | 227 | nsCOMPtr<nsIDOMElement> tableElement = do_QueryInterface(tableNode); |
michael@0 | 228 | int32_t rowCount, colCount; |
michael@0 | 229 | res = GetTableSize(tableElement, &rowCount, &colCount); |
michael@0 | 230 | NS_ENSURE_SUCCESS(res, res); |
michael@0 | 231 | |
michael@0 | 232 | SetAnonymousElementPosition(xHoriz-10, yCell-7, mAddColumnBeforeButton); |
michael@0 | 233 | #ifdef DISABLE_TABLE_DELETION |
michael@0 | 234 | NS_NAMED_LITERAL_STRING(classStr, "class"); |
michael@0 | 235 | |
michael@0 | 236 | if (colCount== 1) { |
michael@0 | 237 | mRemoveColumnButton->SetAttribute(classStr, |
michael@0 | 238 | NS_LITERAL_STRING("hidden")); |
michael@0 | 239 | } |
michael@0 | 240 | else { |
michael@0 | 241 | bool hasClass = false; |
michael@0 | 242 | res = mRemoveColumnButton->HasAttribute(classStr, &hasClass); |
michael@0 | 243 | if (NS_SUCCEEDED(res) && hasClass) |
michael@0 | 244 | mRemoveColumnButton->RemoveAttribute(classStr); |
michael@0 | 245 | #endif |
michael@0 | 246 | SetAnonymousElementPosition(xHoriz-4, yCell-7, mRemoveColumnButton); |
michael@0 | 247 | #ifdef DISABLE_TABLE_DELETION |
michael@0 | 248 | } |
michael@0 | 249 | #endif |
michael@0 | 250 | SetAnonymousElementPosition(xHoriz+6, yCell-7, mAddColumnAfterButton); |
michael@0 | 251 | |
michael@0 | 252 | SetAnonymousElementPosition(xCell-7, yVert-10, mAddRowBeforeButton); |
michael@0 | 253 | #ifdef DISABLE_TABLE_DELETION |
michael@0 | 254 | if (rowCount== 1) { |
michael@0 | 255 | mRemoveRowButton->SetAttribute(classStr, |
michael@0 | 256 | NS_LITERAL_STRING("hidden")); |
michael@0 | 257 | } |
michael@0 | 258 | else { |
michael@0 | 259 | bool hasClass = false; |
michael@0 | 260 | res = mRemoveRowButton->HasAttribute(classStr, &hasClass); |
michael@0 | 261 | if (NS_SUCCEEDED(res) && hasClass) |
michael@0 | 262 | mRemoveRowButton->RemoveAttribute(classStr); |
michael@0 | 263 | #endif |
michael@0 | 264 | SetAnonymousElementPosition(xCell-7, yVert-4, mRemoveRowButton); |
michael@0 | 265 | #ifdef DISABLE_TABLE_DELETION |
michael@0 | 266 | } |
michael@0 | 267 | #endif |
michael@0 | 268 | SetAnonymousElementPosition(xCell-7, yVert+6, mAddRowAfterButton); |
michael@0 | 269 | |
michael@0 | 270 | return NS_OK; |
michael@0 | 271 | } |
michael@0 | 272 |