npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@webcomponents/html-imports

v1.3.1

Published

HTML Imports polyfill

Downloads

5,798

Readme

HTMLImports

This platform feature, and polyfill, is deprecated, please consider using ES Modules instead.

A polyfill for HTMLImports.

Build Status

The polyfill hosts the imported documents in the import link element. E.g.

<link rel="import" href="my-element.html">

<!-- becomes -->

<link rel="import" href="my-element.html">
  <!-- my-element.html contents -->
</link>

The polyfill fires the HTMLImportsLoaded event when imports are loaded, and exposes the HTMLImports.whenReady method. This api is necessary because unlike the native implementation, script elements do not force imports to resolve. Instead, users should wrap code in either an HTMLImportsLoaded handler or after load time in an HTMLImports.whenReady(callback) call.

The polyfill provides the HTMLImports.importForElement() method which can be used to retrieve the <link rel=import> that imported an element.

Caveats / Limitations

<link>.import is not a Document

The polyfill appends the imported contents to the <link> itself to leverage the native implementation of Custom Elements, which expects scripts upgrading the CustomElementRegistry to be connected to the main document.

As a consequence, .ownerDocument will be the main document, while .parentNode of the imported children will be the <link rel=import> itself. Consider using HTMLImports.importForElement() in these cases. e.g:

const ownerDoc = HTMLImports.importForElement(document.currentScript);
let someElement = ownerDoc.querySelector('some-element');
if (ownerDoc !== HTMLImports.importForElement(someElement)) {
  // This element is contained in another import, skip.
  someElement = null;
}

If you require document isolation, use html-imports#v0.

Dynamic imports

The polyfill supports dynamically added imports by observing mutations in <head> and within other imports; it won't detect imports appended in <body>.

If you require to append imports in <body>, notify the polyfill of these additions using the method HTMLImports.loadImports(document.body).

Imported stylesheets in IE/Edge

In IE/Edge, appending <link rel=stylesheet> in a node that is not <head> breaks the cascading order; the polyfill checks if an import contains a <link rel=stylesheet>, and moves all the imported <link rel=stylesheet> and <style> to <head>. It drops a placeholder element in their original place and assigns a reference to the applied element, placeholder.__appliedElement. e.g.

my-element.html imports a stylesheet and applies a style:

<link rel="stylesheet" href="my-linked-style.css" />
<style>
  .blue {
    color: blue;
  }
</style>

And is imported in index.html:

<head>
  <link rel="import" href="my-element.html" />
</head>

This is how the resolved import will look like:

<head>
  <link rel="stylesheet" href="my-linked-style.css">
  <style> .blue { color: blue }; </style>
  <link rel="import" href="my-element.html">
    <link type="import-placeholder">
    <style type="import-placeholder"></style>
  </link>
</head>

The placeholders contain a reference to the applied element:

var myImport = document.head.querySelector('link[rel=import]').import;
var link = myImport.querySelector('link');
console.log(link.__appliedElement || link);
var style = myImport.querySelector('style');
console.log(style.__appliedElement || style);

Building & Running Tests

Build

$ git clone https://github.com/webcomponents/html-imports.git
$ cd html-imports
$ npm i
$ bower i
$ gulp

Run tests

$ npm i -g web-component-tester
$ wct