Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'origin/7.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
joshstoik1 committed Feb 18, 2017
2 parents 458816c + 80597ae commit 88cc92d
Show file tree
Hide file tree
Showing 14 changed files with 673 additions and 159 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"data",
"parsing"
],
"version": "7.0.9",
"version": "7.0.10",
"type": "library",
"homepage": "https://github.com/Blobfolio/blob-common",
"license": "WTFPL",
Expand Down
66 changes: 57 additions & 9 deletions docs/IMAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,14 @@ $svg = blobfolio\common\image::clean_svg('path/to/img.svg');

## clean_svg()

Retrieve and tidy up the source of an SVG file for, e.g., inclusion in HTML:
* If multiple `<style>` tags exist, the rules will be joined under a single tag;
* CSS formatting is cleaned up;
* Identical CSS rules are joined by selector;
* An `xmlns:svg` namespace is created, and the `<style>` tag if any is copied to that namespace. This will help work around frontends that strip inline styles (like `Vue.js`);
* Whitespace is collapsed;
This function cleans up SVG code for safer inline insertion into your document. It fixes some common Illustrator bugs (like broken reference links and the generic `id="Layer_1"` definition), strips `DOCTYPE` headers, and reduces whitespace. Many additional options are available (see below).

Note: this requires `DOMDocument` support.

#### Arguments

* (*string*) Path
* (*array*) (*optional*) Arguments. Default: `NULL`
* (*bool*) (*optional*) Randomize ID. If `TRUE`, a random ID will be generated on the fly, replacing e.g. `"Layer_1"`. Default: `FALSE`
* (*bool*) (*optional*) Strip Title. Default: `FALSE`
* (*array*) (*optional*) Arguments. See below for more details. Default: `NULL`
* (*string*) (*optional*) Output, either `"HTML"` or `"DATA_URI"`. Default: `"HTML"`

#### Returns
Expand All @@ -44,6 +39,59 @@ Returns the SVG source code or `FALSE` on error.

#### Example

```php
//possible arguments
$args = array(
//clean up <style> tag(s):
// merge tags, group identical rules, clean up formatting
'clean_styles'=>false,
//build viewBox from width/height or vice versa
//for every tag which supports viewBox
'fix_dimensions'=>true,
//set up an xmlns:svg namespace as a workaround for
//frameworks like Vue.JS which remove stray <style>
//tags
'namespace'=>false,
//randomize any ID attributes to ensure that e.g.
//a million things aren't all named "layer_1"
'random_id'=>false,
//rename and merge all defined classes. for example,
//an SVG sprite might have a hundred identical
//classes; this will generate a new class name for
//each unique rule and remove all others.
'rewrite_styles'=>false,
//cleaning SVGs in PHP can be slow. this option will
//save the cleaned output so on subsequent calls
//the file can be delivered as-is. the original file
//is renamed *.dirty.123123123 in case you need to
//revert.
'save'=>false,
//remove any data-* attributes
'strip_data'=>false,
//remove all ID attributes
'strip_id'=>false,
//remove all <script> tags and on* attributes. note:
//for performance reasons this does not sanitize
//sneaky stuff like embedding script in an unexpected
//attribute like a src or href.
'strip_js'=>true,
//remove all <style> tags and style/class attributes
'strip_style'=>false,
//remove all <title> tags
'strip_title'=>false
)
```
```html
<div class="logo-wrapper">
<?=blobfolio\common\image::clean_svg('/path/to/logo.svg')?>
Expand Down
17 changes: 17 additions & 0 deletions docs/MB.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ blobfolio\common\ref\mb::strtolower($foo);
* [strlen()](#strlen)
* [str_pad()](#str_pad)
* [strpos()](#strpos)
* [strrpos()](#strrpos)
* [strtolower()](#strtolower)
* [strtoupper()](#strtoupper)
* [substr()](#substr)
Expand Down Expand Up @@ -140,6 +141,22 @@ Returns the starting position of the needle of `FALSE` if not found.



## strrpos()

Find the last occurrence of the needle in the haystack.

#### Arguments

* (*string*) Haystack
* (*string*) Needle
* (*int*) (*optional*) Offset

#### Returns

Returns the last starting position of the needle of `FALSE` if not found.



## strtolower()

Lowercase a string. This function additionally catches various unicode characters with upper/lower variants that `mbstring` doesn't bother checking for.
Expand Down
15 changes: 13 additions & 2 deletions lib/blobfolio/common/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,8 @@ class constants {
'fragment'=>''
);

const SVG_HEADER = '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">';

//svg attribute corrections
const SVG_ATTR_CORRECTIONS = array(
'xmlns="&ns_svg;"'=>'xmlns="http://www.w3.org/2000/svg"',
Expand All @@ -997,8 +999,17 @@ class constants {

//clean svg options
const SVG_CLEAN_OPTIONS = array(
'random_id'=>false,
'strip_title'=>false
'clean_styles'=>false, //consistent formatting, group like rules
'fix_dimensions'=>true, //supply missing width, height, viewBox
'namespace'=>false, //add an svg: namespace
'random_id'=>false, //randomize IDs
'rewrite_styles'=>false, //redo classes for overlaps
'save'=>false, //overwrite the original file with a cleaned version
'strip_data'=>false, //remove data-X attributes
'strip_id'=>false, //remove all IDs
'strip_js'=>true, //remove all Javascript
'strip_style'=>false, //remove all styles
'strip_title'=>false //remove all titles
);

//blank image
Expand Down
Loading

0 comments on commit 88cc92d

Please sign in to comment.