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 ParseAndValidateSrcset(in string, has bool) (string, bool)
- func PreloadImage(e *Context) error
- func ReorderHead(e *Context) error
- func ServerSideRendering(e *Context) error
- func StripJS(e *Context) error
- func StripScriptComments(e *Context) error
- func TransformedIdentifier(e *Context) error
- func URLRewrite(e *Context) error
- func UnusedExtensions(e *Context) error
- func ValidateSrc(in string, has bool) (string, bool)
- type Context
- type HeroImage
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.
If there is no style, a new one is added. RTV and CSS are set if available.
If there are one or more styles already (e.g. from Optimizer), then they are all deleted and replaced with one iff both RTV and CSS are available and newer.
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 <amp-story-page-attachment> tag with attribute:
- cta-image
- cta-image-2
- Any <amp-story-page-outlink> tag with attribute:
- cta-image
- Any <form> tag with attribute:
- action
- action-xhr
- Any <img> tag with attribute:
- longdesc
- Any <link> tag with attribute:
- imagesrcset
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. * It will add "display=optional" to Google Fonts without "display" component.
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 ParseAndValidateSrcset ¶
ParseAndValidateSrcset parses each source in the srcset, ensuring it points to a HTTPS URL, and normalizes the srcset.
func PreloadImage ¶
PreloadImage adds link rel="preload" to head element to preload the most revalent image in the AMP document, and inserts an img tag if the image is an amp-img.
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 <script> tag(s) (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:
- It 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 not application/json, application/ld+json, or text/plain (case-insensitive match on both name and value).
- For all other elements, remove any event attribute that matches "on[A-Za-z].*".
func StripScriptComments ¶
Strips html style comments from all the <script> elements that are child nodes of the given node. Example: <script><!-- var a = 1; --></script> transforms to empty script node. <script><!--[if IE]-->var a=1;<![endif]--></script> transforms to empty script tag.
Attributes and any content outside comment are preserved. <script type="text/javascript"><!-- hello -->var a = 0;</script>
transforms to
<script type="text/javascript">var a = 0;</script>
Any comment that looks like a comment but is a string variable is preserved. <script>var comment = "<!-- comment -->";</script> is not modified.
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/img src>
- <amp-img/amp-anim/img srcset>
- <image href> / <image xlink:href> which are SVG-specific images.
- <link rel=icon href>
- <link rel=preload as=image href>
- <link rel=preload imagesrcset>
- <amp-story-page-attachment cta-image>
- <amp-story-page-attachemnt cta-image-2>
- <amp-story-page-outlink cta-image>
- <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 legacy-exempted 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 }
Context stores the root DOM Node and contextual data used for the transformers.