From 2727a6fdd19c274bd8f0ab6d67ac6b1c8f45e51f Mon Sep 17 00:00:00 2001 From: Matt King Date: Sat, 10 Aug 2024 17:10:33 -0700 Subject: [PATCH 1/4] Try putting code guid md file in root --- Code-Guide.md | 941 +++++++++++++++++++++++ content/about/code-guide/code-guide.html | 24 + 2 files changed, 965 insertions(+) create mode 100644 Code-Guide.md create mode 100644 content/about/code-guide/code-guide.html diff --git a/Code-Guide.md b/Code-Guide.md new file mode 100644 index 0000000000..db89af63e9 --- /dev/null +++ b/Code-Guide.md @@ -0,0 +1,941 @@ +
+ +
+ +# APG Coding standards + +Standards for developing contributing HTML, CSS, and JavaScript to W3C WAI-ARIA APG. + +
+ +
+ +
+ +## Table of contents + +
+ +
+ +
+ +#### [HTML](#html-1) + +- [Syntax](#syntax) +- [HTML doctype](#html-doctype) +- [Language attribute](#language-attribute) +- [Character encoding](#character-encoding) +- [CSS and JavaScript includes](#css-and-javascript-includes) +- [Practicality over purity](#practicality-over-purity) +- [Boolean attributes](#boolean-attributes) +- [Reducing markup](#reducing-markup) +- [JavaScript generated markup](#javascript-generated-markup) + +
+ +
+ +#### [CSS](#css-1) + +- [CSS syntax](#syntax-1) +- [Declaration order](#declaration-order) +- [Don't use @import](#dont-use-import) +- [Media query placement](#media-query-placement) +- [Prefixed properties](#prefixed-properties) +- [Shorthand notation](#shorthand-notation) +- [Comments](#comments) +- [Class names](#class-names) + +
+ +
+ +#### [JavaScript](#javascript-1) + +- [JavaScript style & syntax](#general-code-style--syntax) +- [Function Naming](#function-naming) +- [Function Design](#function-design) +- [Widget Structure](#widget-structure) +- [Comments](#comments-1) +- [Opening example in Codepen](#codepen) + +
+ +
+ +
+ +
+ +## Introduction + +These standards are provided as a guide to contributing to W3C WAI-ARIA APG + +They are based on [Code Guide](http://codeguide.co/) by [@mdo](https://github.com/mdo/code-guide). + +
+ +
+ +
+ +
+ +## Principles + +The following priorities have been used to determine these standards: + +1. Readability / understandability +2. Familiarity +3. Ease of maintenance +4. Ease of contribution +5. Performance + +### Browser support + +- Latest version of Google Chrome +- Latest version of Mozilla Firefox + +
+ +
+ +
+ +## HTML + +
+ +
+ +
+ +### Syntax + +- Use soft tabs with two spaces—they're the only way to guarantee code renders the same in any environment. +- Nested elements should be indented once (two spaces). +- Always use double quotes, never single quotes, on attributes. +- Don't include a trailing slash in self-closing elements—the [HTML standard](https://html.spec.whatwg.org/multipage/syntax.html#syntax-start-tag) says they're optional. +- Don’t omit optional closing tags (e.g. `` or ``). + +
+ +
+ +
+ + + + + + Page title + + + Company +

Hello, world!

+ + + +
+ +
+ +
+ +
+ +
+ +### HTML doctype + +Enforce standards mode and more consistent rendering in every browser possible with this simple doctype at the beginning of every HTML page. + +
+ +
+ +
+ + + +
+ +
+ +
+ +
+ +
+ +### Language attribute + +From the HTML standard: + +> Authors are encouraged to specify a `lang` attribute on the root `html` element, giving the document's language. This aids speech synthesis tools to determine what pronunciations to use, translation tools to determine what rules to use, and so forth. + +Read more about the `lang` attribute [in the spec](https://html.spec.whatwg.org/multipage/semantics.html#the-html-element). + +Head to Sitepoint for a [list of language codes](https://www.sitepoint.com/iso-2-letter-language-codes/). + +
+ +
+ +
+ + + + + +
+ +
+ +
+ +
+ +
+ +### Character encoding + +Use UTF-8, and use the short form `` as the first child in `head`. + +
+ +
+ +
+ + + + ... + + +
+ +
+ +
+ +
+ +
+ +### CSS and JavaScript includes + +Per the HTML standard, there is no need to specify a `type` when including CSS and classic JavaScript files as `text/css` and `text/javascript` are their respective defaults. + +#### HTML standard links + +- [Using `link`](https://html.spec.whatwg.org/multipage/semantics.html#the-link-element) +- [Using `style`](https://html.spec.whatwg.org/multipage/semantics.html#the-style-element) +- [Using `script`](https://html.spec.whatwg.org/multipage/scripting.html#the-script-element) + +
+ +
+ +
+ + + + + + + + + + +
+ +
+ +
+ +
+ +
+ +### Practicality over purity + +Strive to maintain HTML standards and semantics, but not at the expense of practicality. Use the least amount of markup with the fewest intricacies whenever possible. + +
+ +
+ +
+ +
+ +### Boolean attributes + +A boolean attribute is one that needs no declared value. XHTML required you to declare a value, but HTML has no such requirement. + +For further reading, consult the [WHATWG section on boolean attributes](https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes): + +> The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value. + +If you _must_ include the attribute's value, and **you don't need to**, follow this WHATWG guideline: + +> If the attribute is present, its value must either be the empty string or [...] the attribute's canonical name, with no leading or trailing whitespace. + +**In short, don't add a value.** + +
+ +
+ +
+ + + + + + + +
+ +
+ +
+ +
+ +
+ +### Reducing markup + +Whenever possible, avoid superfluous parent elements when writing HTML. Many times this requires iteration and refactoring, but produces less HTML. Take the following example: + +
+ +
+ +
+ + + + + + + + + +
+ +
+ +
+ +
+ +
+ +### JavaScript generated markup + +Writing markup in a JavaScript file makes the content harder to find, harder to edit, and less performant. Avoid it whenever possible. + +
+ +
+ +
+ +## CSS + +
+ +
+ +
+ +### Syntax + +- All CSS must be compatible with the latest release of Chrome, Firefox, and Safari. + +- Use soft tabs with two spaces—they're the only way to guarantee code renders the same in any environment. +- When grouping selectors, keep individual selectors to a single line. +- Include one space before the opening brace of declaration blocks for legibility. +- Place closing braces of declaration blocks on a new line. +- Include one space after `:` for each declaration. +- Each declaration should appear on its own line for more accurate error reporting. +- End all declarations with a semi-colon. The last declaration's is optional, but your code is more error prone without it. +- Comma-separated property values should include a space after each comma (e.g., `box-shadow`). +- Don't include spaces after commas _within_ `rgb()`, `rgba()`, `hsl()`, `hsla()`, or `rect()` values. This helps differentiate multiple color values (comma, no space) from multiple property values (comma with space). +- Do prefix property values or color parameters with a leading zero (e.g., `0.5` instead of `.5` and `-0.5px` instead of `-.5px`). +- Lowercase all hex values, e.g., `#fff`. Lowercase letters are much easier to discern when scanning a document as they tend to have more unique shapes. +- Use shorthand hex values where available, e.g., `#fff` instead of `#ffffff`. +- Quote attribute values in selectors, e.g., `input[type="text"]`. [They’re only optional in some cases](http://mathiasbynens.be/notes/unquoted-attribute-values#css), and it’s a good practice for consistency. +- Avoid specifying units for zero length values, e.g., `margin: 0;` instead of `margin: 0px;`. (Note that values that are not lengths still require a unit, e.g. `0deg`.) + +Questions on the terms used here? See the [syntax section of the Cascading Style Sheets article](http://en.wikipedia.org/wiki/Cascading_Style_Sheets#Syntax) on Wikipedia. + +
+ +
+ +
+ + /* Bad CSS */ + .selector, .selector-secondary, .selector[type=text] { + padding:15px; + margin:0px 0px 15px; + background-color:rgba(0, 0, 0, .5); + box-shadow:0px 1px 2px #CCC,inset 0 1px 0 #FFFFFF + } + + /* Good CSS */ + .selector, + .selector-secondary, + .selector[type="text"] { + padding: 15px; + margin-bottom: 15px; + background-color: rgba(0,0,0,0.5); + box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff; + } + +
+ +
+ +
+ +
+ +
+ +### Declaration order + +Related property declarations should be grouped together following the order: + +1. Positioning +2. Box model +3. Typographic +4. Visual + +Positioning comes first because it can remove an element from the normal flow of the document and override box model related styles. The box model comes next as it dictates a component's dimensions and placement. + +Everything else takes place _inside_ the component or without impacting the previous two sections, and thus they come last. + +For a complete list of properties and their order, please see [Recess](http://twitter.github.com/recess). + +
+ +
+ +
+ + .declaration-order { + /* Positioning */ + position: absolute; + top: 0; + right: 0; + bottom: 0; + left: 0; + z-index: 100; + + /* Box-model */ + display: block; + float: right; + width: 100px; + height: 100px; + + /* Typography */ + font: normal 13px "Helvetica Neue", sans-serif; + line-height: 1.5; + color: #333; + text-align: center; + + /* Visual */ + background-color: #f5f5f5; + border: 1px solid #e5e5e5; + border-radius: 3px; + + /* Misc */ + opacity: 1; + } + +
+ +
+ +
+ +
+ +
+ +### Don't use `@import` + +Compared to ``s, `@import` is slower, adds extra page requests, and can cause other unforeseen problems. Avoid them and instead opt for an alternate approach: + +- Use multiple `` elements +- Compile your CSS with a preprocessor like Sass or Less into a single file +- Concatenate your CSS files with features provided in Rails, Jekyll, and other environments + +For more information, [read this article by Steve Souders](http://www.stevesouders.com/blog/2009/04/09/dont-use-import/). + +
+ +
+ +
+ + + + + + + +
+ +
+ +
+ +
+ +
+ +### Media query placement + +Place media queries as close to their relevant rule sets whenever possible. Don't bundle them all in a separate stylesheet or at the end of the document. Doing so only makes it easier for folks to miss them in the future. Here's a typical setup. + +
+ +
+ +
+ + .element { ... } + .element-avatar { ... } + .element-selected { ... } + + @media (min-width: 480px) { + .element { ...} + .element-avatar { ... } + .element-selected { ... } + } + +
+ +
+ +
+ +
+ +
+ +### Prefixed properties + +When using vendor prefixed properties, indent each property such that the declaration's value lines up vertically for easy multi-line editing. + +In Textmate, use **Text → Edit Each Line in Selection** (⌃⌘A). In Sublime Text 2, use **Selection → Add Previous Line** (⌃⇧↑) and **Selection → Add Next Line** (⌃⇧↓). + +
+ +
+ +
+ + /* Prefixed properties */ + .selector { + -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15); + box-shadow: 0 1px 2px rgba(0,0,0,.15); + } + +
+ +
+ +
+ +
+ +
+ +### Shorthand notation + +Strive to limit use of shorthand declarations to instances where you must explicitly set all the available values. Common overused shorthand properties include: + +- `padding` +- `margin` +- `font` +- `background` +- `border` +- `border-radius` + +Often times we don't need to set all the values a shorthand property represents. For example, HTML headings only set top and bottom margin, so when necessary, only override those two values. Excessive use of shorthand properties often leads to sloppier code with unnecessary overrides and unintended side effects. + +The Mozilla Developer Network has a great article on [shorthand properties](https://developer.mozilla.org/en-US/docs/Web/CSS/Shorthand_properties) for those unfamiliar with notation and behavior. + +
+ +
+ +
+ + /* Bad example */ + .element { + margin: 0 0 10px; + background: red; + background: url("image.jpg"); + border-radius: 3px 3px 0 0; + } + + /* Good example */ + .element { + margin-bottom: 10px; + background-color: red; + background-image: url("image.jpg"); + border-top-left-radius: 3px; + border-top-right-radius: 3px; + } + +
+ +
+ +
+ +
+ +
+ +### Comments + +Code is written and maintained by people. Ensure your code is descriptive, well commented, and approachable by others. Great code comments convey context or purpose. Do not simply reiterate a component or class name. + +Be sure to write in complete sentences for larger comments and succinct phrases for general notes. + +
+ +
+ +
+ + /* Bad example */ + /* Modal header */ + .modal-header { + ... + } + + /* Good example */ + /* Wrapping element for .modal-title and .modal-close */ + .modal-header { + ... + } + +
+ +
+ +
+ +
+ +
+ +### Class names + +- Keep classes lowercase and use dashes (not underscores or camelCase). Dashes serve as natural breaks in related class (e.g., `.btn` and `.btn-danger`). +- Avoid excessive and arbitrary shorthand notation. `.btn` is useful for _button_, but `.s` doesn't mean anything. +- Keep classes as short and succinct as possible. +- Use meaningful names; use structural or purposeful names over presentational. +- Prefix classes based on the closest parent or base class. +- Use `.js-*` classes to denote behavior (as opposed to style), but keep these classes out of your CSS. + +It's also useful to apply many of these same rules when creating Sass and Less variable names. + +
+ +
+ +
+ + /* Bad example */ + .t { ... } + .red { ... } + .header { ... } + + /* Good example */ + .tweet { ... } + .important { ... } + .tweet-header { ... } + +
+ +
+ +
+ +
+ +## JavaScript + +
+ +
+ +
+ +### General Code Style & Syntax + +All JavaScript must be compatible with the latest release of Chrome, Firefox, and Safari. + + + +Please refer to the [AirBnB Javascript Style Guide](https://github.com/airbnb/javascript) for all basic javascript syntax and code style rules. + +
+
+ +
+
+ +### Function Naming + +1. Functions that **return booleans** should be prefixed with `is`, `has`, `are` or similar keywords distinguish the return value, (e.g: `isVisible`, `areEqual`, `hasEncryption`). + + ```js + function isVisible(element) { + return element.classList.contains('open'); + } + + function areEqual(a, b) { + // function logic + return a === b; + } + + function hasEncryption() { + // function logic + return encryption === true; + } + ``` + +1. Functions that **return HTML snippets** should be prefixed with `render`, for example: + + ```js + function renderButton(label) { + return ``; + } + ``` + +1. Functions that are intended to be **event handlers** should be prefixed with `on`, e.g. `onKeyDown`. If the event handler is specifically for one element within a complex widget, include the element name as well: `onButtonClick`. + + ```js + mainEl.addEventListener('click', onClick); + buttonEl.addEventListener('click', onButtonClick); + listboxEl.addEventListener('keydown', onListboxKeyDown); + ``` + +1. Functions that **mutate state** should be prefixed with `update`, or a verb that describes the state change. The full function name should be in the format of "verb + name of state" For example, `openListbox` or `updateFilterString`. + + ```js + function openListbox() { + this.listbox.open = true; + } + + function updateFilterString(value) { + this.filterString = value; + } + + function updateActiveOption(index) { + this.activeOption = index; + } + ``` + +1. Be specific when naming functions so that whenever possible, the function name is descriptive on its own without needing additional comments to clarify. Prefixing function names with verbs is a good practice. + + ```diff + - function buttonLabel(label) { + - ... + - } + + function applyButtonLabel(label) { + + ... + + } + ``` + +### Function Design + +Ideally functions should be single-purpose, short, and avoid side effects. The goal is to write functions that are easy to read, test, and refactor. + +1. **Pure functions and side effects**: whenever possible, computation and logic should be handled in [pure functions](https://scotch.io/tutorials/wielding-pure-functions-in-javascript-and-function-composition), or functions that do not depend on or mutate external state. Functions that do modify external state should follow an `update*` naming convention as described in the "Function Naming" section. For example, combobox logic to filter options as a user types could be split out as follows: + + ```diff + - function onInput(event) { + - // logic here to get an array of filtered options based on event.target.value and this.options + - const filteredOptions = result; + - + - // logic here to update internal state and the DOM, e.g.: + - this.options = filteredOptions; + - this.listboxEl.innerHTML = ''; + - filteredOptions.forEach((option) => { + - // etc + - this.listboxEl.appendChild(newOptionEl); + - }); + - } + + function onInput(event) { + + const filteredOptions = this.filterOptions(event.target.value, this.options); + + this.updateOptions(filteredOptions); + + } + + + + function filterOptions(filterString, optionArray) { + + // logic here to do actual filtering + + // this is the "pure" function + + return result; + + } + + + + function updateOptions(optionArray) { + + this.options = filteredOptions; + + // optionally split this logic into a renderListbox() function + + // this makes sense to split out if there are any other actions that would result in updating the listbox HTML without also updating this.options + + this.listboxEl.innerHTML = ''; + + filteredOptions.forEach((option) => { + + // etc + + this.listboxEl.appendChild(newOptionEl); + + }); + + } + ``` + +1. **Single-purpose functions**: avoid double-barrelled functions, for example `setFocusAndDoStuff()`. Calling `setFocus()` and `doStuff()` makes the logic for each step easier to read and understand. Ideally this also makes them easier to name and potentially refactor. + + ```diff + - function onOptionClick(event) { + - this.closeListboxAndUpdateValue(event.target.innerText); + - } + - + - function closeListboxAndUpdateValue(newValue) { + - // logic here to update listbox state + - // ... + - + - // logic here to update the listbox value + - buttonEl.innerText = newValue; + - } + + function onOptionClick(event) { + + this.closeListbox(); + + this.updateValue(event.target.innerText); + + } + + + + function closeListbox(event) { + + // logic here to update listbox state + + // ... + + } + + + + function updateValue(event) { + + // logic here to update the listbox value + + buttonEl.innerText = newValue; + + } + ``` + +1. Avoid nested conditionals and ternaries + + ```diff + - function getResult() { + - let result; + - if (A) { + - result = resultA(); + - } else { + - if (B) { + - result = resultB(); + - } else { + - if (C) { + - result = resultC(); + - } else { + - result = resultD(); + - } + - } + - } + - return result; + - } + - + + function getResult() { + + if (A) return resultA(); + + if (B) return resultB(); + + if (C) return resultC(); + + return resultD(); + + } + ``` + +### Widget Structure + +When writing the javascript for a new widget, there are certain guidelines for code organization and structure to maintain consistency with other APG widgets. + +1. Widgets should be defined as classes, with one class per widget. +1. Internal variables and functions should be in the following order within the widget class, and sorted alphabetically: + 1. Constructor function + 1. Properties + 1. State variables + 1. All other functions +1. Widgets should not be written to expose any of their internal state variables or functions. Communication should be done through emmitting events and handling property updates. +1. Here is a basic skeleton for creating a new widget class: + + ```js + /* + * This content is licensed according to the W3C Software License at + * https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document + */ + + 'use strict'; + + class Cat { + constructor(el, name) { + // properties + this.el = el; + this.name = name; + + // states + this.isPurring = false; + this.isSleeping = false; + } + + onPet(event) { + // etc + } + + renderCat() { + // create DOM + // attach event handlers + // etc. + } + + updatePurring(purring) { + this.isPurring = true; + } + + // etc. + } + ``` + +### Comments + +1. Refer to the [AirBnB comment styles](https://github.com/airbnb/javascript#comments) for comment formatting. +1. Avoid adding comments that just duplicate information in a function name or variable name. +1. Comments should not explain "what" something is doing, but rather the "why". +1. JSDoc comments are not needed +
+ +### CodePen + +We are constraint slightly in the code design of our examples because we would like all examples to be opened in a CodePen. You can read more about how we add the "Open in CodePen" button on the [CodePen wikipage](https://github.com/w3c/aria-practices/wiki/How-to-add-the-Open-In-Codepen-button-to-an-example-page). + +
+ +<3 + +Heavily inspired by [Idiomatic CSS](https://github.com/necolas/idiomatic-css) and the [GitHub Styleguide](http://github.com/styleguide). Made with all the love in the world by [@mdo](https://twitter.com/mdo). + +Open sourced under MIT. Copyright 2016 [@mdo](https://twitter.com/mdo). + +
diff --git a/content/about/code-guide/code-guide.html b/content/about/code-guide/code-guide.html new file mode 100644 index 0000000000..8bed58c4f5 --- /dev/null +++ b/content/about/code-guide/code-guide.html @@ -0,0 +1,24 @@ + + + + + + + Code Guide + + + + + + + + + + +
+

Code Guide

+ +
+ + + From 3b1c286f04e33915afe91ef2753cf60a9fc5781e Mon Sep 17 00:00:00 2001 From: Matt King Date: Sat, 10 Aug 2024 17:13:53 -0700 Subject: [PATCH 2/4] Rename file --- Code-Guide.md => CODE_GUIDE.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Code-Guide.md => CODE_GUIDE.md (100%) diff --git a/Code-Guide.md b/CODE_GUIDE.md similarity index 100% rename from Code-Guide.md rename to CODE_GUIDE.md From d1d61c6a9f397d53961e7df2a024fb1bfa92443a Mon Sep 17 00:00:00 2001 From: Matt King Date: Sat, 10 Aug 2024 17:42:54 -0700 Subject: [PATCH 3/4] try putting code-guide in a docs subfolder --- README.md | 2 +- CODE_GUIDE.md => docs/code-guide.md | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename CODE_GUIDE.md => docs/code-guide.md (100%) diff --git a/README.md b/README.md index 242de757bc..f34649ddea 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Note: Please feel free to ask questions either through an issue or on the [Autho ### Code conformance -This repository utilizes [linting](https://en.wikipedia.org/wiki/Lint_%28software%29) tools to do static code analysis and ensure consistent code quality across HTML, CSS, and JavaScript. Each linting tool and respective code standards are documented below and in the [code guide](https://github.com/w3c/aria-practices/wiki/Code-Guide). +This repository utilizes [linting](https://en.wikipedia.org/wiki/Lint_%28software%29) tools to do static code analysis and ensure consistent code quality across HTML, CSS, and JavaScript. Each linting tool and respective code standards are documented below and in the [code guide](docs/code-guide.md). Pull requests that contain linting errors will not be merged until the errors are resolved. To make this easier, you can install and run the tools locally before pushing code. Also note that the tools for CSS and JavaScript will automatically fix many issues if you have them installed locally. To install these tools: diff --git a/CODE_GUIDE.md b/docs/code-guide.md similarity index 100% rename from CODE_GUIDE.md rename to docs/code-guide.md From e8edd3b113389bd065c2598af874e3802cd8dc0e Mon Sep 17 00:00:00 2001 From: Matt King Date: Mon, 19 Aug 2024 23:35:11 -0700 Subject: [PATCH 4/4] Remove draft of code guide page from content/about --- content/about/code-guide/code-guide.html | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 content/about/code-guide/code-guide.html diff --git a/content/about/code-guide/code-guide.html b/content/about/code-guide/code-guide.html deleted file mode 100644 index 8bed58c4f5..0000000000 --- a/content/about/code-guide/code-guide.html +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - Code Guide - - - - - - - - - - -
-

Code Guide

- -
- - -