Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVG and CSS display:contents #305

Closed
ghost opened this issue Feb 13, 2017 · 7 comments
Closed

SVG and CSS display:contents #305

ghost opened this issue Feb 13, 2017 · 7 comments

Comments

@ghost
Copy link

ghost commented Feb 13, 2017

While implementing display:contents in Blink, we encountered the question of whether display:contents means an SVG node should be rendered or not. The current draft says all other values than display:none should be rendered [1], but we assume this statement pre-dates the introduction of display:contents. Gecko, which already shipped display:contents, do render SVG nodes for display:contents.

[1] https://svgwg.org/svg2-draft/render.html#VisibilityControl

@AmeliaBR
Copy link
Contributor

AmeliaBR commented Feb 13, 2017

This is a good question, but I think the easiest solution is to follow both specs as written.

Hypothetically, if display: contents were to apply for SVG, the logical implementation would be:

  • If display: contents is on the <svg> element that is the start of the SVG subtree (i.e., a root element or a direct child of an HTML element), don't switch to SVG rendering mode, don't create an SVG canvas, treat all the child nodes as if they were plain CSS-styled-XML nodes. If there are nested <svg> elements, they each start their own inline SVG canvases.

  • If display: contents is on a nested <svg> or a <g>, ignore any transformations applied on that element, including the implicit transformations created by viewBox and x,y,width, and height and the effect they have on percentage units and the user-space coordinate system for child elements. Based on the way <use> behaves in the rendering tree for the Shadow DOM model, it should follow this behavior, with the shadow DOM elements being rendered but without any x/y/transform adjustments from the <use>.

  • If display: contents is on an SVG shape, the child elements would be rendered as if they were children of the parent. Assuming the SVG DOM tree is valid, the effect would be that nothing is rendered: any valid child elements of a shape are not rendered when added directly to an SVG canvas.

  • Setting display: contents on a never-rendered SVG element type (such as defs or pattern) should not have any effect, consistent with the fact that the display mode can never be changed on those elements. (In SVG 2, we define this using an !important user agent stylesheet rule; previously, it was defined in prose.)

All of this is very complicated, and of questionable utility. The only use case that I could see is being able to turn off SVG rendering mode on an <svg> to render its plain text content (including alt text) as CSS-styled text. However, if that is a desired behavior, it would probably be better to have a more explicit switch.

I am therefore inclined to follow the SVG spec as written (and the Gecko implementation), and say that display: contents should not apply to SVG elements that do not have any CSS layout boxes anyway.

For <svg> elements that are direct children of HTML elements (and do have regular CSS layout boxes for positioning the SVG canvas, and are affected by other display values), the guidance from the Display 3 spec applies:

This value has no effect on replaced elements and form controls; it computes to inline exactly as if the UA style sheet had specified display: inline !important on such elements.

An SVG canvas is a "replaced element" for the purpose of the CSS box tree, so display: contents should be treated as display: inline. (I'm not sure why the !important bit is mentioned; I'm assuming this is a "used value" fix that only applies after all the cascade is calculated. But that's an issue to bring up with CSS WG.)

@tabatkins
Copy link
Member

tabatkins commented Feb 13, 2017 via email

@fantasai
Copy link

Okay, so @tabatkins and I added an appendix to the Display module to break down all of this for discussion: https://drafts.csswg.org/css-display/#unbox Comments/suggestions welcome. CSSWG thread still at w3c/csswg-drafts#540

@dbaron
Copy link
Member

dbaron commented Apr 19, 2017

The CSS WG just discussed this; see the IRC log. The resolution was:

RESOLVED: SVG elements <g>, <use>, nested <svg>, and <tspan> are eliminated and their contents are hoisted under display:contents but all other SVG elements are display:none'ed

@AmeliaBR
Copy link
Contributor

So to confirm, that means that display: contents on an <svg> that defines an SVG layout context (e.g. root or child of box model) is effectively display: none?

@tabatkins
Copy link
Member

Yes, as it's a replaced element in the outer page's context (and there's no good answer for what to do with the contents of the SVG if we did just strip the wrapper).

@boggydigital
Copy link
Contributor

This has been resolved in CSS WG and I don't think there is anything required from SVG WG. This is certainly not blocking updated 2.0 CR publication. Closing the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants