@@ -67,18 +67,20 @@ pub enum NodeKind {
67
67
/// https://dom.spec.whatwg.org/#interface-element
68
68
pub struct Element {
69
69
kind : ElementKind ,
70
- //id: String,
71
- //class_name: String,
70
+ attributes : Vec < Attribute > ,
72
71
}
73
72
74
73
impl Element {
75
74
pub fn new ( kind : ElementKind ) -> Self {
76
75
Self {
77
76
kind,
78
- //id: String::new(),
79
- //class_name: String::new(),
77
+ attributes : Vec :: new ( ) ,
80
78
}
81
79
}
80
+
81
+ pub fn set_attribute ( & mut self , name : String , value : String ) {
82
+ self . attributes . push ( Attribute :: new ( name, value) ) ;
83
+ }
82
84
}
83
85
84
86
#[ allow( dead_code) ]
@@ -89,6 +91,8 @@ pub enum ElementKind {
89
91
Html ,
90
92
/// https://html.spec.whatwg.org/multipage/semantics.html#the-head-element
91
93
Head ,
94
+ /// https://html.spec.whatwg.org/multipage/semantics.html#the-link-element
95
+ Link ,
92
96
/// https://html.spec.whatwg.org/multipage/sections.html#the-body-element
93
97
Body ,
94
98
/// https://html.spec.whatwg.org/multipage/grouping-content.html#the-ul-element
@@ -97,6 +101,20 @@ pub enum ElementKind {
97
101
Li ,
98
102
}
99
103
104
+ #[ allow( dead_code) ]
105
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
106
+ /// https://dom.spec.whatwg.org/#attr
107
+ pub struct Attribute {
108
+ name : String ,
109
+ value : String ,
110
+ }
111
+
112
+ impl Attribute {
113
+ pub fn new ( name : String , value : String ) -> Self {
114
+ Self { name, value }
115
+ }
116
+ }
117
+
100
118
#[ allow( dead_code) ]
101
119
#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
102
120
pub enum InsertionMode {
@@ -147,6 +165,8 @@ impl Parser {
147
165
return self . create_element ( ElementKind :: Html ) ;
148
166
} else if tag == "head" {
149
167
return self . create_element ( ElementKind :: Head ) ;
168
+ } else if tag == "link" {
169
+ return self . create_element ( ElementKind :: Link ) ;
150
170
} else if tag == "body" {
151
171
return self . create_element ( ElementKind :: Body ) ;
152
172
} else if tag == "ul" {
@@ -241,19 +261,45 @@ impl Parser {
241
261
self . stack_of_open_elements . push ( node) ;
242
262
}
243
263
264
+ /// Sets an attribute to the current node.
265
+ fn set_attribute_to_current_node ( & mut self , name : String , value : String ) {
266
+ let current = match self . stack_of_open_elements . last ( ) {
267
+ Some ( n) => n,
268
+ None => & self . root ,
269
+ } ;
270
+
271
+ match current. borrow_mut ( ) . kind {
272
+ NodeKind :: Element ( ref mut elem) => {
273
+ elem. set_attribute ( name, value) ;
274
+ return ;
275
+ }
276
+ _ => { }
277
+ }
278
+ }
279
+
244
280
/// Returns true if the current node's kind is same as NodeKind::Element::<element_kind>.
245
281
fn pop_current_node ( & mut self , element_kind : ElementKind ) -> bool {
246
282
let current = match self . stack_of_open_elements . last ( ) {
247
283
Some ( n) => n,
248
284
None => return false ,
249
285
} ;
250
286
251
- if current. borrow ( ) . kind == NodeKind :: Element ( Element :: new ( element_kind) ) {
287
+ let ok = match current. borrow ( ) . kind {
288
+ NodeKind :: Element ( ref elem) => {
289
+ if elem. kind == element_kind {
290
+ true
291
+ } else {
292
+ false
293
+ }
294
+ }
295
+ _ => false ,
296
+ } ;
297
+
298
+ if ok {
252
299
self . stack_of_open_elements . pop ( ) ;
253
- return true ;
254
300
}
255
301
256
- false
302
+ ok
257
303
}
258
304
259
305
/// Pops nodes until a node with `element_kind` comes.
@@ -392,6 +438,25 @@ impl Parser {
392
438
// https://html.spec.whatwg.org/multipage/parsing.html#parsing-main-inhead
393
439
InsertionMode :: InHead => {
394
440
match token {
441
+ Some ( Token :: StartTag {
442
+ ref tag,
443
+ self_closing : _,
444
+ ref attributes,
445
+ } ) => {
446
+ if tag == "link" {
447
+ self . insert_element ( "link" ) ;
448
+ for attr in attributes {
449
+ self . set_attribute_to_current_node (
450
+ attr. name . clone ( ) ,
451
+ attr. value . clone ( ) ,
452
+ ) ;
453
+ }
454
+ // Immediately pop the current node off the stack of open elements.
455
+ assert ! ( self . pop_current_node( ElementKind :: Link ) ) ;
456
+ token = self . t . next ( ) ;
457
+ continue ;
458
+ }
459
+ }
395
460
Some ( Token :: EndTag {
396
461
ref tag,
397
462
self_closing : _,
0 commit comments