Browse Source

LibWeb: Include standard SVG user agent style sheet

For now, part of this is commented-out. Our current implementations of
`<mask>` and `<symbol>` rely on creating layout nodes, so they can't be
`display: none`.
Sam Atkins 1 year ago
parent
commit
ae4b8d86df

+ 8 - 0
Meta/CMake/libweb_generators.cmake

@@ -96,6 +96,14 @@ function (generate_css_implementation)
         NAMESPACE "Web::CSS"
         NAMESPACE "Web::CSS"
     )
     )
 
 
+    embed_as_string_view(
+        "SVGStyleSheetSource.cpp"
+        "${LIBWEB_INPUT_FOLDER}/SVG/Default.css"
+        "SVG/SVGStyleSheetSource.cpp"
+        "svg_stylesheet_source"
+        NAMESPACE "Web::CSS"
+    )
+
     set(CSS_GENERATED_TO_INSTALL
     set(CSS_GENERATED_TO_INSTALL
         "CSS/EasingFunctions.h"
         "CSS/EasingFunctions.h"
         "CSS/Enums.h"
         "CSS/Enums.h"

+ 7 - 0
Meta/gn/secondary/Userland/Libraries/LibWeb/BUILD.gn

@@ -230,6 +230,13 @@ embed_as_string_view("generate_mathml_stylesheet_source") {
   namespace = "Web::CSS"
   namespace = "Web::CSS"
 }
 }
 
 
+embed_as_string_view("generate_svg_stylesheet_source") {
+  input = "SVG/Default.css"
+  output = "$target_gen_dir/SVG/SVGStyleSheetSource.cpp"
+  variable_name = "svg_stylesheet_source"
+  namespace = "Web::CSS"
+}
+
 embed_as_string_view("generate_quirks_mode_stylesheet_source") {
 embed_as_string_view("generate_quirks_mode_stylesheet_source") {
   input = "CSS/QuirksMode.css"
   input = "CSS/QuirksMode.css"
   output = "$target_gen_dir/CSS/QuirksModeStyleSheetSource.cpp"
   output = "$target_gen_dir/CSS/QuirksModeStyleSheetSource.cpp"

+ 1 - 1
Tests/LibWeb/Layout/expected/svg/svg-symbol-with-viewbox.txt

@@ -11,7 +11,7 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
         SVGSVGBox <svg> at (8,8) content-size 300x150 [SVG] children: inline
         SVGSVGBox <svg> at (8,8) content-size 300x150 [SVG] children: inline
           TextNode <#text>
           TextNode <#text>
           Box <use> at (8,8) content-size 0x0 children: inline
           Box <use> at (8,8) content-size 0x0 children: inline
-            Box <symbol#braces> at (8,8) content-size 0x0 children: inline
+            Box <symbol#braces> at (8,8) content-size 0x0 [BFC] children: inline
               TextNode <#text>
               TextNode <#text>
               SVGGeometryBox <path> at (92.375,26.75) content-size 131.25x112.15625 children: inline
               SVGGeometryBox <path> at (92.375,26.75) content-size 131.25x112.15625 children: inline
                 TextNode <#text>
                 TextNode <#text>

+ 1 - 1
Tests/LibWeb/Layout/expected/svg/svg-with-zero-intrinsic-size-and-no-viewbox.txt

@@ -8,7 +8,7 @@ Viewport <#document> at (0,0) content-size 800x600 children: not-inline
         frag 2 from SVGSVGBox start: 0, length: 0, rect: [16,21 0x0]
         frag 2 from SVGSVGBox start: 0, length: 0, rect: [16,21 0x0]
       ImageBox <img> at (8,21) content-size 0x0 children: not-inline
       ImageBox <img> at (8,21) content-size 0x0 children: not-inline
         (SVG-as-image isolated context)
         (SVG-as-image isolated context)
-        Viewport <#document> at (0,0) content-size 0x0 children: inline
+        Viewport <#document> at (0,0) content-size 0x0 [BFC] children: inline
           SVGSVGBox <svg> at (0,0) content-size 0x0 [SVG] children: inline
           SVGSVGBox <svg> at (0,0) content-size 0x0 [SVG] children: inline
             TextNode <#text>
             TextNode <#text>
             SVGGeometryBox <rect> at (0,0) content-size 1x1 children: not-inline
             SVGGeometryBox <rect> at (0,0) content-size 1x1 children: not-inline

+ 1 - 0
Userland/Libraries/LibWeb/CMakeLists.txt

@@ -655,6 +655,7 @@ set(GENERATED_SOURCES
     CSS/TransformFunctions.cpp
     CSS/TransformFunctions.cpp
     CSS/ValueID.cpp
     CSS/ValueID.cpp
     MathML/MathMLStyleSheetSource.cpp
     MathML/MathMLStyleSheetSource.cpp
+    SVG/SVGStyleSheetSource.cpp
 )
 )
 
 
 serenity_lib(LibWeb web)
 serenity_lib(LibWeb web)

+ 11 - 0
Userland/Libraries/LibWeb/CSS/StyleComputer.cpp

@@ -239,6 +239,16 @@ static CSSStyleSheet& mathml_stylesheet(DOM::Document const& document)
     return *sheet;
     return *sheet;
 }
 }
 
 
+static CSSStyleSheet& svg_stylesheet(DOM::Document const& document)
+{
+    static JS::Handle<CSSStyleSheet> sheet;
+    if (!sheet.cell()) {
+        extern StringView svg_stylesheet_source;
+        sheet = JS::make_handle(parse_css_stylesheet(CSS::Parser::ParsingContext(document), svg_stylesheet_source));
+    }
+    return *sheet;
+}
+
 template<typename Callback>
 template<typename Callback>
 void StyleComputer::for_each_stylesheet(CascadeOrigin cascade_origin, Callback callback) const
 void StyleComputer::for_each_stylesheet(CascadeOrigin cascade_origin, Callback callback) const
 {
 {
@@ -247,6 +257,7 @@ void StyleComputer::for_each_stylesheet(CascadeOrigin cascade_origin, Callback c
         if (document().in_quirks_mode())
         if (document().in_quirks_mode())
             callback(quirks_mode_stylesheet(document()));
             callback(quirks_mode_stylesheet(document()));
         callback(mathml_stylesheet(document()));
         callback(mathml_stylesheet(document()));
+        callback(svg_stylesheet(document()));
     }
     }
     if (cascade_origin == CascadeOrigin::User) {
     if (cascade_origin == CascadeOrigin::User) {
         if (m_user_style_sheet)
         if (m_user_style_sheet)

+ 39 - 0
Userland/Libraries/LibWeb/SVG/Default.css

@@ -0,0 +1,39 @@
+/* https://svgwg.org/svg2-draft/styling.html#UAStyleSheet */
+
+@namespace url(http://www.w3.org/2000/svg);
+@namespace xml url(http://www.w3.org/XML/1998/namespace);
+
+svg:not(:root), image, marker, pattern, symbol { overflow: hidden; }
+
+*:not(svg),
+*:not(foreignObject) > svg {
+    transform-origin: 0 0;
+}
+
+*[xml|space=preserve] {
+    text-space-collapse: preserve-spaces;
+}
+
+/* FIXME: Allow setting the rest of these to `display: none`.
+          Currently that breaks <use> and <mask> and probably others. */
+desc, title, metadata,
+pattern, linearGradient, radialGradient,
+script, style {
+    display: none !important;
+}
+/*
+defs,
+clipPath, mask, marker,
+desc, title, metadata,
+pattern, linearGradient, radialGradient,
+script, style,
+symbol {
+    display: none !important;
+}
+*/
+:host(use) > symbol {
+    display: inline !important;
+}
+:link, :visited {
+    cursor: pointer;
+}