npm package: @madgex/safeguard
Description: A JavaScript library that tries to sandbox 3rd party content on a webpage
Author: James Wragg
Latest version: 2.0.3

# Safeguard

A JavaScript library that tries to sandbox 3rd party _**content**_ on your webpage.

Sometimes you want to block the cascade in CSS, i.e. you want to inline some HTML and associated CSS in your page but don't want the CSS from the host page to affect it.

## Why not iframe it

IFrames were meant for embedding 3rd party content on your page, right? Yes, but there are inherent issues with this:

- Search engines consider the content in iframes to belong to another website
- If you want the content to be seamless to your page, it needs to be stand-alone content, i.e. no header/footer/nav etc.
- IFrames don't resize to their content

## How does Safeguard do it differently

Wrap any content in a `<safe-guard>` custom element and watch as Safeguard yanks the content into either a ShadowDOM or a generated, src-less iframe.

## Demo

<https://safeguard.now.sh/>

## Install

```
npm install @madgex/safeguard
```

## Getting started

Either import the script of load from unpkg and you're set:

**From CDN**: Add the following script to the end of your <head> section.

```(html)
<script src="https://unpkg.com/@madgex/safeguard" defer></script>
```

**From NPM**: Install the package from NPM.

```(js)
// App.js
import '@madgex/safeguard';

```

That's it. It will initialize itself.

## Options

### tag

You may have other blocks of HTML/CSS/JS to take along into the SafeGuard, you can `tag` elements by wrapping them in HTML comments:

```html
<html>
  <head>
    <title>My awesome page</title>

    <!-- [sg:starttag:myWidget] -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" />
    <!-- [sg:endtag:myWidget] -->

    <link rel="stylesheet" href="/css/styles.css" />
  </head>
  <body>
    ...my document

    <safe-guard tag="myWidget">
      <h1>My document</h1>
      <p>Once mounted in SafeGuard, I'll only have bootstrap CSS applied, and nothing from /css/styles.css.</p>
    </safe-guard>
  </body>
</html>
```

Any other elements to take along?

- `tag: null`

Force using an iframe

- `iframe: false`

If iframe, inject the [iframe-resizer](https://github.com/davidjbradshaw/iframe-resizer/) to adapt window height

- `iframeResizer: true`

Option pass-through to [iframe-resizer](https://github.com/davidjbradshaw/iframe-resizer/)

- `iframeResizerOptions: {}`

### Examples

```(html)
  <safe-guard>
    <p>No styling.</p>
  </safe-guard>
```

Ensure the styles go with the content into ShadowDOM

```(html)
  <head>
    <!-- [sg:starttag:myWidget] -->
    <style>
      .customStyles { color: red; }
    </style>
    <!-- [sg:endtag:myWidget] -->
  </head>

  <body>
    …
    <safe-guard tag="myWidget">
      <p class="customStyles">Custom styling</p>
    </safe-guard>
    …
  </body>
```

Force safeguard to use an iframe instead of ShadowDOM

```(html)
  <head>
    <!-- [sg:starttag:myWidget] -->
    <style>
      .customStyles { color: red; }
    </style>
    <!-- [sg:endtag:myWidget] -->
  </head>

  <body>
    …
    <safe-guard iframe>
      <p class="customStyles">Custom styling</p>
    </safe-guard>
    …
  </body>
```