Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / html / parser / HTMLSrcsetParser.h
blobddcd90774b946398d68e2dc3f16f44fbc15481ec
1 /*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef HTMLSrcsetParser_h
33 #define HTMLSrcsetParser_h
35 #include "core/CoreExport.h"
36 #include "wtf/Allocator.h"
37 #include "wtf/text/WTFString.h"
39 namespace blink {
41 class Document;
43 enum { UninitializedDescriptor = -1 };
45 class DescriptorParsingResult {
46 STACK_ALLOCATED();
47 public:
48 DescriptorParsingResult()
49 : m_density(UninitializedDescriptor)
50 , m_resourceWidth(UninitializedDescriptor)
51 , m_resourceHeight(UninitializedDescriptor)
55 bool hasDensity() const { return m_density >= 0; }
56 bool hasWidth() const { return m_resourceWidth >= 0; }
57 bool hasHeight() const { return m_resourceHeight >= 0; }
59 float density() const { ASSERT(hasDensity()); return m_density; }
60 unsigned resourceWidth() const { ASSERT(hasWidth()); return m_resourceWidth; }
61 unsigned resourceHeight() const { ASSERT(hasHeight()); return m_resourceHeight; }
63 void setResourceWidth(int width) { ASSERT(width >= 0); m_resourceWidth = (unsigned)width; }
64 void setResourceHeight(int height) { ASSERT(height >= 0); m_resourceHeight = (unsigned)height; }
65 void setDensity(float densityToSet) { ASSERT(densityToSet >= 0); m_density = densityToSet; }
67 private:
68 float m_density;
69 int m_resourceWidth;
70 int m_resourceHeight;
73 class ImageCandidate {
74 ALLOW_ONLY_INLINE_ALLOCATION();
75 public:
76 enum OriginAttribute {
77 SrcsetOrigin,
78 SrcOrigin
81 ImageCandidate()
82 : m_density(1.0)
83 , m_resourceWidth(UninitializedDescriptor)
84 , m_originAttribute(SrcsetOrigin)
88 ImageCandidate(const String& source, unsigned start, unsigned length, const DescriptorParsingResult& result, OriginAttribute originAttribute)
89 : m_string(source.createView(start, length))
90 , m_density(result.hasDensity()?result.density():UninitializedDescriptor)
91 , m_resourceWidth(result.hasWidth()?result.resourceWidth():UninitializedDescriptor)
92 , m_originAttribute(originAttribute)
96 String toString() const
98 return String(m_string.toString());
101 AtomicString url() const
103 return AtomicString(m_string.toString());
106 void setDensity(float factor)
108 m_density = factor;
111 float density() const
113 return m_density;
116 int resourceWidth() const
118 return m_resourceWidth;
121 bool srcOrigin() const
123 return (m_originAttribute == SrcOrigin);
126 inline bool isEmpty() const
128 return m_string.isEmpty();
131 private:
132 StringView m_string;
133 float m_density;
134 int m_resourceWidth;
135 OriginAttribute m_originAttribute;
138 ImageCandidate bestFitSourceForSrcsetAttribute(float deviceScaleFactor, float sourceSize, const String& srcsetAttribute, Document* = nullptr);
140 CORE_EXPORT ImageCandidate bestFitSourceForImageAttributes(float deviceScaleFactor, float sourceSize, const String& srcAttribute, const String& srcsetAttribute, Document* = nullptr);
142 String bestFitSourceForImageAttributes(float deviceScaleFactor, float sourceSize, const String& srcAttribute, ImageCandidate& srcsetImageCandidate);
146 #endif