Menu Developer Moovweb University

fns Namespace

Use the `fns` namespace for standard Moovweb functionality.


.absolutize(href)
functions
Source (L379)

Converts a given origin URL to a fully-absolutized URL. Returns the input URL if it is already absolutized, or if the address does not reference the origin.

Parameters

Name Type Description
href String A string for the URL to be absolutized.

Return Value

A string for the absolutized version of the input URL.

Examples

fns.absolutize("/test1/test2");
// => Returns: "//www.mysite.com/test1/test2"
fns.absolutize("test1");
// => Returns (when on http://m.mysite.com):
//    "//www.mysite.com/www.mysite.com/test1"
// => Returns (when on http://m.mysite.com/test2):
//    "//www.mysite.com/www.mysite.com/test1"
// => Returns (when on http://m.mysite.com/test2/):
//    "//www.mysite.com/www.mysite.com/test2/test1"
fns.absolutize("www.mysite.com/test");
// => Returns: "//www.mysite.com/www.mysite.com/test"
fns.absolutize("http://www.mysite.com/test");
// => Returns (stays the same): "http://www.mysite.com/test"
fns.absolutize("http://mysite.com/test");
// => Returns (stays the same, provided that the naked domain is in your
//    moov_config.json): "http://mysite.com/test"
fns.absolutize("http://mlocal.mysite.com/test");
// => Returns (stays the same): "http://mlocal.mysite.com/test"
fns.absolutize("http://www.google.com/");
// => Returns (stays the same, provided that Google is _not_ in your
//    moov_config.json): "http://www.google.com/"

Back to top


.absolutizeSrcs()
functions
Source (L425)

Absolutizes all image and script `src` attribute values.

Return Value

`undefined`

Example

fns.absolutizeSrcs();
// => HTML input:
//    <head>
//      <script src="/script1.js"></script>
//    </head>
//    <body>
//      <img src="http://mysite.com/img1.jpeg" />
//      <img src="img2.gif" />
//      <script src="//www.mysite.com/script2.js"></script>
//    </body>
// => HTML output (when on http://m.mysite.com/test):
//    <head>
//      <script src="//www.mysite.com/script1.js"></script>
//    </head>
//    <body>
//      <img src="http://mysite.com/img1.jpeg" />
//      <img src="//www.mysite.com/test/img2.gif" />
//      <script src="//www.mysite.com/script2.js"></script>
//    </body>

Back to top


.activeLayers()
functions
Source (L881)

Retrieves a list of active layers used to transform the project.

Return Value

An array of strings representing each active layer.

Example

fns.activeLayers();
// => Context: Project has multiple layers, presumably for tests, called
//    "testA", "testB", "testC", and "testD." User starts a server running
//    only the "testA" and "testC" layers.
// => Returns: ["testA", "testC"]

Back to top


.addAssets()
functions
Source (L701)

Adds JavaScript and Sass assets. Sass assets get a `data-mw-keep="true"` attribute-value pair added, and script assets get a `data-keep="true"` pair added.

Return Value

`undefined`

Example

fns.addAssets();
// => HTML input:
//    <head></head>
//    <body></body>
// => HTML output (local):
//    <head>
//      <link rel="stylesheet" type="text/css" href="//mlocal.mysite.com/_moovweb_local_assets_/stylesheets/.css/main.css" data-mw-keep="true">
//    </head>
//    <body>
//      <script data-keep="true" type="text/javascript" src="//mlocal.mysite.com/_moovweb_local_assets_/javascript/main.js"></script>
//    </body>
// => HTML output (production):
//    <head>
//      <link rel="stylesheet" type="text/css" href="//assets.moovweb.net/project-uid/mode-uid/build-version/stylesheets/.css/main.css">
//    </head>
//    <body>
//      <script data-keep="true" type="text/javascript" src="//assets.moovweb.net/project-uid/mode-uid/build-version/javascript/main.js"></script>
//    </body>

Back to top


.addCanonicalTag()
functions
Source (L148)

Injects a canonical tag into the head, as long as one doesn't already exist. Additionally, remove any alternate tags.

Return Value

`undefined`

Example

fns.addCanonicalTag();
// => HTML input:
//    <head></head>
// => HTML output:
//    <head>
//      <link rel="canonical" href="http://www.mysite.com/">
//    </head>

Back to top


.addCookie(name, value, [domain], [path], [expires], [secure], [httpOnly])
stdlib
Source (L53)

Adds a cookie while processing the current request.

Parameters

Name Type Description
name String A string for the name of the cookie.
value String A string for the value of the cookie.
[ domain ] String Optional. A string for the domain of the cookie.
[ path ] String Optional. A string for the path of the cookie.
[ expires ] String Optional. A string for the expiratoin date of the cookie.
[ secure ] Boolean Optional. A boolean for if the cookie should only be transmitted via HTTPS.
[ httpOnly ] Boolean Optional. A boolean for if the cookie requires protection against cross-site attacks.

Return Value

`undefined`

Examples

fns.addCookie("favoriteDog", "joe", ".mysite.com");
// => Result: "favoriteDog" cookie added to the upstream domain
//    ".mysite.com", with a value of "joe"
fns.addCookie("favoriteDog", "joe", ".mysite.com", "/mydir", "Thu, 01 Jan 2021 00:00:00 GMT", true, true);
// => Result: "favoriteDog" cookie added to the upstream domain
//    ".mysite.com", with a value of "joe", over the "/mydir" path, set
//    to expire at midnight GMT on January 1, 2021, only transmitted over
//    https, with protection against cross-site attacks.

Back to top


.assert(conditional, callback)
functions
Source (L968)

Make an assertion for conditional statement and run the callback if the statement is correct. Generallyu used to evaluate expressions and throw exceptions when developing locally.

Parameters

Name Type Description
conditional Boolean A boolean for whether the callback should run. Usually should take the form of a full conditional statement.
callback function A callback function to run if the conditional is true.

Return Value

A boolean for the original condition.

Examples

fns.assert($body.children().length > 0, function() {
  console.log("The body is nonempty.");
});
// => HTML input:
//    <body></body>
// => Throws (local): generic JavaScript Error, with broken page functionality.
// => Returns (production): false
fns.assert($body.children().length === 0, function() {
  console.log("The body is empty.");
});
// => HTML input:
//    <body></body>
// => Logs: "The body is empty."
// => Returns: true

Back to top


.asset(pathToFile)
functions
Source (L121)

Retrieves the URL for the specified JavaScript or image asset path, which changes depending upon the project environment.

Parameters

Name Type Description
pathToFile String A string for the relative path to the asset file. Must be relative to the assets directory in your project structure.

Return Value

A string containing the URL for the JavaScript or image asset.

Example

$body.append(tag("script", {type: "text/javascript", src: fns.asset("javascript/custom_script.js")}));
// => HTML input:
//    <body></body>
// => HTML output (local):
//    <body>
//      <script type="text/javascript" src="//mlocal.mysite.com/_moovweb_local_assets_/javascript/custom_script.js"></script>
//    </body>
// => HTML output (production):
//    <body>
//      <script type="text/javascript" src="//assets.moovweb.net/project-uid/mode-uid/build-version/javascript/custom_script.js"></script>
//    </body>

Back to top


.cleanMobileMetaTags()
functions
Source (L585)

Sanitizes any `meta` tags related to mobile development. This entails resetting any existing `viewport` or `format-detection` meta tags to be Moovweb-specific, in addition to an `http-equiv` tag.

Return Value

`undefined`

Example

fns.cleanMobileMetaTags();
// => HTML input:
//    <head>
//      <meta name="viewport" id="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0">
//      <meta name="format-detection" content="telephone=yes">
//    </head>
// => HTML output:
//    <head>
//      <meta http-equiv="Content-Type" content="text/html">
//      <meta name="viewport" id="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
//      <meta name="format-detection" content="telephone=no">
//    </head>

Back to top


.export(key, val)
stdlib
Source (L21)

Sets custom environment variables. Certain environment variables will correspond to actual response headers, including the Location header (`Location`), the "Content-Type" header (`Content-Type`), the "Content-Encoding" header (`compression`), and the cache time field for the Cache-Control header (`Cache-Time`).

Parameters

Name Type Description
key String A string for the environment variable key.
val String A string for the environment variable value.

Return Value

`undefined`

Examples

fns.export("Location", "http://www.google.com");
// => Result: Adds "Location": "http://www.google.com" to the `env` object
//    (and thus sends a redirect to Google).
fns.export("Content-Type", "application/json");
// => Result: Adds "Content-Type": "application/json" to the `env` object.
fns.export("compression", "gzip");
// => Result: Adds "Content-Encoding": "gzip" to the `env` object.
fns.export("Cache-Time", "10");
// => Result: Adds "Cache-Time": "public, max-age=fake-cache-time" to the
//    `env` object.

Back to top


.getPageType()
functions
Source (L1221)

Access the internal `pageType` property of the environment `env`, for the given address.

Return Value

A string for the name of the given address's `pageType`.

Example

fns.getPageType();
// => Returns: name for the pageType, e.g. "home"

Back to top


.handleRobots(body)
functions
Source (L793)

Disallows robot crawling for non-production environments. Retains origin production robot crawling settings for transformed production environments.

Parameters

Name Type Description
body String A string containing the original robots.txt response.

Return Value

A string for the possibly-modified outgoing robots.txt response.

Example

fns.handleRobots("User-agent: *\nDisallow: /account.php\nDisallow: /cart.php");
// => Returns (local, stage): "
//      User-agent: *
//      Disallow: /
//    "
// => Returns (production; stays the same): "
//      User-agent: *
//      Disallow: /account.php
//      Disallow: /cart.php
//    "

Back to top


.highlightDevelopment([position])
functions
Source (L1236)

Inserts a banner on the page when developing locally. This allows for a clear differentiation between the proxied single domain site and the original site. Alias for the legacy fns.highlight_development() method.

Parameters

Name Type Description
[ position ] string Optional. A string for the position to place the banner ("top" or "bottom"). Defaults to bottom positioning if "top" is not specified.

Return Value

`undefined`

Example

fns.highlightDevelopment("top");
// => HTML output: (omitted).

Back to top


.highlightPreview([position])
functions
Source (L1261)

Inserts a banner on the page if viewing a preview mode. This allows for a clear differentiation between preview modes and live modes. Alias for the legacy fns.highlight_preview() method.

Parameters

Name Type Description
[ position ] string Optional. A string for the position to place the banner ("top" or "bottom"). Defaults to bottom positioning if "top" is not specified.

Example

fns.highlightPreview("top");
// => HTML output: (omitted).

Back to top


.init$(body, [opts])
stdlib
Source (L191)

Initializes $ by loading a UTF-8 encoded string of an HTML request into the Cheerio parsing library (generally, this would be the global `body` variable - no `$` prefix). This also provides global variables to efficiently query the DOM thereafter. These global variables include the `$`, `$root`, `$html`, `$head`, and `$body` objects, which allow you to create new Cheerio objects associated with specific DOM traversals. This also provides access to the `scoped$` global function.

Parameters

Name Type Description
body String A string for the content document.
[ opts ] Object Optional. An object for parsing settings, taken from htmlparser2 and DomHandler.
Properties
Name Type Description
[ xmlMode ] Boolean Optional. A boolean for whether special elements (`script` and `style`) should get special treatment and if "empty" tags (eg. `br`) can have children. If false, the content of special tags will be text only. For feeds and other XML content (documents that don't consist of HTML), set this to true. Default: false.
[ decodeEntities ] Boolean Optional. A boolean for whether entities within the document will be _encoded_ (i.e., whether decoded entities should _not_ be retained). Defaults to true.
[ lowerCaseTags ] Boolean Optional. A boolean for whether all tags should be lowercased. If `xmlMode` is disabled, this defaults to true.
[ lowerCaseAttributeNames ] Boolean Optional. A boolean for whether attribute names should be lowercased. This has a noticeable impact on speed, so it defaults to false.
[ recognizeCDATA ] Boolean Optional. A boolean for whether CDATA sections will be recognized as text even if the `xmlMode` option is not enabled. Note: If `xmlMode` is set to true, then CDATA sections will always be recognized as text.
[ recognizeSelfClosing ] Boolean Optional. A boolean for whether self-closing tags will trigger the `onclosetag` event even if xmlMode is not set to true. Note: If `xmlMode` is set to true, then self-closing tags will always be recognized.
[ normalizeWhitespace ] Boolean Optional. A boolean for whether whitespace in text nodes should be normalized (replaced by single spaces). Default: false.
[ withDomLvl1 ] Boolean Optional. A boolean for whether DOM level 1 properties should be added to all elements.
[ withStartIndices ] Boolean Optional. A boolean for whether a `startIndex` property will be added to nodes. When the parser is used in a non-streaming fashion, `startIndex` is an integer indicating the position of the start of the node in the document. Default: false.

Return Value

An object containing property names that correspond to the global variables set.

Examples

fns.init$(body);
// => HTML input:
//    <html>
//      <body>&</body>
//    </html>
// => Returns: {
//      $: $,
//      scoped$: scoped$,
//      root: $root,
//      html: $html,
//      head: $head,
//      body: $body
//    }
// => HTML input:
//    <html>
//      <body>&amp;</body>
//    </html>
// => Result: The document is parsed and usable by Cheerio.
fns.init$(body, {decodeEntities: false});
// => HTML input:
//    <html>
//      <body>&</body>
//    </html>
// => Returns: {
//      $: $,
//      scoped$: scoped$,
//      root: $root,
//      html: $html,
//      head: $head,
//      body: $body
//    }
// => HTML input (stays the same):
//    <html>
//      <body>&</body>
//    </html>
// => Result: The document is parsed and usable by Cheerio.

Back to top


.insertDnsPrefetch()
functions
Source (L1153)

Insert DNS prefetch elements to the `head`, allowing to resolve DNSs before the referenced items are needed. Note that the order of the generated prefetch tags may not be in the same order as the asset order.

Return Value

`undefined`

Example

fns.insertDnsPrefetch();
// => HTML input:
//    <head>
//      <link rel="stylesheet" href="http://www.twitter.com/stylesheet1.css">
//    </head>
//    <body>
//      <a href="http://www.bing.com/"></a>
//      <img src="http://www.yahoo.com/"></a>
//      <script type="text/javascript" src="http://www.google.com/script1.js"></a>
//    </body>
// => HTML output:
//    <head>
//      <link rel="stylesheet" href="http://www.twitter.com/stylesheet1.css">
//      <link rel="dns-prefetch" href="http://www.yahoo.com/">
//      <link rel="dns-prefetch" href="http://www.google.com/">
//      <link rel="dns-prefetch" href="http://www.twitter.com/">
//      <link rel="dns-prefetch" href="http://www.bing.com/">
//    </head>
//    <body>
//      <a href="http://www.facebook.com/"></a>
//      <img src="http://www.microsoft.com/img1.js" />
//      <script type="text/javascript" src="http://www.google.com/script1.js"></a>
//    </body>

Back to top


.insertSubresource(resource)
functions
Source (L1098)

Inserts an asset as a subresource to enable early loading of resources within the current page. For more on subresources, see the Chromium documentation.

Parameters

Name Type Description
resource String A string for the href value to be used as a subresource.

Return Value

A Cheerio object associated with the newly-inserted subresource element.

Example

fns.insertSubresource("http://www.google.com/script1.js");
// => HTML input:
//    <head></head>
// => Returns: DOM object associated with the newly-created subresource
//    element.
// => HTML output:
//    <head>
//      <link rel="subresource" href="http://www.google.com/script1.js">
//    </head>

Back to top


.isRobots()
functions
Source (L779)

Checks if the current path is "/robots.txt."

Return Value

A boolean for whether the current path is /robots.txt.

Example

fns.isRobots();
// => Returns (if on /robots.txt): true

Back to top


.layer(layer)
functions
Source (L898)

Indicates whether or not the specified layer is active, with the indicator as a boolean.

Parameters

Name Type Description
layer String A string for the name of the layer to query.

Return Value

A boolean for whether the layer is active.

Example

fns.layer("tablet");
// => Returns (if tablet layer is active): true
// => Returns (if tablet layer is not active): false

Back to top


.layerNot(layer)
functions
Source (L915)

Indicates whether or not the specified layer is inactive, with the indicator as a boolean.

Parameters

Name Type Description
layer string A string for the name of the layer to query.

Return Value

A boolean for whether the layer is inactive.

Example

fns.layerNot("tablet");
// => Returns (if tablet layer is active): false
// => Returns (if tablet layer is not active): true

Back to top


.moveStylesAboveScripts()
functions
Source (L1013)

Move `style` and `link` elements above the `script` elements.

Return Value

A Cheerio object for the first `script` element found in the HTML.

Example

fns.moveStylesAboveScripts();
// => HTML input:
//    <head>
//      <link rel="stylesheet" href="/styles.scss">
//    </head>
//    <body>
//      <script type="text/javascript" src="/script1.js"></script>
//      <style>
//        body {
//          color: red;
//        }
//      </style>
//      <script type="text/javascript" src="/script2.js"></script>
//    </body>
// => Returns: Cheerio object associated with the "script1.js" `script`
//    element.
// => HTML output:
//    <head></head>
//    <body>
//      <link rel="stylesheet" href="/styles.scss">
//      <style>
//        body {
//          color: red;
//        }
//      </style>
//      <script type="text/javascript" src="/script1.js"></script>
//      <script type="text/javascript" src="/script2.js"></script>
//    </body>

Back to top


.moveStylesToHead()
functions
Source (L1055)

Prepend `style` and `link` elements to the `head`. To be used in conjuction (after) the moveStylesAboveScripts() method, which takes effect if there is no `script` elements in the `head` (this is otherwise redundant if the first `script` is already there).

Return Value

A Cheerio object associated with the `head` element.

Example

// => HTML input:
//    <head>
//      <link rel="stylesheet" href="/styles.scss">
//    </head>
//    <body>
//      <script type="text/javascript" src="/script1.js"></script>
//      <style>
//        body {
//          color: red;
//        }
//      </style>
//      <script type="text/javascript" src="/script2.js"></script>
//    </body>
// => Returns: Cheerio object associated with the `head` element
// => HTML output:
//    <head>
//      <link rel="stylesheet" href="/styles.scss">
//      <style>
//        body {
//          color: red;
//        }
//      </style>
//    </head>
//    <body>
//      <script type="text/javascript" src="/script3.js"></script>
//      <script type="text/javascript" src="/script4.js"></script>
//    </body>

Back to top


.onDevelopment(callback)
functions
Source (L932)

Runs a callback function if the project is being run on a development environment.

Parameters

Name Type Description
callback function A callback function to run if the project is being run on a development environment.

Return Value

A boolean indicating whether the project is being run on a development environment (and by extension, whether the callback functoin was run).

Example

fns.onDevelopment(function() {
  $body.append(tag("div"));
});
// => HTML input:
//    <body></body>
// => HTML input (local):
//    <body>
//      <div></div>
//    </body>
// => HTML output (production; stays the same):
//    <body></body>

Back to top


.perfectProxy()
functions
Source (L492)

Rewrites the `href` links in any `a` elements to point to the Moovweb proxy if it does not already, as well as the `src` attribute for any `script` elements. Relative paths remain, since they are already passing through the Moovweb engine. This uses rewriteLinks() and rewriteJsSrcs().

Return Value

`undefined`

Example

fns.perfectProxy();
// => HTML input:
//    <head>
//      <script src="/script1.js"></script>
//    </head>
//    <body>
//      <a href="test">Test</a>
//      <a href="http://www.mysite.com/test">Test</a>
//      <a href="http://www.google.com/">Test</a>
//      <script src="//www.mysite.com/script2.js"></script>
//      <script src="//www.google.com/script3.js"></script>
//    </body>
// => HTML output (production):
//    <head>
//      <script src="/script1.js"></script>
//    </head>
//    <body>
//      <a href="test">Test</a>
//      <a href="http://m.mysite.com/test">Test</a>
//      <a href="http://www.google.com/">Test</a>
//      <script src="//m.mysite.com/script2.js"></script>
//      <script src="//www.google.com/script3.js"></script>
//    </body>

Back to top


.queryLayerText(layer)
functions
Source (L862)

Indicates whether or not the specified layer is active, with the indicator as a string.

Parameters

Name Type Description
layer String A string for the name of the layer to query.

Return Value

A string for a boolean number, i.e. "1" if layer is active, "0" otherwise.

Example

fns.queryLayerText("tablet");
// => Returns (if tablet layer is active): "1"
// => Returns (if tablet layer is not active): "0"

Back to top


.relocateScripts()
functions
Source (L824)

Moves `script` elements to the bottom of the `body`, retaining the original sequential order.

Return Value

`undefined`

Example

fns.relocateScripts();
// => HTML input:
//    <head>
//      <script type="text/javascript" src="/script1.js"></script>
//      <meta name="viewport" id="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0">
//      <script type="text/javascript" src="/script2.js"></script>
//    </head>
//    <body>
//      <script type="text/javascript" src="/script3.js"></script>
//      <div></div>
//      <script type="text/javascript" src="/script4.js"></script>
//    </body>
// => HTML output:
//    <head>
//      <meta name="viewport" id="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0">
//    </head>
//    <body>
//      <div></div>
//      <script type="text/javascript" src="/script1.js"></script>
//      <script type="text/javascript" src="/script2.js"></script>
//      <script type="text/javascript" src="/script3.js"></script>
//      <script type="text/javascript" src="/script4.js"></script>
//    </body>

Back to top


.removeAllStyles()
functions
Source (L558)

Removes all `link` and `style` elements.

Return Value

`undefined`

Example

fns.removeAllStyles();
// => HTML input:
//    <head>
//     <link rel="stylesheet" href="http://www.mysite.com/my.css" />
//    </head>
//    <body>
//      <style>
//        body {
//          color: red;
//        }
//      </style>
//    </body>
// => HTML output:
//    <head></head>
//    <body></body>

Back to top


.removeDesktopJs()
functions
Source (L734)

Removes non-ASP.NET desktop scripts that aren't explicitly specified to be removed (using the `data-keep` attribute).

Return Value

`undefined`

Examples

fns.removeDesktopJs();
// => HTML input:
//    <head>
//      <script type="text/javascript" src="/script1.js"></script>
//      <script data-keep="true" type="text/javascript" src="/script2.js"></script>
//    </head>
//    <body>
//      <script data-keep="false" type="text/javascript" src="/script3.js"></script>
//      <script data-keep="true" type="text/javascript" src="/script4.js"></script>
//    </body>
// => HTML output:
//    <head>
//      <script data-keep="true" type="text/javascript" src="/script2.js"></script>
//    </head>
//    <body>
//      <script data-keep="true" type="text/javascript" src="/script4.js"></script>
//    </body>
fns.removeDesktopJs();
// => HTML input:
//    <head>
//      <script src="//m.mysite.com/ScriptResource.axd?d=long_hash&t=long_hash"></script>
//    </head>
// => HTML output (stays the same):
//    <head>
//      <script src="//m.mysite.com/ScriptResource.axd?d=long_hash&t=long_hash"></script>
//    </head>

Back to top


.removeDesktopMobileIcon()
functions
Source (L616)

Removes any mobile icons on the origin site.

Return Value

`undefined`

Example

fns.removeDesktopMobileIcon();
// => HTML input:
//    <head>
//      <link rel="shortcut icon" href="http://www.mysite.com/64.png" sizes="64x64">
//      <link rel="shortcut icon" href="http://www.mysite.com/128.png" sizes="128x128">
//      <link rel="shortcut icon" href="http://www.mysite.com/192.png" sizes="192x192">
//      <link rel="apple-touch-icon" href="http://www.mysite.com/76.png" sizes="76x76">
//      <link rel="apple-touch-icon" href="http://www.mysite.com/120.png" sizes="120x120">
//      <link rel="apple-touch-icon" href="http://www.mysite.com/152.png" sizes="152x152">
//      <link rel="apple-touch-icon" href="http://www.mysite.com/180.png" sizes="180x180">
//    </head>
// => HTML input:
//    <head></head>

Back to top


.removeHtmlComments()
functions
Source (L641)

Removes HTML comments.

Return Value

`undefined`

Example

fns.removeHtmlComments();
// => HTML input:
//    <body>
//    <!-- Empty body element -->
//    </body>
// => HTML output:
//    <body></body>

Back to top


.rewriteAspNetScripts()
functions
Source (L532)

Rewrites only ASP.NET-related scripts, usually for the purpose of access or content rewriting.

Return Value

`undefined`

Example

fns.rewriteAspNetScripts();
// => HTML input:
//    <head>
//      <script src="//www.mysite.com/script1.js"></script>
//      <script src="//www.mysite.com/ScriptResource.axd?d=long_hash&t=long_hash"></script>
//    </head>
// => HTML output (production):
//    <head>
//      <script src="//www.mysite.com/script1.js"></script>
//      <script src="//m.mysite.com/ScriptResource.axd?d=long_hash&t=long_hash"></script>
//    </head>

Back to top


.rewriteJsSrcs()
functions
Source (L458)

Rewrites all `src` attributes for `script` elements. Works on HTML fragments, since this navigates from the root element `$root`.

Return Value

`undefined`

Example

fns.rewriteJsSrcs();
// => HTML input:
//    <head>
//      <script src="/script1.js"></script>
//    </head>
//    <body>
//      <img src="http://mysite.com/img1.jpeg" />
//      <img src="img2.gif" />
//      <script src="//www.mysite.com/script2.js"></script>
//    </body>
// => HTML output (production):
//    <head>
//      <script src="//m.mysite.com/script1.js"></script>
//    </head>
//    <body>
//      <img src="http://mysite.com/img1.jpeg" />
//      <img src="img2.gif" />
//      <script src="//m.mysite.com/script2.js"></script>
//    </body>

Back to top


Rewrites a given absolute link to the Moovweb proxy (using rewriteToProxy(), with default settings applied).

Parameters

Name Type Description
hostHH String A string for the host link to be rewritten.

Return Value

A string containing the link rewritten to the Moovweb proxy.

Examples

fns.rewriteLink("www.mysite.com");
// => Returns (local): "mlocal.mysite.com"
// => Returns (production with CNAME): "m.mysite.com"
// => Returns (moovapp staging): "m.mysite.com.moovapp.com"
fns.rewriteLink("//www.mysite.com");
// => Returns (local): "//mlocal.mysite.com"
// => Returns (production with CNAME): "//m.mysite.com"
// => Returns (moovapp staging): "//m.mysite.com.moovapp.com"
fns.rewriteLink("https://mysite.com");
// => Returns (local, if naked subdomain in moov_config.json):
//    "https://mlocal.mysite.com"
// => Returns (production CNAME, if naked subdomain in moov_config.json):
//    "https://m.mysite.com"
// => Returns (moovapp staging, if naked subdomain in moov_config.json):
//    "https://m.mysite.com.moovapp.com"
// => Returns (if naked subdomain _not_ in moov_config.json):
//    "https://mysite.com"
fns.rewriteLink("http://www.google.com/");
//=> Returns (if origin is not Google): "http://www.google.com/"
fns.rewriteLink("/test.js");
// => Returns (stays the same): "/test.js"

Back to top


Rewrites all links (`a` and `base` elements) to the correct domain.

Return Value

`undefined`

Example

fns.rewriteLinks();
// => HTML input:
//    <head>
//      <base href="http://www.mysite.com/">
//    </head>
//    <body>
//      <a href="www.mysite.com/">Test</a>
//      <a href="//www.mysite.com/">Test</a>
//      <a href="//www.google.com/">Test</a>
//    </body>
// => HTML output:
//    <head>
//      <base href="http://m.mysite.com/" />
//    </head>
//    <body>
//      <a href="m.mysite.com/">Test</a>
//      <a href="//m.mysite.com/">Test</a>
//      <a href="//www.google.com/">Test</a>
//    </body>

Back to top


.rewriteToProxy(hostHH, secure, catchAll)
functions
Source (L175)

Rewrites a given link to the Moovweb proxy, given additional security and domain settings.

Parameters

Name Type Description
hostHH String A string for the host link to be rewritten.
secure Boolean A boolean for whether you're on the secure protocol, allowing for the rewriting process to speed up if in the proper environment
catchAll String A string for a catch-all domain, such as .moovapp.com (for when a CNAME isn't set up and your project is pushed to the Moovweb staging cluster).

Return Value

A string containing the link rewritten to the Moovweb proxy.

Examples

fns.rewriteToProxy("www.mysite.com", false, env.__catch_all__);
// => Returns (local): "mlocal.mysite.com"
// => Returns (production with CNAME): "m.mysite.com"
// => Returns (moovapp staging): "m.mysite.com.moovapp.com"
fns.rewriteToProxy("//www.mysite.com", false, env.__catch_all__);
// => Returns (local): "//mlocal.mysite.com"
// => Returns (production with CNAME): "//m.mysite.com"
// => Returns (moovapp staging): "//m.mysite.com.moovapp.com"

Back to top


.sass(baseName)
functions
Source (L674)

Retrieves the URL for the specified Sass asset path, which changes depending upon the project environment.

Parameters

Name Type Description
baseName String A string for the relative path to the asset file. Must be relative to the assets/stylesheets directory in your project structure.

Return Value

A string containing the URL for the compiled CSS asset.

Example

$head.append(tag("link", {rel: "stylesheet", type: "text/css", href: fns.sass("main")}));
// => HTML input:
//    <head></head>
// => HTML output (local):
//    <head>
//      <link rel="stylesheet" type="text/css" href="//mlocal.mysite.com/_moovweb_local_assets_/stylesheets/.css/main.css">
//    </head>
// => HTML output (production):
//    <head>
//      <link rel="stylesheet" type="text/css" href="//assets.moovweb.net/project-uid/mode-uid/build-version/stylesheets/.css/main.css">
//    </head>

Back to top


.scoped$($)
stdlib
Source (L99)

Narrows down parts of the DOM you wish to use. `$this` is implicitly redefined to the Cheerio object for `this`. Nesting `scoped$` will maintain the current scope. When you reference a Cheerio object outside the scope from within it supercedes the scope and can be used to access DOM elements outside the current scope. For example, you can access and modify specific elements outside the scope using `$body.find()`.

Parameters

Name Type Description
$ Object A Cheerio object to traverse down.

Return Value

A modified Cheerio reference given the original input selector as the new "root."

Examples

scoped$($body, function(){
    scoped$("script", function(){
        $this.attr("data-mw-keep", true);
    });
});
// => HTML input:
//    <body>
//      <script type="text/javascript" src="/script1.js"></script>
//    </body>
// => Returns: Cheerio object assocaited with the `body` element
// => HTML input:
//    <body>
//      <script type="text/javascript" src="/script1.js" data-mw-keep="true"></script>
//    </body>
scoped$($body, function() {
  scoped$("script", function() {
    $this.attr("data-body", true);
  });
  $body.find("script").attr("data-global", true);
});
// => HTML input:
//    <head>
//      <script type="text/javascript" src="/script1.js"></script>
//    </head>
//    <body>
//      <script type="text/javascript" src="/script2.js"></script>
//    </body>
// => Returns: Cheerio object assocaited with the `body` element
// => HTML input:
//    <head>
//      <script type="text/javascript" src="/script1.js" data-global="true"></script>
//    </head>
//    <body>
//      <script type="text/javascript" src="/script2.js" data-body="true" data-global="true"></script>
//    </body>

Back to top


.setPageType(type)
functions
Source (L1198)

Sets the internal `pageType` property for the global `env` environment. In production, this is specifically used for writing in the "pageType" field on the Moovweb Control Center's Stack Trace feature.

Parameters

Name Type Description
type string A string for the name of the `pageType` to set.

Return Value

`undefined`

Example

fns.setPageType(routes.pageType.name);
// => Result: Stack Trace reads the `pageType` as "home".
// => Note: Already included in the generator's fns.constructPageType()
//    method call.

Back to top


.slashPath()
functions
Source (L356)

Gives the string for the given path's parent directory.

Return Value

A string for the slash path.

Example

fns.slashPath();
// => Returns (when on http://m.mysite.com/):
//    "/"
// => Returns (when on http://m.mysite.com/product):
//    "/"
// => Returns (when on http://m.mysite.com/product/):
//    "/product"
// => Returns (when on http://m.mysite.com/product/mittens):
//    "/product"
// => Returns (when on http://m.mysite.com/product/mittens/):
//    "/product/mittens"

Back to top


Last updated Tue Nov 15 2016 22:28:30 GMT+0000 (UTC)