Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Img alt text support #749

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 38 additions & 11 deletions src/formats/bbcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,22 @@
editor,
caller,
selected,
function (url, width, height) {
function (url, width, height, alt) {
var attrs = '';

if (width) {
attrs += ' width=' + width;
attrs += ' width="' +
escapeEntities(width, true) + '"';
}

if (height) {
attrs += ' height=' + height;
attrs += ' height="' +
escapeEntities(height, true) + '"';
}

if (alt) {
attrs += ' alt="' +
escapeEntities(alt, true) + '"';
}

editor.insertText(
Expand Down Expand Up @@ -555,9 +562,9 @@
}
},
allowedChildren: ['#'],
quoteType: QuoteType.never,
quoteType: QuoteType.auto,
format: function (element, content) {
var width, height,
var width, height, alt,
attribs = '',
style = function (name) {
return element.style ? element.style[name] : null;
Expand All @@ -570,24 +577,40 @@

width = attr(element, 'width') || style('width');
height = attr(element, 'height') || style('height');
alt = attr(element, 'alt');

// only add width and height if one is specified
if ((element.complete && (width || height)) ||
(width && height)) {
if (alt) {
if (width) {
attribs += ' width="' +
escapeEntities(width, true) + '"';
}

if (height) {
attribs += ' height="' +
escapeEntities(height, true) + '"';
}

attribs = '=' + dom.width(element) + 'x' +
dom.height(element);
attribs += ' alt="' + escapeEntities(alt, true) + '"';
} else {
// only add width and height if one is specified
if ((element.complete && (width || height)) ||
(width && height)) {

attribs = '=' + dom.width(element) + 'x' +
dom.height(element);
}
}

return '[img' + attribs + ']' + attr(element, 'src') + '[/img]';
},
html: function (token, attrs, content) {
var undef, width, height, match,
var undef, width, height, alt, match,
attribs = '';

// handle [img width=340 height=240]url[/img]
width = attrs.width;
height = attrs.height;
alt = attrs.alt;

// handle [img=340x240]url[/img]
if (attrs.defaultattr) {
Expand All @@ -605,6 +628,10 @@
attribs += ' height="' + escapeEntities(height, true) + '"';
}

if (alt !== undef) {
attribs += ' alt="' + escapeEntities(alt, true) + '"';
}

return '<img' + attribs +
' src="' + escapeUriScheme(content) + '" />';
}
Expand Down
6 changes: 5 additions & 1 deletion src/formats/xhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
editor,
caller,
selected,
function (url, width, height) {
function (url, width, height, alt) {
var attrs = '';

if (width) {
Expand All @@ -139,6 +139,10 @@
attrs += ' height="' + height + '"';
}

if (alt) {
attrs += ' alt="' + alt + '"';
}

editor.insertText(
'<img' + attrs + ' src="' + url + '" />'
);
Expand Down
10 changes: 8 additions & 2 deletions src/lib/defaultCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ var defaultCmds = {
url: editor._('URL:'),
width: editor._('Width (optional):'),
height: editor._('Height (optional):'),
alt: editor._('ALT text (optional):'),
insert: editor._('Insert')
}, true));

Expand All @@ -473,7 +474,8 @@ var defaultCmds = {
cb(
urlInput.value,
dom.find(content, '#width')[0].value,
dom.find(content, '#height')[0].value
dom.find(content, '#height')[0].value,
dom.find(content, '#alt')[0].value
);
}

Expand All @@ -490,7 +492,7 @@ var defaultCmds = {
editor,
caller,
'',
function (url, width, height) {
function (url, width, height, alt) {
var attrs = '';

if (width) {
Expand All @@ -501,6 +503,10 @@ var defaultCmds = {
attrs += ' height="' + height + '"';
}

if (alt) {
attrs += ' alt="' + alt + '"';
}

editor.wysiwygEditorInsertHtml(
'<img' + attrs + ' src="' + url + '" />'
);
Expand Down
2 changes: 2 additions & 0 deletions src/lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ var _templates = {
'<input type="text" id="width" size="2" dir="ltr" /></div>' +
'<div><label for="height">{height}</label> ' +
'<input type="text" id="height" size="2" dir="ltr" /></div>' +
'<div><label for="alt">{alt}</label> ' +
'<input type="text" id="alt" dir="ltr" /></div>' +
'<div><input type="button" class="button" value="{insert}" />' +
'</div>',

Expand Down
20 changes: 18 additions & 2 deletions tests/unit/formats/bbcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,12 +635,28 @@ QUnit.test('Horizontal rule', function (assert) {
QUnit.test('Image', function (assert) {
assert.equal(
this.htmlToBBCode(
'<img width=10 height=10 src="http://example.com/test.png" />'
'<img width=10 height=20 src="http://example.com/test.png" />'
),
'[img=10x10]http://example.com/test.png[/img]',
'[img=10x20]http://example.com/test.png[/img]',
'Image tag with width and height attributes'
);

assert.equal(
this.htmlToBBCode(
'<img width="10" height="20" src="http://example.com/test.png" />'
),
'[img=10x20]http://example.com/test.png[/img]',
'Image tag with quoted width and height attributes'
);

assert.equal(
this.htmlToBBCode(
'<img width="10" height="20" alt="ALT text" src="http://example.com/test.png" />'
),
'[img width=10 height=20 alt="ALT text"]http://example.com/test.png[/img]',
'Image tag with width,height and ALT attributes'
);

assert.equal(
this.htmlToBBCode(
'<img src="http://www.sceditor.com/emoticons/smile.png" />'
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/formats/bbcode.parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,27 @@ QUnit.test('Image', function (assert) {
'Normal'
);

assert.htmlEqual(
this.parser.toHTML('[img width="10" height="20"]http://test.com/test.png[/img]'),
'<div><img width="10" height="20" ' +
'src="http://test.com/test.png" /></div>\n',
'Expanded'
);

assert.htmlEqual(
this.parser.toHTML('[img width="10" height="20" alt="ALT text"]http://test.com/test.png[/img]'),
'<div><img width="10" height="20" alt="ALT text" ' +
'src="http://test.com/test.png" /></div>\n',
'Width, height and ALT attributes'
);

assert.htmlEqual(
this.parser.toHTML('[img alt="ALT text"]http://test.com/test.png[/img]'),
'<div><img alt="ALT text" ' +
'src="http://test.com/test.png" /></div>\n',
'ALT only'
);

assert.htmlEqual(
this.parser.toHTML('[img width=10]http://test.com/test.png[/img]'),
'<div><img width="10" src="http://test.com/test.png" /></div>\n',
Expand Down