<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by kmsheng on Medium]]></title>
        <description><![CDATA[Stories by kmsheng on Medium]]></description>
        <link>https://medium.com/@kmsheng?source=rss-45a490cd3e32------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Sqy02MMZDymaZx8x9zmvug.png</url>
            <title>Stories by kmsheng on Medium</title>
            <link>https://medium.com/@kmsheng?source=rss-45a490cd3e32------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 22:03:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kmsheng/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Linux set 的用法]]></title>
            <link>https://kmsheng.medium.com/linux-set-%E7%9A%84%E7%94%A8%E6%B3%95-ca3338686c51?source=rss-45a490cd3e32------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca3338686c51</guid>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[kmsheng]]></dc:creator>
            <pubDate>Fri, 16 Sep 2022 12:10:10 GMT</pubDate>
            <atom:updated>2022-09-16T12:12:27.171Z</atom:updated>
            <content:encoded><![CDATA[<p>.</p><h4>預設 ( 不加任何參數時 )</h4><p>set 指令在預設不加任何參數執行時，會列出系統所有的環境變數與方法，例如:</p><pre>set</pre><pre>&#39;!&#39;=0<br>&#39;#&#39;=0<br>&#39;$&#39;=84322<br>&#39;*&#39;=(  )<br>-=569JNRTXZghilms<br>0=-zsh<br>&#39;?&#39;=0<br>@=(  )<br>ARGC=0</pre><h4>-e 參數</h4><p>執行 script 時遇到任何錯誤就停下來，錯誤為非 0 的 exit status</p><pre>#!/bin/bash<br>set -e<br>cat blahblahblah<br>echo &quot;will this line be called ?&quot;</pre><h4>-C 參數</h4><p>啟用時確保 script 不會覆蓋任何現有檔案</p><pre>#!/bin/bash<br>echo &quot;content&quot; &gt; file<br>set -C<br>echo &quot;try to override it&quot; &gt;&gt; file</pre><h4>-f 參數</h4><p>啟用時禁止使用 globbing，例如</p><pre>#!/bin/bash<br>set -f<br>ls *.txt<br># ls: *.txt: No such file or directory</pre><h4>-x 參數</h4><p>印出目前正在執行的程式</p><h3>如何取消已經設定的選項 ?</h3><p>使用 + 符號把選項取消，例如: set +x</p><h3>如何得知目前有設定哪些選項 ?</h3><p>檢查 - 這個變數，例如: echo $-</p><p>參考連結:</p><p><a href="https://www.baeldung.com/linux/set-command">https://www.baeldung.com/linux/set-command</a></p><p><a href="https://superuser.com/questions/472340/how-can-i-know-if-the-set-e-option-is-enabled-or-not">https://superuser.com/questions/472340/how-can-i-know-if-the-set-e-option-is-enabled-or-not</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca3338686c51" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue 3 Computed Properties 重點整理]]></title>
            <link>https://kmsheng.medium.com/vue-3-computed-properties-%E9%87%8D%E9%BB%9E%E6%95%B4%E7%90%86-653e7ac9f34a?source=rss-45a490cd3e32------2</link>
            <guid isPermaLink="false">https://medium.com/p/653e7ac9f34a</guid>
            <category><![CDATA[vue-3]]></category>
            <dc:creator><![CDATA[kmsheng]]></dc:creator>
            <pubDate>Thu, 08 Sep 2022 06:47:41 GMT</pubDate>
            <atom:updated>2022-09-08T06:48:32.766Z</atom:updated>
            <content:encoded><![CDATA[<h3>基本用法</h3><p>有時候會需要使用 reactive 物件來組成另外一個資料顯示，例如:「姓」跟「名」組成「全名」，可以使用 computed 方法印出組合後的結果</p><pre>&lt;script setup&gt;<br>import { computed, ref } from &#39;vue&#39;</pre><pre>const firstName = ref(&#39;Kuan&#39;)<br>const lastName = ref(&#39;Sheng&#39;)<br>const fullName = computed(() =&gt; firstName.value + &#39; &#39; + lastName.value)<br>&lt;/script&gt;</pre><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;div&gt;First Name: {{ firstName }}&lt;/div&gt;<br>    &lt;div&gt;Last Name: {{ lastName }}&lt;/div&gt;<br>    &lt;div&gt;Full Name: {{ fullName }}&lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><h4>computed 與一般方法的差異</h4><p>computed 方法 return 出去的東西可以依照 reactive 物件做 caching，一般的方法如果沒有特別實作的話，則沒有 cache 功能</p><h3>可寫入的 computed</h3><p>參考底下範例，computed 可以透過 setter 設定 firstName 與 lastName 的 ref value</p><pre>&lt;script setup&gt;<br>import { computed, ref } from &#39;vue&#39;</pre><pre>const firstName = ref(&#39;Kuan&#39;)<br>const lastName = ref(&#39;Sheng&#39;)</pre><pre>const fullName = computed({<br>  get() {<br>    return firstName.value + &#39; &#39; + lastName.value<br>  },<br>  set(newValue) {<br>    [firstName.value, lastName.value] = newValue.split(&#39; &#39;)<br>  }<br>})</pre><pre>const change = () =&gt; fullName.value = &#39;Someone Else&#39;<br>&lt;/script&gt;</pre><pre>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;div&gt;First Name: {{ firstName }}&lt;/div&gt;<br>    &lt;div&gt;Last Name: {{ lastName }}&lt;/div&gt;<br>    &lt;div&gt;Full Name: {{ fullName }}&lt;/div&gt;</pre><pre>&lt;button <a href="http://twitter.com/click">@click</a>=&quot;change&quot;&gt;change First Name and Last Name&lt;/button&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><h4>最佳實踐</h4><p><strong>Computed 當 getters 用時要避免 side effect</strong></p><p>例如非同步請求或修改 dom，此類有 side effect 的行為不應在 computed 的 getter 裡面做</p><p><strong>避免修改 computed 產生的結果</strong></p><p>因為 computed 本來就會依照 reactive 物件計算出結果，直接修改計算後的結果是沒有意義的</p><p>參考連結:</p><p><a href="https://vuejs.org/guide/essentials/computed.html">https://vuejs.org/guide/essentials/computed.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=653e7ac9f34a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vue 3 的 Reactivity Fundamentals 重點整理]]></title>
            <link>https://kmsheng.medium.com/vue-js-%E7%9A%84-reactivity-fundamentals-%E9%87%8D%E9%BB%9E%E6%95%B4%E7%90%86-94bdb0053de8?source=rss-45a490cd3e32------2</link>
            <guid isPermaLink="false">https://medium.com/p/94bdb0053de8</guid>
            <category><![CDATA[vue-3]]></category>
            <dc:creator><![CDATA[kmsheng]]></dc:creator>
            <pubDate>Thu, 08 Sep 2022 05:03:22 GMT</pubDate>
            <atom:updated>2022-09-08T06:04:52.092Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>reactive</strong></h3><p>想要修改物件順便自動更新畫面時，可以使用 Vue 提供的 reactive 方法，reactive 的內部實作是透過 JS 的 Proxy 來追蹤物件屬性的更新，</p><p>用法如下:</p><pre>import { reactive } from &#39;vue&#39;</pre><pre>const data = reactive({ count: 0 })</pre><pre>// 下面如果有 template 叫用 data.count 時，畫面會更新 count 的數字<br>data.count++</pre><p>使用 reactive 要小心底下狀況:</p><ol><li>產生出來的 proxy 物件不等於原始資料</li></ol><pre>const raw = {}<br>const cloned = reactive(raw)</pre><pre>// raw 不會等於 cloned<br>// 因為 cloned 是 reactive 生出來的 proxy 物件<br>// 所以不會有相同的 reference。<br>raw === cloned    // false</pre><p>2. 指派到 reactive 物件屬性的值也會自動被轉成 proxy 物件</p><pre>const data = reactive({})<br>const raw = {}<br>data.raw = raw</pre><pre>// Vue 的 reactive 物件會把設定到屬性的值 ( 非 primitive )<br>// 也轉成 proxy 物件<br>data.raw === raw   // false</pre><p>3. 被重新指派時，會無法追蹤原來物件的資料</p><pre>let data = reactive({ count: 0 })</pre><pre>// 重新指派新的 reactive 物件會讓舊的 data 失效<br>data = reactive({ count: 1 })</pre><h3>ref</h3><p>如果是 primitive type，例如字串或數字沒有辦法使用 reactive 方法時，可以使用 ref 替代，例如:</p><pre>import { ref } from &#39;vue&#39;</pre><pre>const count = ref(0)</pre><pre>// 必須透過 value 屬性操作<br>count.value++</pre><p>.</p><pre>// 把物件丟到 ref 時<br>const obj = ref({ msg: &#39;hello&#39; })</pre><pre>// 生成的 obj.value 為 proxy 物件<br>obj.value</pre><p>.</p><pre>const obj = {<br>  ref1: ref(1),<br>  ref2: ref(2)<br>}</pre><pre>// ref1, ref2 一樣可以被 destruct，拿來單獨使用<br>const { ref1, ref2 } = obj</pre><p>ref 餵到 template 或 reactive 方法時，會自動被解開，例如:</p><pre>const count = ref(0)</pre><pre>.</pre><pre>&lt;div&gt;{{ count }}&lt;/div&gt;<br>&lt;!-- 這裡寫 count 就好，不需要寫成 count.value --&gt;</pre><p>自動解開的行為只會套用在第一層屬性，如果是超過一層時，則還是需要寫 .value 來存取，例如 root.count.value</p><p>.</p><pre>const count = ref(0)<br>const obj = reactive({ count })</pre><pre>obj.count    // 0</pre><h3>Reactivity Transform</h3><p>實驗性質的功能，有啟用這個選項時，操作 ref 的物件不需要呼叫 .value</p><p>參考連結:</p><p><a href="https://vuejs.org/guide/essentials/reactivity-fundamentals.html">https://vuejs.org/guide/essentials/reactivity-fundamentals.html</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=94bdb0053de8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[小琉球旅遊雜記]]></title>
            <link>https://kmsheng.medium.com/%E5%B0%8F%E7%90%89%E7%90%83%E6%97%85%E9%81%8A%E9%9B%9C%E8%A8%98-e2ce7e1a6286?source=rss-45a490cd3e32------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2ce7e1a6286</guid>
            <category><![CDATA[小琉球]]></category>
            <dc:creator><![CDATA[kmsheng]]></dc:creator>
            <pubDate>Sat, 26 Mar 2022 14:08:20 GMT</pubDate>
            <atom:updated>2022-03-26T14:47:02.843Z</atom:updated>
            <content:encoded><![CDATA[<p>小琉球旅遊雜記</p><p>因為疫情的關係，已經好久沒有外出旅遊了。這次聽從老婆的建議，決定去<strong>小琉球</strong>來個 2 天 1 夜的小旅行，<strong>沒有目的性的</strong>事先規劃什麼行程，所有吃喝玩樂、住宿、旅遊景點都是在旅途中臨時用手機上網找的。</p><p>要去小琉球必須從屏東的<strong>東港碼頭</strong>搭船，剛停好車要前往碼頭買票時，在門外就先遇到一個戴著斗笠的大媽推銷船票。雖然個人不是很喜歡被阻街推銷，但因為她開的價錢還不錯，就買了。</p><p>去的時候運氣不佳遇到較大的風浪，所以在船上一邊暈船一邊看著電視播放著保護海龜的宣導影片，大約航行 30 分後就到達目的地了。下船後又遇到另一個阻街推銷租機車的阿罵，這次沒有直接買，而是先頂著高溫的大太陽在附近走路勘察一下地形後，才決定回頭去找她租機車。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*cSYJEWzR8JMgP-QJiPe37Q.jpeg" /><figcaption>小琉球的公車站牌</figcaption></figure><p>下午小琉球的南海岸真的很美，看著看著就消除了在都市生活的那種緊張感 ! 心情變得很好很放鬆 !</p><p>騎車去買完飲料後，第一站是先到<strong>海岸海景熱炒</strong>吃午飯，不得不說這家店的門口跟室內裝潢有很大的改善空間，本來還在門外猶豫到底要不要進去吃，看了 Google 地圖的評論後，決定還是進去給他一次機會。進去之後老闆娘熱情的推薦我們坐後面的座位，說是風景比較好。我們經過「廁所」、穿過「廚房」，從後門出去後，有幾桌的座位是可以直接看著海岸吃飯的，風景如下圖:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*14Tt918xCm3Olt34U69m1g.jpeg" /><figcaption>海岸海景熱炒不是浪得虛名啊 ! 真的有「海景」啊 !</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*TF40l4XeLhm4bMzFsTRaeA.jpeg" /></figure><p>上菜吃了幾口後，我對這家餐廳的疑慮完全消除了。廚藝 100 分 ! 算是少數我會誇讚的熱炒店，特別推薦炸魚蛋，超級好吃 !</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*Dw7E0z90W1ENDHEJodTVJw.jpeg" /><figcaption>夏之嶼民宿窗外的美景</figcaption></figure><p>在入住前跟民宿老闆小聊了一下，發現小琉球當地有入戶籍的大約有<strong>一萬兩千</strong>人，當地產業除了主要的漁業之外，也有機車出租、民宿、餐飲、建築等行業。共通點是當地的老闆普遍都呈現缺人狀態，所以只要來應徵，錄取率 1,000% !</p><p>交通方面，小琉球沒有紅綠燈，常有小擦撞。沒有大型醫院，一個衛生所、一個警察局。</p><p>琉球人的生活步調很慢，晚上只要稍微接近打烊時間去某些小店，你會發現店家其實不太想做生意 。他們可能會先滑一下手機，然後才來招呼你，這點跟台灣本島的都市非常不同。</p><p>當地也有一個<strong>很特殊</strong>的現象，是我無意間在網路上搜集小琉球的資訊發現的。臉書上有一個叫<strong>小琉球聯盟</strong>的社團，功能幾乎等同於小琉球當地的公告欄或討論區，貼文類型五花八門，你可以看到徵才、買房、廣告、失物招領，甚至誰家的狗不小心搭上船了，都有人熱心地貼到這個社團跟留言。</p><p>這種<strong>守望相助</strong>的精神，真的讓我這個外地人看了感觸良多。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*iBOA56HI0ftFjlt5WqbXkw.jpeg" /><figcaption>在落日亭拍的落日美景</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6E17xr2uvPmHUNce9pu5fg.jpeg" /><figcaption>小琉球冰箱冰鋪的草莓冰與巧克力冰</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*7kx2Cuy39-DtJxToBOhrDg.jpeg" /><figcaption>餵食紅蘿蔔給可愛的梅花鹿</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*PEW4X82F3kOOr8GTk9vhiw.jpeg" /><figcaption>小琉球海洋館</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*BHKXOp4DEEh7h0s2ePR45A.jpeg" /><figcaption>海龜的故事</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*hoZFR-k9Ev36Usxr6qUIeQ.jpeg" /><figcaption>蜜仔琉部的「白酒蒜香蝦蝦飯」與「辣炒珍蟹義大利麵」</figcaption></figure><p>底下整理這次去過的交通資訊:</p><p>Day1:</p><p><strong>吳家紅茶冰</strong> <a href="https://goo.gl/maps/LSj9jMNmBxdLxU7i9">https://goo.gl/maps/LSj9jMNmBxdLxU7i9</a></p><p><strong>海景海岸熱炒</strong> <a href="https://goo.gl/maps/v6rqg6nb7Phr1pH68">https://goo.gl/maps/v6rqg6nb7Phr1pH68</a></p><p><strong>夏之嶼民宿 </strong><a href="https://goo.gl/maps/zVxVtCwofsddiMBE7">https://goo.gl/maps/zVxVtCwofsddiMBE7</a></p><p><strong>落日亭</strong> <a href="https://goo.gl/maps/NsmCLEgQo3PdLsL18">https://goo.gl/maps/NsmCLEgQo3PdLsL18</a></p><p>Day2:</p><p><strong>日星島 AM.PM Brunch</strong> <a href="https://goo.gl/maps/DTg95cqGCpaiREaC9">https://goo.gl/maps/DTg95cqGCpaiREaC9</a></p><p><strong>可不可熟成紅茶</strong> <a href="https://g.page/kebukeXiaoliuqiu?share">https://g.page/kebukeXiaoliuqiu?share</a></p><p><strong>海龜的故事</strong> <a href="https://goo.gl/maps/YhQ7DcAisfDKwJM77">https://goo.gl/maps/YhQ7DcAisfDKwJM77</a></p><p><strong>蜜仔琉部 Mizi’s House 餐館</strong> <a href="https://g.page/mizi2019?share">https://g.page/mizi2019?share</a></p><p><strong>小琉球海洋館</strong> <a href="https://goo.gl/maps/wLcsVDUcfw4fCgFi8">https://goo.gl/maps/wLcsVDUcfw4fCgFi8</a></p><p><strong>鹿粼梅花鹿園區 </strong><a href="https://g.page/XiaoLiuqiuwonderlandofdeer?share">https://g.page/XiaoLiuqiuwonderlandofdeer?share</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2ce7e1a6286" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HTML Live Standard 筆記 ( 1. Intro )]]></title>
            <link>https://kmsheng.medium.com/html-live-standard-%E7%AD%86%E8%A8%98-1-intro-4c0aca4d6863?source=rss-45a490cd3e32------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c0aca4d6863</guid>
            <category><![CDATA[xml]]></category>
            <category><![CDATA[html]]></category>
            <category><![CDATA[whatwg]]></category>
            <category><![CDATA[browsers]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[kmsheng]]></dc:creator>
            <pubDate>Wed, 10 Mar 2021 09:57:10 GMT</pubDate>
            <atom:updated>2021-03-10T09:57:10.045Z</atom:updated>
            <content:encoded><![CDATA[<h3>HTML Live Standard 的<strong>設計目標</strong></h3><ul><li>script 執行的<strong>可串行性 ( </strong>Serializability )</li><li>遵守其他 spec</li><li>可擴展性 ( Extensibility )</li></ul><h4>script 執行的<strong>可串行性 ( </strong>Serializability )</h4><p>為了避免開發者遇到多執行緒的相關開發問題，HTML 與 DOM 的 API 設計成 <strong>程式不能偵測其他正在同時執行的程式</strong>，即使是 worker 也不行 ( 例如 web worker 不可以存取 DOM 物件、不能存取 window )</p><p>Serializability 唯一的例外是 SharedArrayBuffer ，使用 SharedArrayBuffer 可以觀察到其他 UA 正在執行的程式</p><p>JS 是單執行緒，所以不會有多執行緒的 race condition 問題 ( 例如同一個變數被不同執行緒存取，或是執行到一半切換到其他執行緒 )</p><p>常見的 JS 操作如 <strong>macrotask</strong> 的 xhr、setTimeout、setInterval、event handler 或 <strong>microtask</strong> 的 Promise、MutationObserver ，都會透過 event loop 加入到 call stack 按照順序執行</p><h4><strong>遵守其他 spec ( </strong>Compliance )</h4><p>HTML Live Standard 盡可能的遵守其他 spec，當某些特殊情況發生時，在文件會被標記為 <strong>willful violation </strong>並特別說明原因 ( 大部分是與古老的 XML spec 有衝突 )</p><h4>可擴展性 ( Extensibility )</h4><p>以下是一些可擴展性的案例</p><ul><li>可以用 class 屬性實作 <a href="https://microformats.org/wiki/Main_Page"><strong>Microformats</strong></a></li><li>可以用 data-*開頭的屬性來夾帶客製化資料給 script 使用， data-* 開頭的屬性不會造成延伸問題，可以放心用它</li><li>可以用 &lt;meta name=&quot;&quot; content=&quot;&quot;&gt; 標記網頁的 <strong>metadata</strong></li><li>可以用 rel 屬性定義連結的特殊意義，已經定義過的 rel 值可以參考<a href="https://microformats.org/wiki/existing-rel-values#HTML5_link_type_extensions">這裡</a> ( 例如 css link &lt;link rel=&quot;stylesheet&quot; link=&quot;xxx.scss&gt; )</li><li>可以用 <a href="https://html.spec.whatwg.org/multipage/scripting.html#the-script-element">&lt;script type=&quot;&quot;&gt;</a> 指定自訂義的 type 名稱，把夾帶的資料給其他程式處理 ( 例如 vue 的 x-template 、mustache 的 x-tmpl-mustache )</li><li>可以用 embed 元素製作外掛 ( 例如以前的 Flash )</li><li>可以透過 JS prototype 機制擴充 API</li><li>可以用 itemscope 與 itemprop 屬性實作 <a href="https://www.w3.org/TR/microdata/"><strong>Microdata</strong></a></li></ul><h3>HTML vs XML 語法</h3><ul><li>如果檔案是用 text/html 傳輸，瀏覽器就會視為是 HTML 文件並且用 HTML parser 解析內容</li><li>如果檔案是用 application/xhtml+xml 傳輸則會被視為 XML 文件並且用 XML parser 解析內容</li><li>XML 容錯性較差、HTML 容錯性較佳，一個小錯誤可能導致 XML 無法正常顯示而 HTML 可能只是單純忽略掉</li><li>DOM、HTML 語法、XML 語法不是完全互通的 ( 參考底下案例 )</li></ul><p>無法同時用 HTML、XML 與 DOM 呈現的案例:</p><ul><li>命名空間 <strong>namespaces</strong> 無法用 HTML 語法表示，但是可以用 DOM 或 XML 表示 ( 例如 create svg 要用 createElementNS 代入 svg 的 namespace、xml 的 xmlns 屬性 )</li><li>noscript 標籤的概念只出現在 HTML 上，無法用 DOM 或 XML 呈現</li><li>一個包含字串 --&gt; 的註解節點只能用 DOM 呈現而無法用 HTML 或 XML 呈現 ( 例如 document.createComment(&#39;--&gt;&#39;) )</li></ul><h4>Producer 與 Consumer</h4><ul><li>producer — 提供 HTML 的媒介 ( 例如伺服器或 CDN )</li><li>consumer — 解析 HTML 的瀏覽器或其他軟體 ( 例如閱讀器 )</li></ul><p>需求寫給 producer 與 consumer 是不一樣的</p><pre>例: foo 屬性的值必須是合法的數字，是給 producer 的需求<br>反過來說，foo 屬性的值必須由解析數字的規則來解析，是給 consumer 的需求</pre><p>給 producer 的需求 <strong>不見得跟 consumer 有關係</strong></p><pre>例: 給 producer 的需求，某屬性必須是<strong>合法的數字</strong></pre><pre>上面 <strong>並沒有說</strong> consumer 要怎麼處理某屬性</pre><pre>也許 consumer 當作是字串處理，值是不是符合規範也不受影響</pre><pre>也許 consumer 必須用某種解析數字的規則，那規則還定義了非數字的情況要怎麼處理</pre><h4>HTML 簡單的介紹</h4><p>一個基本的 HTML 大概長這樣:</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br> &lt;head&gt;<br>  &lt;title&gt;Sample page&lt;/title&gt;<br> &lt;/head&gt;<br> &lt;body&gt;<br>  &lt;h1&gt;Sample page&lt;/h1&gt;<br>  &lt;p&gt;This is a &lt;a href=&quot;demo.html&quot;&gt;simple&lt;/a&gt; sample.&lt;/p&gt;<br>  &lt;!-- this is a comment --&gt;<br> &lt;/body&gt;<br>&lt;/html&gt;</pre><p>HTML 文件是由樹狀的元素與文字構成的，每個元素是由起始標籤與結束標籤表示 ( 例如 &lt;body&gt; 與 &lt;/body&gt; )</p><p>在某些<strong>特殊狀況</strong>，</p><p>開始標籤與結束標間有可能會因為其他標籤的關係而<strong>被忽略</strong></p><p>標籤可以包含其他標籤，但不可以<strong>重疊</strong>，例如</p><pre>&lt;p&gt;這樣 &lt;em&gt;錯得 &lt;strong&gt;非常離譜&lt;/em&gt;!&lt;/strong&gt;&lt;/p&gt;</pre><pre>&lt;p&gt;這樣 &lt;em&gt;才是 &lt;strong&gt;正確的&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;</pre><p>元素可以有屬性，例如這是一個超連結，使用 a 元素附帶一個 href 的屬性</p><pre>&lt;a href=&quot;demo.html&quot;&gt;simple&lt;/a&gt;</pre><p>屬性 是放在開始標籤上的，由屬性名稱 ( name ) 與屬性值 ( value ) 組成，用等號 = 區隔。屬性的值如果沒有包含 ASCII 的空白或 &quot; &#39; ` = &lt; or &gt;</p><p>可以<strong>不用</strong>被單引號或雙引號括起來</p><p>除此之外，必須用單引號或雙引號括起來</p><pre>&lt;!-- 這樣其實是合法的，但不建議這樣寫 --&gt;<br>&lt;input name=address maxlength=200&gt;</pre><pre>&lt;!-- 值為空字串的屬性 --&gt;<br>&lt;!-- 屬性的值如果是空字串，可以省略等號 = 之後的東西 --&gt;<br>&lt;input name=&quot;address&quot; disabled=&quot;&quot;&gt;<br>&lt;input name=&quot;address&quot; disabled&gt;</pre><pre>&lt;!-- 屬性有值--&gt;<br>&lt;input name=&#39;address&#39; maxlength=&#39;200&#39;&gt;<br>&lt;input name=&quot;address&quot; maxlength=&quot;200&quot;&gt;</pre><p>HTML 由瀏覽器解析成 DOM tree，DOM tree 是用記憶體去呈現 HTML 文件的表現形式</p><p>DOM tree 有幾種類型的節點，例如</p><ul><li>表示文件類型的 DocumentType</li><li>元素節點 Element ( &lt;body&gt;, &lt;a&gt;, &lt;p&gt; 這些 )</li><li>文字節點 Text</li><li>註解節點 Comment</li><li>處理 &lt;?xml-stylesheet …?&gt; 宣告的 ProcessingInstruction</li></ul><p>其他更詳細的節點類型跟說明可以參考 <a href="https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType">這裡</a></p><p>上面基本的 HTML 轉成 DOM tree 會長成如下圖這樣</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3TQ2bUwkhEv_CdoktNBU0g.jpeg" /></figure><ul><li>document指的是 html 元素，包含 head 與 body ，中間還有一些 Text 節點</li><li>標籤排版的換行與空白符號也會被收錄到 DOM tree 的 Text 節點</li><li>因為一些<strong>歷史因素</strong>，不是所有的空白與換行符號都會被收錄到 DOM tree 上 ( head 的開始標籤之前與 body 的結束標籤之後的換行與空白符號會被瀏覽器忽略，不顯示出來 )</li><li>如上圖，head 元素有 1 個 title 元素，title 元素有 1 個 Text 節點</li><li>body 元素有 h1、 p 元素與 1 個comment 節點</li></ul><p>DOM tree 可以被頁面的 script 或 <strong>event handler content attributes</strong> 操作</p><p>onclick、onsubmit 這種都算 <strong>event handler content attributes</strong></p><p>以下面的 HTML 為例，這是一個 form夾帶了一個 script ，並且把 output 的值設定為 Hello World</p><pre>&lt;<a href="https://html.spec.whatwg.org/multipage/forms.html#the-form-element">form</a> <a href="https://html.spec.whatwg.org/multipage/forms.html#attr-form-name">name</a>=&quot;main&quot;&gt;<br>  Result: &lt;<a href="https://html.spec.whatwg.org/multipage/form-elements.html#the-output-element">output</a> <a href="https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-name">name</a>=&quot;result&quot;&gt;&lt;/output&gt;<br>  &lt;<a href="https://html.spec.whatwg.org/multipage/scripting.html#the-script-element">script</a>&gt;   <a href="https://html.spec.whatwg.org/multipage/dom.html#document">document</a>.<a href="https://html.spec.whatwg.org/multipage/dom.html#dom-document-forms">forms</a>.main.<a href="https://html.spec.whatwg.org/multipage/forms.html#dom-form-elements">elements</a>.result.<a href="https://html.spec.whatwg.org/multipage/form-elements.html#dom-output-value">value</a> = &#39;Hello World&#39;;  &lt;/script&gt; <br>&lt;/form&gt;</pre><p>每個元素在 DOM tree 都有對應的物件，每個物件都有它自己的 API，所以它們可以被操作</p><p>例如下面是一個使用不同方法修改 a 元素 href 的範例</p><pre>var a = <a href="https://html.spec.whatwg.org/multipage/dom.html#document">document</a>.<a href="https://html.spec.whatwg.org/multipage/dom.html#dom-document-links">links</a>[0]; // 取得文件中第一個超連結<br>a.<a href="https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-href">href</a> = &#39;sample.html&#39;; // 設定超連結的網址<br>a.<a href="https://html.spec.whatwg.org/multipage/links.html#dom-hyperlink-protocol">protocol</a> = &#39;https&#39;; // 修改網址的 scheme<br>a.setAttribute(&#39;href&#39;, &#39;https://example.com/&#39;); // 直接修改 attribute</pre><p>HTML 文件呈現了一個與<strong>傳播媒介脫鉤</strong>的互動內容，它可以被</p><ul><li>螢幕顯示</li><li>語音合成器 ( speech synthesizer ) 朗讀</li><li>盲用點字顯示器 ( braille display ) 顯示</li></ul><p>如果要修改它呈現的方式，可以使用樣式語言 <strong>CSS</strong></p><p>例如底下的範例示範如何把 body 改成藍底黃字</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>  &lt;head&gt;<br>   &lt;title&gt;Sample styled page&lt;/title&gt;<br>   &lt;style&gt;<br>    body { background: navy; color: yellow; }<br>   &lt;/style&gt;<br>  &lt;/head&gt;<br>  &lt;body&gt;<br>   &lt;h1&gt;Sample styled page&lt;/h1&gt;<br>   &lt;p&gt;This page is just a demo.&lt;/p&gt;<br>  &lt;/body&gt;<br> &lt;/html&gt;</pre><h4><strong>撰寫安全的 HTML 應用</strong></h4><p>資安模型以來源 ( <strong>origins</strong> ) 的概念為基礎，大部分潛在的攻擊都出現在跨網域行為 ( <strong>cross-origin action</strong> )</p><p><strong>開發者常見的錯誤</strong></p><ul><li>沒有驗證使用者輸入</li><li><strong>XSS</strong></li></ul><pre>假設網址 https://example.com/message.cgi?say=something 沒有驗證 query string 的內容，所以 query &quot;say&quot; 有什麼東西，網頁上都會把 say 的內容顯示出來</pre><pre>惡意的使用者代入騷擾的語法，放到其他地方給別的使用者點擊<br>https://example.com/message.cgi?say=%3Cscript%3Ealert%28%27Oh%20no%21%27%29%3C/script%3E</pre><pre>所有從這個連結點進去的使用者都會看到 alert 視窗...<br>當然，看到 alert 只是小意思<br>如果是 cookie 或 access token 之類的東西被收錄到外部伺服器，後果不堪設想</pre><ul><li><strong>SQL injection</strong></li></ul><p>沒有過濾從外部輸入的資料 ( 例如 URL path、query string 或 form input )，導致查詢非預期的 db query</p><pre>假設網址 <a href="https://example.com/products/12">https://example.com/products/1</a> 最終會進行以下商品查詢語法<br>SELECT * FROM Products WHERE id = &#39;1&#39;;</pre><pre>惡意的使用者在網址上的商品 id 後面加了一些料...<br><a href="https://example.com/products/12;DROP%20TABLE%20Products">https://example.com/products/1;DROP%20TABLE%20Products</a></pre><pre>因為開發者沒有過濾 query string 的內容，導致網址列上輸入的惡意語法也餵到 db 裡面去查詢，網站的商品 table 被刪除了</pre><ul><li>CSRF</li></ul><p>因為 form 的送出請求是<strong>允許跨網域</strong>的，所以在沒有驗證請求來源 ( 例如檢查 CSRF token 的機制 ) 的情況下有可能被偽造請求</p><pre>例如點擊某個不明網址，就在購物網站上面下單了</pre><ul><li><strong>Clickjacking</strong></li></ul><p>一種用 iframe<strong> </strong>框住別人網站，<strong>騙取點擊</strong>的惡意行為</p><pre>某網站舉辦網美投票，每個登入帳號後的使用者點擊網美頭像可以投 1 次票</pre><pre>某駭客想要幫小模 A 灌票，就做了一個假的抽獎網頁，騙使用者去點抽獎按鈕<br>( 其實抽獎按鈕上面疊了 1 個透明的 iframe，網址連到網美投票的網站，顯示的範圍剛好是小模 A 的頭像按鈕 )</pre><pre><strong>不知情的使用者以為自己是在抽獎的情況按下按鈕，其實是幫小模 A 在灌票...</strong></pre><p>防範機制</p><ul><li>前端檢查 window 是不是等同於 window.top，確保自己的網站沒有被 iframe 嵌入</li><li>後端設定 header X-FRAME-OPTIONS SAMEORIGIN 或 DENY，但比較老的瀏覽器不會認這個 header</li></ul><h4>使用 scripting API的常見錯誤</h4><p>瀏覽器在解析 HTML 的時候是一段一段處理的，所以通常讀到有 <strong>event handler content attribute</strong> 或 <strong>script </strong>的內容會立刻處理</p><pre>&lt;img src=&quot;food.png&quot; alt=&quot;Food&quot; onload=&quot;foodHasLoaded&quot;  /&gt;<br>&lt;script&gt;<br>function foodHasLoaded() {</pre><pre>  // 這裡處理 food 被讀取後的事情<br>}</pre><pre>&lt;/script&gt;</pre><p>參考上面這段 HTML 與 JS，因為讀取圖片需要一段時間，所以圖片讀完後觸發 onload 時，底下的 script 內容早就被瀏覽器讀取了，所以可以正常呼叫 foodHasLoaded</p><p>但是如果 food.png 早就已經在網站的其他地方被讀取過了，瀏覽器已經有暫存了，瀏覽器在讀完 onload屬性後就直接觸發 onload呼叫 foodHasLoaded ，但這時 foodHasLoaded <strong>並未被定義</strong>，就會出錯</p><h4>驗證 HTML 的正確性</h4><p><a href="https://whatwg.org/validator/">WHATWG</a></p><h4>樣式標籤</h4><p>像 font 、 b、 i …這些被廢棄的標籤應該避免使用，</p><p>因為會有一些問題:</p><ul><li>干擾無障礙輔具讀取資訊</li><li>增加維護的複雜度</li><li>造成更大的 HTML 文件大小</li></ul><p>唯一能用的樣式標籤應該是 style，不過在開發時 style 標籤雖然很方便，正式上線的網站應該還是應該把 stylesheet 獨立出去用 link 或其他方式載入</p><h4>語法錯誤</h4><p><strong>不直覺的錯誤處理行為</strong></p><p>某些語法結構解析後呈現在 DOM tree 會變得非常不直覺</p><pre>例如: hr 出現在 table 之後<br>&lt;table&gt;&lt;hr&gt;...</pre><p><strong>某些情況遇到錯誤可以選擇性恢復</strong></p><p>使用者代理 ( User Agent )，以下簡稱 UA</p><p>為了讓 UA 是在一個可控的環境下使用，避免實作太奇怪或極度複雜的錯誤處理規則 。 UA 在任何時候遇到解析錯誤時都允許拋出例外</p><p><strong>不相容於 streaming UA 的錯誤</strong></p><p>一些類似上面 table hr 範例的錯誤，都與 streaming UA 不相容，streaming UA 是指那些只掃過 HTML 檔案一次、不儲存狀態的 UA 。為了避免這些 UA 發生互通性 ( Interoperability ) 的問題，任何類似的語法問題都會被視為錯誤</p><p><strong>引起 infoset coercion 的錯誤</strong></p><p>XML based 的 UA 連接 HTML parser 時，有可能套用一些 XML 的規則 ( 例如: HTML 檔案的元素或屬性包含多個冒號 ( : ) 違反 XML 規範 )</p><p>處理這個問題可能需要把 DOM tree 轉成 XML 信息集，任何需要處理以上行為的語法結構都被視為錯誤</p><p>註解包含 2 個連字號 ( - )，或是用連字號結尾是例外，是合法的 HTML 語法</p><p><strong>引起效能變很差的錯誤</strong></p><pre>例如下面的範例，每個 i 標籤在每個 p 都需要被重新建立<br>結果每個 p 產生更多元素</pre><pre>&lt;p&gt;&lt;i&gt;She dreamt. <br>&lt;p&gt;&lt;i&gt;She dreamt that she ate breakfast. <br>&lt;p&gt;&lt;i&gt;Then lunch. <br>&lt;p&gt;&lt;i&gt;And finally dinner.</pre><pre>上面的 HTML 轉成 DOM tree</pre><pre>p<br>  i<br>    #text: She dreamt.<br>p<br>  i<br>    i<br>      #text: She dreamt that she ate breakfast.<br>p<br>  i<br>    i<br>      i<br>        #text: Then lunch.<br>p<br>  i<br>    i<br>      i<br>        i<br>          #text: And finally dinner.</pre><p><strong>錯誤包含脆弱的語法結構</strong></p><p>有些語法因為歷史因素的緣故而比較脆弱</p><pre>底下語法片段，屬性的值是 ?bill&amp;ted<br>&lt;a href=&quot;?bill&amp;ted&quot;&gt;Bill and Ted&lt;/a&gt;</pre><pre>下面的語法片段，屬性值其實是 ?art©，不是 ?art&amp;copy<br>因為就算沒有分號 ( ; ) 結尾，&amp;copy 與 &amp;copy; 都會被轉譯成 ©<br>&lt;a href=&quot;?art&amp;copy&quot;&gt;Art and Copy&lt;/a&gt;</pre><pre>為了避免上述問題，所有的 named character references 都要用分號結尾，沒有用分號結尾的 named character references 會被視為錯誤</pre><pre>所以，正確的寫法應該如下:</pre><pre>&lt;a href=&quot;?bill&amp;ted&quot;&gt;Bill and Ted&lt;/a&gt; &lt;!-- &amp;ted 沒問題, 因為它不是 named character reference --&gt;</pre><pre>&lt;a href=&quot;?art&amp;amp;copy&quot;&gt;Art and Copy&lt;/a&gt; &lt;!-- &amp; 需要被跳脫, 因為 &amp;copy 是 named character reference --&gt;</pre><p><strong>錯誤包含在老舊 UA 上發生的已知互通性問題</strong></p><p>一些語法結構會導致老舊 UA 產生微妙或嚴重的問題，所以標為<strong>不合法</strong>，讓開發者可以避開它們</p><pre>例: U+0060 (`)不允許出現在沒有用引號包住的屬性<br>因為某些老舊的瀏覽器會把 ` 視為是引號處理</pre><pre>DOCTYPE 為了避免 quirks 模式出現而必須被使用，因為 quirks 模式通常涵蓋了很多沒有被記錄到 spec 上的行為</pre><p><strong>錯誤讓開發者陷入危險攻擊</strong></p><pre>例: 限制使用 UTF-7 編碼以避免開發者被 UTF-7 編碼的 XSS 攻擊</pre><p><strong>開發者意圖不明顯的情境</strong></p><p>意圖如果不明顯會被視為不合格，例如下面的案例應該被修正</p><pre>&lt;h1&gt;Contact details&lt;/h2&gt;<br>&lt;!-- 你到底是要用 H1 還是 H2 阿? 搞得我好亂 --&gt;</pre><p><strong>打錯字的案例</strong></p><p>如果是簡單的錯字，如果被視為錯誤回報給開發者可以省下很多偵錯的時間</p><pre>&lt;caption&gt; 被打成 &lt;capton&gt;，這個會被視為錯誤，這樣開發者可以很快發現錯誤並修正</pre><p><strong>與未來新語法有衝突的錯誤</strong></p><p>為了讓語法可以在未來被擴充，一些無傷大雅的功能會被禁用</p><pre>例: 在結束標籤的屬性目前會被忽略，但是會被視為<strong>違規</strong>。<br>這樣未來如果再擴充語法時，就不會與已經上線的網站語法起衝突</pre><p>一些開發者發現，在一致性與簡潔中，選擇<strong>一致性</strong>的寫法較有幫助</p><p>HTML 驗證器可以把這些檢查視為選項，讓開發者啟用</p><ul><li>屬性值都用引號括起來</li><li>總是使用 optional tags，例如 tbody 、&lt;/p&gt; 、&lt;button&gt; 等同於 &lt;button type=”submit”&gt;</li></ul><p><strong>錯誤包含可疑的語意</strong></p><p>避免元素被誤用，內容模型 ( content models ) 被定義來規範元素如何被嵌套</p><pre>例: section 標籤不可以被嵌套在 kdb 標籤內<br>不太可能整個 section 都輸入到鍵盤輸入元素內...</pre><p><strong>錯誤的語意衝突</strong></p><p>當誤用元素時，為了能引起開發者的注意，明顯語意有衝突的使用方式會被視為<strong>錯誤</strong></p><pre>&lt;hr role=&quot;cell&quot;&gt;<br>&lt;!-- 水平分隔線當作表格 !? --&gt;</pre><pre>&lt;input type=radio role=progressbar&gt;<br>&lt;!-- 是 radio 元還是讀取條 !? --&gt;</pre><p>另外一個常見的案例是 ul 只允許包含 0 或多個li ，如果 ul 包了其他東西，語意表達就不清楚了</p><p><strong>預設樣式可能會引起誤解的情境</strong></p><pre>避免把 block 元素放到 inline 元素內</pre><pre>錯誤示範: span &gt; div</pre><pre>正確示範: div &gt; div<br>         div &gt; span<br>         span &gt; span</pre><p>.</p><pre>另外一個<strong>錯誤示範</strong>，把 interactive content 的 textarea 放到 button 裡面</pre><pre>&lt;button&gt;&lt;textarea&gt;&lt;/textarea&gt;&lt;/button&gt;</pre><p><strong>用來提示誤解 spec 的錯誤</strong></p><p>有些時候一些東西被禁止是因為允許它的話會困擾開發者</p><pre>例: 不可以設定 disabled=&quot;false&quot;</pre><pre>因為看似好像寫了一個 &quot;不可以禁用&quot; ( 也就是啟用 ) 的寫法，其實結果還是禁用<br>瀏覽器是認有沒有 disabled 這個屬性來決定要不要禁用，他的值是怎樣不重要</pre><p><strong>用來簡化語言的錯誤</strong></p><p>開發者必須學習一些簡化了語言的錯誤</p><pre>例: area 元素的 shape 屬性，可以允許設定 circ 與 circle 這兩個值</pre><pre>不允許設定 circ 這個值可以簡化一些教學與教材。同時允許兩個值並沒有好處，而且會造成學習上的困擾</pre><p><strong>會引發 parser 怪異行為的錯誤</strong></p><p>一些元素因為歷史因入會用怪異的方式解析，它們的內容模型 ( Content Model ) 限制就是設計來避免這些問題</p><pre>例: form 標籤不允許出現在 phrasing content 裡面，因為開始解析 form 時，會自動讓 p 結尾，所以以下的寫法會產生 <strong>2</strong> 個 p 段落，不是 1 個</pre><pre>&lt;p&gt;Welcome. &lt;form&gt;&lt;label&gt;Name:&lt;/label&gt; &lt;input&gt;&lt;/form&gt;</pre><pre>會被解析成:</pre><pre>&lt;p&gt;Welcome. &lt;/p&gt;&lt;form&gt;&lt;label&gt;Name:&lt;/label&gt; &lt;input&gt;&lt;/form&gt;</pre><p><strong>會造成非常難 debug 的錯誤</strong></p><p>一些錯誤是被設計出來讓問題清楚浮現</p><pre>例: 不允許一個標籤可以有兩個以上的 id 屬性，重複的 id 會讓程式抓取錯誤的元素<br>，造成不易察覺的災難</pre><p><strong>浪費開發者時間的錯誤</strong></p><p>一些語法結構是不允許使用的，因為過去浪費了開發者太多時間。</p><pre>例如 script 的 src 屬性會造成 script 夾帶的語法被忽略，<br>這不是一個很容易被察覺的錯誤。</pre><pre>規範讓這個錯誤能夠明確的被丟出來，能夠節省開發者很多時間</pre><p><strong>同時相容 HTML 與 XML 的錯誤</strong></p><p>一些開發者會寫一些同時相容於 HTML 與 XML 的語法。通常要避免這種做法，因為會引發無數細微的後遺症 ( 特別是 script、style 或自動序列化的程式 )</p><p>這裡定義出一些限制，試圖緩解這些後遺症，讓開發者在 XML 與 HTML 之間比較容易轉換</p><pre>例: 一些圍繞在 lang 與 xml:lang 複雜的規則，試圖讓兩者同步</pre><p>.</p><pre>例: 在 HTML 序列化時限制 xmlns 屬性，讓元素是基於同一個命名空間下處理 ( HTML 或 XML 命名空間 )</pre><p><strong>與未來擴充保留有關的錯誤</strong></p><pre>例: target 的值都是限制以 U+005F (_ 底線) 開頭，以避免跟開發者自訂義的值衝突</pre><p><strong>顯示其他 spec 的錯誤</strong></p><p>一些限制被訂出來是為了能夠支援其他 specs 定下的限制</p><pre>例: HTML media 屬性值的合法性必須參照 media query spec 所定義的內容為主</pre><p>參考:</p><p><a href="https://html.spec.whatwg.org/multipage/introduction.html#introduction">https://html.spec.whatwg.org/multipage/introduction.html#introduction</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c0aca4d6863" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HTML 的歷史時間軸]]></title>
            <link>https://kmsheng.medium.com/html-%E7%9A%84%E6%AD%B7%E5%8F%B2%E6%99%82%E9%96%93%E8%BB%B8-8cd0f36cccb7?source=rss-45a490cd3e32------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cd0f36cccb7</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[html5]]></category>
            <dc:creator><![CDATA[kmsheng]]></dc:creator>
            <pubDate>Sat, 27 Feb 2021 05:37:57 GMT</pubDate>
            <atom:updated>2021-02-27T05:37:57.545Z</atom:updated>
            <content:encoded><![CDATA[<ul><li>1989年 Tim Berners-Lee 在 CERN 工作時發明了 HTML</li><li>1990 — 1995 年，HTML 先後在 CERN 與 IETF 歷經了一些版本的修改與擴充</li><li>1995 年 HTML 3.0</li><li>1997 年完成 HTML 3.2</li><li>1998 年發布 HTML 4.0</li><li>W3C 開始發展 XHTML 1.0</li><li>W3C 開始發展 XHTML2 ( 不相容 HTML 與 XHTML )</li><li>1998 年 HTML 的發展有些停滯，DOM Level 1 出現</li><li>2000 — 2003 年 DOM Level 2 Core &amp; DOM Level 2 HTML</li><li>2003 年 XForms ( web form 的前身 ) 出現</li><li>2004 年 DOM Level 3問世</li><li><strong>W3C 拒絕了某些由 Mozilla 與 Opera 提出的 HTML5 早期提案 </strong>( form 相關的功能 )。原因是因為與 W3C 期望的 web 發展路線不符，W3C 當時傾向發展 XML 相關的解決方案為主</li><li><strong>Apple, Mozilla 與 Opera 拿著被拒絕的提案離家出走，成立了 WHATWG</strong>。授權屬於這三家廠商並且允許重複使用這些標準。</li><li>2006 年 W3C 提議建立一個工作小組與 WHATWG 共同制定 HTML5 標準</li><li>Apple, Mozilla 與 Opera 同意 W3C 發布 HTML5 標準並取得其授權，WHATWG 那邊則有一個限制較小的授權版本</li><li>2011年， W3C 與 WHATWG 理念不合。W3C 想要發布一個 “完成版” 的 HTML5 標準，WHATWG 則認為 HTML5 與其被凍結，其實可以持續演進與優化，修正更多已知問題</li><li>2019 年 W3C 與 WHATWG 簽了一個共同合作制定 spec 的協議</li></ul><p><a href="https://stackoverflow.com/questions/6825713/html5-w3c-vs-whatwg-which-gives-the-most-authoritative-spec">這裡</a> 與 <a href="https://www.reddit.com/r/javascript/comments/5swe9b/what_is_the_difference_between_the_w3c_and_the/">這裡</a>可以看更多 W3C 的骯髒事</p><p>參考:</p><p><a href="https://html.spec.whatwg.org/multipage/introduction.html#introduction">https://html.spec.whatwg.org/multipage/introduction.html#introduction</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cd0f36cccb7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[臉書 redux-react-hook 原始碼拆解]]></title>
            <link>https://kmsheng.medium.com/%E8%87%89%E6%9B%B8-redux-react-hook-%E5%8E%9F%E5%A7%8B%E7%A2%BC%E6%8B%86%E8%A7%A3-6c80ee4cb62e?source=rss-45a490cd3e32------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c80ee4cb62e</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-redux-hooks]]></category>
            <dc:creator><![CDATA[kmsheng]]></dc:creator>
            <pubDate>Mon, 08 Apr 2019 07:57:46 GMT</pubDate>
            <atom:updated>2019-04-08T08:07:48.471Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*JeY4idv7au966oNS72Dbrg.png" /></figure><p>在 React 的 hooks API 推出沒多久，很多開發者都迫不及待的拋棄 react-redux，使用 context 來自幹介接 redux 的 hook。</p><p>現在臉書已推出了官方版本的 <a href="https://github.com/facebookincubator/redux-react-hook">redux-react-hook</a>，本文將會針對版本 3.3.0 逐行講解程式碼，藉由閱讀程式碼的過程了解一下官方實作的版本考慮了什麼因素進去。</p><p>比較驚訝的是這個 redux-react-hook 竟然是用微軟的 TypeScript 寫的，這不是跟自家 Flow 打對台嗎 XD ?</p><h4>index.ts</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1760e5580f3738ee5083f42c1e1d1c25/href">https://medium.com/media/1760e5580f3738ee5083f42c1e1d1c25/href</a></iframe><h4>create.ts</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5508a88c4f0ef0003b0b33ffe53df93e/href">https://medium.com/media/5508a88c4f0ef0003b0b33ffe53df93e/href</a></iframe><h4>shallowEqual.ts</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9924f560519f13b3d757ba0f6a843620/href">https://medium.com/media/9924f560519f13b3d757ba0f6a843620/href</a></iframe><h4>References</h4><ul><li><a href="https://github.com/facebookincubator/redux-react-hook">https://github.com/facebookincubator/redux-react-hook</a></li><li><a href="https://github.com/reduxjs/redux">https://github.com/reduxjs/redux</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c80ee4cb62e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[使用 git worktree 建立多個工作區]]></title>
            <link>https://kmsheng.medium.com/%E4%BD%BF%E7%94%A8-git-worktree-%E5%BB%BA%E7%AB%8B%E5%A4%9A%E5%80%8B%E5%B7%A5%E4%BD%9C%E5%8D%80-5a02f6d9d3fd?source=rss-45a490cd3e32------2</link>
            <guid isPermaLink="false">https://medium.com/p/5a02f6d9d3fd</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[git-worktree]]></category>
            <dc:creator><![CDATA[kmsheng]]></dc:creator>
            <pubDate>Tue, 02 Apr 2019 09:36:03 GMT</pubDate>
            <atom:updated>2019-04-02T09:36:49.472Z</atom:updated>
            <content:encoded><![CDATA[<h4>除了 git stash 外的另一種方案</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*ew_J8aTHB4d4QHFerrYdyw.png" /></figure><p>本文是基於 git 版本 2.21.0 撰寫，如果你的 git 低於這個版本有可能無法使用 worktree 或是不支援部分參數。</p><h4>什麼是 worktree ?</h4><p>以前在做 git init 或 git clone 後，git 只允許你在同一個 repo 使用一個目錄對應一個分支，支援 worktree 後你可以自行新增目錄對應到不同分支或 commit。</p><h4>為什麼要用 worktree ?</h4><p>你可能會遇到一種狀況，當你很認真的在某個 feature 分支寫了很多程式，但是還沒有 commit 到 repo，所以 repo 上面有滿滿的已更動或未追蹤檔案。</p><p>這時你老闆出現了。「喂 ~ 那個誰 ! 線上程式爆炸了，趕快修一下。」</p><p>可是你的程式還沒 commit 完啊 ! 怎麼辦 ?</p><p>這時可能有幾種解法:</p><ol><li>clone 一個同樣的 repo 到別的目錄，分支切到 master 去處理爆炸的事情。</li><li>把沒 commit 的東西全部透過 git add . &amp;&amp; git stash 收起來，分支切到 master 去處理爆炸的事情，處理好之後再切回 feature 分支做 git stash pop 。</li></ol><p>解法 1 的缺點是如果 repo 太大，再 clone 一次會很耗時間。解法 2 的缺點是你的編輯器如果會自動偵測檔案消失或變動的話，你可能再 checkout 過去就會看到一大堆訊息。</p><p>所以考慮以上因素，這時 worktree 就很有用了 !</p><blockquote>worktree 可以讓你直接開一個獨立的工作區域切換出去做別的事，未 commit 的檔案原封不動留在原來的 repo。</blockquote><h4>worktree 的相關指令</h4><p>底下用 <a href="https://github.com/kmsheng/git-worktree-demo">https://github.com/kmsheng/git-worktree-demo</a> 這個 repo 說明，原始的 repo git-worktree-demo 稱作 main working tree，後來透過 git-work-tree 指令建立出來的工作區叫 linked working tree，兩者之間有對應關係。</p><h4>add &lt;path&gt; [commit-ish]</h4><p>新增一個工作區，用法有幾種變化</p><pre>$ git worktree add space1<br># 因為分支 space1 在本地或遠端都不存在，所以 git 幫我建了一個目錄名為<br># space1 並且用當前分支 HEAD 建了一個本地分支 space1 對應到目錄 space1<br># 一個成功建立好的 worktree 目錄底下會有一個 .git 的 &quot;檔案&quot;，裡面會紀錄<br># main working tree 到 linked working tree 的路徑。<br># 例如:<br># gitdir: ~/git-worktree-demo/.git/worktrees/space1</pre><pre>$ git worktree add space2 feat2<br># git 幫我建了一個 space2 的工作區並且對應到 feat2 分支<br># 因為 feat2 這個分支有存在於遠端，所以會被綁定<br># 分支搜尋的順序為先本地、後遠端，所以如果本地有同名分支會優先被綁定。</pre><pre>$ git worktree add --detach detached-space<br># 因為有加 --detach 參數，所以 git 建立了一個工作區名為 detached-space，直接對應到 HEAD 的 commit，不另外建立本地分支。</pre><pre>$ git worktree add --lock -q -b feat2-cloned space3 feat2<br># 使用 --lock 直接建立一個被鎖住的工作區，效果等同於 git worktree add 再 git work tree lock<br># 使用 -q 參數對 git 消音 ( 提示訊息將不會顯示 )<br># 使用 -b 參數指定要建立的分支名稱，以上面指令為例，會拿 feat2 去建立 feat2-cloned 分支。<br># git 預設不會覆蓋同名分支，使用 -B 參數可以強制覆蓋。</pre><h4>move</h4><p>改變工作區的路徑</p><pre>$ git worktree move space1 s1<br># 把工作區目錄由 space1 改成 s1</pre><pre>$ git worktree move s1 space1<br># 改回來</pre><h4>list</h4><p>列出工作區</p><pre>$ git worktree list<br># 可以在 main working tree 和 linked working tree 列清單</pre><pre>git-worktree-demo                 ee1bbbb [master]<br>git-worktree-demo/detached-space  ee1bbbb (detached HEAD)<br>git-worktree-demo/space1          ee1bbbb [space1]<br>git-worktree-demo/space2          cfcc123 [feat2]<br>/Users/kmsheng/work/git-worktree-demo/space3          cfcc123 [feat2-cloned]</pre><pre>$ git worktree list --porcelain<br># 有參數 --porcfelain，列出比較好剖析的格式</pre><pre>worktree /Users/kmsheng/work/git-worktree-demo<br>HEAD ee1bbbb754664f0a637810669d97bd1e398b4999<br>branch refs/heads/master</pre><pre>worktree /Users/kmsheng/work/git-worktree-demo/detached-space<br>HEAD ee1bbbb754664f0a637810669d97bd1e398b4999<br>detached</pre><pre>worktree /Users/kmsheng/work/git-worktree-demo/space1<br>HEAD ee1bbbb754664f0a637810669d97bd1e398b4999<br>branch refs/heads/space1</pre><pre>worktree /Users/kmsheng/work/git-worktree-demo/space2<br>HEAD cfcc123d362706b6e46abc27ea25177c8f7fbe97<br>branch refs/heads/feat2</pre><pre>worktree /Users/kmsheng/work/git-worktree-demo/space3<br>HEAD cfcc123d362706b6e46abc27ea25177c8f7fbe97<br>branch refs/heads/feat2-cloned</pre><h4>remove</h4><p>移除一個工作區</p><pre>$ git worktree remove space3<br># 刪除 space3 工作區</pre><pre>$ git worktree remove -f space3<br># 如果 space3 裡面還有東西沒 commit，使用一個 -f 來強制刪除目錄</pre><pre>$ git worktree remove -f -f space3<br># 如果要刪除被鎖起來的目錄，需要指定兩次 -f</pre><h4>prune</h4><p>大量移除路徑不存在的工作區</p><pre>$ rm -rf detached-space</pre><pre>$ git worktree prune -n<br># 有 -n 就不會真的刪，先告訴你有哪些工作區會被刪掉</pre><pre>$ git worktree prune -v<br># 要清除了，並且把清除解果印出來</pre><h4>lock</h4><p>鎖住一個工作區，即使路徑被刪除， prune 指令也刪不到他。</p><p>如果你的工作區路徑是指到隨身碟或是 NFS 的路徑，在工作期間你可以考慮把工作區 lock 起來。</p><pre>$ git worktree lock space2 --reason &quot;flashdisk&quot;<br># 把 space2 lock 起來<br># 使用 --reason 參數可以把為什麼要鎖的原因寫上來<br>#<br># 目前還找不到一個方法可以簡單列出被 lock 的 worktree<br># tree .git/worktrees</pre><pre>.git/worktrees<br>├── space1<br>│   ├── HEAD<br>│   ├── ORIG_HEAD<br>│   ├── commondir<br>│   ├── gitdir<br>│   ├── index<br>│   └── logs<br>│       └── HEAD<br>└── space2<br>    ├── HEAD<br>    ├── ORIG_HEAD<br>    ├── commondir<br>    ├── gitdir<br>    ├── index<br>    ├── locked    &lt;----- space2 的 locked 檔<br>    └── logs<br>        └── HEAD</pre><h4>unlock</h4><p>解除工作區的鎖定狀態，此時這個工作區可以被刪了。</p><pre>$ git worktree unlock space2</pre><h4>常見錯誤</h4><p>要注意不能兩個目錄切到同一個分支，假設我的 main working tree 在 master，我卻要把某個工作區的分支切換到 master 就會發生錯誤。</p><pre>fatal: &#39;master&#39; is already checked out at &#39;git-worktree-demo&#39;</pre><h4>References:</h4><p>— <a href="https://git-scm.com/docs/git-worktree">https://git-scm.com/docs/git-worktree</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5a02f6d9d3fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[使用 git rerere 自動解除同類型的衝突]]></title>
            <link>https://kmsheng.medium.com/%E4%BD%BF%E7%94%A8-git-rerere-%E8%87%AA%E5%8B%95%E8%A7%A3%E9%99%A4%E5%90%8C%E9%A1%9E%E5%9E%8B%E7%9A%84%E8%A1%9D%E7%AA%81-1f60748ce55b?source=rss-45a490cd3e32------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f60748ce55b</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[git-rerere]]></category>
            <dc:creator><![CDATA[kmsheng]]></dc:creator>
            <pubDate>Mon, 01 Apr 2019 08:24:23 GMT</pubDate>
            <atom:updated>2019-04-01T08:26:49.184Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*ew_J8aTHB4d4QHFerrYdyw.png" /></figure><p>git rerere 算是一個命名有點特別的指令，三個 re 分別來自 reuse、recorded 與 resolution。</p><pre>git-rerere - Reuse recorded resolution of conflicted merges</pre><p>在學 rerere 之前至少要知道怎麼 merge 一個 conflict，如果不會的話建議先弄清楚 git 如何 merge conflict 再來看這篇會比較好懂。</p><p>git rerere 能夠記錄你之前發生衝突的程式片段與衝突的解法，下次同類型的衝突再發生時，git 就會自動套用之前已經紀錄的衝突解法到檔案上。這個指令好用的地方在於如果同類型的衝突發生次數很高，可以省去手動解衝突的時間。</p><h4>啟用 rerere 的方式</h4><p>git rerere 的衝突紀錄行為預設是關閉的，必須輸入 git config rerere.enabled true 打開，不想用時可以輸入 git config --unset rerere.enabled 禁用。如果要每個專案都用的話可以加上 --global 設定為全域。</p><p>設定的結果可以在 .git/config 上看到，如果有加 --global 則是在 ~/.gitconfig 上可以看到設定結果。</p><h4>rerere 的記錄衝突與自動套用衝突解法</h4><p>我準備了一個 <a href="https://github.com/kmsheng/git-rerere-demo">repo</a> 裡面有兩個檔案 file1.txt 與 file2.txt，檔案裡面的內容都是 This is a file，然後有兩個分支 feat-file1-add-period 與 feat-file2-add-period 分別在檔案 file1.txt 與 file2.txt 加上句號。</p><p>理論上我只要把 repo clone 下來後，改過 file1.txt 的字串內容，commit 後，再試圖 merge 分支 feat-file1-add-period，第一個衝突就會發生。</p><pre>$ git clone <a href="/service/mailto:git@github.com">git@github.com</a>:kmsheng/git-rerere-demo.git</pre><pre>Cloning into &#39;git-rerere-demo&#39;...<br>remote: Enumerating objects: 11, done.<br>remote: Counting objects: 100% (11/11), done.<br>remote: Compressing objects: 100% (8/8), done.<br>remote: Total 11 (delta 1), reused 7 (delta 0), pack-reused 0<br>Receiving objects: 100% (11/11), done.<br>Resolving deltas: 100% (1/1), done.</pre><pre>$ cd git-rerere-demo<br>$ git config rerere.enabled true</pre><pre>$ echo &quot;This is a file\!&quot; &gt; file1.txt<br>$ git commit -am &quot;add exclamation mark&quot;</pre><pre>[master 79ee3ad] add exclamation mark<br> 1 file changed, 1 insertion(+), 1 deletion(-)</pre><pre>$ git merge origin/feat-file1-add-period</pre><pre>Auto-merging file1.txt<br>CONFLICT (content): Merge conflict in file1.txt<br>Recorded preimage for &#39;file1.txt&#39;<br>Automatic merge failed; fix conflicts and then commit the result.</pre><p>這時衝突發生了，可以看到 git 把衝突的內容記錄到 preimage 這個檔案裡面。preimage 可以在 .git/rr-cache 這個目錄找到。</p><pre>tree .git/rr-cache<br>.git/rr-cache<br>└── 65a5ea7bf9975dcc87539ba5a2739f90553ff830<br>    └── preimage</pre><pre>1 directory, 1 file</pre><pre>$ cat .git/rr-cache/65a5ea7bf9975dcc87539ba5a2739f90553ff830/preimage</pre><pre>&lt;&lt;&lt;&lt;&lt;&lt;&lt;<br>This is a file!<br>=======<br>This is a file.<br>&gt;&gt;&gt;&gt;&gt;&gt;&gt;</pre><p>執行 git rerere status 可以列出會被記錄衝突解法的檔案，輸入 git rerere remaining 則可以列出有衝突但沒有被自動解除衝突的檔案。</p><p>如果這時我想放棄解衝突，而且我也不要上面 preimage 的紀錄，我可以使用 git rerere clear 把 preimage 刪除。( 這個步驟建議跑完整個自動解除衝突的流程再來試 )</p><pre>$ git rerere clear<br>$ git merge --abort</pre><p>正常解除衝突之後，.git/rr-cache 目錄裡面會多出一個 postimage 檔案，儲存了你對衝突的解法。</p><pre>$ echo &quot;This is a file @.@&quot; &gt; file1.txt<br>$ git add file1.txt<br>$ git commit<br>Recorded resolution for &#39;file1.txt&#39;.<br>[master 274cebd] Merge remote-tracking branch &#39;origin/feat-file1-add-period&#39;</pre><pre>$ tree .git/rr-cache<br>.git/rr-cache<br>└── 65a5ea7bf9975dcc87539ba5a2739f90553ff830<br>    ├── postimage<br>    └── preimage</pre><pre>$ cat .git/rr-cache/65a5ea7bf9975dcc87539ba5a2739f90553ff830/postimage</pre><pre>This is a file @.@</pre><p>這時我在對 file2.txt 製造出一個一模一樣的衝突，git rerere 會使用 postimage 的內容幫我自動代入到衝突的檔案，如果在 git merge 時有加上 -- rerere-autoupdate，解好衝突的檔案會變成 staged 狀態。</p><pre>$ echo &quot;This is a file\!&quot; &gt; file2.txt<br>$ git commit -am &quot;add exclamation mark&quot;<br>$ git merge origin/feat-file2-add-period<br>Auto-merging file2.txt<br>CONFLICT (content): Merge conflict in file2.txt<br>Resolved &#39;file2.txt&#39; using previous resolution.<br>Automatic merge failed; fix conflicts and then commit the result.</pre><p>rerere 也可以讓你單獨刪除一個檔案的衝突解法紀錄，例如:</p><pre>git rerere forget file2.txt</pre><p>這時 file2.txt 的 postimage 會被刪除。</p><h4>rerere 的資源回收機制</h4><p>輸入 git rerere gc 可以把陳舊的紀錄檔刪除，超過15 天未解決的衝突檔案與超過 60 天已解決衝突的檔案會被刪除。上述兩種檔案可以透過設定 gc.rerereUnresolved 與 gc.rerereResolved 設定天數。</p><p>References</p><ul><li><a href="https://git-scm.com/docs/git-rerere">https://git-scm.com/docs/git-rerere</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f60748ce55b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[如何使用 git bisect 快速找出有 bug 的 commit ?]]></title>
            <link>https://kmsheng.medium.com/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8-git-bisect-%E5%BF%AB%E9%80%9F%E6%89%BE%E5%87%BA%E6%9C%89-bug-%E7%9A%84-commit-3b38f22d52bf?source=rss-45a490cd3e32------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b38f22d52bf</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[git-bisect]]></category>
            <dc:creator><![CDATA[kmsheng]]></dc:creator>
            <pubDate>Sun, 31 Mar 2019 12:59:11 GMT</pubDate>
            <atom:updated>2019-04-01T02:22:23.988Z</atom:updated>
            <content:encoded><![CDATA[<p>如何使用 git bisect 快速找出有 bug 的 commit ?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*ew_J8aTHB4d4QHFerrYdyw.png" /></figure><p>在知道 git bisect 的用法之前如果要找出有 bug 的 commit，往往是 checkout 回之前的 commit，測試程式碼，再 checkout 到更早之前的 commit，測試程式碼，一直試到這個 commit 沒問題，才知道程式是在下一個 commit 開始壞掉的，這樣的找法實在很沒效率。</p><p>git 提供你一個更快速的方法，使用 git bisect 做二分搜尋法來找出有問題的 bug commit。你必須先告訴 git 一個已知是壞掉的 commit 當作是開頭，然後一個已知是正常的 commit 當作是結尾。 git bisect 會用這個範圍做二分搜尋法不斷地問你這個 commit 是好的還是壞的，一直問到找出有 bug 的 commit 為止。</p><p>以下指令將會使用 <a href="https://github.com/kmsheng/git-bisect-demo">https://github.com/kmsheng/git-bisect-demo</a> 這個 repo 說明，有興趣操作的讀者可以自行 clone 下來。有 bug 的 commit 是 02acafd feat: This is the commit that breaks add function，從這個 commit 開始 add 方法會丟出 error 不能使用。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a197163db8fdc99b83826a9e2c73097f/href">https://medium.com/media/a197163db8fdc99b83826a9e2c73097f/href</a></iframe><p>使用的方法大概是這樣，先執行 git bisect start 進入 bisect 模式，這時候再下 git status 可以發現已經進入 bisect 模式了，執行 git bisect reset可以取消退出。</p><pre>$ git bisect start<br>$ git status</pre><pre>On branch master<br>Your branch is up to date with &#39;origin/master&#39;.</pre><pre>You are currently bisecting, started from branch &#39;master&#39;.<br>  (use &quot;git bisect reset&quot; to get back to the original branch)</pre><pre>nothing to commit, working tree clean</pre><p>Ok，我的 HEAD 現在是在 d6b143d add file multply.js，是爛的，所以我執行 git bisect bad 告訴 git 我當前的 commit 是爛的，請當做是執行 bisect 的開頭。( git bisect bad 後面沒有分支名稱或 hash，就表示使用當前所在的 commit )</p><p>然後我確定 commit fa38f8d add file add.js 是好的，所以可以當作 bisect 搜尋的結尾。執行 git bisect good fa38f8d</p><pre>$ git bisect good e6910a1513e673e70deee47526793307f46e02ab</pre><pre>Bisecting: 4 revisions left to test after this (roughly 2 steps)<br>[c56f1ef0689081bf621e69e74fcc1bb0e53668e4] docs: add comments for function add params</pre><p>然後繼續執行 git bisect good 或 git bisect bad 直到找出錯的 commit 為止 ( 畫面會出現 {hash} is the first bad commit )</p><pre>$ git bisect good<br>02acafda466e88fa95a04b7a712b1d50869d9f59 is the first bad commit</pre><pre>commit 02acafda466e88fa95a04b7a712b1d50869d9f59<br>Author: kmsheng &lt;<a href="/service/mailto:kmsh3ng@gmail.com">kmsh3ng@gmail.com</a>&gt;<br>Date:   Sun Mar 31 18:56:16 2019 +0800</pre><pre>feat: This is the commit that breaks add function.</pre><pre>:100644 100644 047e14165724dba2831d6b68d8dacaf2e43d5e20 6ef896f4d49969a37f90d735b2ac8b035af479c1 M      add.js</pre><p>git 在 bisect 模式可以允許你跳過不需要檢查的 commit，例如我有很多 commit 是排版或是改文件的，可以執行 git bisect skip {hash} 來跳過這些 commit。</p><p>有的時候我們不是要檢查程式是否有 bug，而是程式的行為是否有改變，這個時候用 good 或 bad 來形容就怪怪的，所以 git 也允許你使用 old 與 new。</p><p>例如 git bisect old 或 git bisect new，或者，你也可以定義自己想要的關鍵字。</p><pre>git bisect start --term-old fast --term-new slow</pre><p>定義完忘記，還可以執行 git bisect terms 來看看目前定義的關鍵字是什麼</p><pre>$ git bisect terms<br>Your current terms are fast for the old state<br>and slow for the new state.</pre><p>git bisect 支援印出 log，使用的指令是 git bisect log 。</p><p>例如:</p><pre>$ git bisect log<br>git bisect start<br># bad: [41d0e0c26061f24b085cba44372d770fe47efd9e] docs: add references to README.md<br>git bisect bad 41d0e0c26061f24b085cba44372d770fe47efd9e<br># good: [e6910a1513e673e70deee47526793307f46e02ab] add file add.js<br>git bisect good e6910a1513e673e70deee47526793307f46e02ab</pre><p>可以把存到一個檔案，再利用 git bisect replay 重新回到修改後的 bisect 狀態。如果之前有選錯 good 或 bad，也可以修改 log 上面的 good 或 bad 修正。</p><pre>$ git bisect log &gt; bisect.log</pre><p>透過 git bisect replay 指令還原 bisect 的狀態:</p><pre>$ git bisect replay bisect.log<br>Previous HEAD position was 0c2623b docs: add comments for function add<br>Switched to branch &#39;master&#39;<br>Your branch is up to date with &#39;origin/master&#39;.<br>Bisecting: 4 revisions left to test after this (roughly 2 steps)<br>[0c2623b6ac03eb34a74825dad4399e296f030691] docs: add comments for function add</pre><p>除了可以手動執行 node check.js 檢查之外，也可以使用 git bisect run 搭配程式路徑自動檢查 commit 是不是好的。</p><p>Exit code 為 0 時視為程式正常、125 時執行 git bisect skip、1 ~ 127 視為程式爛掉。</p><pre>$ git bisect start<br>$ git bisect bad d6b143d<br>$ git bisect good e6910a1<br>$ git bisect run bash -c &#39;node ./check.js&#39;</pre><p>References:</p><p><a href="https://git-scm.com/docs/git-bisect">https://git-scm.com/docs/git-bisect</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b38f22d52bf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>