blob: 7622c1e8140dad5fba66938855185e369f7ce15a [file] [log] [blame]
mjs84e724c2005-05-24 07:21:47 +00001/*
darin@apple.come95b53d2008-12-23 21:42:46 +00002 * Copyright (C) 2005, 2008 Apple Inc. All rights reserved.
mjs84e724c2005-05-24 07:21:47 +00003 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
mjsb64c50a2005-10-03 21:13:12 +000026#include "config.h"
darina68e0432006-02-14 21:40:54 +000027#include "WrapContentsInDummySpanCommand.h"
mjs84e724c2005-05-24 07:21:47 +000028
darina68e0432006-02-14 21:40:54 +000029#include "ApplyStyleCommand.h"
darinb9481ed2006-03-20 02:57:59 +000030#include "HTMLElement.h"
mjs84e724c2005-05-24 07:21:47 +000031
darin85c3a502006-02-17 01:08:41 +000032namespace WebCore {
mjs84e724c2005-05-24 07:21:47 +000033
darin@apple.com48ac3c42008-06-14 08:46:51 +000034WrapContentsInDummySpanCommand::WrapContentsInDummySpanCommand(PassRefPtr<Element> element)
darin@apple.come95b53d2008-12-23 21:42:46 +000035 : SimpleEditCommand(element->document())
36 , m_element(element)
mjs84e724c2005-05-24 07:21:47 +000037{
38 ASSERT(m_element);
mjs84e724c2005-05-24 07:21:47 +000039}
40
41void WrapContentsInDummySpanCommand::doApply()
42{
darin@apple.come95b53d2008-12-23 21:42:46 +000043 Vector<RefPtr<Node> > children;
44 for (Node* child = m_element->firstChild(); child; child = child->nextSibling())
45 children.append(child);
mjs84e724c2005-05-24 07:21:47 +000046
darin@apple.come95b53d2008-12-23 21:42:46 +000047 RefPtr<HTMLElement> span = createStyleSpanElement(document());
eseidelef508982006-01-03 09:19:17 +000048
darin@apple.come95b53d2008-12-23 21:42:46 +000049 ExceptionCode ec;
mjs84e724c2005-05-24 07:21:47 +000050
darin@apple.come95b53d2008-12-23 21:42:46 +000051 size_t size = children.size();
52 for (size_t i = 0; i < size; ++i)
53 span->appendChild(children[i].release(), ec);
54
55 m_element->appendChild(span.get(), ec);
56
57 m_dummySpan = span.release();
mjs84e724c2005-05-24 07:21:47 +000058}
59
60void WrapContentsInDummySpanCommand::doUnapply()
61{
62 ASSERT(m_element);
mjs84e724c2005-05-24 07:21:47 +000063
darin@apple.come95b53d2008-12-23 21:42:46 +000064 RefPtr<HTMLElement> span = m_dummySpan.release();
65 if (!span)
66 return;
mjs84e724c2005-05-24 07:21:47 +000067
darin@apple.come95b53d2008-12-23 21:42:46 +000068 Vector<RefPtr<Node> > children;
69 for (Node* child = span->firstChild(); child; child = child->nextSibling())
70 children.append(child);
mjs84e724c2005-05-24 07:21:47 +000071
darin@apple.come95b53d2008-12-23 21:42:46 +000072 ExceptionCode ec;
mjs84e724c2005-05-24 07:21:47 +000073
darin@apple.come95b53d2008-12-23 21:42:46 +000074 size_t size = children.size();
75 for (size_t i = 0; i < size; ++i)
76 m_element->appendChild(children[i].release(), ec);
77
78 span->remove(ec);
mjs84e724c2005-05-24 07:21:47 +000079}
80
darinb9481ed2006-03-20 02:57:59 +000081} // namespace WebCore