14
12
2010
WebDev:FrontendCodeStandards
bigCat Posted in JavaScript - Comment.isBlankWebDev:FrontendCodeStandards
WebDev:FrontendCodeStandards
| Contents
[hide] |
Browser Requirements
Support Levels
- A grade:
- Roughly 90% or more of functionality and design is supported and functional
- 大約 90% 的功能和版面要能正確運作
- Variations between browsers is acceptable
- 不同瀏覽器的表現可以有差異
- 'Advanced' browsers can receive a higher fidelity experience with other browsers receiving a lower fidelity
- 使用較好的瀏覽器可以得到比較好的使用體驗
- B grade:
- Larger rendering issues allowed
- 允許較難處理的問題存在
- Core functionality and content should still be accessible
- 主要的功能和內容仍需要可以使用
- Fallback to basic html from dhtml/ajax where necessary
- 需要時降到純 HTML 而不使用 DHTML/AJAX
If it's not on the chart, we don't support it.
不在表上的表示不支援
Mobile Devices
| Support Level | |
| Mobile IE | A-Grade |
| Fennec | A-Grade |
| Opera Mobile | A-Grade |
Internal Tools
| Linux | Win XP | Vista | OS 10.5 | OS 10.6 | |
| Fx 3.5 | A-Grade | A-Grade | A-Grade | A-Grade | A-Grade |
| Fx 3 | B-Grade | B-Grade | B-Grade | B-Grade | B-Grade |
Minimum Browser Support
Support of other browsers is left to each product team.
其餘的瀏覽器支援交給各產品團隊決定
| Linux | Win7 | Win XP | Vista | OS 10.5 | OS 10.6 | ||
| IE 8 | A-Grade | A-Grade | A-Grade | ||||
| IE 7 | A-Grade | A-Grade | |||||
| Fx 3.5 | A-Grade | A-Grade | A-Grade | A-Grade | A-Grade | A-Grade | |
| Fx 3 | A-Grade | A-Grade | A-Grade | A-Grade | A-Grade | A-Grade | |
| Safari 4 | A-Grade | A-Grade | A-Grade | A-Grade | A-Grade | ||
| Opera 10 | A-Grade | A-Grade | A-Grade | A-Grade | A-Grade | A-Grade | |
| Chrome | A-Grade | A-Grade | A-Grade | ||||
| Gecko based | B-Grade | B-Grade | B-Grade | B-Grade | B-Grade | B-Grade |
Other Support
- New Sites/Projects: Progressive enhancement for advanced functionality
- 新網站/計畫:進階功能使用漸進增強方式加上
- Current Sites: make new functionality degrade, don't rewrite current code
- 現有網站:不要修改現有的程式,讓新功能?
- Need to work on accessibility! (ex: AMO Control Panel)
- 需要關注親和力問題
QA Requirements
QA should test and file bugs appropriately for the browser support chart that applies to each site.
Accessibility Requirements
- Add basic ARIA support where appropriate
- 加入基本的 ARIA 支援(包括 landmark role, required, invalid, live region)
Code Standards
- Layered semantic markup
- Semantic class/id names and appropriate HTML tags for content
- 有語意的 class/id 名稱和適當的 HTML 標籤
- No inline CSS & JS unless absolutely necessary
- 非必要不使用行內 CSS/JS
- Progressive enhancement (see bolded QA requirements)
- 漸進增強
- HTML:
- HTML5 doctype. Safe to use for all browsers.
- 使用 HTML5 doctype 宣告,對所有瀏覽器都適用
- Pages should validate. Not a strict requirement. Some layouts can't be achieved without a few errors.
- 頁面需要盡量通過標準檢測,此項要求並非絕對需要遵守,因為部分版面設計無法在完全遵守標準的狀況下實作
- Use most meaningful tags for content
- 使用最能表現物件內容意義的標籤
- Images/bg images with text in them must have alternate textual representation (alt text or text positioned offscreen for screenreaders). Avoid these due to L10n difficulties anyway.
- 圖片或背景圖有文字時,需要加上文字資訊(例如替代文字或讓文字出現在版面範圍之外)
- CSS:
- Separate stylesheets for other browsers (IE) are ok
- 可以為特定瀏覽器獨立 CSS 檔案
- If only 1 or 2 properties are changed, keeping them in the original stylesheet is ok
- 如果只有意兩個屬性的修改,可以不必分到不同檔案
- For new sites, look into YUI Fonts & Reset, this will reduce browser layout differences
- 新網站可以參考 YUI Fonts 和 Reset ,這會幫助減少花在處理不同瀏覽器的版面差異的時間
- Semantic classnames
- 有語意的 class 名稱
- Don't use position: absolute unless absolutely necessary
- 非必要不使用 absolute
- Use least amount of selectors possible to allow for overriding
- 近可以能減少 selector 的使用,讓他比較容易被覆蓋
- Don't use !important (see above)
- 不要使用 !importanr
- Specify font sizes in EMs or %
- 使用 EM 或 % 指定字型大小
- Clear floats with zoom:1 & content:after
- 使用 zoom:1 和 content:after 來 clear float
- Hide content for screen readers with position:absolute and position offscreen. Display:none hides content from screen readers.
- 如果要隱藏部分訊息,使用 position: absolute ,並把位置移到畫面外,使用 display: none 的話,會讓螢幕報讀器也看不到這些內容
- Design layouts to stretch according to their contents (good for localization and good for font-resizing)
- Avoid selectors that are native element names (i.e. don't style on "div" or "p") except when resetting.
- 避免只使用標籤名稱做 selector ,除非要重設
- Specify in comments the scope of generic sounding class names (what does .block mean and where is it applied? one page or multiple pages?)
- Try avoiding generic class names altogether
- JS
- Don't pollute the global namespace (put all functions into objects/closures)
- 別使用全域變數
- Use Module pattern for singletons? (http://yuiblog.com/blog/2007/06/12/module-pattern/)
- Use .prototype for objects
- 物件可以使用 .prototype
- Use jQuery
- 使用 jQuery
- Very fast & light
- 非常快且輕量
- If you need another library, please concatenate & use as little as possible
- 如果需要使用其他函式庫,盡可能減少使用
- Make JS reusable as best as possible.
- 盡量讓 JS 可以重複利用
- var your variables
- 變數都用 var 宣告
- Use JSlint : http://www.jslint.com/
- 使用 JSLint
Performance Standards
Page requirements
- Min score of B on homepage and 1 other 'major' page
- ~400k max page size
- 頁面最大不要超過 400k
Recommendations
- Follow Yahoo!'s 34 rules
- 遵守 Yahoo 的 34 條規範
- http://developer.yahoo.com/performance/rules.html
- Follow these best practices:
- 遵守這些最佳實踐
- http://developer.yahoo.net/blog/archives/2008/01/the_7_habits_fo.html
- Sprite images
- 使用 Sprite images 技巧(多長小圖放在一張大圖內)
- Concat and minify JS & CSS with
- 串皆並最小化 JS 和 CSS
- YUI Compressor (http://www.julienlecomte.net/blog/2007/08/13/)
- or Minify (http://code.google.com/p/minify/)
- Max 3s load time for all pages (tracked via Gomez)
- 頁面讀取時間不超過 3 秒
- Use YSlow to see where pages can be improved
- 使用 YSlow 檢查頁面是否還有可以加強的地方
(see Webtools:Scalability for an overview of tools/infrastructure)
Security Standards
To be branched off into separate document
Also IT's responsibility, we should work with them together on this
- Protect against cross-site request forgeries with crumbs (http://developer.yahoo.com/security/)
- Filter all input (SQL, HTML, JS, CSS)
- 所有的使用者輸入都要過濾
- Verify user has permissions on content they are editing/deleting
- Don't include JS from 3rd party domains
- 不要從其它網域讀取 JS 檔案
- Read http://phpsec.org/projects/guide/
- More?
Video Standards
- Players, in order of degradation...
- 播放器採用以下的優先順序
- <video>, obviously the first choice
- <video> 當然是第一優先
- Supported by FF 3.5+, Safari 3+, Chrome
- FF 3.5+, Safari 3+, Chrome 都支援
- Quicktime
- IE w/ quicktime installed, iPhone
- IE 且有安裝 Quicktime, iPhone
- Flash
- Check out open source JW-FLV player
- 先使用開放原碼的播放器
- YouTube, DailyMotion, etc are also options
- 也可以使用 YouTube, 或 DailyMotion 來放置影片
- Download links
- If all else fails, at least let the user download easily
- 如果以上都不可行,那就讓訪客可以簡單下載檔案
- Formats
- OGG - Firefox
- MP4 / H.264 - everything else
- Consider high vs. low vs. audio-only formats?
- Resources
- Video For Everybody - excellent page detailing complete setup, from codecs to HTML
- Open source flash player
- Hosting
- Currently, file a bug with IT (mozilla.org:server ops) and host at videos.mozilla.org
- https://intranet.mozilla.org/Uploading_Videos
- Link to videos should use CDN (videos-cdn.mozilla.net)