Skip to content

Instantly share code, notes, and snippets.

@DigiTec
DigiTec / vendor_enum.js
Last active August 29, 2015 14:14
Vendor Prefix Enumeration - Note prior to Chrome 43 we have to use instances to get values back. So there is a switch for some of the known instances, but this is heavily incomplete. Starting with Chrome 43 more information is on the prototype. Still, things like window and CSSStyleDeclaration lack type system and so you still need an instance f…
// http://jsfiddle.net/8n6c1dpz/12/
var out = document.getElementById("output");
var vendorPrefixes = [/^ms|^onms/i, /^moz|^onmoz/i, /^webkit|^onwebkit/i];
var vendorProps = vendorPrefixes.map(function (prefix) {
return Object.getOwnPropertyNames(window).filter(function (name) {
try {
return (this[name].prototype && this[name].prototype.constructor === this[name]);
} catch (exc) {
return false;
@DigiTec
DigiTec / arrayIfy.js
Last active August 29, 2015 14:15
Duck Typing Demos for Array -> HTMLCollection
// http://jsfiddle.net/w3mw1r4q/
var ary = Array.apply(null, document.body.children);
console.log(Array.isArray(document.body.children));
console.log(Array.isArray(ary));
console.log(ary.length);
@DigiTec
DigiTec / evil_vendor_prefix.js
Created April 14, 2015 18:04
Ever wonder why browser vendors dis-like bad feature detection (in this case I'd argue this is browser detection)? Well this is why. Vendor prefix sniffing that ends up assuming support of one vendor prefix means all properties are implemented that way. First, nearly all of the vendor prefixed properties appear very late in the enumeration, so y…
<script>
var r = {};
r.VENDOR_PREFIX = function () {
var e = /^(Webkit|Khtml|Moz|ms|O)(?=[A-Z])/, t = document.getElementsByTagName("script")[0].style, n;
for (n in t)
if (e.test(n))
return n.match(e)[0].toLowerCase();
return "WebkitOpacity" in t ? "webkit" : "KhtmlOpacity" in t ? "khtml" : "";
}();
</script>
@DigiTec
DigiTec / evil_vendor_prefix2.js
Created April 25, 2015 19:34
And the vendor prefixes attack again. This time, the ordinal indexing of the CSSStyleDeclaration is being used to see what the "first" vendor prefix is within the list of parser names supported by the browser. Since -ms- comes before -webkit- it will prioritize that over anything else. Also, the implicit assumption that if you support one vendor…
// Utilize duck typed slice, to enumerate 0..length indices.
// Indices on CSSStyleDeclaration return the "parser" names for properties.
// Based on the parser names, determine the vendor prefix to use.
var vendor_prefix = Array.prototype.slice.call(window.getComputedStyle(document.body)).join('').match(/-(ms|webkit|moz)-/)[0];
console.log(vendor_prefix);
@DigiTec
DigiTec / basic_constructors.js
Last active August 29, 2015 14:20
This gist is for my article on how a Browser implements constructors for DOM objects. It contains a short list of samples that are inserted into the article for pretty printing. Here is the link to the article: http://www.justrog.com/2015/05/constructors-by-construction-in.html
// Per spec configures a location on the global of name Node with a value
// of the instance of the function.
function ANode() {
}
// We derive from Object
ANode.prototype = Object.create(Object.prototype);
// Complete the loop back to ourselves
ANode.prototype.constructor = ANode;
@DigiTec
DigiTec / element_tag_names.js
Last active August 29, 2015 14:20
A gist of element tag names in various namespaces
// Lists primarily based on MSHTML support IE5 through IE11
// Namespaces
const nsXHTML = "http://www.w3.org/1999/xhtml";
const nsSVG = "http://www.w3.org/2000/svg";
const nsMATHML = "http://www.w3.org/1998/Math/MathML";
const nsXML = "http://www.w3.org/XML/1998/namespace";
// "x-ms-webview" added for WWA
// "main" tag added for EdgeHTML
@DigiTec
DigiTec / chrome_xhtml.js
Last active August 29, 2015 14:20
A gist of results mapping all known elements to prototypes for some set of browsers. To be updated periodically. Example fiddle. http://jsfiddle.net/sqmqzjjv/3/
var xhtml_tags_chrome_prototypes = {
"DOCTYPE": "HTMLUnknownElement",
"a": "HTMLAnchorElement",
"abbr": "HTMLElement",
"acronym": "HTMLElement",
"address": "HTMLElement",
"applet": "HTMLAppletElement",
"area": "HTMLAreaElement",
"article": "HTMLElement",
"aside": "HTMLElement",
@DigiTec
DigiTec / test_edgehtml_webkitprefixes.html
Last active August 29, 2015 14:21
This is a quick gist for examining the list of EdgeHTML supported webkit properties that can be run on any device. This helps to narrow down why certain properties were added since not all WebKit browsers support all of the properties. This list tends to be a union of Chrome 42+ and iOS 8 Safari.
<div id="log" style="whitespace: pre"></div>
@DigiTec
DigiTec / Readme.md
Last active August 29, 2015 14:21
Browser Lexical Scope Functions created using inline event handlers.

Some additional JSFiddle links that I produced afterwards to experiment with timing around when the browser would create its scope chains. It turns out browsers don't tend to create scope chains until the first time the event is fired.

In this example we have a button in a form. We can check its scope first, then change it, then check it again to see one behavior. We can then change scope and execute it to see a different behavior. http://jsfiddle.net/oko08kaw/

This example switches us from one form to another, which is a slightly more complicated behavior but achieves similar results. http://jsfiddle.net/p7bu92jw/2/

@DigiTec
DigiTec / error_recovery.cpp
Last active August 29, 2015 14:23
Code Snippets for a short article or series on error recovery models and reliability
typedef unsigned long ErrorCode;
typedef void* MemoryHandle;
ErrorCode OutOfMemory = 0x1;
ErrorCode Success = 0x2;
ErrorCode TerribleFailure = 0x3;
void FreeHandle(MemoryHandle handle)
{
}