Skip to content

Commit 556555c

Browse files
Psychpsyopbrw
authored andcommitted
LibWeb: Hide audio element when controls is not set
When an audio element has no controls attribute, it should not render at all and take up no space.
1 parent e30f279 commit 556555c

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

Libraries/LibWeb/Layout/AudioBox.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,21 @@ GC::Ptr<Painting::Paintable> AudioBox::create_paintable() const
3434
return Painting::AudioPaintable::create(*this);
3535
}
3636

37+
bool AudioBox::should_paint() const
38+
{
39+
auto const& audio_element = dom_node();
40+
return audio_element.has_attribute(HTML::AttributeNames::controls) || audio_element.is_scripting_disabled();
41+
}
42+
43+
void AudioBox::prepare_for_replaced_layout()
44+
{
45+
if (should_paint()) {
46+
set_natural_width(300);
47+
set_natural_height(40);
48+
} else {
49+
set_natural_width(0);
50+
set_natural_height(0);
51+
}
52+
}
53+
3754
}

Libraries/LibWeb/Layout/AudioBox.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ class AudioBox final : public ReplacedBox {
1717
GC_DECLARE_ALLOCATOR(AudioBox);
1818

1919
public:
20+
virtual void prepare_for_replaced_layout() override;
21+
2022
HTML::HTMLAudioElement& dom_node();
2123
HTML::HTMLAudioElement const& dom_node() const;
2224

2325
virtual GC::Ptr<Painting::Paintable> create_paintable() const override;
2426

27+
bool should_paint() const;
28+
2529
private:
2630
AudioBox(DOM::Document&, DOM::Element&, GC::Ref<CSS::ComputedProperties>);
2731
};

Libraries/LibWeb/Painting/AudioPaintable.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ void AudioPaintable::paint(PaintContext& context, PaintPhase phase) const
4343
if (!is_visible())
4444
return;
4545

46+
if (!layout_box().should_paint())
47+
return;
48+
4649
Base::paint(context, phase);
4750

4851
if (phase != PaintPhase::Foreground)
@@ -57,11 +60,7 @@ void AudioPaintable::paint(PaintContext& context, PaintPhase phase) const
5760

5861
auto const& audio_element = layout_box().dom_node();
5962
auto mouse_position = MediaPaintable::mouse_position(context, audio_element);
60-
61-
auto paint_user_agent_controls = audio_element.has_attribute(HTML::AttributeNames::controls) || audio_element.is_scripting_disabled();
62-
63-
if (paint_user_agent_controls)
64-
paint_media_controls(context, audio_element, audio_rect, mouse_position);
63+
paint_media_controls(context, audio_element, audio_rect, mouse_position);
6564
}
6665

6766
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Viewport <#document> at (0,0) content-size 800x600 children: not-inline
2+
BlockContainer <html> at (0,0) content-size 800x59 [BFC] children: not-inline
3+
BlockContainer <body> at (8,8) content-size 784x43 children: inline
4+
frag 0 from AudioBox start: 0, length: 0, rect: [8,8 300x40] baseline: 40
5+
frag 1 from TextNode start: 0, length: 1, rect: [308,34 8x17] baseline: 13.296875
6+
" "
7+
frag 2 from AudioBox start: 0, length: 0, rect: [316,48 0x0] baseline: 0
8+
AudioBox <audio> at (8,8) content-size 300x40 children: not-inline
9+
TextNode <#text>
10+
AudioBox <audio> at (316,48) content-size 0x0 children: not-inline
11+
12+
ViewportPaintable (Viewport<#document>) [0,0 800x600]
13+
PaintableWithLines (BlockContainer<HTML>) [0,0 800x59]
14+
PaintableWithLines (BlockContainer<BODY>) [8,8 784x43]
15+
AudioPaintable (AudioBox<AUDIO>) [8,8 300x40]
16+
TextPaintable (TextNode<#text>)
17+
AudioPaintable (AudioBox<AUDIO>) [316,48 0x0]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<!DOCTYPE html>
2+
<audio controls=""></audio>
3+
<audio></audio>

0 commit comments

Comments
 (0)