Slide 1

Slide 1 text

∞ things I didn’t know about HTML, CSS & JavaScript @mathias · #arrrrcamp

Slide 2

Slide 2 text

@mathias

Slide 3

Slide 3 text

Back to the ’90s kickin’ it old skool

Slide 4

Slide 4 text

Background colors

Slide 5

Slide 5 text

Background colors

Slide 6

Slide 6 text

Background colors

Slide 7

Slide 7 text

Background colors

Slide 8

Slide 8 text

Background colors ! #FF33FF

Slide 9

Slide 9 text

Background colors ! #0F030F

Slide 10

Slide 10 text

Background colors ! #00C000

Slide 11

Slide 11 text

Background colors ! #000000

Slide 12

Slide 12 text

Background colors ! #C00000

Slide 13

Slide 13 text

Background colors ! #C0A000

Slide 14

Slide 14 text

Background colors ! #000000

Slide 15

Slide 15 text

Background colors ! #A000A0 http://mths.be/bpf

Slide 16

Slide 16 text

“Valid HTML” conformance criteria

Slide 17

Slide 17 text

Document Type Definition

Slide 18

Slide 18 text

DTD

Lorem ipsum.

Slide 19

Slide 19 text

DTD

Slide 20

Slide 20 text

DTD fail

Slide 21

Slide 21 text

DTD fail foo bar

Slide 22

Slide 22 text

DTD fail → can still be checked by a computer foo bar

Slide 23

Slide 23 text

Computer says no
not a quote

Slide 24

Slide 24 text

Computer says no → can only be checked by a human
not a quote

Slide 25

Slide 25 text

Take-away Don’t obsess about HTML validation. • not the whole story • validators have bugs, too Functionality trumps validation anyway.

Slide 26

Slide 26 text

Character references aka. “entities”

Slide 27

Slide 27 text

Character references

foo & bar

Slide 28

Slide 28 text

;

Slide 29

Slide 29 text

github:twbs/bootstrap#3057

Slide 30

Slide 30 text

;

Slide 31

Slide 31 text

; PRETENDS TO BE OPTIONAL BREAKS STUFF WHEN OMITTED

Slide 32

Slide 32 text

Character references

foo & bar

Slide 33

Slide 33 text

Character references

foo &amp bar

Slide 34

Slide 34 text

Character references

foo&ampbar

Slide 35

Slide 35 text

Character references

foo & bar

http://mths.be/bdu

Slide 36

Slide 36 text

Character references

foo > bar

Slide 37

Slide 37 text

Character references

foo &gt bar

Slide 38

Slide 38 text

Character references

foo&gtbar

Slide 39

Slide 39 text

Character references

foo > bar

Slide 40

Slide 40 text

Tags

Slide 41

Slide 41 text

Valid HTML Foo …

Slide 42

Slide 42 text

Valid HTML Foo …

Slide 43

Slide 43 text

Valid HTML Huh? …

Slide 44

Slide 44 text

Valid HTML Huh? …

Slide 45

Slide 45 text

Valid HTML lolwut …

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

^ n FTFY

Slide 49

Slide 49 text

Using CSS without HTML

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Attribute values $ curl -i http://mathiasbynens.be/demo/css-without-html HTTP/1.1 200 OK Date: Wed, 03 Oct 2013 13:33:37 GMT Link: ;rel=stylesheet Content-Length: 0 Content-Type: text/html; charset=UTF-8 http://mths.be/bpe

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Font family names

Slide 55

Slide 55 text

CSS html { font-family: 'Comic Sans MS'; } “If there’s whitespace in the font family name, it must be quoted.”

Slide 56

Slide 56 text

CSS html { font-family: Comic Sans MS; } “If there’s whitespace in the font family name, it must be quoted.” http://mths.be/bft

Slide 57

Slide 57 text

CSS html { font-family: 456bereastreet; }

Slide 58

Slide 58 text

CSS html { font-family: 456bereastreet; }

Slide 59

Slide 59 text

CSS html { font-family: \34 56bereastreet; }

Slide 60

Slide 60 text

CSS html { font-family: \34 56bereastreet; }

Slide 61

Slide 61 text

CSS html { font-family: '456bereastreet'; }

Slide 62

Slide 62 text

CSS http://mths.be/bjm

Slide 63

Slide 63 text

Attribute values

Slide 64

Slide 64 text

Attribute values a[href="foo"] { background: hotpink; }

Slide 65

Slide 65 text

Attribute values a[href=foo] { background: hotpink; }

Slide 66

Slide 66 text

Attribute values a[href=foo|bar] { background: hotpink; }

Slide 67

Slide 67 text

Attribute values a[href=foo|bar] { background: hotpink; }

Slide 68

Slide 68 text

Attribute values a[href="foo|bar"] { background: hotpink; } http://mths.be/bal

Slide 69

Slide 69 text

Attribute values http://mths.be/bjn

Slide 70

Slide 70 text

Characters in JS

Slide 71

Slide 71 text

JavaScript string length >> 'A'.length // U+0041 1 >> 'A' == '\u0041' true >> 'B'.length // U+0042 1 >> 'B' == '\u0042' true

Slide 72

Slide 72 text

String length ≠ char count >> 'A'.length // U+1D400 2 >> 'A' == '\uD835\uDC00' true >> 'B'.length // U+1D401 2 >> 'B' == '\uD835\uDC01' true http://mths.be/bed

Slide 73

Slide 73 text

Surrogate pairs // for non-BMP code points (> 0xFFFF) function getSurrogates(codePoint) { var high = Math.floor((codePoint - 0x10000) / 0x400) + 0xD800; var low = (codePoint - 0x10000) % 0x400 + 0xDC00; return [ high, low ]; } function getCodePoint(high, low) { var codePoint = (high - 0xD800) * 0x400 + low - 0xDC00 + 0x10000; return codePoint; } >> getSurrogates('A'); // U+1D400 [ 0xD835, 0xDC00 ] >> getCodePoint(0xD835, 0xDC00); 0x1D400 http://mths.be/bed

Slide 74

Slide 74 text

JS string character count function countSymbols(string) { return punycode.ucs2.decode(string).length; } >> countSymbols('A') // U+0041 1 >> countSymbols('A') // U+1D400 1 http://mths.be/punycode

Slide 75

Slide 75 text

JS variable names

Slide 76

Slide 76 text

Which is invalid? var H ̹̙̦̮͉̩̗̗ͧ̇̏̊̾ E ͨ͆͒̆ͮ̃͏̷̮̣̫̤̣ C ͯ̂͐͏̨̛͔̦̟͈̻ O ̜͎͍͙͚̬̝̣̽ͮ͐͗̀ͤ̍̀͢ M ̴̡̲̭͍͇̼̟̯̦̉̒͠Ḛ̛̙̞̪̗ͥͤͩ̾͑̔͐ͅṮ̴̷̷̗̼͍̿̿̓̽͐ H ̙̙̔̄͜ ; var !_ಠӹಠ_!; var ໦ϗ_₹îਓ Ǝ ℵೋ⏺ Δ େ; var ʃː λ ˀπ ᐩᐨᐟᑉᐦᐸᐳƘƖ\u200C; var a; http://mths.be/ber

Slide 77

Slide 77 text

♥ JS var H ̹̙̦̮͉̩̗̗ͧ̇̏̊̾ E ͨ͆͒̆ͮ̃͏̷̮̣̫̤̣ C ͯ̂͐͏̨̛͔̦̟͈̻ O ̜͎͍͙͚̬̝̣̽ͮ͐͗̀ͤ̍̀͢ M ̴̡̲̭͍͇̼̟̯̦̉̒͠Ḛ̛̙̞̪̗ͥͤͩ̾͑̔͐ͅṮ̴̷̷̗̼͍̿̿̓̽͐ H ̙̙̔̄͜ ; // valid var !_ಠӹಠ_!; // valid var ໦ϗ_₹îਓ Ǝ ℵೋ⏺ Δ େ; // valid var ʃː λ ˀπ ᐩᐨᐟᑉᐦᐸᐳƘƖ\u200C; // valid var a; // syntax error http://mths.be/ber

Slide 78

Slide 78 text

/^(?!(?:do|if|in|for|let|new|try|var|case|else| enum|eval|false|null|this|true|void|with|break| catch|class|const|super|throw|while|yield|delete| export|import|public|return|static|switch|typeof| default|extends|finally|package|private|continue| debugger|function|arguments|interface|protected| implements|instanceof)$)[$A-Z\_a-z\xaa\xb5\xba \xc0-\xd6\xd8-\xf6\xf8-\u02c1\u02c6-\u02d1\u02e0- \u02e4\u02ec\u02ee\u0370- \u0374\u0376\u0377\u037a-\u037d\u0386\u0388- \u038a\u038c\u038e-\u03a1\u03a3-\u03f5\u03f7- \u0481\u048a-\u0527\u0531-\u0556\u0559\u0561- \u0587\u05d0-\u05ea\u05f0-\u05f2\u0620-\u064a http://mths.be/bpg

Slide 79

Slide 79 text

\u203f\u2040\u2054\u20d0-\u20dc\u20e1\u20e5- \u20f0\u2cef-\u2cf1\u2d7f\u2de0-\u2dff\u302a- \u302f\u3099\u309a\ua620-\ua629\ua66f\ua674- \ua67d\ua69f\ua6f0\ua6f1\ua802\ua806\ua80b\ua823- \ua827\ua880\ua881\ua8b4-\ua8c4\ua8d0- \ua8d9\ua8e0-\ua8f1\ua900-\ua909\ua926-\ua92d \ua947-\ua953\ua980-\ua983\ua9b3-\ua9c0\ua9d0- \ua9d9\uaa29-\uaa36\uaa43\uaa4c\uaa4d\uaa50- \uaa59\uaa7b\uaab0\uaab2-\uaab4\uaab7\uaab8\uaabe \uaabf\uaac1\uaaeb-\uaaef\uaaf5\uaaf6\uabe3- \uabea\uabec\uabed\uabf0-\uabf9\ufb1e\ufe00- \ufe0f\ufe20-\ufe26\ufe33\ufe34\ufe4d-\ufe4f \uff10-\uff19\uff3f]*$/

Slide 80

Slide 80 text

JS variable names http://mths.be/bjo

Slide 81

Slide 81 text

Truthiness in JS

Slide 82

Slide 82 text

JavaScript if (condition) { log('`condition` is truthy'); } else { log('`condition` is falsy'); }

Slide 83

Slide 83 text

ToBoolean(condition) Argument type Result undefined false null false boolean same as input number +0, -0, NaN → false otherwise → true string empty string → false otherwise → true object true

Slide 84

Slide 84 text

>> document.all [object HTMLCollection] >> !!document.all false “All objects are truthy” except in the DOM…

Slide 85

Slide 85 text

if (document.all) { // code that uses `document.all`, // for ancient browsers } else if (document.getElementById) { // code that uses `document.getElementById`, // for “modern” browsers } But… Why?

Slide 86

Slide 86 text

Benchmarking JavaScript timers

Slide 87

Slide 87 text

JavaScript var totalTime, start = new Date, iterations = 6; while (iterations--) { // Code snippet goes here } // totalTime ! the number of milliseconds it took // to execute the code snippet 6 times totalTime = new Date - start;

Slide 88

Slide 88 text

JavaScript var hz, period, startTime = new Date, runs = 0; do { // Code snippet goes here runs++; totalTime = new Date - startTime; } while (totalTime < 1000);

Slide 89

Slide 89 text

JavaScript // convert ms to seconds totalTime /= 1000; // period ! how long per operation period = totalTime / runs; // hz ! the number of operations per second hz = 1 / period; // can be shortened to // hz = (runs * 1000) / totalTime;

Slide 90

Slide 90 text

JavaScript “When Windows XP boots, the typical default clock interrupt period is 10 milliseconds, although a period of 15 milliseconds is used on some systems. That means that every 10 milliseconds, the operating system receives an interrupt from the system timer hardware.”

Slide 91

Slide 91 text

JavaScript “When Windows XP boots, the typical default clock interrupt period is 10 milliseconds, although a period of 15 milliseconds is used on some systems. That means that every 10 milliseconds, the operating system receives an interrupt from the system timer hardware.”

Slide 92

Slide 92 text

Benchmark.js • Detects various timers • (new Date).getTime() (ms) • chrome.Interval (µs) • performance.now (µs) • Node microtime module (µs) • Node’s high-res timer (ns) • Java timer applet (ns) • Performs statistical analysis of results • Used in RoboHornet and on jsPerf.com

Slide 93

Slide 93 text

jsPerf.com

Slide 94

Slide 94 text

Questions? @mathias