Skip to content
Permalink
Browse files
TextTrackCue should get its document for its ActiveDOMObject base class
https://bugs.webkit.org/show_bug.cgi?id=240660
<rdar://93512736>

Reviewed by Eric Carlson.

TextTrackCue should get its document for its ActiveDOMObject base class instead
of requiring an extra data member to store it.

* Source/WebCore/html/track/TextTrackCue.cpp:
(WebCore::TextTrackCue::TextTrackCue):
(WebCore::TextTrackCue::scriptExecutionContext const):
(WebCore::TextTrackCue::document const):
(WebCore::TextTrackCue::getCueAsHTML):
(WebCore::TextTrackCue::rebuildDisplayTree):
* Source/WebCore/html/track/TextTrackCue.h:
(WebCore::TextTrackCue::ownerDocument): Deleted.
* Source/WebCore/html/track/TextTrackCueGeneric.cpp:
(WebCore::TextTrackCueGeneric::createDisplayTree):
(WebCore::TextTrackCueGeneric::setFontSize):
* Source/WebCore/html/track/TextTrackCueGeneric.h:
* Source/WebCore/html/track/VTTCue.cpp:
(WebCore::VTTCue::VTTCue):
(WebCore::VTTCue::initialize):
(WebCore::VTTCue::createDisplayTree):
(WebCore::VTTCue::displayTreeInternal):
(WebCore::VTTCue::createWebVTTNodeTree):
(WebCore::VTTCue::getCueAsHTML):
(WebCore::VTTCue::createCueRenderingTree):
(WebCore::VTTCue::getDisplayTree):
(WebCore::VTTCue::removeDisplayTree):
* Source/WebCore/html/track/VTTCue.h:

Canonical link: https://commits.webkit.org/250757@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@294498 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
cdumez committed May 19, 2022
1 parent 4176964 commit 8cfadef7857b524891610891f64e1bbe867aa6c4
Showing 6 changed files with 61 additions and 35 deletions.
@@ -216,7 +216,6 @@ TextTrackCue::TextTrackCue(Document& document, const MediaTime& start, const Med
: ActiveDOMObject(document)
, m_startTime(start)
, m_endTime(end)
, m_document(document)
, m_cueNode(WTFMove(cueFragment))
{
}
@@ -225,13 +224,17 @@ TextTrackCue::TextTrackCue(Document& document, const MediaTime& start, const Med
: ActiveDOMObject(document)
, m_startTime(start)
, m_endTime(end)
, m_document(document)
{
}

ScriptExecutionContext* TextTrackCue::scriptExecutionContext() const
{
return &m_document;
return ActiveDOMObject::scriptExecutionContext();
}

Document* TextTrackCue::document() const
{
return downcast<Document>(scriptExecutionContext());
}

void TextTrackCue::willChange()
@@ -436,7 +439,11 @@ RefPtr<DocumentFragment> TextTrackCue::getCueAsHTML()
if (!m_cueNode)
return nullptr;

auto clonedFragment = DocumentFragment::create(ownerDocument());
auto* document = this->document();
if (!document)
return nullptr;

auto clonedFragment = DocumentFragment::create(*document);
m_cueNode->cloneChildNodes(clonedFragment);

for (Node* node = clonedFragment->firstChild(); node; node = node->nextSibling())
@@ -485,21 +492,25 @@ void TextTrackCue::rebuildDisplayTree()
if (!m_cueNode)
return;

RefPtr document = this->document();
if (!document)
return;

ScriptDisallowedScope::EventAllowedScope allowedScopeForReferenceTree(*m_cueNode);

if (!m_displayTree) {
m_displayTree = TextTrackCueBox::create(ownerDocument(), *this);
m_displayTree = TextTrackCueBox::create(*document, *this);
m_displayTree->setPseudo(ShadowPseudoIds::webkitGenericCueRoot());
}

m_displayTree->removeChildren();
auto clonedFragment = DocumentFragment::create(ownerDocument());
auto clonedFragment = DocumentFragment::create(*document);
m_cueNode->cloneChildNodes(clonedFragment);
m_displayTree->appendChild(clonedFragment);

if (m_fontSize) {
if (auto page = ownerDocument().page()) {
auto style = HTMLStyleElement::create(HTMLNames::styleTag, ownerDocument(), false);
if (auto page = document->page()) {
auto style = HTMLStyleElement::create(HTMLNames::styleTag, *document, false);
style->setTextContent(makeString(page->captionUserPreferencesStyleSheet(),
" ::", ShadowPseudoIds::cue(), "{font-size:", m_fontSize, m_fontSizeIsImportant ? "px !important}" : "px}"));
m_displayTree->appendChild(style);
@@ -126,7 +126,7 @@ class TextTrackCue : public RefCounted<TextTrackCue>, public EventTargetWithInli
protected:
TextTrackCue(Document&, const MediaTime& start, const MediaTime& end);

Document& ownerDocument() { return m_document; }
Document* document() const;

virtual bool cueContentsMatch(const TextTrackCue&) const;
virtual void toJSON(JSON::Object&) const;
@@ -154,8 +154,6 @@ class TextTrackCue : public RefCounted<TextTrackCue>, public EventTargetWithInli

TextTrack* m_track { nullptr };

Document& m_document;

RefPtr<DocumentFragment> m_cueNode;
RefPtr<TextTrackCueBox> m_displayTree;

@@ -171,9 +171,11 @@ TextTrackCueGeneric::TextTrackCueGeneric(Document& document, const MediaTime& st
{
}

Ref<VTTCueBox> TextTrackCueGeneric::createDisplayTree()
RefPtr<VTTCueBox> TextTrackCueGeneric::createDisplayTree()
{
return TextTrackCueGenericBoxElement::create(ownerDocument(), *this);
if (auto* document = this->document())
return TextTrackCueGenericBoxElement::create(*document, *this);
return nullptr;
}

ExceptionOr<void> TextTrackCueGeneric::setLine(const LineAndPositionSetting& line)
@@ -205,7 +207,8 @@ void TextTrackCueGeneric::setFontSize(int fontSize, const IntSize& videoSize, bo
double size = videoSize.height() * baseFontSizeRelativeToVideoHeight() / 100;
if (fontSizeMultiplier())
size *= fontSizeMultiplier() / 100;
displayTreeInternal().setInlineStyleProperty(CSSPropertyFontSize, lround(size), CSSUnitType::CSS_PX);
if (auto* displayTree = displayTreeInternal())
displayTree->setInlineStyleProperty(CSSPropertyFontSize, lround(size), CSSUnitType::CSS_PX);
}

bool TextTrackCueGeneric::cueContentsMatch(const TextTrackCue& otherTextTrackCue) const
@@ -69,7 +69,7 @@ class TextTrackCueGeneric final : public VTTCue {
bool isOrderedBefore(const TextTrackCue*) const final;
bool isPositionedAbove(const TextTrackCue*) const final;

Ref<VTTCueBox> createDisplayTree() final;
RefPtr<VTTCueBox> createDisplayTree() final;

bool cueContentsMatch(const TextTrackCue&) const final;

@@ -266,14 +266,14 @@ VTTCue::VTTCue(Document& document, const MediaTime& start, const MediaTime& end,
, m_content(WTFMove(content))
, m_originalStartTime(MediaTime::zeroTime())
{
initialize();
initialize(document);
}

VTTCue::VTTCue(Document& document, const WebVTTCueData& cueData)
: TextTrackCue(document, MediaTime::zeroTime(), MediaTime::zeroTime())
, m_originalStartTime(cueData.originalStartTime())
{
initialize();
initialize(document);
setText(cueData.content());
setStartTime(cueData.startTime());
setEndTime(cueData.endTime());
@@ -285,25 +285,27 @@ VTTCue::~VTTCue()
{
}

void VTTCue::initialize()
void VTTCue::initialize(Document& document)
{
m_cueBackdropBox = HTMLDivElement::create(ownerDocument());
m_cueHighlightBox = HTMLSpanElement::create(spanTag, ownerDocument());
m_cueBackdropBox = HTMLDivElement::create(document);
m_cueHighlightBox = HTMLSpanElement::create(spanTag, document);
m_snapToLines = true;
m_displayTreeShouldChange = true;
m_notifyRegion = true;
}

Ref<VTTCueBox> VTTCue::createDisplayTree()
RefPtr<VTTCueBox> VTTCue::createDisplayTree()
{
return VTTCueBox::create(ownerDocument(), *this);
if (auto* document = this->document())
return VTTCueBox::create(*document, *this);
return nullptr;
}

VTTCueBox& VTTCue::displayTreeInternal()
VTTCueBox* VTTCue::displayTreeInternal()
{
if (!m_displayTree)
m_displayTree = createDisplayTree();
return *m_displayTree;
return m_displayTree.get();
}

void VTTCue::didChange()
@@ -588,8 +590,8 @@ void VTTCue::setText(const String& text)

void VTTCue::createWebVTTNodeTree()
{
if (!m_webVTTNodeTree)
m_webVTTNodeTree = WebVTTParser::createDocumentFragmentFromCueText(ownerDocument(), m_content);
if (!m_webVTTNodeTree && document())
m_webVTTNodeTree = WebVTTParser::createDocumentFragmentFromCueText(*document(), m_content);
}

static void copyWebVTTNodeToDOMTree(ContainerNode& webVTTNode, Node& parent)
@@ -612,7 +614,11 @@ RefPtr<DocumentFragment> VTTCue::getCueAsHTML()
if (!m_webVTTNodeTree)
return nullptr;

auto clonedFragment = DocumentFragment::create(ownerDocument());
auto* document = this->document();
if (!document)
return nullptr;

auto clonedFragment = DocumentFragment::create(*document);
copyWebVTTNodeToDOMTree(*m_webVTTNodeTree, clonedFragment);
return clonedFragment;
}
@@ -623,7 +629,11 @@ RefPtr<DocumentFragment> VTTCue::createCueRenderingTree()
if (!m_webVTTNodeTree)
return nullptr;

auto clonedFragment = DocumentFragment::create(ownerDocument());
auto* document = this->document();
if (!document)
return nullptr;

auto clonedFragment = DocumentFragment::create(*document);

// The cloned fragment is never exposed to author scripts so it's safe to dispatch events here.
ScriptDisallowedScope::EventAllowedScope allowedScope(clonedFragment);
@@ -961,8 +971,8 @@ void VTTCue::updateDisplayTree(const MediaTime& movieTime)

RefPtr<TextTrackCueBox> VTTCue::getDisplayTree(const IntSize& videoSize, int fontSize)
{
Ref<VTTCueBox> displayTree = displayTreeInternal();
if (!m_displayTreeShouldChange || !track()->isRendered())
RefPtr displayTree = displayTreeInternal();
if (!displayTree || !m_displayTreeShouldChange || !track()->isRendered())
return displayTree;

// 10.1 - 10.10
@@ -1035,9 +1045,13 @@ void VTTCue::removeDisplayTree()
m_region->willRemoveTextTrackCueBox(m_displayTree.get());
}

RefPtr displayTree = displayTreeInternal();
if (!displayTree)
return;

// The display tree is never exposed to author scripts so it's safe to dispatch events here.
ScriptDisallowedScope::EventAllowedScope allowedScope(displayTreeInternal());
displayTreeInternal().remove();
ScriptDisallowedScope::EventAllowedScope allowedScope(*displayTree);
displayTree->remove();
}

std::pair<double, double> VTTCue::getPositionCoordinates() const
@@ -195,15 +195,15 @@ class VTTCue : public TextTrackCue {

bool cueContentsMatch(const TextTrackCue&) const override;

virtual Ref<VTTCueBox> createDisplayTree();
VTTCueBox& displayTreeInternal();
virtual RefPtr<VTTCueBox> createDisplayTree();
VTTCueBox* displayTreeInternal();

void toJSON(JSON::Object&) const override;

private:
VTTCue(Document&, const WebVTTCueData&);

void initialize();
void initialize(Document&);
void createWebVTTNodeTree();

void parseSettings(const String&);

0 comments on commit 8cfadef

Please sign in to comment.