diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f28506d --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2017 doyoe.com + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md index 79c98b7..f529ea8 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ 3. [通用约定](#general) * [文档目录结构](#directory) * [分离](#separate) + * [文件命名](#file-name) * [缩进](#indentation) * [编码](#encoding) * [小写](#lowercase) @@ -27,15 +28,15 @@ * [按模块添加注释](#html-comment) * [格式](#format) * [语义化标签](#semantic) + * [模块化](#html-module) 5. [CSS约定](#css) * [文件引用](#link) * [命名-组成元素](#element) * [命名-词汇规范](#word) * [命名-缩写规范](#abbr) * [命名-前缀规范](#prefix) - * [复用与重写](#reuse) * [id与class](#id) - * [视觉封装](#packaging) + * [书写格式](#packaging) * [规则与分号](#semicolon) * [0与单位](#unit) * [0与小数](#decimal) @@ -44,9 +45,15 @@ * [属性书写顺序](#order) * [注释规范](#css-comment) * [hack规范](#hack) - * [避免类型选择器](#type-selector) + * [避免低效率选择器](#low-selector) * [属性缩写与分拆](#override) -6. [结语](#end) + * [模块化](#css-module) +6. [图像约定](#img) + * [图像压缩](#img-compress) + * [背景图](#background-image) + * [前景图](#image) + * [Sprite](#sprite) +7. [结语](#end) ## 规范概述 @@ -56,16 +63,17 @@ * 降低每个组员介入项目的门槛成本; * 提高工作效率及协同开发的便捷性; * 高度统一的代码风格; -* 提供一整套HTML、CSS解决方案,来帮助开发人员快速做出高质量的符合要求的页面; +* 提供一整套HTML、CSS解决方案,来帮助开发人员快速做出高质量的符合要求的页面。 ## 基本信息 规范名称 | Cook --------|------| -当前版本 | v0.0.1 -规范制定 | [杜瑶(@doyoe)](http://weibo.com/doyoe) -最后更新 | 2014.10.11 +当前版本 | v1.0 beta +规范发起 | [杜瑶(@doyoe)](http://weibo.com/doyoe) +参与人群 | Qunar FED +最后更新 | 2015.03.20 ## 通用约定 @@ -73,7 +81,54 @@ ### 1.文档目录结构 -待添加。。。 +``` +|-- 项目名 + |-- src 开发环境 + |-- html 静态页面模板目录 + |-- bgimg 背景图目录(假设有的话) + |-- image 前景图目录(假设有的话) + |-- font 字体目录(假设有的话) + |-- scripts 脚本目录 + |-- styles(Yo) 样式目录 + |-- lib 基础库 + |-- core 核心代码:reset + |-- element 元素 + |-- fragment 公用碎片 + |-- layout 布局 + |-- widget 组件 + |-- usage 项目具体实现 + |-- project 某个子项目 + |-- core 核心代码:桥接lib中的core,可以进行项目级扩展 + |-- fragment 项目公用碎片 + |-- module 模块 + |-- page page桥接文件目录:src-list + |-- export page pack之后的文件目录 + |-- prd 生产环境 + |-- bgimg 背景图目录(假设有的话) + |-- image 前景图目录(假设有的话) + |-- font 字体目录(假设有的话) + |-- scripts 脚本目录 + |-- styles(Yo) 样式目录 + |-- project1 子项目 + |-- index.css + |-- login.css + |-- and etc... + |-- project2 子项目 + |-- index.css + |-- login.css + |-- and etc... + |-- and etc... +``` + +> `src`, `scripts`, `styles` 三个目录是为了和现有项目保持一致,避免修改过大,所以保持不变。 + +> `html` 目录,用于存放前端开发做的静态页面,以备查阅、备份、review或给后端套页面。 + +> `bgimg`, `image`, `font` 三个目录在Qunar一般不会直接存在,因为我们有source服务器,这些资源都会在那上面管理;不过特殊情况也会有,比如一些独立的项目,没有使用source的,那么就需要遵循这样的目录划分。 + +> 至于 `html`, `bgimg`, `image`, `font` 这几个目录为什么没有加 `s`,主要是因为不希望大家去想某个目录是否为复数,简单点就好。 + +> `prd` 为生产环境目录,以 `xxx` 项目中的一个子项目 `mobile` 为例,其生产环境中的某个CSS外链大致如下:`//sitename.com/prd/styles/mobile/index.css` ### 2.分离 @@ -82,32 +137,59 @@ > 将结构与表现、行为分离,保证它们之间的最小耦合,这对前期开发和后期维护都至关重要。 + +### 3.文件命名 + +* CSS模块文件,其文件名必须与模块名一致; + +假定有这样一个模块: + + .m-detail { sRules; } + .m-detail-hd { sRules; } + .m-detail-bd { sRules; } + .m-detail-ft { sRules; } + +> 那么该模块的文件名应该为:`m-detail.css` + +* CSS页面文件,其文件名必须与HTML文件名一致; + +> 假定有一个HTML页面叫 `product.html`,那么其相对应的CSS页面文件名应该为:`product.css` + +> 假定现在有一个 `product.html`,里面有2个模块: + + +
+ +
+ +那么开发人员能快速找到与该页面相关的3个直接CSS文件,包括:`product.css`, `m-list.css`, `m-info.css` + -### 3.缩进 +### 4.缩进 使用tab(4个空格宽度)来进行缩进,可以在IDE里进行设置 -### 4.编码 +### 5.编码 * 以 UTF-8 无 BOM 编码作为文件格式; * 在HTML中文档中用 `` 来指定编码; -* 不需要为CSS显示定义编码,因为它默认为utf-8; +* 为每个CSS文档显示的定义编码,在文档首行定义 `@charset "utf-8";` + +> 在 Sass 中,如果文档中出现中文,却未显示定义编码,将会编译出错,为了统一各种写法,且提前规避错误几率,统一要求每个CSS文档都需要定义编码 -### 5.小写 +### 6.小写 -* 所有的HTML标签必须小写 -* 所有的HTML属性必须小写 -* 所有的样式名及规则必须小写 +* 所有的HTML标签必须小写; +* 所有的HTML属性必须小写; +* 所有的样式名及规则必须小写。 -### 6.注释 +### 7.注释 尽可能的为你的代码写上注释。解释为什么要这样写,它是新鲜的方案还是解决了什么问题。 -### 7.待办事项 +### 8.待办事项 用 TODO 标示待办事项和正在开发的条目 @@ -117,15 +199,15 @@ ... /* TODO: 图文混排 comm: g-imgtext */ - .g-imgtext{...} + .g-imgtext { sRules; } -### 8.行尾空格 +### 9.行尾空格 删除行尾空格,这些空格没有必要存在 -### 9.省略嵌入式资源协议头 +### 10.省略嵌入式资源协议头 省略图像、媒体文件、样式表和脚本等URL协议头部声明 ( http: , https: )。如果不是这两个声明的URL则不省略。 @@ -138,7 +220,7 @@ 推荐: - + 不推荐: ``` @@ -158,10 +240,10 @@ > 注:省略协议头在IE7-8下会有一点小问题,外部CSS文件(link和@import)会被下载两遍,所以该条目的约定看具体项目。 -### 10.代码有效性 +### 11.代码有效性 * 使用 [W3C HTML Validator](http://validator.w3.org/) 来验证你的HTML代码有效性; -* 使用 [W3C CSS Validator](http://jigsaw.w3.org/css-validator/) 来验证你的CSS代码有效性; +* 使用 [W3C CSS Validator](http://jigsaw.w3.org/css-validator/) 来验证你的CSS代码有效性。 > 代码验证不是最终目的,真的目的在于让开发者在经过多次的这种验证过程后,能够深刻理解到怎样的语法或写法是非标准和不推荐的,即使在某些场景下被迫要使用非标准写法,也可以做到心中有数。 @@ -171,15 +253,15 @@ ### 1.文档类型 -* 统一使用HTML5的标准文档类型:`` +* 统一使用HTML5的标准文档类型:``; > HTML5文档类型具备前后兼容的特质,并且易记易书写 -* 在文档doctype申明之前,不允许加上任何非空字符 +* 在文档doctype申明之前,不允许加上任何非空字符; > 任何出现在doctype申明之前的字符都将使得你的HTML文档进入非标准模式 -* 不允许添加属性强制改变文档模式 +* 不允许添加 `` 标签强制改变文档模式。 > 避免出现不可控的问题 @@ -188,12 +270,12 @@ 在调用CSS和JavaScript时,可以将type属性省略不写 -不推荐: +不允许: -推荐: +应该: @@ -205,12 +287,12 @@ 非必须属性值可以省略 -不推荐: +不允许: -推荐: +应该: @@ -222,11 +304,11 @@ 所有的标签属性值必须要用双引号包裹,同时也不允许有的用双引号,有的用单引号的情况 -不推荐: +不允许: 去哪儿网 -推荐: +应该: 去哪儿网 @@ -235,28 +317,28 @@ 所有元素必须正确嵌套 -* 不允许交叉 -* 不允许inline元素包含block元素 -* 不允许类似在ul下出现除了li外的其它子元素等等 +* 不允许交叉; -不推荐: +不允许: + + 交叉嵌套 + +应该: + + 交叉嵌套 + +* 不允许非法的子元素嵌套。 + +不允许: - -

这是一个块级h1元素

-

这是一个块级p元素

-
-推荐: +应该: -
-

这是一个块级h1元素

-

这是一个块级p元素

-

xx列表

    @@ -265,19 +347,41 @@
-> 规则可参考:[嵌套规则](http://www.cs.tut.fi/~jkorpela/html/strict.html)。由于某些现实原因,在HTML5中对a元素做了一些变更,a元素除了可以包含inline元素外,也将可以包含block元素了。 +* 不推荐inline元素包含block元素; + +不推荐: + + +

这是一个块级h1元素

+

这是一个块级p元素

+
+ +推荐: + +
+

这是一个块级h1元素

+

这是一个块级p元素

+
+ +> 规则可参考: + +> HTML4/XHTML1.0 Strict: [嵌套规则](http://www.cs.tut.fi/~jkorpela/html/strict.html)。 +> +> HTML5: [嵌套规则](http://www.w3.org/TR/html5/) +> +> 举个例子,在HTML5中,a元素同时属于 Flow content, Phrasing content, Interactive content, Palpable content 4个分类,那些子元素是 phrasing 元素的元素可以是 a 的父元素,a 允许的子元素是以它的父元素允许的子元素为准,但不能包含 interactive 元素。 ### 6.标签闭合 所有标签必须闭合 -不推荐: +不允许:
balabala... -推荐: +应该:
balabala...
@@ -287,9 +391,9 @@ ### 7.多媒体替代方案 -* 为img元素加上alt属性 -* 为视频内容提供音轨替代 -* 为音频内容提供字母替代等等 +* 为img元素加上alt属性; +* 为视频内容提供音轨替代; +* 为音频内容提供字母替代等等。 不推荐: @@ -306,15 +410,15 @@ 为表单元素label加上for属性 -不推荐: +不允许: - - + + -推荐: +应该: - - + + > for属性能让点击label标签的时候,同时focus到对应的 input 和 textarea上,增加响应区域 @@ -334,15 +438,15 @@ > 注释内容左右两边保留和注释符号有1个空格位,在注释内容内不允许再出现中划线“-”,某些浏览器会报错。 -> +> > 注释风格保持与原生HTML的语法相似:成对出现 `` ### 10.格式 -* 将每个块元素、列表元素或表格元素都放在新行 -* inline元素视情况换行,以长度不超过编辑器一屏为宜 -* 每个子元素都需要相对其父级缩进 +* 将每个块元素、列表元素或表格元素都放在新行; +* inline元素视情况换行,以长度不超过编辑器一屏为宜; +* 每个子元素都需要相对其父级缩进(参见[缩进约定](#indentation))。 不推荐: @@ -358,23 +462,45 @@ ### 11.语义化标签 -* 根据HTML元素的本身用途去使用它们 -* 禁止使用被废弃的用于表现的标签,如 center, font 等 -* 部分在XHTML1中被废弃的标签,在HTML5中被重新赋予了新的语义,在选用时请先弄清其语义,如:b, i, u等 +* 根据HTML元素的本身用途去使用它们; +* 禁止使用被废弃的用于表现的标签,如 center, font 等; +* 部分在XHTML1中被废弃的标签,在HTML5中被重新赋予了新的语义,在选用时请先弄清其语义,如:b, i, u等。 -不推荐: +不允许:

标题

-推荐: +应该:

标题

- + > 虽然使用p标签,也可以通过CSS去定义它的外观和标题相同,但p标签本身的并不是表示标题,而是表示文本段落 -> 参阅:[HTML5 Elements](http://www.w3.org/TR/html5/) +> 参阅:[HTML5 Elements](http://www.w3.org/TR/html5/) + +### 12.模块化 +* 每个模块必须有一个模块名; +* 每个模块的基本组成部分应该一致; +* 模块的子节点类名需带上模块名(防止模块间嵌套时产生不必要的覆盖); +* 孙辈节点无需再带模块名。 + +代码如: + +
+
+

模块标题

+
+
+

一些实际内容

+
+ +
+ +> 其中 `.m-detail-hd`, `.m-detail-bd`, `.m-detail-ft` 为可选,视具体模块情况决定是否需要抽象为这种 **头,中,尾** 的结构 ## CSS约定 @@ -383,6 +509,9 @@ ### 1.文件引用 * 一律使用link的方式调用外部样式 +* 不允许在页面中使用 `