Copyright 2019 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
Package pagecheck implements HTML checkers for discovery site pages. It uses the general-purpose checkers in internal/testing/htmlcheck to define site-specific checkers.
package pagecheck

import (
	
	
	

	
)
Page describes a discovery site web page for a package, module or directory.
ModulePath is the module path for the unit page.
Suffix is the unit path element after module path; empty for a module
Version is the full version of the module, or the go tag if it is the stdlib.
FormattedVersion is the version of the module, or go tag if it is the stdlib. The version string may be truncated if it is a pseudoversion.
Title is output of frontend.pageTitle.
LicenseType is name of the license.
LicenseFilePath is the path of the license relative to the module directory.
IsLatestMinor is the latest minor version of this module.
MissingInMinor says that the unit is missing in the latest minor version of this module.
IsLatestMajor is the latest major version of this series.
LatestLink is the href of "Go to latest" link.
LatestMajorVersion is the suffix of the latest major version, empty if v0 or v1.
LatestMajorVersionLink is the link to the latest major version of the unit. If the unit does not exist at the latest major version, it is a link to the latest major version of the module.
UnitURLFormat is the relative unit URL, with one %s for "@version".
ModuleURL is the relative module URL.
CommitTime is the output of frontend.absoluteTime for the commit time.
UnitHeader checks a main page header for a unit.
func ( *Page,  bool,  bool) htmlcheck.Checker {
	 := unitURLPath(, )
	 := path.Base(.Suffix)
	if .Suffix == "" {
		 = .ModulePath
	}
	 := .LicenseType
	 :=  + "?tab=licenses"
	if .LicenseType == "" {
		 = "not legal advice"
		 = "/license-policy"
	}

	 := in("",
		in(`[data-test-id="UnitHeader-imports"]`,
			in("a",
				href(+"?tab=imports"),
				text(`Imports: [0-9]+\+?`))),
		in(`[data-test-id="UnitHeader-importedby"]`,
			in("a",
				href(+"?tab=importedby"),
				text(`Imported by: [0-9]+\+?`))))
	if ! {
		 = nil
	}

	var  htmlcheck.Checker
	if .IsLatestMajor {
		 = htmlcheck.NotIn(`[data-test-id="UnitHeader-majorVersionBanner"]`)
	} else {
		 = in(`[data-test-id="UnitHeader-majorVersionBanner"]`,
			in("span",
				text("The highest tagged major version is"),
				in("a",
					href(.LatestMajorVersionLink),
					text(.LatestMajorVersion),
				),
			),
		)
	}
	return in("",
		versionBadge(),
		in(`[data-test-id="UnitHeader-breadcrumbCurrent"]`, text()),
		in(`[data-test-id="UnitHeader-title"]`, text(.Title)),
		,
		in(`[data-test-id="UnitHeader-version"]`,
			in("a",
				href("?tab=versions"),
				exactText("Version: "+.FormattedVersion))),
		in(`[data-test-id="UnitHeader-commitTime"]`,
			text(.CommitTime)),
		in(`[data-test-id="UnitHeader-licenses"]`,
			in("a",
				href(),
				text())),
		)
}
UnitReadme checks the readme section of the main page.
func () htmlcheck.Checker {
	return in(".UnitReadme",
		in(`[data-test-id="Unit-readmeContent"]`, text("readme")),
	)
}
UnitDoc checks the doc section of the main page.
func () htmlcheck.Checker {
	return in(".Documentation", text(`Overview`))
}
UnitDirectories checks the directories section of the main page. If firstHref isn't empty, it and firstText should exactly match href and text of the first link in the Directories table.
func (,  string) htmlcheck.Checker {
	var  htmlcheck.Checker
	if  != "" {
		 = in(`[data-test-id="UnitDirectories-table"] a`, href(), exactText())
	}
	return in("",
		in("th:nth-child(1)", text("^Path$")),
		in("th:nth-child(2)", text("^Synopsis$")),
		)
}
CanonicalURLPath checks the canonical url for the unit on the page.
func ( string) htmlcheck.Checker {
	return in(".js-canonicalURLPath", attr("data-canonical-url-path", ))
}
SubdirectoriesDetails checks the detail section of a subdirectories tab. If firstHref isn't empty, it and firstText should exactly match href and text of the first link in the Directories table.
func (,  string) htmlcheck.Checker {
	var  htmlcheck.Checker
	if  != "" {
		 = in("table.Directories a", href(), exactText())
	}
	return in("",
		in("th:nth-child(1)", text("^Path$")),
		in("th:nth-child(2)", text("^Synopsis$")),
		)
}
LicenseDetails checks the details section of a license tab.
func (, ,  string) htmlcheck.Checker {
	return in("",
		in(".License",
			text(regexp.QuoteMeta()),
			text("This is not legal advice"),
			in("a",
				href("/license-policy"),
				exactText("Read disclaimer.")),
			in(".License-contents",
				text(regexp.QuoteMeta()))),
		in(".License-source",
			exactText("Source: "+)))
}
versionBadge checks the latest-version badge on a header.
func ( *Page) htmlcheck.Checker {
	 := "DetailsHeader-badge"
	switch {
	case .MissingInMinor:
		 += "--notAtLatest"
	case .IsLatestMinor:
		 += "--latest"
	default:
		 += "--goToLatest"
	}
	return in(`[data-test-id="UnitHeader-minorVersionBanner"]`,
		attr("class", `\b`+regexp.QuoteMeta()+`\b`), // the badge has this class too
		in("a", href(.LatestLink), exactText("Go to latest")))
}

func ( *Page,  bool) string {
	 := ""
	if  {
		 = "@" + .Version
	}
	return fmt.Sprintf(.UnitURLFormat, )