1f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)/*
2f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
3f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) *
4f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
5f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * modification, are permitted provided that the following conditions are
6f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * met:
7f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) *
8f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) *     * Redistributions of source code must retain the above copyright
9f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * notice, this list of conditions and the following disclaimer.
10f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) *     * Redistributions in binary form must reproduce the above
11f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
12f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * in the documentation and/or other materials provided with the
13f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * distribution.
14f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
15f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * contributors may be used to endorse or promote products derived from
16f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * this software without specific prior written permission.
17f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) *
18f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles) */
30f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
31f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)#include "config.h"
3209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "core/css/CSSGridTemplateAreasValue.h"
33f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
34f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)#include "wtf/text/StringBuilder.h"
35f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
36f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)namespace WebCore {
37f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
3809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)CSSGridTemplateAreasValue::CSSGridTemplateAreasValue(const NamedGridAreaMap& gridAreaMap, size_t rowCount, size_t columnCount)
3909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    : CSSValue(GridTemplateAreasClass)
40f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    , m_gridAreaMap(gridAreaMap)
41f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    , m_rowCount(rowCount)
42f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    , m_columnCount(columnCount)
43f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles){
44f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    ASSERT(m_rowCount);
45f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    ASSERT(m_columnCount);
46f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)}
47f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
48f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)static String stringForPosition(const NamedGridAreaMap& gridAreaMap, size_t row, size_t column)
49f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles){
50f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    Vector<String> candidates;
51f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
52f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    NamedGridAreaMap::const_iterator end = gridAreaMap.end();
53f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    for (NamedGridAreaMap::const_iterator it = gridAreaMap.begin(); it != end; ++it) {
54f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)        const GridCoordinate& coordinate = it->value;
55a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        if (row >= coordinate.rows.resolvedInitialPosition.toInt() && row <= coordinate.rows.resolvedFinalPosition.toInt())
56f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)            candidates.append(it->key);
57f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    }
58f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
59f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    end = gridAreaMap.end();
60f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    for (NamedGridAreaMap::const_iterator it = gridAreaMap.begin(); it != end; ++it) {
61f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)        const GridCoordinate& coordinate = it->value;
62a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        if (column >= coordinate.columns.resolvedInitialPosition.toInt() && column <= coordinate.columns.resolvedFinalPosition.toInt() && candidates.contains(it->key))
63f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)            return it->key;
64f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    }
65f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
66f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    return ".";
67f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)}
68f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
6909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)String CSSGridTemplateAreasValue::customCSSText() const
70f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles){
71f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    StringBuilder builder;
72f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    for (size_t row = 0; row < m_rowCount; ++row) {
733c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdoch        builder.append('\"');
74f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)        for (size_t column = 0; column < m_columnCount; ++column) {
75f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)            builder.append(stringForPosition(m_gridAreaMap, row, column));
76f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)            if (column != m_columnCount - 1)
77f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)                builder.append(' ');
78f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)        }
793c9e4aeaee9f9b0a9a814da07bcb33319c7ea363Ben Murdoch        builder.append('\"');
80f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)        if (row != m_rowCount - 1)
81f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)            builder.append(' ');
82f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    }
83f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)    return builder.toString();
84f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)}
85f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)
86f5e4ad553afbc08dd2e729bb77e937a9a94d5827Torne (Richard Coles)} // namespace WebCore
87