amphtml

package
v0.0.0-...-57974ef Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package amphtml contains common constants and utilies for working with AMP HTML.

Index

Constants

View Source
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

func AMPExtensionName(n *html.Node) (string, bool)

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 AMPExtensionScriptDefinition

func AMPExtensionScriptDefinition(n *html.Node) (string, bool)

AMPExtensionScriptDefinition returns a unique script definition that takes into account the extension name, version and if it is module/nomodule. Example (amp-ad): amp-ad-0.1.js (regular/nomodule), amp-ad-0.1.mjs (module). The AMP Validator prevents a mix of regular and nomodule extensions. If the pattern is not found then uses value of "src" attribute. Returns ok=false if this isn't an extension.

func GetSrcsetWidths

func GetSrcsetWidths(w int) ([]int, bool)

GetSrcsetWidths returns a slice of widths based on the input width, or false if there are not at least two legitimate widths.

func IsAMPCustomElement

func IsAMPCustomElement(n *html.Node) bool

IsAMPCustomElement returns true if the node is an AMP custom element.

func IsCacheURL

func IsCacheURL(input string) bool

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

func IsScriptAMPExtension(n *html.Node) bool

IsScriptAMPExtension returns true if the node is a script tag representing an extension.

func IsScriptAMPRuntime

func IsScriptAMPRuntime(n *html.Node) bool

IsScriptAMPRuntime returns true if the node is of the form <script async src=https://cdn.ampproject.org...(v0.js|v0.mjs|amp4ads-v0.js|amp4ads-v0.mjs)></script>

func IsScriptAMPViewer

func IsScriptAMPViewer(n *html.Node) bool

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

func IsScriptRenderDelaying(n *html.Node) bool

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

func ToAbsoluteURL(documentURL string, baseURL *url.URL,
	urlParam string) string

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

func ToCacheURLSubdomain(originHost string) string

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

func (c *CacheURL) OriginDomain() string

OriginDomain returns the scheme and host name, ignoring any path info.

func (*CacheURL) String

func (c *CacheURL) String() string

String reassembles the URL into a URL string

type DOM

type DOM struct {
	RootNode *html.Node
	HTMLNode *html.Node
	HeadNode *html.Node
	BodyNode *html.Node
}

DOM encapsulates the various HTML nodes a transformer may need access to.

func NewDOM

func NewDOM(n *html.Node) (*DOM, error)

NewDOM constructs and returns a pointer to a DOM struct by finding the HTML nodes relevant to an AMP Document or an error if there was a problem. TODO(alin04): I don't think this can EVER return an error. The golang parser creates all these nodes if they're missing.

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
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL