# scottjehl/picturefill architecture

> Architecture diagram of the GitHub repository scottjehl/picturefill, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/scottjehl/picturefill
- Repository: https://github.com/scottjehl/picturefill

## Overview

#### Purpose and delivery
Picturefill is an archived JavaScript polyfill for responsive-image markup, primarily `<picture>`, `srcset`, `sizes`, and media/type-based source selection. The distributable browser entry point is `dist/picturefill.js`; source development centers on `src/picturefill.js`. It is intended to run directly in browsers, not as a server application.

#### Core polyfill engine
`src/picturefill.js` owns responsive-image evaluation and candidate selection. It scans applicable images/picture elements, parses responsive source metadata, evaluates viewport/device conditions, and assigns the selected image resource to the browser. The built artifact mirrors this in `dist/picturefill.js`.

#### Markup-to-resource selection flow
The principal flow is:
1. Browser startup or explicit reevaluation invokes Picturefill.
2. Image and `<picture><source>` markup is collected.
3. Media conditions, MIME support, `sizes`, and `srcset` candidates are evaluated against the current environment.
4. The best resource candidate is selected and applied to the target image.

The key boundary is DOM markup and browser capabilities in, selected image URL and image-element state out. Native responsive-image support may handle parts of this flow; the polyfill supplies fallback/compatibility behavior.

#### Environment and compatibility integration
The engine relies on browser DOM APIs, media-query evaluation, viewport metrics, image loading behavior, and type support. A Gecko-specific compatibility layer is maintained in `src/plugins/gecko-picture/pf.gecko-picture.js`; release plugins are separately shipped under `dist/plugins/`.

#### Optional plugin stages
Plugins extend the core at lifecycle boundaries rather than replacing selection logic:
- Mutation-driven rescans for dynamically changed markup: `src/plugins/mutation/pf.mutation.js`
- Print-mode reevaluation: `src/plugins/print/pf.print.js`
- Intrinsic-dimension handling for responsive images: `src/plugins/intrinsic-dimension/pf.intrinsic.js`
- Image MIME/type support handling: `src/plugins/typesupport/pf.type.js`
- Legacy-browser accommodations: `src/plugins/oldie/pf.oldie.js`

Corresponding browser-consumable plugin files live under `dist/plugins/`.

#### Distribution and consumption boundary
Consumers can load the standalone bundle at `dist/picturefill.js` and selectively add compatible plugin scripts from `dist/plugins/`. Source files under `src/` are the implementation boundary; `dist/` is the publication boundary. Package metadata supports JavaScript package ecosystems through `package.json` and `bower.json`, with `composer.json` also present.

#### Validation and examples
Browser-based QUnit test pages exercise core selection and mutation behavior through `tests/index.html`, `tests/index-functional.html`, and `tests/index-mutation.html`. Runnable markup examples in `examples/demo-01.html` through `examples/demo-05.html` demonstrate supported responsive-image patterns and provide manual integration fixtures.

Main entry points: [`src/picturefill.js`](https://github.com/scottjehl/picturefill/blob/master/src/picturefill.js) (Picturefill engine), [`dist/picturefill.js`](https://github.com/scottjehl/picturefill/blob/master/dist/picturefill.js) (Picturefill bundle), [`src/plugins/mutation/pf.mutation.js`](https://github.com/scottjehl/picturefill/blob/master/src/plugins/mutation/pf.mutation.js) (Mutation rescan), [`src/plugins/print/pf.print.js`](https://github.com/scottjehl/picturefill/blob/master/src/plugins/print/pf.print.js) (Print reevaluation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_source["Source implementation"]
  node_engine_source["Picturefill engine<br/>polyfill source<br/>[picturefill.js]"]
  node_gecko["Gecko compatibility<br/>compatibility layer"]
end

subgraph group_plugins["Optional plugins"]
  node_mutation["Mutation rescan<br/>plugin source<br/>[pf.mutation.js]"]
  node_print["Print reevaluation<br/>plugin source<br/>[pf.print.js]"]
  node_intrinsic["Intrinsic dimensions<br/>plugin source<br/>[pf.intrinsic.js]"]
  node_types["Type support<br/>plugin source<br/>[pf.type.js]"]
  node_oldie["Legacy browser support<br/>plugin source<br/>[pf.oldie.js]"]
end

subgraph group_distribution["Published browser assets"]
  node_bundle["Picturefill bundle<br/>browser entry<br/>[picturefill.js]"]
  node_plugin_dist["Plugin bundles<br/>browser assets"]
  node_packages["Package metadata<br/>package manifests<br/>[package.json]"]
end

subgraph group_browser["Browser environment"]
  node_markup["Responsive image markup<br/>DOM input"]
  node_capabilities{{"Browser capabilities<br/>browser APIs"}}
  node_image_state["Selected image resource<br/>DOM output"]
end

node_tests["Browser QUnit tests<br/>validation<br/>[index.html]"]
node_examples["Responsive-image demos<br/>examples<br/>[demo-01.html]"]

node_markup -->|"collected by"| node_engine_source
node_capabilities -->|"evaluates against"| node_engine_source
node_engine_source -->|"selects and applies"| node_image_state
node_gecko -.->|"adds compatibility"| node_engine_source
node_mutation -.->|"triggers rescans"| node_engine_source
node_print -.->|"triggers reevaluation"| node_engine_source
node_intrinsic -.->|"extends lifecycle"| node_engine_source
node_types -.->|"extends type handling"| node_engine_source
node_oldie -.->|"adds legacy support"| node_engine_source
node_engine_source -->|"published as"| node_bundle
node_mutation -->|"published with"| node_plugin_dist
node_print -->|"published with"| node_plugin_dist
node_intrinsic -->|"published with"| node_plugin_dist
node_types -->|"published with"| node_plugin_dist
node_oldie -->|"published with"| node_plugin_dist
node_bundle -->|"runs against"| node_markup
node_bundle -->|"uses"| node_capabilities
node_plugin_dist -.->|"optionally extends"| node_bundle
node_tests -->|"exercises"| node_bundle
node_examples -->|"loads"| node_bundle
node_packages -->|"distributes"| node_bundle

click node_engine_source "https://github.com/scottjehl/picturefill/blob/master/src/picturefill.js"
click node_gecko "https://github.com/scottjehl/picturefill/blob/master/src/plugins/gecko-picture/pf.gecko-picture.js"
click node_mutation "https://github.com/scottjehl/picturefill/blob/master/src/plugins/mutation/pf.mutation.js"
click node_print "https://github.com/scottjehl/picturefill/blob/master/src/plugins/print/pf.print.js"
click node_intrinsic "https://github.com/scottjehl/picturefill/blob/master/src/plugins/intrinsic-dimension/pf.intrinsic.js"
click node_types "https://github.com/scottjehl/picturefill/blob/master/src/plugins/typesupport/pf.type.js"
click node_oldie "https://github.com/scottjehl/picturefill/blob/master/src/plugins/oldie/pf.oldie.js"
click node_bundle "https://github.com/scottjehl/picturefill/blob/master/dist/picturefill.js"
click node_packages "https://github.com/scottjehl/picturefill/blob/master/package.json"
click node_tests "https://github.com/scottjehl/picturefill/blob/master/tests/index.html"
click node_examples "https://github.com/scottjehl/picturefill/blob/master/examples/demo-01.html"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_engine_source,node_gecko toneBlue
class node_mutation,node_print,node_intrinsic,node_types,node_oldie toneAmber
class node_bundle,node_plugin_dist,node_packages toneMint
class node_markup,node_capabilities,node_image_state toneRose
class node_tests,node_examples toneNeutral
```

## Components

### Source implementation

- [Picturefill engine](https://github.com/scottjehl/picturefill/blob/master/src/picturefill.js): polyfill source · Scans markup, evaluates sources, and applies the best candidate. · `src/picturefill.js`
- [Gecko compatibility](https://github.com/scottjehl/picturefill/blob/master/src/plugins/gecko-picture/pf.gecko-picture.js): compatibility layer · `src/plugins/gecko-picture/pf.gecko-picture.js`

### Optional plugins

Lifecycle extensions compiled for selective browser use.

- [Mutation rescan](https://github.com/scottjehl/picturefill/blob/master/src/plugins/mutation/pf.mutation.js): plugin source · Rescans dynamically changed markup. · `src/plugins/mutation/pf.mutation.js`
- [Print reevaluation](https://github.com/scottjehl/picturefill/blob/master/src/plugins/print/pf.print.js): plugin source · `src/plugins/print/pf.print.js`
- [Intrinsic dimensions](https://github.com/scottjehl/picturefill/blob/master/src/plugins/intrinsic-dimension/pf.intrinsic.js): plugin source · `src/plugins/intrinsic-dimension/pf.intrinsic.js`
- [Type support](https://github.com/scottjehl/picturefill/blob/master/src/plugins/typesupport/pf.type.js): plugin source · Handles image MIME/type support. · `src/plugins/typesupport/pf.type.js`
- [Legacy browser support](https://github.com/scottjehl/picturefill/blob/master/src/plugins/oldie/pf.oldie.js): plugin source · `src/plugins/oldie/pf.oldie.js`

### Published browser assets

- [Picturefill bundle](https://github.com/scottjehl/picturefill/blob/master/dist/picturefill.js): browser entry · Standalone browser-consumable core artifact. · `dist/picturefill.js`
- **Plugin bundles**: browser assets · Selectable compatible plugin scripts.
- [Package metadata](https://github.com/scottjehl/picturefill/blob/master/package.json): package manifests · Supports JavaScript package ecosystems. · `package.json`

### Browser environment

- **Responsive image markup**: DOM input · Images, picture sources, srcset, sizes, media, and type attributes.
- **Browser capabilities**: browser APIs · DOM, media queries, viewport metrics, image loading, and MIME support.
- **Selected image resource**: DOM output · Chosen URL and target image-element state.

### Other components

- [Browser QUnit tests](https://github.com/scottjehl/picturefill/blob/master/tests/index.html): validation · Exercises core selection and mutation behavior. · `tests/index.html`
- [Responsive-image demos](https://github.com/scottjehl/picturefill/blob/master/examples/demo-01.html): examples · Manual integration fixtures for supported markup patterns. · `examples/demo-01.html`

## Connections

- Responsive image markup → Picturefill engine: collected by
- Browser capabilities → Picturefill engine: evaluates against
- Picturefill engine → Selected image resource: selects and applies
- Gecko compatibility → Picturefill engine: adds compatibility
- Mutation rescan → Picturefill engine: triggers rescans
- Print reevaluation → Picturefill engine: triggers reevaluation
- Intrinsic dimensions → Picturefill engine: extends lifecycle
- Type support → Picturefill engine: extends type handling
- Legacy browser support → Picturefill engine: adds legacy support
- Picturefill engine → Picturefill bundle: published as
- Mutation rescan → Plugin bundles: published with
- Print reevaluation → Plugin bundles: published with
- Intrinsic dimensions → Plugin bundles: published with
- Type support → Plugin bundles: published with
- Legacy browser support → Plugin bundles: published with
- Picturefill bundle → Responsive image markup: runs against
- Picturefill bundle → Browser capabilities: uses
- Plugin bundles → Picturefill bundle: optionally extends
- Browser QUnit tests → Picturefill bundle: exercises
- Responsive-image demos → Picturefill bundle: loads
- Package metadata → Picturefill bundle: distributes
