How to use Attributes method of html Package

Best K6 code snippet using html.Attributes

service.go

Source:service.go Github

copy

Full Screen

...111 // "mobile": "13811299225",112 "User": "ec2admin",113 "Group": "ec2admin",114 } {115 data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{116 Name: k, FriendlyName: k,117 NameFormat: "urn:oasis:names:tc:SAML:2.0:attrname-format:uri",118 Values: []string{v},119 })120 }121 case "https://samltest.id/saml/sp": // samltest.id SSO122 data.NameId = "yunion"123 data.NameIdFormat = samlutils.NAME_ID_FORMAT_TRANSIENT124 data.AudienceRestriction = sp.GetEntityId()125 for _, v := range []struct {126 name string127 friendlyName string128 value string129 }{130 {131 name: "urn:oid:0.9.2342.19200300.100.1.1",132 friendlyName: "uid",133 value: "9646D89D-F5E7-F0E4-C545A9B2F4B7956B",134 },135 {136 name: "urn:oid:0.9.2342.19200300.100.1.3",137 friendlyName: "mail",138 value: "samltest@yunion.io",139 },140 {141 name: "urn:oid:2.5.4.4",142 friendlyName: "sn",143 value: "Jian",144 },145 {146 name: "urn:oid:2.5.4.42",147 friendlyName: "givenName",148 value: "Jian",149 },150 } {151 data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{152 Name: v.name,153 FriendlyName: v.friendlyName,154 NameFormat: "urn:oasis:names:tc:SAML:2.0:attrname-format:uri",155 Values: []string{v.value},156 })157 }158 case "cloud.tencent.com": // 腾讯云 role SSO159 data.NameId = "cvmcosreadonly"160 data.NameIdFormat = samlutils.NAME_ID_FORMAT_TRANSIENT161 data.AudienceRestriction = "https://cloud.tencent.com"162 for _, v := range []struct {163 name string164 friendlyName string165 value string166 }{167 {168 name: "https://cloud.tencent.com/SAML/Attributes/Role",169 friendlyName: "RoleEntitlement",170 value: "qcs::cam::uin/100008182714:roleName/cvmcosreadonly,qcs::cam::uin/100008182714:saml-provider/saml.yunion.io",171 },172 {173 name: "https://cloud.tencent.com/SAML/Attributes/RoleSessionName",174 friendlyName: "RoleSessionName",175 value: "cvmcosreadonly",176 },177 } {178 data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{179 Name: v.name,180 FriendlyName: v.friendlyName,181 Values: []string{v.value},182 })183 }184 case "urn:federation:MicrosoftOnline":185 data.NameId = sp.Username186 data.NameIdFormat = samlutils.NAME_ID_FORMAT_PERSISTENT187 data.AudienceRestriction = sp.GetEntityId()188 for _, v := range []struct {189 name string190 friendlyName string191 value string192 }{193 {194 name: "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress",195 value: data.NameId,196 },197 } {198 data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{199 Name: v.name,200 FriendlyName: v.friendlyName,201 Values: []string{v.value},202 })203 }204 return data, nil205 case "google.com/a/yunion-hk.com":206 data.NameId = "qiujian"207 data.NameIdFormat = samlutils.NAME_ID_FORMAT_TRANSIENT208 data.AudienceRestriction = sp.GetEntityId()209 for k, v := range map[string]string{210 "user.email": "qiujian@yunion-hk.com",211 } {212 data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{213 Name: k, FriendlyName: k,214 NameFormat: "urn:oasis:names:tc:SAML:2.0:attrname-format:uri",215 Values: []string{v},216 })217 }218 case "google.com":219 data.NameId = "qiujian@yunion-hk.com"220 data.NameIdFormat = samlutils.NAME_ID_FORMAT_EMAIL221 data.AudienceRestriction = sp.GetEntityId()222 for k, v := range map[string]string{223 "user.email": "qiujian@yunion-hk.com",224 } {225 data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{226 Name: k, FriendlyName: k,227 NameFormat: "urn:oasis:names:tc:SAML:2.0:attrname-format:uri",228 Values: []string{v},229 })230 }231 }232 return data, nil233 }234 idpFunc := func(ctx context.Context, sp *idp.SSAMLServiceProvider, idpId string) (samlutils.SSAMLIdpInitiatedLoginData, error) {235 log.Debugf("Recive IDP initiated Login: %s", sp.GetEntityId())236 data := samlutils.SSAMLIdpInitiatedLoginData{}237 switch sp.GetEntityId() {238 case "urn:alibaba:cloudcomputing": // 阿里云role SSO239 data.NameId = "ecsossreadonly"240 data.NameIdFormat = samlutils.NAME_ID_FORMAT_PERSISTENT241 data.AudienceRestriction = sp.GetEntityId()242 for k, v := range map[string]string{243 "https://www.aliyun.com/SAML-Role/Attributes/Role": "acs:ram::1123247935774897:role/administrator,acs:ram::1123247935774897:saml-provider/saml.yunion.io",244 "https://www.aliyun.com/SAML-Role/Attributes/RoleSessionName": "ecsossreadonly",245 "https://www.aliyun.com/SAML-Role/Attributes/SessionDuration": "1800",246 } {247 data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{248 Name: k,249 Values: []string{v},250 })251 }252 data.RelayState = "https://homenew.console.aliyun.com/"253 case "urn:amazon:webservices:cn-north-1": // AWS CN role SSO254 data.NameId = "ec2s3readonly"255 data.NameIdFormat = samlutils.NAME_ID_FORMAT_PERSISTENT256 data.AudienceRestriction = "https://signin.amazonaws.cn/saml"257 for _, v := range []struct {258 name string259 friendlyName string260 value string261 }{262 {263 name: "https://aws.amazon.com/SAML/Attributes/Role",264 friendlyName: "RoleEntitlement",265 value: "arn:aws-cn:iam::248697896586:role/ec2s3readonly,arn:aws-cn:iam::248697896586:saml-provider/saml.yunion.io",266 },267 {268 name: "https://aws.amazon.com/SAML/Attributes/RoleSessionName",269 friendlyName: "RoleSessionName",270 value: "ec2s3readonly",271 },272 {273 name: "urn:oid:1.3.6.1.4.1.5923.1.1.1.3",274 friendlyName: "eduPersonOrgDN",275 value: "ec2s3readonly",276 },277 } {278 data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{279 Name: v.name,280 FriendlyName: v.friendlyName,281 Values: []string{v.value},282 })283 }284 data.RelayState = "https://console.amazonaws.cn/"285 case "urn:amazon:webservices": // AWS Global role SSO286 data.NameId = "ec2s3readonly"287 data.NameIdFormat = samlutils.NAME_ID_FORMAT_PERSISTENT288 data.AudienceRestriction = "https://signin.aws.amazon.com/saml"289 for _, v := range []struct {290 name string291 friendlyName string292 value string293 }{294 {295 name: "https://aws.amazon.com/SAML/Attributes/Role",296 friendlyName: "RoleEntitlement",297 value: "arn:aws:iam::285906155448:role/ec2s3readonly,arn:aws:iam::285906155448:saml-provider/saml.yunion.cn",298 },299 {300 name: "https://aws.amazon.com/SAML/Attributes/RoleSessionName",301 friendlyName: "RoleSessionName",302 value: "ec2s3readonly",303 },304 {305 name: "urn:oid:1.3.6.1.4.1.5923.1.1.1.3",306 friendlyName: "eduPersonOrgDN",307 value: "ec2s3readonly",308 },309 } {310 data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{311 Name: v.name,312 FriendlyName: v.friendlyName,313 Values: []string{v.value},314 })315 }316 data.RelayState = "https://console.aws.amazon.com/"317 case "cloud.tencent.com": // 腾讯云 role SSO318 data.NameId = "cvmcosreadonly"319 data.NameIdFormat = samlutils.NAME_ID_FORMAT_TRANSIENT320 data.AudienceRestriction = "https://cloud.tencent.com"321 for _, v := range []struct {322 name string323 friendlyName string324 value string325 }{326 {327 name: "https://cloud.tencent.com/SAML/Attributes/Role",328 friendlyName: "RoleEntitlement",329 value: "qcs::cam::uin/100008182714:roleName/cvmcosreadonly,qcs::cam::uin/100008182714:saml-provider/saml.yunion.io",330 },331 {332 name: "https://cloud.tencent.com/SAML/Attributes/RoleSessionName",333 friendlyName: "RoleSessionName",334 value: "cvmcosreadonly",335 },336 } {337 data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{338 Name: v.name,339 FriendlyName: v.friendlyName,340 Values: []string{v.value},341 })342 }343 data.RelayState = "https://console.cloud.tencent.com/"344 }345 return data, nil346 }347 logoutFunc := func(ctx context.Context, idpId string) string {348 return fmt.Sprintf(`<!DOCTYPE html><html lang="zh_CN"><head><meta charset="utf-8"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><h1>成功退出登录,<a href="%s">重新登录</a></h1></body></html>`, httputils.JoinPath(options.Entity, "SAML/idp"))349 }350 idpInst := idp.NewIdpInstance(saml, spFunc, idpFunc, logoutFunc)351 for _, spMetaFile := range options.SpMeta {352 err := idpInst.AddSPMetadataFile(spMetaFile)353 if err != nil {354 return errors.Wrapf(err, "AddSPMetadataFile %s", spMetaFile)355 }356 }357 idpInst.AddHandlers(app, "SAML/idp", nil)358 idpInst.SetHtmlTemplate(i18n.NewTableEntry().CN(`<!DOCTYPE html><html lang="zh_CN"><head><meta charset="utf-8"><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><h1>正在跳转到云控制台,请等待。。。</h1>$FORM$</body></html>`))359 app.AddHandler("GET", "SAML/idp", func(ctx context.Context, w http.ResponseWriter, r *http.Request) {360 idpInitUrl := httputils.JoinPath(options.Entity, "SAML/idp/sso")361 htmlBuf := strings.Builder{}362 htmlBuf.WriteString(`<!doctype html><html lang=en><body><ol>`)363 // IDP initiated364 for _, v := range []struct {365 name string366 entityID string367 }{368 {369 name: "Aliyun Role SSO",370 entityID: "urn:alibaba:cloudcomputing",371 },372 {373 name: "AWS CN Role SSO",374 entityID: "urn:amazon:webservices:cn-north-1",375 },376 {377 name: "AWS Global Role SSO",378 entityID: "urn:amazon:webservices",379 },380 {381 name: "Tencent Cloud Role SSO",382 entityID: "cloud.tencent.com",383 },384 } {385 query := samlutils.SIdpInitiatedLoginInput{386 EntityID: v.entityID,387 IdpId: options.IdpId,388 }389 htmlBuf.WriteString(fmt.Sprintf(`<li><a href="%s?%s">%s (IDP-Initiated)</a></li>`, idpInitUrl, jsonutils.Marshal(query).QueryString(), v.name))390 }391 for _, v := range []struct {392 name string393 url string394 }{395 /*{396 name: "Huawei cloud partner SSO",397 url: "https://auth.huaweicloud.com/authui/saml/login?xAccountType=yunion_IDP&isFirstLogin=false&service=https%3a%2f%2fconsole.huaweicloud.com%2fiam%2f",398 },*/399 {400 name: "Huawei cloud SSO",401 url: "https://auth.huaweicloud.com/authui/federation/websso?domain_id=052d45a3e70010440f92c000d9e3f260&idp=yunion&protocol=saml",402 },403 {404 name: "Tencent cloud SSO",405 url: "https://cloud.tencent.com/login/forwardIdp/100008182714/saml.yunion.io",406 },407 {408 name: "Google cloud SSO",409 url: "https://www.google.com/a/yunion-hk.com/ServiceLogin?continue=https://console.cloud.google.com",410 },411 {412 name: "Azure cloud SSO",413 url: "https://login.microsoftonline.com/redeem?rd=https%3a%2f%2finvitations.microsoft.com%2fredeem%2f%3ftenant%3d17493ddf-fa90-4f95-8576-5df011c126e5%26user%3d3bc1c055-aa14-4795-aef0-5970b00d03c7%26ticket%3d0GDu%252bZ7nLbg01rYL5u%252b401%252bOLyZjxPewSBJIAZZ7E0U%253d%26ver%3d2.0",414 },415 } {416 htmlBuf.WriteString(fmt.Sprintf(`<li><a href="%s">%s (SP-Initiated)</a></li>`, v.url, v.name))417 }418 htmlBuf.WriteString(`</ol></body></html>`)419 appsrv.SendHTML(w, htmlBuf.String())420 })421 consumeFunc := func(ctx context.Context, w http.ResponseWriter, idp *sp.SSAMLIdentityProvider, result sp.SSAMLAssertionConsumeResult) error {422 html := strings.Builder{}423 html.WriteString("<!doctype html><html lang=en><head><meta charset=\"utf-8\"><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body><ol>")424 html.WriteString(fmt.Sprintf("<li>RequestId: %s</li>", result.RequestID))425 html.WriteString(fmt.Sprintf("<li>RelayState: %s</li>", result.RelayState))426 for _, v := range result.Attributes {427 html.WriteString(fmt.Sprintf("<li>%s(%s): %s</li>", v.Name, v.FriendlyName, v.Values))428 }429 html.WriteString("</ol></body></html>")430 appsrv.SendHTML(w, html.String())431 return nil432 }433 spLoginFunc := func(ctx context.Context, idp *sp.SSAMLIdentityProvider) (sp.SSAMLSpInitiatedLoginRequest, error) {434 result := sp.SSAMLSpInitiatedLoginRequest{}435 result.RequestID = samlutils.GenerateSAMLId()436 return result, nil437 }438 spInst := sp.NewSpInstance(saml, "Yunion SAML Demo Service", consumeFunc, spLoginFunc)439 for _, idpFile := range options.IdpMeta {440 err := spInst.AddIdpMetadataFile(idpFile)...

Full Screen

Full Screen

summary.go

Source:summary.go Github

copy

Full Screen

1package handlers2import (3 "encoding/json"4 "errors"5 "io"6 "log"7 "net/http"8 "net/url"9 "strconv"10 "strings"11 "golang.org/x/net/html"12)13//PreviewImage represents a preview image for a page14type PreviewImage struct {15 URL string `json:"url,omitempty"`16 SecureURL string `json:"secureURL,omitempty"`17 Type string `json:"type,omitempty"`18 Width int `json:"width,omitempty"`19 Height int `json:"height,omitempty"`20 Alt string `json:"alt,omitempty"`21}22//PageSummary represents summary properties for a web page23type PageSummary struct {24 Type string `json:"type,omitempty"`25 URL string `json:"url,omitempty"`26 Title string `json:"title,omitempty"`27 SiteName string `json:"siteName,omitempty"`28 Description string `json:"description,omitempty"`29 Author string `json:"author,omitempty"`30 Keywords []string `json:"keywords,omitempty"`31 Icon *PreviewImage `json:"icon,omitempty"`32 Images []*PreviewImage `json:"images,omitempty"`33}34//SummaryHandler handles requests for the page summary API.35//This API expects one query string parameter named `url`,36//which should contain a URL to a web page. It responds with37//a JSON-encoded PageSummary struct containing the page summary38//meta-data.39func SummaryHandler(w http.ResponseWriter, r *http.Request) {40 /*TODO: add code and additional functions to do the following:41 - Add an HTTP header to the response with the name42 `Access-Control-Allow-Origin` and a value of `*`. This will43 allow cross-origin AJAX requests to your server.44 - Get the `url` query string parameter value from the request.45 If not supplied, respond with an http.StatusBadRequest error.46 - Call fetchHTML() to fetch the requested URL. See comments in that47 function for more details.48 - Call extractSummary() to extract the page summary meta-data,49 as directed in the assignment. See comments in that function50 for more details51 - Close the response HTML stream so that you don't leak resources.52 - Finally, respond with a JSON-encoded version of the PageSummary53 struct. That way the client can easily parse the JSON back into54 an object. Remember to tell the client that the response content55 type is JSON.56 Helpful Links:57 https://golang.org/pkg/net/http/#Request.FormValue58 https://golang.org/pkg/net/http/#Error59 https://golang.org/pkg/encoding/json/#NewEncoder60 */61 // adds an http header to the response62 w.Header().Add("Access-Control-Allow-Origin", "*")63 // gets the url query string paramter value from the request64 form := r.FormValue("url")65 if len(form) == 0 {66 // respond with a http.StatusBadRequest error67 text := http.StatusText(http.StatusBadRequest)68 w.Write([]byte(text))69 }70 w.Header().Add("Content-Type", "application/json")71 htmlStream, err := fetchHTML(form)72 if err == nil {73 pageSum, err := extractSummary(form, htmlStream)74 json.NewEncoder(w).Encode(pageSum)75 if err != nil {76 panic(err)77 }78 }79}80//fetchHTML fetches `pageURL` and returns the body stream or an error.81//Errors are returned if the response status code is an error (>=400),82//or if the content type indicates the URL is not an HTML page.83func fetchHTML(pageURL string) (io.ReadCloser, error) {84 /*TODO: Do an HTTP GET for the page URL. If the response status85 code is >= 400, return a nil stream and an error. If the response86 content type does not indicate that the content is a web page, return87 a nil stream and an error. Otherwise return the response body and88 no (nil) error.89 To test your implementation of this function, run the TestFetchHTML90 test in summary_test.go. You can do that directly in Visual Studio Code,91 or at the command line by running:92 go test -run TestFetchHTML93 Helpful Links:94 https://golang.org/pkg/net/http/#Get95 */96 resp, err := http.Get(pageURL)97 if err == nil {98 if resp.StatusCode >= 400 {99 err = errors.New("Bad status code")100 return nil, err101 }102 ctype := resp.Header.Get("Content-Type")103 if !strings.HasPrefix(ctype, "text/html") {104 err = errors.New("Non html page")105 return nil, err106 }107 return resp.Body, nil108 } else {109 return nil, errors.New("404 page not found")110 }111}112//extractSummary tokenizes the `htmlStream` and populates a PageSummary113//struct with the page's summary meta-data.114func extractSummary(pageURL string, htmlStream io.ReadCloser) (*PageSummary, error) {115 /*TODO: tokenize the `htmlStream` and extract the page summary meta-data116 according to the assignment description.117 To test your implementation of this function, run the TestExtractSummary118 test in summary_test.go. You can do that directly in Visual Studio Code,119 or at the command line by running:120 go test -run TestExtractSummary121 Helpful Links:122 https://drstearns.github.io/tutorials/tokenizing/123 http://ogp.me/124 https://developers.facebook.com/docs/reference/opengraph/125 https://golang.org/pkg/net/url/#URL.ResolveReference126 */127 pageSum := &PageSummary{}128 if htmlStream == nil {129 return nil, nil130 }131 tokenizer := html.NewTokenizer(htmlStream)132 imgs := []*PreviewImage{}133 foundImage := false134 for {135 tokenType := tokenizer.Next()136 // handles error token / end of file edge case137 if tokenType == html.ErrorToken {138 err := tokenizer.Err()139 if err == io.EOF {140 break141 }142 }143 if tokenType == html.EndTagToken {144 token := tokenizer.Token()145 if "head" == token.Data {146 break147 }148 }149 // handles title150 if tokenType == html.StartTagToken || tokenType == html.SelfClosingTagToken {151 token := tokenizer.Token()152 if "link" == token.Data {153 img := &PreviewImage{}154 attributes := token.Attr155 for i := 0; i < len(attributes); i++ {156 if attributes[i].Key == "href" {157 link := attributes[i].Val158 u, err := url.Parse(link)159 if err != nil {160 log.Fatal(err)161 }162 base, err := url.Parse(pageURL)163 if err != nil {164 log.Fatal(err)165 }166 img.URL = base.ResolveReference(u).String()167 }168 if attributes[i].Key == "type" {169 img.Type = attributes[i].Val170 }171 if attributes[i].Key == "sizes" && attributes[i].Val != "any" {172 sizes := strings.Split(attributes[i].Val, "x")173 img.Height, _ = strconv.Atoi(sizes[0])174 if len(sizes) == 2 {175 img.Width, _ = strconv.Atoi(sizes[1])176 }177 }178 }179 pageSum.Icon = img180 }181 if "meta" == token.Data {182 attributes := token.Attr183 for i := 0; i < len(attributes); i++ {184 if attributes[i].Val == "keywords" {185 keywordSlice := strings.Split(attributes[i+1].Val, ",")186 for j := 0; j < len(keywordSlice); j++ {187 keywordSlice[j] = strings.TrimSpace(keywordSlice[j])188 }189 pageSum.Keywords = keywordSlice190 }191 if attributes[i].Val == "og:title" {192 for j := 0; j < len(attributes); j++ {193 if attributes[j].Key == "content" {194 pageSum.Title = attributes[j].Val195 println(attributes[j].Val)196 }197 }198 }199 if attributes[i].Val == "og:type" {200 for j := 0; j < len(attributes); j++ {201 if attributes[j].Key == "content" {202 pageSum.Type = attributes[j].Val203 }204 }205 }206 if attributes[i].Val == "og:url" {207 for j := 0; j < len(attributes); j++ {208 if attributes[j].Key == "content" {209 pageSum.URL = attributes[j].Val210 }211 }212 }213 if attributes[i].Val == "og:site_name" {214 for j := 0; j < len(attributes); j++ {215 if attributes[j].Key == "content" {216 pageSum.SiteName = attributes[j].Val217 }218 }219 }220 if attributes[i].Val == "og:description" {221 for j := 0; j < len(attributes); j++ {222 if attributes[j].Key == "content" {223 pageSum.Description = attributes[j].Val224 }225 }226 } else if attributes[i].Val == "description" && pageSum.Description == "" {227 for j := 0; j < len(attributes); j++ {228 if attributes[j].Key == "content" {229 pageSum.Description = attributes[j].Val230 }231 }232 }233 if attributes[i].Val == "author" {234 for j := 0; j < len(attributes); j++ {235 if attributes[j].Key == "content" {236 pageSum.Author = attributes[j].Val237 }238 }239 }240 if attributes[i].Val == "og:image" {241 link := ""242 for j := 0; j < len(attributes); j++ {243 if attributes[j].Key == "content" {244 link = attributes[j].Val245 }246 }247 foundImage = true248 img := &PreviewImage{}249 u, err := url.Parse(link)250 if err != nil {251 log.Fatal(err)252 }253 base, err := url.Parse(pageURL)254 if err != nil {255 log.Fatal(err)256 }257 img.URL = base.ResolveReference(u).String()258 imgs = append(imgs, img)259 }260 if attributes[i].Val == "og:image:secure_url" {261 for j := 0; j < len(attributes); j++ {262 if attributes[j].Key == "content" {263 imgs[len(imgs)-1].SecureURL = attributes[j].Val264 }265 }266 }267 if attributes[i].Val == "og:image:type" {268 for j := 0; j < len(attributes); j++ {269 if attributes[j].Key == "content" {270 imgs[len(imgs)-1].Type = attributes[j].Val271 }272 }273 }274 if attributes[i].Val == "og:image:width" {275 for j := 0; j < len(attributes); j++ {276 if attributes[j].Key == "content" {277 num, err := strconv.Atoi(attributes[j].Val)278 if err == nil {279 imgs[len(imgs)-1].Width = num280 }281 }282 }283 }284 if attributes[i].Val == "og:image:height" {285 for j := 0; j < len(attributes); j++ {286 if attributes[j].Key == "content" {287 num, err := strconv.Atoi(attributes[j].Val)288 if err == nil {289 imgs[len(imgs)-1].Height = num290 }291 }292 }293 }294 if attributes[i].Val == "og:image:alt" {295 for j := 0; j < len(attributes); j++ {296 if attributes[j].Key == "content" {297 imgs[len(imgs)-1].Alt = attributes[j].Val298 }299 }300 }301 }302 }303 if "title" == token.Data {304 tokenType = tokenizer.Next()305 if tokenType == html.TextToken {306 if pageSum.Title == "" {307 pageSum.Title = tokenizer.Token().Data308 }309 }310 }311 }312 }313 pageSum.Images = imgs314 if foundImage == false {315 pageSum.Images = nil316 }317 htmlStream.Close()318 return pageSum, nil319}...

Full Screen

Full Screen

elements.go

Source:elements.go Github

copy

Full Screen

...6 "sort"7 "strings"8)9type Element struct {10 Attributes map[string]string11 Children []*Element12 HasEndTag bool13 TagName string14 Text string15}16// AddAttributeValue appends value to the named attribute’s value, separated by17// a space. If the attribute does not exist, it is created first. This method is18// chainable.19func (e *Element) AddAttributeValue(name, value string) *Element {20 if e == nil {21 return nil22 } else if e.Attributes == nil {23 e.Attributes = make(map[string]string)24 }25 if _, ok := e.Attributes[name]; ok {26 e.Attributes[name] += " "27 }28 e.Attributes[name] += value29 return e30}31// SetAttributeValue replaces the value of the named attribute with the provided32// one. If the attribute does not exist, it is created first. This method is33// chainable.34func (e *Element) SetAttributeValue(name, value string) *Element {35 if e == nil {36 return nil37 } else if e.Attributes == nil {38 e.Attributes = make(map[string]string)39 }40 e.Attributes[name] = value41 return e42}43// Html is the same as String, but the HTML code can be used in templates.44func (e *Element) Html() template.HTML {45 return template.HTML(e.String())46}47// String returns the element as HTML code.48func (e *Element) String() string {49 if e == nil || e.TagName == "" {50 return ""51 }52 capacity := 2 + len(e.Children)53 if len(e.Attributes) > 0 {54 capacity += 155 }56 if e.HasEndTag {57 capacity += 158 }59 pieces := make([]string, 0, capacity)60 pieces = append(pieces, "<"+e.TagName)61 if len(e.Attributes) > 0 {62 attributes := make(sort.StringSlice, 0, len(e.Attributes))63 for k, v := range e.Attributes {64 if v == "" {65 attributes = append(attributes, k)66 } else {67 attributes = append(attributes, k+`="`+html.EscapeString(v)+`"`)68 }69 }70 sort.Sort(attributes)71 pieces = append(pieces, " "+strings.Join(attributes, " "))72 }73 pieces = append(pieces, ">")74 for _, child := range e.Children {75 pieces = append(pieces, child.String())76 }77 if e.HasEndTag {...

Full Screen

Full Screen

Attributes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, err := html.Parse(os.Stdin)4 if err != nil {5 fmt.Fprintf(os.Stderr, "findlinks1: %v6 os.Exit(1)7 }8 for _, link := range visit(nil, doc) {9 fmt.Println(link)10 }11}12func visit(links []string, n *html.Node) []string {13 if n.Type == html.ElementNode && n.Data == "a" {14 for _, a := range n.Attr {15 if a.Key == "href" {16 links = append(links, a.Val)17 }18 }19 }20 for c := n.FirstChild; c != nil; c = c.NextSibling {21 links = visit(links, c)22 }23}24import (25func main() {26 doc, err := html.Parse(os.Stdin)27 if err != nil {28 fmt.Fprintf(os.Stderr, "findlinks1: %v29 os.Exit(1)30 }31 for _, link := range visit(nil, doc) {32 fmt.Println(link)33 }34}35func visit(links []string, n *html.Node) []string {36 if n.Type == html.ElementNode && n.Data == "a" {37 for _, a := range n.Attr {38 if a.Key == "href" {39 links = append(links, a.Val)40 }41 }42 }43 for c := n.FirstChild; c != nil; c = c.NextSibling {44 links = visit(links, c)45 }46}47import (48func main() {49 doc, err := html.Parse(os.Stdin)50 if err != nil {51 fmt.Fprintf(os.Stderr, "findlinks1: %v52 os.Exit(1)53 }54 for _, link := range visit(nil, doc) {55 fmt.Println(link)56 }57}58func visit(links []string, n *html.Node) []string {

Full Screen

Full Screen

Attributes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 defer resp.Body.Close()7 doc, err := html.Parse(resp.Body)8 if err != nil {9 log.Fatal(err)10 }11 for _, link := range visit(nil, doc) {12 fmt.Println(link)13 }14}15func visit(links []string, n *html.Node) []string {16 if n.Type == html.ElementNode && n.Data == "a" {17 for _, a := range n.Attr {18 if a.Key == "href" {

Full Screen

Full Screen

Attributes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 for _, url := range os.Args[1:] {4 links, err := findLinks(url)5 if err != nil {6 fmt.Fprintf(os.Stderr, "findLinks: %v7 }8 for _, link := range links {9 fmt.Println(link)10 }11 }12}13func findLinks(url string) ([]string, error) {14 resp, err := http.Get(url)15 if err != nil {16 }17 if resp.StatusCode != http.StatusOK {18 resp.Body.Close()19 return nil, fmt.Errorf("getting %s: %s", url, resp.Status)20 }21 doc, err := html.Parse(resp.Body)22 resp.Body.Close()23 if err != nil {24 return nil, fmt.Errorf("parsing %s as HTML: %v", url, err)25 }26 return visit(nil, doc), nil27}28func visit(links []string, n *html.Node) []string {29 if n.Type == html.ElementNode && n.Data == "a" {30 for _, a := range n.Attr {31 if a.Key == "href" {32 links = append(links, a.Val)33 }34 }35 }36 for c := n.FirstChild; c != nil; c = c.NextSibling {37 links = visit(links, c)38 }39}

Full Screen

Full Screen

Attributes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 log.Fatal(err)5 }6 defer resp.Body.Close()7 doc, err := html.Parse(resp.Body)8 if err != nil {9 log.Fatal(err)10 }11 var f func(*html.Node)12 f = func(n *html.Node) {13 if n.Type == html.ElementNode && n.Data == "a" {14 for _, a := range n.Attr {15 if a.Key == "href" {16 fmt.Println(a.Val)17 }18 }19 }20 for c := n.FirstChild; c != nil; c = c.NextSibling {21 f(c)22 }23 }24 f(doc)25}

Full Screen

Full Screen

Attributes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println("Error in http.Get", err)5 }6 defer resp.Body.Close()7 doc, err := html.Parse(resp.Body)8 if err != nil {9 fmt.Println("Error in html.Parse", err)10 }11 attrs := make([]html.Attribute, 0)12 attrs = getAttrs(attrs, doc)13 for _, attr := range attrs {14 fmt.Println("Attribute:", attr.Key, "Value:", attr.Val)15 }16}17func getAttrs(attrs []html.Attribute, n *html.Node) []html.Attribute {18 if n.Type == html.ElementNode && len(n.Attr) > 0 {19 attrs = append(attrs, n.Attr...)20 }21 for c := n.FirstChild; c != nil; c = c.NextSibling {22 attrs = getAttrs(attrs, c)23 }24}

Full Screen

Full Screen

Attributes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 z := html.NewTokenizer(resp.Body)4 for {5 tt := z.Next()6 switch {7 t := z.Token()8 if isAnchor {9 for _, a := range t.Attr {10 if a.Key == "href" {11 fmt.Printf("link: %s12 }13 }14 }15 }16 }17}

Full Screen

Full Screen

Attributes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 f, _ := os.Open("test.html")4 doc, _ := html.Parse(f)5 node := ElementByID(doc, "h1")6 fmt.Println(node.Data)7}8func ElementByID(doc *html.Node, id string) *html.Node {9 return forEachNode(doc, id, startElement, endElement)10}11func forEachNode(n *html.Node, id string, pre, post func(n *html.Node, id string) bool) *html.Node {12 if pre != nil {13 if pre(n, id) {14 }15 }16 for c := n.FirstChild; c != nil; c = c.NextSibling {17 if forEachNode(c, id, pre, post) != nil {18 }19 }20 if post != nil {21 if post(n, id) {22 }23 }24}25func startElement(n *html.Node, id string) bool {26 if n.Type == html.ElementNode {27 for _, a := range n.Attr {28 if a.Key == "id" && a.Val == id {29 }30 }31 }32}33func endElement(n *html.Node, id string) bool {34}

Full Screen

Full Screen

Attributes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 res, err := http.Get(url)4 if err != nil {5 log.Fatal(err)6 }7 defer res.Body.Close()8 if res.StatusCode != 200 {9 log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)10 }11 doc, err := html.Parse(res.Body)12 if err != nil {13 log.Fatal(err)14 }15 var f func(*html.Node)16 f = func(n *html.Node) {17 if n.Type == html.ElementNode && n.Data == "a" {18 for _, a := range n.Attr {19 if a.Key == "href" {20 hrefs = append(hrefs, a.Val)21 }22 }23 }24 for c := n.FirstChild; c != nil; c = c.NextSibling {25 f(c)26 }27 }28 f(doc)29 fmt.Println(hrefs)30}31import (

Full Screen

Full Screen

Attributes

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 doc, err := html.Parse(strings.NewReader(s))4 if err != nil {5 fmt.Println(err)6 }7 fmt.Println(doc)8 visit(doc)9}10func visit(n *html.Node) {11 if n.Type == html.ElementNode {12 fmt.Println(n.Data)13 for _, a := range n.Attr {14 fmt.Println(a.Key, a.Val)15 }16 }17 for c := n.FirstChild; c != nil; c = c.NextSibling {18 visit(c)19 }20}21&{1

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run K6 automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful