Slide 1

Slide 1 text

Why Polyfilling CSS Is Hard by Julian Viereck
 Webtuesday, Zurich - 14.10.2014

Slide 2

Slide 2 text

https://medium.com/@jviereck/why-do-all-code-editors-look-the- same-43de0df2daed Personal Prototyping Project

Slide 3

Slide 3 text

Outline of the talk • What is a Polyfill? • CSS is the Swiss Knife of the browser • CSS + JS 㱺 Polyfill all the things? • Short answer: No - but why? • Conclusion

Slide 4

Slide 4 text

https://remysharp.com/2010/10/08/what-is-a-polyfill

Slide 5

Slide 5 text

Polyfill • Replicate something the browser doesn’t have yet • Personally also: • Performance less important • Should not break existing features • E.g. setting style=“float: right” should still work

Slide 6

Slide 6 text

– The Extensible Web Manifesto “Make it possible to polyfill more of the platform's new features.” Extensible Web Manifesto <3 Polyfill

Slide 7

Slide 7 text

http://extensiblewebmanifesto.org/

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

www.thedailysnap.com

Slide 11

Slide 11 text

Image from: http://www.freevector.com/swiss-knife-vector/ CSS CSS: the Swiss Knife of the browser?

Slide 12

Slide 12 text

What’s in the CSS Box • CSS interacts with: • Visual appearance (text color, border, …) • Layout: • Dimensions of elements (width, height, …) • Position of elements (absolute position, CSS translation, …) • Interaction with other elements (inline vs block display, …) • Time evolution (CSS animations) • Content definition (via content property), GPU Acceleration • CSS “contains”: • Constraint solver • Interactive Query language (e.g. div:hover { color: red }) • And many more (CSS Variables, …)

Slide 13

Slide 13 text

Content via CSS http://jsbin.com/dofizicepiwa/1/

Slide 14

Slide 14 text

First idea: Polyfill CSS via CSS + JS Where does this work? 㱺

Slide 15

Slide 15 text

Polyfill CSS via CSS + JS • Example: • Polyfill CSS Animations • Set position in every requestAnimationFrame • From JS: `element.style.top = i +“px”` • Existing CSS features + JS 㱺 Polyfill ✓

Slide 16

Slide 16 text

When does CSS Polyfill
 work at all? • Works if: • Can express new CSS features in terms of existing CSS features Goal: Existing features: Polyfill-able? ✓=yes ✓*=well

Slide 17

Slide 17 text

What’s in the CSS Box • CSS interacts with: • Visual appearance (text color, border, …) • Layout: • Dimensions of elements (width, height, …) • Position of elements (absolute position, CSS translation, …) • Interaction with other elements (inline vs block display, …) • Time evolution (CSS animations) • Content definition (via content property), GPU Acceleration • CSS “contains”: • Constraint solver • Interactive Query language (e.g. div:hover { color: red }) • And many more (CSS Variables, …) ✓* ✓* x ✓/x ✓ Polyfill-able with CSS + JS: x=no , ✓=yes, ✓*=well x ✓ ✓

Slide 18

Slide 18 text

So, let’s build a layout Engine in JS? What could go wrong?

Slide 19

Slide 19 text

http://gridstylesheets.org/

Slide 20

Slide 20 text

Grid Style Sheet • Constraint CSS (CCSS):! • #article[font-size] == #box[height] / 12 !strong • Grid-Flavored VFL! • Turns: • #panel[left] + 10 == #button1[left]; • #button1[right] + 10 == #button2[left]; • Into: • @horizontal |-[#button1]-[#button2]-| in(#panel) gap(10); • And more crazy things!

Slide 21

Slide 21 text

GSS Example

Slide 22

Slide 22 text

http://vimeo.com/91393694 GSS Architecture Overview

Slide 23

Slide 23 text

http://webplatform.adobe.com/css-regions-polyfill/

Slide 24

Slide 24 text

https://github.com/FremyCompany/css-regions-polyfill/ blob/master/documentation/GLOBAL_OVERVIEW.md Useful libraries for extending CSS

Slide 25

Slide 25 text

http://docs.webplatform.org/wiki/css/tutorials/css-regions

Slide 26

Slide 26 text

Demo

Slide 27

Slide 27 text

Under the hood

Slide 28

Slide 28 text

Problem with 
 “Custom” Layout Engine • Need to map to existing features • DOM gets lots of style=“…” and 
 DOM event listeners • Need to work with existing CSS features • Is “float: left” handled in your engine? Getting floats right is hard, see: http://pcwalton.github.io/blog/2014/02/25/revamped-parallel-layout-in-servo/

Slide 29

Slide 29 text

Might be okay, but… • Polyfill layout might be “okay’ish” • But, recall - CSS Is The Swiss Knife: • Interacts with many parts of the browser • But not all internals are exposed!

Slide 30

Slide 30 text

Browser Render Pipeline http://www.html5rocks.com/en/tutorials/internals/howbrowserswork/

Slide 31

Slide 31 text

Browser internals http://www.html5rocks.com/en/tutorials/internals/howbrowserswork/ What can CSS do here?

Slide 32

Slide 32 text

CSS & Render Tree http://www.html5rocks.com/en/tutorials/internals/howbrowserswork/ • Possibilities: Shake tree • Change tree location • Add new boxes • Change sizes • …

Slide 33

Slide 33 text

Possibilities ≠ 
 Existing Features • Possibilities: • Change tree location • Add new boxes • Pollute styles • Change sizes • … (see next slide) ➡ Handwaving Solutions ➡ renderInSameBoxAs? ➡ GhostDom? ➡ ShadowStyles? ➡ ? ➡ ?

Slide 34

Slide 34 text

Possibilities ≠ 
 Existing Features • And Even More: • Controlling font / image loading & rendering • Line breaking • RTL layout • Style single letter • … https://github.com/igrigorik/css-font-timeout/blob/patch-1/README.md http://extensiblewebreportcard.org/#toc-20 Internals are not ! exposed to developer more details:

Slide 35

Slide 35 text

Scripting CSS? • How about making CSS “turing complete”/scriptable? • No :) • One turing complete language in the browser is enough (aka. JavaScript) • Keeps a lot of things simpler (GC, …) • Problem are underlaying missing exposed features • Better: More ways to express intends

Slide 36

Slide 36 text

Conclusion • Writing a Polyfill for CSS: • Works fine for some features (animations, …) • For layout • Solutions are more hacks than polyfills • For rendering • Try to map to SVG/Canvas or bad luck :/ • Therefore: • Need new low-level CSS features to enable CSS Polyfills Not sure if this
 is a good idea!

Slide 37

Slide 37 text

Julian Viereck
 @jviereck
 +JulianViereck Thanks!