1/**
2 * Copyright (C) 2008 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
3 *
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB.  If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#include "config.h"
25
26#if ENABLE(WML)
27#include "WMLPElement.h"
28
29#include "Attribute.h"
30#include "CSSPropertyNames.h"
31#include "CSSValueKeywords.h"
32#include "Document.h"
33#include "HTMLNames.h"
34#include "NodeList.h"
35#include "WMLNames.h"
36
37namespace WebCore {
38
39using namespace WMLNames;
40
41WMLPElement::WMLPElement(const QualifiedName& tagName, Document* doc)
42    : WMLElement(tagName, doc)
43{
44}
45
46PassRefPtr<WMLPElement> WMLPElement::create(const QualifiedName& tagName, Document* document)
47{
48    return adoptRef(new WMLPElement(tagName, document));
49}
50
51bool WMLPElement::mapToEntry(const QualifiedName& attrName, MappedAttributeEntry& result) const
52{
53    if (attrName == HTMLNames::alignAttr) {
54        result = eBlock; // We can share with DIV here.
55        return false;
56    }
57
58    return WMLElement::mapToEntry(attrName, result);
59}
60
61void WMLPElement::parseMappedAttribute(Attribute* attr)
62{
63    if (attr->name() == HTMLNames::alignAttr) {
64        const AtomicString& value = attr->value();
65        if (equalIgnoringCase(value, "middle") || equalIgnoringCase(value, "center"))
66            addCSSProperty(attr, CSSPropertyTextAlign, CSSValueWebkitCenter);
67        else if (equalIgnoringCase(value, "left"))
68            addCSSProperty(attr, CSSPropertyTextAlign, CSSValueWebkitLeft);
69        else if (equalIgnoringCase(value, "right"))
70            addCSSProperty(attr, CSSPropertyTextAlign, CSSValueWebkitRight);
71        else
72            addCSSProperty(attr, CSSPropertyTextAlign, value);
73    } else if (attr->name() == modeAttr) {
74        m_mode = attr->value();
75        if (m_mode == "wrap")
76            addCSSProperty(attr, CSSPropertyWordWrap, CSSValueBreakWord);
77        else if (m_mode == "nowrap")
78            addCSSProperty(attr, CSSPropertyWhiteSpace, CSSValueNowrap);
79    } else
80        WMLElement::parseMappedAttribute(attr);
81}
82
83void WMLPElement::insertedIntoDocument()
84{
85    WMLElement::insertedIntoDocument();
86
87    // If not explicitly specified, the linewrap mode is identical to
88    // the line-wrap mode of the previous paragraph in the text flow of
89    // a card. The default mode for the first paragraph in a card is wrap.
90    if (!m_mode.isEmpty())
91        return;
92
93    RefPtr<NodeList> nodeList = document()->getElementsByTagName("p");
94    if (!nodeList)
95        return;
96
97    unsigned length = nodeList->length();
98    if (length < 2)
99        return;
100
101    // Assure we're the last inserted paragraph element
102    // This only works while parsing, otherwhise this statement is never true.
103    if (nodeList->item(length - 1) != this)
104        return;
105
106    WMLPElement* lastParagraph = static_cast<WMLPElement*>(nodeList->item(length - 2));
107    ASSERT(lastParagraph);
108
109    String lastMode = lastParagraph->getAttribute(modeAttr);
110    if (lastMode.isEmpty() || lastMode == "wrap") // Default value, do nothing.
111        return;
112
113    setAttribute(modeAttr, lastMode);
114}
115
116}
117
118#endif
119