Documentation ¶
Overview ¶
Package transformers performs AMP transformations/optimizations on the DOM tree.
Index ¶
- func AMPBoilerplate(e *Context) error
- func AMPRuntimeCSS(e *Context) error
- func AbsoluteURL(e *Context) error
- func LinkTag(e *Context) error
- func NodeCleanup(e *Context) error
- func PreloadImage(e *Context) error
- func ReorderHead(e *Context) error
- func ServerSideRendering(e *Context) error
- func StripJS(e *Context) error
- func TransformedIdentifier(e *Context) error
- func URLRewrite(e *Context) error
- func UnusedExtensions(e *Context) error
- type Context
- type PreloadData
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AMPBoilerplate ¶
AMPBoilerplate removes <style> and <noscript> tags in <head>, keeping only the amp-custom style tag. It then inserts the amp-boilerplate.
func AMPRuntimeCSS ¶
AMPRuntimeCSS inlines the contents of the AMP HTML CSS RTV.
func AbsoluteURL ¶
AbsoluteURL operates on URL attributes. It rewrites URLs as Absolute URLs. These are based on the URL in the attribute and the base url of the document. A base URL is the final URL of the document after all redirects. If the attribute URL is relative, then it is relative to the base_url. There is special handling for URLs that contain fragments.
URLs are also canonicalized: * leading and trailing whitespace are trimmed.
* The following attributes may be rewritten:
- Any tag with attribute:
- background
- href
- poster
- src
- Any <amp-install-serviceworker> with attribute:
- data-iframe-src
- data-no-service-worker-fallback-shell-url
- Any <amp-story> tag with attribute:
- background-audio
- bookend-config-src
- poster-landscape-src
- poster-square-src
- publisher-logo-src
- Any <amp-story-page> tag with attribute:
- background-audio
- Any <form> tag with attribute:
- action
- action-xhr
- Any <img> tag with attribute:
- longdesc
URLs in stylesheets and srcsets are handled by the ExternalUrlRewrite transformer.
func LinkTag ¶
LinkTag operates on the <link> tag. * It will add a preconnect link tag for Google Font resources. * It will add a preconnect link tag to the publisher's own origin.
func NodeCleanup ¶
NodeCleanup cleans up the DOM tree, including, but not limited to:
- stripping comment nodes.
- stripping noscript elements.
- removing duplicate attributes
- stripping nonce attributes
- Escape JSP/ASP characters in <script> and <style>
- sanitizing URI values
- removing extra <title> elements
func PreloadImage ¶
PreloadImage adds link rel="prefetch" head element to preload the most revalent image in the AMP document.
func ReorderHead ¶
ReorderHead reorders the children of <head>. Specifically, it orders the <head> like so: (0) <meta charset> tag (1) <style amp-runtime> (inserted by ampruntimecss.go) (2) remaining <meta> tags (those other than <meta charset>) (3) AMP runtime .js <script> tag (4) AMP viewer runtime .js <script> tag (5) <script> tags that are render delaying (6) <script> tags for remaining extensions (7) <link> tag for favicons (8) <link> tag for resource hints (9) <link rel=stylesheet> tags before <style amp-custom> (10) <style amp-custom> (11) any other tags allowed in <head> (12) AMP boilerplate (first style amp-boilerplate, then noscript)
func ServerSideRendering ¶
ServerSideRendering implements server-side rendering, described in http://go/amp-ssr. In short, it calculates the AMP layout server-side and expresses it by annotating the document with style attributes etc. And if possible, it removes the boilerplate.
func StripJS ¶
StripJS removes non-AMP javascript from the DOM. - For <script> elements, remove where any of the following is true:
- has a src attribute whose value is not prefixed by https://cdn.ampproject.org/ (case-insensitive match).
- It has no src attribute and no type attribute (case-insensitive match).
- It has a type attribute whose value is neither application/json nor application/ld+json (case-insensitive match on both name and value).
- For all other elements, remove any event attribute that matches "on[A-Za-z].*".
func TransformedIdentifier ¶
TransformedIdentifier identifies that transformations were made for a specific platform and version on this document.
func URLRewrite ¶
URLRewrite rewrites links to point to the AMP Cache and adds DNS preconnects to the <head> Affected links:
- <amp-img/amp-anim src>
- <amp-img/amp-anim srcset>
- <img src> / <img srcset> within <noscript>
- <image href> / <image xlink:href> which are SVG-specific images.
- <link rel=icon href>
- <amp-video poster>
- <use xlink:href>
- a background image given in the <style amp-custom> tag / style attribute
- any fonts given in the <style amp-custom> tag / style attribute
- background attributes.
func UnusedExtensions ¶
UnusedExtensions removes script tags for unused grandfathered extensions.
Types ¶
type Context ¶
type Context struct { // The DOM of the parsed HTML input. DOM *amphtml.DOM // The public URL of the document, i.e. the location that should appear in the browser URL bar. // This is the URL-typed equivalent of Request.DocumentUrl. DocumentURL *url.URL // The base URL of the document, derived from the <base> tag, if any. If the base href is // relative, then it is parsed in the context of DocumentURL. BaseURL *url.URL // The version to use when transforming the DOM. Version int64 // The request parameters. Request *rpb.Request // Preload data. This data gets appended in: // - <link rel="preload"> head tag. // - Link: HTTP Header. Preloads []PreloadData }
Context stores the root DOM Node and contextual data used for the transformers.