1/*
2 * Copyright (C) 2013 Apple 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
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 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 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
26#include "config.h"
27#include "WebVTTElement.h"
28
29#if ENABLE(VIDEO_TRACK)
30
31#include "HTMLSpanElement.h"
32#include "RubyElement.h"
33#include "RubyTextElement.h"
34#include "TextTrack.h"
35#include <wtf/IsoMallocInlines.h>
36
37namespace WebCore {
38
39WTF_MAKE_ISO_ALLOCATED_IMPL(WebVTTElement);
40
41static const QualifiedName& nodeTypeToTagName(WebVTTNodeType nodeType)
42{
43 static NeverDestroyed<QualifiedName> cTag(nullAtom(), "c", nullAtom());
44 static NeverDestroyed<QualifiedName> vTag(nullAtom(), "v", nullAtom());
45 static NeverDestroyed<QualifiedName> langTag(nullAtom(), "lang", nullAtom());
46 static NeverDestroyed<QualifiedName> bTag(nullAtom(), "b", nullAtom());
47 static NeverDestroyed<QualifiedName> uTag(nullAtom(), "u", nullAtom());
48 static NeverDestroyed<QualifiedName> iTag(nullAtom(), "i", nullAtom());
49 static NeverDestroyed<QualifiedName> rubyTag(nullAtom(), "ruby", nullAtom());
50 static NeverDestroyed<QualifiedName> rtTag(nullAtom(), "rt", nullAtom());
51 switch (nodeType) {
52 case WebVTTNodeTypeClass:
53 return cTag;
54 case WebVTTNodeTypeItalic:
55 return iTag;
56 case WebVTTNodeTypeLanguage:
57 return langTag;
58 case WebVTTNodeTypeBold:
59 return bTag;
60 case WebVTTNodeTypeUnderline:
61 return uTag;
62 case WebVTTNodeTypeRuby:
63 return rubyTag;
64 case WebVTTNodeTypeRubyText:
65 return rtTag;
66 case WebVTTNodeTypeVoice:
67 return vTag;
68 case WebVTTNodeTypeNone:
69 default:
70 ASSERT_NOT_REACHED();
71 return cTag; // Make the compiler happy.
72 }
73}
74
75WebVTTElement::WebVTTElement(WebVTTNodeType nodeType, Document& document)
76 : Element(nodeTypeToTagName(nodeType), document, CreateElement)
77 , m_isPastNode(0)
78 , m_webVTTNodeType(nodeType)
79{
80}
81
82Ref<WebVTTElement> WebVTTElement::create(WebVTTNodeType nodeType, Document& document)
83{
84 return adoptRef(*new WebVTTElement(nodeType, document));
85}
86
87Ref<Element> WebVTTElement::cloneElementWithoutAttributesAndChildren(Document& targetDocument)
88{
89 Ref<WebVTTElement> clone = create(static_cast<WebVTTNodeType>(m_webVTTNodeType), targetDocument);
90 clone->setLanguage(m_language);
91 return clone;
92}
93
94Ref<HTMLElement> WebVTTElement::createEquivalentHTMLElement(Document& document)
95{
96 RefPtr<HTMLElement> htmlElement;
97
98 switch (m_webVTTNodeType) {
99 case WebVTTNodeTypeClass:
100 case WebVTTNodeTypeLanguage:
101 case WebVTTNodeTypeVoice:
102 htmlElement = HTMLSpanElement::create(document);
103 htmlElement->setAttributeWithoutSynchronization(HTMLNames::titleAttr, attributeWithoutSynchronization(voiceAttributeName()));
104 htmlElement->setAttributeWithoutSynchronization(HTMLNames::langAttr, attributeWithoutSynchronization(langAttributeName()));
105 break;
106 case WebVTTNodeTypeItalic:
107 htmlElement = HTMLElement::create(HTMLNames::iTag, document);
108 break;
109 case WebVTTNodeTypeBold:
110 htmlElement = HTMLElement::create(HTMLNames::bTag, document);
111 break;
112 case WebVTTNodeTypeUnderline:
113 htmlElement = HTMLElement::create(HTMLNames::uTag, document);
114 break;
115 case WebVTTNodeTypeRuby:
116 htmlElement = RubyElement::create(document);
117 break;
118 case WebVTTNodeTypeRubyText:
119 htmlElement = RubyTextElement::create(document);
120 break;
121 }
122
123 ASSERT(htmlElement);
124 if (htmlElement)
125 htmlElement->setAttributeWithoutSynchronization(HTMLNames::classAttr, attributeWithoutSynchronization(HTMLNames::classAttr));
126 return htmlElement.releaseNonNull();
127}
128
129} // namespace WebCore
130
131#endif
132