Documentation ¶
Overview ¶
Package amphtml contains common constants and utilies for working with AMP HTML.
Index ¶
- Constants
- func AMPExtensionName(n *html.Node) (string, bool)
- func GetSrcsetWidths(w int) ([]int, bool)
- func IsAMPCustomElement(n *html.Node) bool
- func IsCacheURL(input string) bool
- func IsScriptAMPExtension(n *html.Node) bool
- func IsScriptAMPRuntime(n *html.Node) bool
- func IsScriptAMPViewer(n *html.Node) bool
- func IsScriptRenderDelaying(n *html.Node) bool
- func ToAbsoluteURL(documentURL string, baseURL *url.URL, urlParam string) string
- func ToCacheURLSubdomain(originHost string) string
- type CacheURL
- type DOM
- type SubresourceOffset
- type SubresourceType
Constants ¶
const ( AMPAudio = "amp-audio" AMPBoilerplate = "amp-boilerplate" AMPBoilerplateCSS = "" /* 598-byte string literal not displayed */ AMPBoilerplateNoscriptCSS = "body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}" AMPCacheHostName = "cdn.ampproject.org" AMPCacheSchemeAndHost = "https://cdn.ampproject.org" AMPCacheRootURL = "https://cdn.ampproject.org/" AMP4AdsBoilerplate = "amp4ads-boilerplate" AMP4AdsAndAMP4EMailBoilerplateCSS = "body{visibility:hidden}" AMP4EmailBoilerplate = "amp4email-boilerplate" AMPCustom = "amp-custom" AMPCustomElement = "custom-element" AMPCustomTemplate = "custom-template" AMPHostService = "host-service" AMPDynamicCSSClasses = "amp-dynamic-css-classes" AMPExperiment = "amp-experiment" AMPRuntime = "amp-runtime" AMPStory = "amp-story" IAMPHTMLLayout = "i-amphtml-layout" )
Common AMP string constants.
Variables ¶
This section is empty.
Functions ¶
func AMPExtensionName ¶
AMPExtensionName returns the name of the extension this node represents. For most extensions this is the value of the "custom-element" attribute. Returns ok=false if this isn't an extension.
func GetSrcsetWidths ¶
GetSrcsetWidths returns a slice of widths based on the input width, or false if there are not at least two legitimate widths.
func IsAMPCustomElement ¶
IsAMPCustomElement returns true if the node is an AMP custom element.
func IsCacheURL ¶
IsCacheURL returns true if the given string is from the AMPCache domain. This check is overly simplistic and does no actual verification that the URL resolves (doesn't 404), nor if the URL is of the correct format for the resource type (image, or otherwise).
func IsScriptAMPExtension ¶
IsScriptAMPExtension returns true if the node is a script tag representing an extension.
func IsScriptAMPRuntime ¶
IsScriptAMPRuntime returns true if the node is of the form <script async src=https://cdn.ampproject.org...v0.js></script>
func IsScriptAMPViewer ¶
IsScriptAMPViewer returns true if the node is of the form <script async src=https://cdn.ampproject.org/v0/amp-viewer-integration-...js></script>
func IsScriptRenderDelaying ¶
IsScriptRenderDelaying returns true if the node has one of these values for attribute 'custom-element': amp-dynamic-css-classes, amp-experiment, amp-story.
func ToAbsoluteURL ¶
ToAbsoluteURL absolute-ifies |urlParam|, using |baseURL| as the base if |urlParam| is relative. If |urlParam| contains a fragment, this method will return only a fragment if its absolute URL matches |documentURL|, which prevents changing an in-document navigation to a out-of-document navigation.
func ToCacheURLSubdomain ¶
ToCacheURLSubdomain converts an origin domain name to a dot-free human-readable string, that can be used in combination with an AMP Cache domain to identify the publisher's subdomain within that cache. If problems are encountered, fallback to a one-way hash.
1. Converts the origin domain from IDN (Punycode) to UTF-8. 2. Replaces every "-" (dash) with "--"(2 dashes). 3. Replaces every "." (dot) with a "-" (dash). 4. Converts back to IDN (Punycode).
For example, if the origin is www.example.com, this returns www-example-com. On Google's AMP Cache, this will be prepended to the Google cache domain resulting in www-example-com.cdn.ampproject.org . See https://developers.google.com/amp/cache/overview for more info
Types ¶
type CacheURL ¶
type CacheURL struct { Subdomain string // publisher's subdomain within the cache. e.g. "example-com" *url.URL // contains filtered or unexported fields }
CacheURL represents an AMP Cache URL
func (*CacheURL) OriginDomain ¶
OriginDomain returns the scheme and host name, ignoring any path info.
type DOM ¶
DOM encapsulates the various HTML nodes a transformer may need access to.
type SubresourceOffset ¶
type SubresourceOffset struct { SubType SubresourceType // The offset position denoting the start of the substring (inclusive) Start int // The offset position denoting the end of the substring (exclusive) End int // If the type is an image, an optional width to convert the image so. DesiredImageWidth int }
SubresourceOffset describes the location of a subresource URL within some text. For example, if the text value is ".a {background-image:url(foo.jpg)}", then Start === 25 and End === 32
func ParseSrcset ¶
func ParseSrcset(in string) (string, []SubresourceOffset)
ParseSrcset parses the given srcset attribute value of its image candidates (as defined by https://html.spec.whatwg.org/multipage/images.html#image-candidate-string) and returns the normalized srcset value, and a slice of SubresourceOffset structs corresponding to the normalized value. If there is no width or pixel density, it defaults to 1x. If any portion of the input is unparseable, or if there are duplicate widths or pixel densities, return input unparsed.
func (*SubresourceOffset) GetCacheURL ¶
func (so *SubresourceOffset) GetCacheURL(documentURL string, base *url.URL, input string) (*CacheURL, error)
GetCacheURL returns an AMP Cache URL structure for the URL identified by the given offset (relative to 'input') or an error if the URL could not be parsed.
type SubresourceType ¶
type SubresourceType int8
SubresourceType describes the type of subresource
const ( // ImageType is a subresource for an image ImageType SubresourceType = iota // OtherType is a subresource for everything besides an image. OtherType )