Home | History | Annotate | Download | only in track
      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 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 
     26 #include "config.h"
     27 #include "core/html/track/WebVTTElement.h"
     28 
     29 #include "HTMLElementFactory.h"
     30 
     31 namespace WebCore {
     32 
     33 static const QualifiedName& nodeTypeToTagName(WebVTTNodeType nodeType)
     34 {
     35     DEFINE_STATIC_LOCAL(QualifiedName, cTag, (nullAtom, "c", nullAtom));
     36     DEFINE_STATIC_LOCAL(QualifiedName, vTag, (nullAtom, "v", nullAtom));
     37     DEFINE_STATIC_LOCAL(QualifiedName, langTag, (nullAtom, "lang", nullAtom));
     38     DEFINE_STATIC_LOCAL(QualifiedName, bTag, (nullAtom, "b", nullAtom));
     39     DEFINE_STATIC_LOCAL(QualifiedName, uTag, (nullAtom, "u", nullAtom));
     40     DEFINE_STATIC_LOCAL(QualifiedName, iTag, (nullAtom, "i", nullAtom));
     41     DEFINE_STATIC_LOCAL(QualifiedName, rubyTag, (nullAtom, "ruby", nullAtom));
     42     DEFINE_STATIC_LOCAL(QualifiedName, rtTag, (nullAtom, "rt", nullAtom));
     43     switch (nodeType) {
     44     case WebVTTNodeTypeClass:
     45         return cTag;
     46     case WebVTTNodeTypeItalic:
     47         return iTag;
     48     case WebVTTNodeTypeLanguage:
     49         return langTag;
     50     case WebVTTNodeTypeBold:
     51         return bTag;
     52     case WebVTTNodeTypeUnderline:
     53         return uTag;
     54     case WebVTTNodeTypeRuby:
     55         return rubyTag;
     56     case WebVTTNodeTypeRubyText:
     57         return rtTag;
     58     case WebVTTNodeTypeVoice:
     59         return vTag;
     60     case WebVTTNodeTypeNone:
     61     default:
     62         ASSERT_NOT_REACHED();
     63         return cTag; // Make the compiler happy.
     64     }
     65 }
     66 
     67 WebVTTElement::WebVTTElement(WebVTTNodeType nodeType, Document* document)
     68     : Element(nodeTypeToTagName(nodeType), document, CreateElement)
     69     , m_isPastNode(0)
     70     , m_webVTTNodeType(nodeType)
     71 {
     72 }
     73 
     74 PassRefPtr<WebVTTElement> WebVTTElement::create(WebVTTNodeType nodeType, Document* document)
     75 {
     76     return adoptRef(new WebVTTElement(nodeType, document));
     77 }
     78 
     79 PassRefPtr<Element> WebVTTElement::cloneElementWithoutAttributesAndChildren()
     80 {
     81     RefPtr<WebVTTElement> clone = create(static_cast<WebVTTNodeType>(m_webVTTNodeType), document());
     82     clone->setLanguage(m_language);
     83     return clone;
     84 }
     85 
     86 PassRefPtr<HTMLElement> WebVTTElement::createEquivalentHTMLElement(Document* document)
     87 {
     88     RefPtr<HTMLElement> htmlElement;
     89     switch (m_webVTTNodeType) {
     90     case WebVTTNodeTypeClass:
     91     case WebVTTNodeTypeLanguage:
     92     case WebVTTNodeTypeVoice:
     93         htmlElement = HTMLElementFactory::createHTMLElement(HTMLNames::spanTag, document);
     94         htmlElement.get()->setAttribute(HTMLNames::titleAttr, getAttribute(voiceAttributeName()));
     95         htmlElement.get()->setAttribute(HTMLNames::langAttr, getAttribute(langAttributeName()));
     96         break;
     97     case WebVTTNodeTypeItalic:
     98         htmlElement = HTMLElementFactory::createHTMLElement(HTMLNames::iTag, document);
     99         break;
    100     case WebVTTNodeTypeBold:
    101         htmlElement = HTMLElementFactory::createHTMLElement(HTMLNames::bTag, document);
    102         break;
    103     case WebVTTNodeTypeUnderline:
    104         htmlElement = HTMLElementFactory::createHTMLElement(HTMLNames::uTag, document);
    105         break;
    106     case WebVTTNodeTypeRuby:
    107         htmlElement = HTMLElementFactory::createHTMLElement(HTMLNames::rubyTag, document);
    108         break;
    109     case WebVTTNodeTypeRubyText:
    110         htmlElement = HTMLElementFactory::createHTMLElement(HTMLNames::rtTag, document);
    111         break;
    112     default:
    113         ASSERT_NOT_REACHED();
    114     }
    115 
    116     htmlElement.get()->setAttribute(HTMLNames::classAttr, getAttribute(HTMLNames::classAttr));
    117     return htmlElement;
    118 }
    119 
    120 } // namespace WebCore
    121 
    122