Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Day25. 如何解析 HTML 語法
Search
Kalan
October 04, 2020
Programming
220
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Day25. 如何解析 HTML 語法
Kalan
October 04, 2020
More Decks by Kalan
See All by Kalan
單頁式應用中的無障礙設計
kjj6198
0
860
選擇 Svelte 的三個理由 - JSDC
kjj6198
0
460
Svelte - 如何在前端框架中脫穎而出 | ModernWeb'21
kjj6198
0
260
状態管理を楽にする道
kjj6198
1
470
Day24. Svelte 如何編譯程式碼(2)
kjj6198
0
260
Svelte 如何編譯程式碼(1)
kjj6198
0
280
Day22. Svelte 經驗談
kjj6198
0
260
Day18. UI 實戰篇 - 圖片檢視器
kjj6198
0
240
Day17. UI 實戰篇 - 音樂播放器
kjj6198
0
140
Other Decks in Programming
See All in Programming
Workers Cache を知る
syumai
0
350
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.6k
技術的負債の返済は、AI時代の複利で効く投資 — 経営としての意思決定とその遂行
curekoshimizu
1
2.3k
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
980
contenteditable と日本語入力に向き合う
colorful12
0
190
ActionController meets Protocol Buffers
sorah
0
130
個人開発者よ、Honoを使え(おじさんver)
tanahiro2010
2
240
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
380
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
160
Are APIs Still Relevant in the AI Era?
soyuka
0
450
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
250
Jetpack Compose Mechanisms
skydoves
2
720
Featured
See All Featured
Color Theory Basics | Prateek | Gurzu
gurzu
1
490
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.6k
The agentic SEO stack - context over prompts
schlessera
0
970
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Exploring anti-patterns in Rails
aemeredith
4
530
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
520
What's in a price? How to price your products and services
michaelherold
247
13k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
710
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
1
280
Transcript
第 12 屆 IT 鐵⼈賽 30 天從 0 到 1
學 Svelte Day25 - 如何解析 HTML(svelte)語法?
第 12 屆 IT 鐵⼈賽 解析 HTML 的⽬的 將語法轉成比較容易給程式處理的語法樹(AST)
第 12 屆 IT 鐵⼈賽 流程概覽 詞法分析 (Lexer) 語法分析 ⽣成抽象語法樹
⽣成程式碼
第 12 屆 IT 鐵⼈賽 流程概覽 詞法分析 (Lexer) 語法分析 ⽣成抽象語法樹
⽣成程式碼
第 12 屆 IT 鐵⼈賽 如何將語法轉為語法樹 div p p p
Hello text text <div> <p>Hello"</p> <p>text"</p> <p>text"</p> "</div>
第 12 屆 IT 鐵⼈賽 有限狀態機 tag_open tag_name < read_text
attribute tag_close /> attribute_name = data-toggle attribute_value " "
第 12 屆 IT 鐵⼈賽 範例 1 <div>Hello</div> 讀取 "<":
進入 tag_open 狀態,開始讀取 tag_name 讀取 "d":進入 tag_name 狀態,name += "d" 讀取 "i":tag_name 狀態,name += "i" 讀取 "v":tag_name 狀態,name += "v" 讀取 ">":回到初始狀態,保存當前 tag_name
第 12 屆 IT 鐵⼈賽 範例 1 <div>Hello</div> 讀取 "H":進入
text 狀態,讀取 text += "H" 讀取 "e":text 狀態,讀取 text += "e" 讀取 "l":text 狀態,讀取 text += "l" 讀取 "l":text 狀態,讀取 text += "l" 讀取 "o":text 狀態,讀取 text += "o" 讀取 "<":進入 tag_open 狀態 讀取:"/":進入 tag_close 狀態 讀取 "div":將 tag_name 設為 div 讀取 ">":回到初始狀態,完成解析
第 12 屆 IT 鐵⼈賽 解析後產⽣的資料 type: Element tag_name: div
children: [{ type: 'Text', data: 'Hello' }]
第 12 屆 IT 鐵⼈賽 範例 2 <p role="status">Hello</p> 讀取
"<": 進入 tag_open 狀態,開始讀取 tag_name 讀取 "p":進入 tag_name 狀態,name += "p" 讀取 " ":忽略 讀取 "r":進入 attribute_name 狀態,attr += "r" 讀取 "o":attribute_name 狀態,attr += "o" 讀取 "l":attribute_name 狀態,attr += "l" 讀取 "e":attribute_name 狀態,attr += "e"
第 12 屆 IT 鐵⼈賽 範例 2 <p role="status">Hello</p> 讀取
"=": 進入 attribute_value 狀態,開始讀取 value 讀取 """:attribute_value_double_quote 狀態 讀取 "s":attribute_value 狀態,attr += "s" 讀取 "t":attribute_value 狀態,attr += "t" 讀取 "a":attribute_value 狀態,attr += "a" 讀取 "t":attribute_value 狀態,attr += "t" 讀取 "u":attribute_value 狀態,attr += "u" 讀取 "s":attribute_value 狀態,attr += "s"
第 12 屆 IT 鐵⼈賽 範例 2 <p role="status">Hello</p> 讀取
""": 退出 attribute_value 狀態 讀取 ">":進入 tag_close 狀態 讀取...:流程省略
第 12 屆 IT 鐵⼈賽 解析後產⽣的資料 type: Element tag_name: p
attrs: { role: "status" } children: [{ type: 'Text', data: 'Hello' }]
第 12 屆 IT 鐵⼈賽 HTML 是可遞迴的標記語⾔ 例如: <div> <p>Text
1</p> <ul> <li>1234</li> <li>45567</li> </ul> </div>
第 12 屆 IT 鐵⼈賽 遞迴下降解析器
第 12 屆 IT 鐵⼈賽 為什麼 Svelte 要⾃⼰寫解析器? ⽀援 svelte
語法如 bind, on, if block 等等 解析後可以在編譯時做到依賴追蹤、變數宣告與否、警告等
第 12 屆 IT 鐵⼈賽 ⼀些要注意的事情 HTML 可⽀援某些無 close 的標籤
⼤⼩寫 省略了像是 CDATA、comment 等處理 https://html.spec.whatwg.org/multipage/parsing.html