@@ -86,6 +86,9 @@ func ExtractLinks(weburl string) ([]string, error) {
8686
8787func ElementsNext (doc * html.Node ) []* html.Node {
8888 nodes := []* html.Node {}
89+ if doc == nil {
90+ return nil
91+ }
8992 visitNode := func (n * html.Node ) {
9093 if n .NextSibling != nil {
9194 nodes = append (nodes , n )
@@ -96,7 +99,7 @@ func ElementsNext(doc *html.Node) []*html.Node {
9699}
97100
98101func ElementsRmByTag (doc * html.Node , name ... string ) {
99- if len (name ) == 0 {
102+ if len (name ) == 0 || doc == nil {
100103 return
101104 }
102105 visitNode := func (n * html.Node ) {
@@ -123,7 +126,7 @@ func ElementsRmByTag(doc *html.Node, name ...string) {
123126
124127func ElementsByTag (doc * html.Node , name ... string ) []* html.Node {
125128 var nodes []* html.Node
126- if len (name ) == 0 {
129+ if len (name ) == 0 || doc == nil {
127130 return nil
128131 }
129132 if doc .Type == html .ElementNode {
@@ -141,7 +144,7 @@ func ElementsByTag(doc *html.Node, name ...string) []*html.Node {
141144
142145func ElementsByTagAndClass (doc * html.Node , tag , class string ) []* html.Node {
143146 var nodes []* html.Node
144- if tag == "" || class == "" {
147+ if tag == "" || class == "" || doc == nil {
145148 return nil
146149 }
147150 if doc .Type == html .ElementNode {
@@ -160,6 +163,9 @@ func ElementsByTagAndClass(doc *html.Node, tag, class string) []*html.Node {
160163}
161164
162165func ElementsByTagAndClass2 (raw []byte , tag , class string ) []byte {
166+ if raw == nil || tag == "" || class == "" {
167+ return nil
168+ }
163169 z := html .NewTokenizer (bytes .NewReader (raw ))
164170 var b bytes.Buffer
165171 for {
@@ -184,7 +190,7 @@ func ElementsByTagAndClass2(raw []byte, tag, class string) []byte {
184190
185191func ElementsByTagAndId (doc * html.Node , tag , id string ) []* html.Node {
186192 var nodes []* html.Node
187- if tag == "" || id == "" {
193+ if doc == nil || tag == "" || id == "" {
188194 return nil
189195 }
190196 if doc .Type == html .ElementNode {
@@ -203,6 +209,9 @@ func ElementsByTagAndId(doc *html.Node, tag, id string) []*html.Node {
203209}
204210
205211func ElementsByTagAndId2 (raw []byte , tag , id string ) []byte {
212+ if raw == nil || tag == "" || id == "" {
213+ return nil
214+ }
206215 z := html .NewTokenizer (bytes .NewReader (raw ))
207216 for {
208217 tt := z .Next ()
@@ -225,7 +234,7 @@ func ElementsByTagAndId2(raw []byte, tag, id string) []byte {
225234}
226235func ElementsByTagAndType (doc * html.Node , tag , attrType string ) []* html.Node {
227236 var nodes []* html.Node
228- if tag == "" || attrType == "" {
237+ if tag == "" || attrType == "" || doc == nil {
229238 return nil
230239 }
231240 if doc .Type == html .ElementNode {
@@ -245,6 +254,9 @@ func ElementsByTagAndType(doc *html.Node, tag, attrType string) []*html.Node {
245254
246255func ElementsNextByTag (doc * html.Node , tag string ) []* html.Node {
247256 var nodes []* html.Node
257+ if tag == "" || doc == nil {
258+ return nil
259+ }
248260 if doc == nil || tag == "" {
249261 return nil
250262 }
0 commit comments