DOMEditor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "config.h"
  31. #if ENABLE(INSPECTOR)
  32. #include "DOMEditor.h"
  33. #include "DOMPatchSupport.h"
  34. #include "Document.h"
  35. #include "Element.h"
  36. #include "ExceptionCode.h"
  37. #include "InspectorHistory.h"
  38. #include "Node.h"
  39. #include "Text.h"
  40. #include "markup.h"
  41. #include <wtf/RefPtr.h>
  42. using namespace std;
  43. namespace WebCore {
  44. class DOMEditor::RemoveChildAction : public InspectorHistory::Action {
  45. WTF_MAKE_NONCOPYABLE(RemoveChildAction);
  46. public:
  47. RemoveChildAction(Node* parentNode, Node* node)
  48. : InspectorHistory::Action("RemoveChild")
  49. , m_parentNode(parentNode)
  50. , m_node(node)
  51. {
  52. }
  53. virtual bool perform(ExceptionCode& ec)
  54. {
  55. m_anchorNode = m_node->nextSibling();
  56. return redo(ec);
  57. }
  58. virtual bool undo(ExceptionCode& ec)
  59. {
  60. return m_parentNode->insertBefore(m_node.get(), m_anchorNode.get(), ec);
  61. }
  62. virtual bool redo(ExceptionCode& ec)
  63. {
  64. return m_parentNode->removeChild(m_node.get(), ec);
  65. }
  66. private:
  67. RefPtr<Node> m_parentNode;
  68. RefPtr<Node> m_node;
  69. RefPtr<Node> m_anchorNode;
  70. };
  71. class DOMEditor::InsertBeforeAction : public InspectorHistory::Action {
  72. WTF_MAKE_NONCOPYABLE(InsertBeforeAction);
  73. public:
  74. InsertBeforeAction(Node* parentNode, PassRefPtr<Node> node, Node* anchorNode)
  75. : InspectorHistory::Action("InsertBefore")
  76. , m_parentNode(parentNode)
  77. , m_node(node)
  78. , m_anchorNode(anchorNode)
  79. {
  80. }
  81. virtual bool perform(ExceptionCode& ec)
  82. {
  83. if (m_node->parentNode()) {
  84. m_removeChildAction = adoptPtr(new RemoveChildAction(m_node->parentNode(), m_node.get()));
  85. if (!m_removeChildAction->perform(ec))
  86. return false;
  87. }
  88. return m_parentNode->insertBefore(m_node.get(), m_anchorNode.get(), ec);
  89. }
  90. virtual bool undo(ExceptionCode& ec)
  91. {
  92. if (!m_parentNode->removeChild(m_node.get(), ec))
  93. return false;
  94. if (m_removeChildAction)
  95. return m_removeChildAction->undo(ec);
  96. return true;
  97. }
  98. virtual bool redo(ExceptionCode& ec)
  99. {
  100. if (m_removeChildAction && !m_removeChildAction->redo(ec))
  101. return false;
  102. return m_parentNode->insertBefore(m_node.get(), m_anchorNode.get(), ec);
  103. }
  104. private:
  105. RefPtr<Node> m_parentNode;
  106. RefPtr<Node> m_node;
  107. RefPtr<Node> m_anchorNode;
  108. OwnPtr<RemoveChildAction> m_removeChildAction;
  109. };
  110. class DOMEditor::RemoveAttributeAction : public InspectorHistory::Action {
  111. WTF_MAKE_NONCOPYABLE(RemoveAttributeAction);
  112. public:
  113. RemoveAttributeAction(Element* element, const String& name)
  114. : InspectorHistory::Action("RemoveAttribute")
  115. , m_element(element)
  116. , m_name(name)
  117. {
  118. }
  119. virtual bool perform(ExceptionCode& ec)
  120. {
  121. m_value = m_element->getAttribute(m_name);
  122. return redo(ec);
  123. }
  124. virtual bool undo(ExceptionCode& ec)
  125. {
  126. m_element->setAttribute(m_name, m_value, ec);
  127. return true;
  128. }
  129. virtual bool redo(ExceptionCode&)
  130. {
  131. m_element->removeAttribute(m_name);
  132. return true;
  133. }
  134. private:
  135. RefPtr<Element> m_element;
  136. String m_name;
  137. String m_value;
  138. };
  139. class DOMEditor::SetAttributeAction : public InspectorHistory::Action {
  140. WTF_MAKE_NONCOPYABLE(SetAttributeAction);
  141. public:
  142. SetAttributeAction(Element* element, const String& name, const String& value)
  143. : InspectorHistory::Action("SetAttribute")
  144. , m_element(element)
  145. , m_name(name)
  146. , m_value(value)
  147. , m_hadAttribute(false)
  148. {
  149. }
  150. virtual bool perform(ExceptionCode& ec)
  151. {
  152. m_hadAttribute = m_element->hasAttribute(m_name);
  153. if (m_hadAttribute)
  154. m_oldValue = m_element->getAttribute(m_name);
  155. return redo(ec);
  156. }
  157. virtual bool undo(ExceptionCode& ec)
  158. {
  159. if (m_hadAttribute)
  160. m_element->setAttribute(m_name, m_oldValue, ec);
  161. else
  162. m_element->removeAttribute(m_name);
  163. return true;
  164. }
  165. virtual bool redo(ExceptionCode& ec)
  166. {
  167. m_element->setAttribute(m_name, m_value, ec);
  168. return true;
  169. }
  170. private:
  171. RefPtr<Element> m_element;
  172. String m_name;
  173. String m_value;
  174. bool m_hadAttribute;
  175. String m_oldValue;
  176. };
  177. class DOMEditor::SetOuterHTMLAction : public InspectorHistory::Action {
  178. WTF_MAKE_NONCOPYABLE(SetOuterHTMLAction);
  179. public:
  180. SetOuterHTMLAction(Node* node, const String& html)
  181. : InspectorHistory::Action("SetOuterHTML")
  182. , m_node(node)
  183. , m_nextSibling(node->nextSibling())
  184. , m_html(html)
  185. , m_newNode(0)
  186. , m_history(adoptPtr(new InspectorHistory()))
  187. , m_domEditor(adoptPtr(new DOMEditor(m_history.get())))
  188. {
  189. }
  190. virtual bool perform(ExceptionCode& ec)
  191. {
  192. m_oldHTML = createMarkup(m_node.get());
  193. DOMPatchSupport domPatchSupport(m_domEditor.get(), m_node->ownerDocument());
  194. m_newNode = domPatchSupport.patchNode(m_node.get(), m_html, ec);
  195. return !ec;
  196. }
  197. virtual bool undo(ExceptionCode& ec)
  198. {
  199. return m_history->undo(ec);
  200. }
  201. virtual bool redo(ExceptionCode& ec)
  202. {
  203. return m_history->redo(ec);
  204. }
  205. Node* newNode()
  206. {
  207. return m_newNode;
  208. }
  209. private:
  210. RefPtr<Node> m_node;
  211. RefPtr<Node> m_nextSibling;
  212. String m_html;
  213. String m_oldHTML;
  214. Node* m_newNode;
  215. OwnPtr<InspectorHistory> m_history;
  216. OwnPtr<DOMEditor> m_domEditor;
  217. };
  218. class DOMEditor::ReplaceWholeTextAction : public InspectorHistory::Action {
  219. WTF_MAKE_NONCOPYABLE(ReplaceWholeTextAction);
  220. public:
  221. ReplaceWholeTextAction(Text* textNode, const String& text)
  222. : InspectorHistory::Action("ReplaceWholeText")
  223. , m_textNode(textNode)
  224. , m_text(text)
  225. {
  226. }
  227. virtual bool perform(ExceptionCode& ec)
  228. {
  229. m_oldText = m_textNode->wholeText();
  230. return redo(ec);
  231. }
  232. virtual bool undo(ExceptionCode& ec)
  233. {
  234. m_textNode->replaceWholeText(m_oldText, ec);
  235. return true;
  236. }
  237. virtual bool redo(ExceptionCode& ec)
  238. {
  239. m_textNode->replaceWholeText(m_text, ec);
  240. return true;
  241. }
  242. private:
  243. RefPtr<Text> m_textNode;
  244. String m_text;
  245. String m_oldText;
  246. };
  247. class DOMEditor::ReplaceChildNodeAction : public InspectorHistory::Action {
  248. WTF_MAKE_NONCOPYABLE(ReplaceChildNodeAction);
  249. public:
  250. ReplaceChildNodeAction(Node* parentNode, PassRefPtr<Node> newNode, Node* oldNode)
  251. : InspectorHistory::Action("ReplaceChildNode")
  252. , m_parentNode(parentNode)
  253. , m_newNode(newNode)
  254. , m_oldNode(oldNode)
  255. {
  256. }
  257. virtual bool perform(ExceptionCode& ec)
  258. {
  259. return redo(ec);
  260. }
  261. virtual bool undo(ExceptionCode& ec)
  262. {
  263. return m_parentNode->replaceChild(m_oldNode, m_newNode.get(), ec);
  264. }
  265. virtual bool redo(ExceptionCode& ec)
  266. {
  267. return m_parentNode->replaceChild(m_newNode, m_oldNode.get(), ec);
  268. }
  269. private:
  270. RefPtr<Node> m_parentNode;
  271. RefPtr<Node> m_newNode;
  272. RefPtr<Node> m_oldNode;
  273. };
  274. class DOMEditor::SetNodeValueAction : public InspectorHistory::Action {
  275. WTF_MAKE_NONCOPYABLE(SetNodeValueAction);
  276. public:
  277. SetNodeValueAction(Node* node, const String& value)
  278. : InspectorHistory::Action("SetNodeValue")
  279. , m_node(node)
  280. , m_value(value)
  281. {
  282. }
  283. virtual bool perform(ExceptionCode& ec)
  284. {
  285. m_oldValue = m_node->nodeValue();
  286. return redo(ec);
  287. }
  288. virtual bool undo(ExceptionCode& ec)
  289. {
  290. m_node->setNodeValue(m_oldValue, ec);
  291. return !ec;
  292. }
  293. virtual bool redo(ExceptionCode& ec)
  294. {
  295. m_node->setNodeValue(m_value, ec);
  296. return !ec;
  297. }
  298. private:
  299. RefPtr<Node> m_node;
  300. String m_value;
  301. String m_oldValue;
  302. };
  303. DOMEditor::DOMEditor(InspectorHistory* history) : m_history(history) { }
  304. DOMEditor::~DOMEditor() { }
  305. bool DOMEditor::insertBefore(Node* parentNode, PassRefPtr<Node> node, Node* anchorNode, ExceptionCode& ec)
  306. {
  307. return m_history->perform(adoptPtr(new InsertBeforeAction(parentNode, node, anchorNode)), ec);
  308. }
  309. bool DOMEditor::removeChild(Node* parentNode, Node* node, ExceptionCode& ec)
  310. {
  311. return m_history->perform(adoptPtr(new RemoveChildAction(parentNode, node)), ec);
  312. }
  313. bool DOMEditor::setAttribute(Element* element, const String& name, const String& value, ExceptionCode& ec)
  314. {
  315. return m_history->perform(adoptPtr(new SetAttributeAction(element, name, value)), ec);
  316. }
  317. bool DOMEditor::removeAttribute(Element* element, const String& name, ExceptionCode& ec)
  318. {
  319. return m_history->perform(adoptPtr(new RemoveAttributeAction(element, name)), ec);
  320. }
  321. bool DOMEditor::setOuterHTML(Node* node, const String& html, Node** newNode, ExceptionCode& ec)
  322. {
  323. OwnPtr<SetOuterHTMLAction> action = adoptPtr(new SetOuterHTMLAction(node, html));
  324. SetOuterHTMLAction* rawAction = action.get();
  325. bool result = m_history->perform(action.release(), ec);
  326. if (result)
  327. *newNode = rawAction->newNode();
  328. return result;
  329. }
  330. bool DOMEditor::replaceWholeText(Text* textNode, const String& text, ExceptionCode& ec)
  331. {
  332. return m_history->perform(adoptPtr(new ReplaceWholeTextAction(textNode, text)), ec);
  333. }
  334. bool DOMEditor::replaceChild(Node* parentNode, PassRefPtr<Node> newNode, Node* oldNode, ExceptionCode& ec)
  335. {
  336. return m_history->perform(adoptPtr(new ReplaceChildNodeAction(parentNode, newNode, oldNode)), ec);
  337. }
  338. bool DOMEditor::setNodeValue(Node* node, const String& value, ExceptionCode& ec)
  339. {
  340. return m_history->perform(adoptPtr(new SetNodeValueAction(node, value)), ec);
  341. }
  342. static void populateErrorString(const ExceptionCode& ec, ErrorString* errorString)
  343. {
  344. if (ec) {
  345. ExceptionCodeDescription description(ec);
  346. *errorString = description.name;
  347. }
  348. }
  349. bool DOMEditor::insertBefore(Node* parentNode, PassRefPtr<Node> node, Node* anchorNode, ErrorString* errorString)
  350. {
  351. ExceptionCode ec = 0;
  352. bool result = insertBefore(parentNode, node, anchorNode, ec);
  353. populateErrorString(ec, errorString);
  354. return result;
  355. }
  356. bool DOMEditor::removeChild(Node* parentNode, Node* node, ErrorString* errorString)
  357. {
  358. ExceptionCode ec = 0;
  359. bool result = removeChild(parentNode, node, ec);
  360. populateErrorString(ec, errorString);
  361. return result;
  362. }
  363. bool DOMEditor::setAttribute(Element* element, const String& name, const String& value, ErrorString* errorString)
  364. {
  365. ExceptionCode ec = 0;
  366. bool result = setAttribute(element, name, value, ec);
  367. populateErrorString(ec, errorString);
  368. return result;
  369. }
  370. bool DOMEditor::removeAttribute(Element* element, const String& name, ErrorString* errorString)
  371. {
  372. ExceptionCode ec = 0;
  373. bool result = removeAttribute(element, name, ec);
  374. populateErrorString(ec, errorString);
  375. return result;
  376. }
  377. bool DOMEditor::setOuterHTML(Node* node, const String& html, Node** newNode, ErrorString* errorString)
  378. {
  379. ExceptionCode ec = 0;
  380. bool result = setOuterHTML(node, html, newNode, ec);
  381. populateErrorString(ec, errorString);
  382. return result;
  383. }
  384. bool DOMEditor::replaceWholeText(Text* textNode, const String& text, ErrorString* errorString)
  385. {
  386. ExceptionCode ec = 0;
  387. bool result = replaceWholeText(textNode, text, ec);
  388. populateErrorString(ec, errorString);
  389. return result;
  390. }
  391. } // namespace WebCore
  392. #endif // ENABLE(INSPECTOR)