1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00

Fix invalid HTML generated by AnimationDisabler

The W3C Validator was reporting an error:

> Element style not allowed as child of element body in this context.
>
> Contexts in which element style may be used:
>     Where metadata content is expected.
>     In a noscript element that is a child of a head element.
> Content model for element body:
>     Flow content.

It looks like there can't be any <style> tags inside the <body> element
[1].

The <script> part of the markup template remains at the end of the
<body> element because it only makes sense if jQuery has already been
defined, and jQuery could be defined anywhere inside the <head> or
<body> elements.

[1] https://html.spec.whatwg.org/multipage/semantics.html#the-style-element
This commit is contained in:
Javi Martín 2021-09-26 01:39:55 +02:00
parent 0468de5a81
commit cad1efc952
2 changed files with 21 additions and 9 deletions

View file

@ -16,7 +16,10 @@ module Capybara
def initialize(app)
@app = app
@disable_markup = format(DISABLE_MARKUP_TEMPLATE, selector: self.class.selector_for(Capybara.disable_animation))
@disable_css_markup = format(DISABLE_CSS_MARKUP_TEMPLATE,
selector: self.class.selector_for(Capybara.disable_animation))
@disable_js_markup = format(DISABLE_JS_MARKUP_TEMPLATE,
selector: self.class.selector_for(Capybara.disable_animation))
end
def call(env)
@ -33,22 +36,17 @@ module Capybara
private
attr_reader :disable_markup
attr_reader :disable_css_markup, :disable_js_markup
def html_content?
/html/.match?(@headers['Content-Type'])
end
def insert_disable(html)
html.sub(%r{(</body>)}, "#{disable_markup}\\1")
html.sub(%r{(</head>)}, "#{disable_css_markup}\\1").sub(%r{(</body>)}, "#{disable_js_markup}\\1")
end
DISABLE_MARKUP_TEMPLATE = <<~HTML
<script>
//<![CDATA[
(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);
//]]>
</script>
DISABLE_CSS_MARKUP_TEMPLATE = <<~HTML
<style>
%<selector>s, %<selector>s::before, %<selector>s::after {
transition: none !important;
@ -58,6 +56,14 @@ module Capybara
}
</style>
HTML
DISABLE_JS_MARKUP_TEMPLATE = <<~HTML
<script>
//<![CDATA[
(typeof jQuery !== 'undefined') && (jQuery.fx.off = true);
//]]>
</script>
HTML
end
end
end

View file

@ -381,6 +381,12 @@ RSpec.shared_examples 'Capybara::Session' do |session, mode|
@animation_session = Capybara::Session.new(session.mode, TestApp.new)
end
it 'should add CSS to the <head> element' do
@animation_session.visit('with_animation')
expect(@animation_session).to have_selector(:css, 'head > style', text: 'transition: none', visible: :hidden)
end
it 'should disable CSS transitions' do
@animation_session.visit('with_animation')
@animation_session.click_link('transition me away')