Skip to content

Commit 51afd94

Browse files
authored
Merge pull request #246 from wordpress-mobile/issue/245-image-in-paragraph
Issue/245 image in paragraph
2 parents 22ea632 + a1a47c1 commit 51afd94

File tree

5 files changed

+28
-317
lines changed

5 files changed

+28
-317
lines changed

aztec/src/main/java/org/wordpress/aztec/Html.java

Lines changed: 7 additions & 294 deletions
Original file line numberDiff line numberDiff line change
@@ -161,267 +161,6 @@ public static Spanned fromHtml(String source, TagHandler tagHandler, OnMediaTapp
161161
return converter.convert();
162162
}
163163

164-
// region Unused Code
165-
166-
/**
167-
* Returns an HTML representation of the provided Spanned text. A best effort is
168-
* made to add HTML tags corresponding to spans. Also note that HTML metacharacters
169-
* (such as "<" and "&") within the input text are escaped.
170-
*
171-
* @param text input text to convert
172-
* @return string containing input converted to HTML
173-
*/
174-
// public static String toHtml(Spanned text) {
175-
// StringBuilder out = new StringBuilder();
176-
// withinHtml(out, text);
177-
// return out.toString();
178-
// }
179-
180-
/**
181-
* Returns an HTML escaped representation of the given plain text.
182-
*/
183-
// public static String escapeHtml(CharSequence text) {
184-
// StringBuilder out = new StringBuilder();
185-
// withinStyle(out, text, 0, text.length());
186-
// return out.toString();
187-
// }
188-
189-
// private static void withinHtml(StringBuilder out, Spanned text) {
190-
// int len = text.length();
191-
//
192-
// int next;
193-
// for (int i = 0; i < text.length(); i = next) {
194-
// next = text.nextSpanTransition(i, len, ParagraphStyle.class);
195-
// ParagraphStyle[] style = text.getSpans(i, next, ParagraphStyle.class);
196-
// String elements = " ";
197-
// boolean needDiv = false;
198-
//
199-
// for (int j = 0; j < style.length; j++) {
200-
// if (style[j] instanceof AlignmentSpan) {
201-
// Layout.Alignment align =
202-
// ((AlignmentSpan) style[j]).getAlignment();
203-
// needDiv = true;
204-
// if (align == Layout.Alignment.ALIGN_CENTER) {
205-
// elements = "align=\"center\" " + elements;
206-
// } else if (align == Layout.Alignment.ALIGN_OPPOSITE) {
207-
// elements = "align=\"right\" " + elements;
208-
// } else {
209-
// elements = "align=\"left\" " + elements;
210-
// }
211-
// }
212-
// }
213-
// if (needDiv) {
214-
// out.append("<div ").append(elements).append(">");
215-
// }
216-
//
217-
// withinDiv(out, text, i, next);
218-
//
219-
// if (needDiv) {
220-
// out.append("</div>");
221-
// }
222-
// }
223-
// }
224-
225-
// private static void withinDiv(StringBuilder out, Spanned text,
226-
// int start, int end) {
227-
// int next;
228-
// for (int i = start; i < end; i = next) {
229-
// next = text.nextSpanTransition(i, end, QuoteSpan.class);
230-
// QuoteSpan[] quotes = text.getSpans(i, next, QuoteSpan.class);
231-
//
232-
// for (QuoteSpan quote : quotes) {
233-
// out.append("<blockquote>");
234-
// }
235-
//
236-
// withinBlockquote(out, text, i, next);
237-
//
238-
// for (QuoteSpan quote : quotes) {
239-
// out.append("</blockquote>\n");
240-
// }
241-
// }
242-
// }
243-
244-
// private static void withinBlockquote(StringBuilder out, Spanned text,
245-
// int start, int end) {
246-
// out.append(text);
247-
//
248-
// int next;
249-
// for (int i = start; i < end; i = next) {
250-
// next = TextUtils.indexOf(text, '\n', i, end);
251-
// if (next < 0) {
252-
// next = end;
253-
// }
254-
//
255-
// int nl = 0;
256-
//
257-
// while (next < end && text.charAt(next) == '\n') {
258-
// nl++;
259-
// next++;
260-
// }
261-
//
262-
// if (withinParagraph(out, text, i, next - nl, nl, next == end)) {
263-
// /* Paragraph should be closed */
264-
// out.append("</p>\n");
265-
// out.append(text);
266-
// }
267-
// }
268-
//
269-
// out.append("</p>\n");
270-
// }
271-
272-
/* Returns true if the caller should close and reopen the paragraph. */
273-
// private static boolean withinParagraph(StringBuilder out, Spanned text,
274-
// int start, int end, int nl,
275-
// boolean last) {
276-
// int next;
277-
// for (int i = start; i < end; i = next) {
278-
// next = text.nextSpanTransition(i, end, CharacterStyle.class);
279-
// CharacterStyle[] style = text.getSpans(i, next,
280-
// CharacterStyle.class);
281-
//
282-
// for (int j = 0; j < style.length; j++) {
283-
// if (style[j] instanceof AztecStyleSpan) {
284-
// AztecStyleSpan styleSpan = (AztecStyleSpan)style[j];
285-
// out.append("<");
286-
// out.append(styleSpan.getStartTag());
287-
// out.append(">");
288-
// }
289-
// if (style[j] instanceof TypefaceSpan) {
290-
// String s = ((TypefaceSpan) style[j]).getFamily();
291-
//
292-
// if ("monospace".equals(s)) {
293-
// out.append("<tt>");
294-
// }
295-
// }
296-
// if (style[j] instanceof SuperscriptSpan) {
297-
// out.append("<sup>");
298-
// }
299-
// if (style[j] instanceof SubscriptSpan) {
300-
// out.append("<sub>");
301-
// }
302-
// if (style[j] instanceof UnderlineSpan) {
303-
// out.append("<u>");
304-
// }
305-
// if (style[j] instanceof StrikethroughSpan) {
306-
// out.append("<strike>");
307-
// }
308-
// if (style[j] instanceof URLSpan) {
309-
// out.append("<a href=\"");
310-
// out.append(((URLSpan) style[j]).getURL());
311-
// out.append("\">");
312-
// }
313-
// if (style[j] instanceof ImageSpan) {
314-
// out.append("<img src=\"");
315-
// out.append(((ImageSpan) style[j]).getSource());
316-
// out.append("\">");
317-
//
318-
// // Don't output the dummy character underlying the image.
319-
// i = next;
320-
// }
321-
// if (style[j] instanceof AbsoluteSizeSpan) {
322-
// out.append("<font size =\"");
323-
// out.append(((AbsoluteSizeSpan) style[j]).getSize() / 6);
324-
// out.append("\">");
325-
// }
326-
// if (style[j] instanceof ForegroundColorSpan) {
327-
// out.append("<font color =\"#");
328-
// String color = Integer.toHexString(((ForegroundColorSpan)
329-
// style[j]).getForegroundColor() + 0x01000000);
330-
// while (color.length() < 6) {
331-
// color = "0" + color;
332-
// }
333-
// out.append(color);
334-
// out.append("\">");
335-
// }
336-
// }
337-
//
338-
// withinStyle(out, text, i, next);
339-
//
340-
// for (int j = style.length - 1; j >= 0; j--) {
341-
// if (style[j] instanceof ForegroundColorSpan) {
342-
// out.append("</font>");
343-
// }
344-
// if (style[j] instanceof AbsoluteSizeSpan) {
345-
// out.append("</font>");
346-
// }
347-
// if (style[j] instanceof URLSpan) {
348-
// out.append("</a>");
349-
// }
350-
// if (style[j] instanceof StrikethroughSpan) {
351-
// out.append("</strike>");
352-
// }
353-
// if (style[j] instanceof UnderlineSpan) {
354-
// out.append("</u>");
355-
// }
356-
// if (style[j] instanceof SubscriptSpan) {
357-
// out.append("</sub>");
358-
// }
359-
// if (style[j] instanceof SuperscriptSpan) {
360-
// out.append("</sup>");
361-
// }
362-
// if (style[j] instanceof TypefaceSpan) {
363-
// String s = ((TypefaceSpan) style[j]).getFamily();
364-
//
365-
// if (s.equals("monospace")) {
366-
// out.append("</tt>");
367-
// }
368-
// }
369-
// if (style[j] instanceof AztecStyleSpan) {
370-
// AztecStyleSpan styleSpan = (AztecStyleSpan)style[j];
371-
// out.append("<");
372-
// out.append(styleSpan.getEndTag());
373-
// out.append("/>");
374-
// }
375-
// }
376-
// }
377-
//
378-
// if (nl == 1) {
379-
// out.append("<br>\n");
380-
// return false;
381-
// } else {
382-
// for (int i = 2; i < nl; i++) {
383-
// out.append("<br>");
384-
// }
385-
// return !last;
386-
// }
387-
// }
388-
389-
// private static void withinStyle(StringBuilder out, CharSequence text,
390-
// int start, int end) {
391-
// for (int i = start; i < end; i++) {
392-
// char c = text.charAt(i);
393-
//
394-
// if (c == '<') {
395-
// out.append("&lt;");
396-
// } else if (c == '>') {
397-
// out.append("&gt;");
398-
// } else if (c == '&') {
399-
// out.append("&amp;");
400-
// } else if (c >= 0xD800 && c <= 0xDFFF) {
401-
// if (c < 0xDC00 && i + 1 < end) {
402-
// char d = text.charAt(i + 1);
403-
// if (d >= 0xDC00 && d <= 0xDFFF) {
404-
// i++;
405-
// int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
406-
// out.append("&#").append(codepoint).append(";");
407-
// }
408-
// }
409-
// } else if (c > 0x7E || c < ' ') {
410-
// out.append("&#").append((int) c).append(";");
411-
// } else if (c == ' ') {
412-
// while (i + 1 < end && text.charAt(i + 1) == ' ') {
413-
// out.append("&nbsp;");
414-
// i++;
415-
// }
416-
//
417-
// out.append(' ');
418-
// } else {
419-
// out.append(c);
420-
// }
421-
// }
422-
// }
423-
424-
// endregion
425164
public static StringBuilder stringifyAttributes(Attributes attributes) {
426165
StringBuilder sb = new StringBuilder();
427166
if (attributes != null) {
@@ -440,10 +179,6 @@ class HtmlToSpannedConverter implements ContentHandler, LexicalHandler {
440179
public int unknownTagLevel = 0;
441180
public Unknown unknown;
442181

443-
private static final float[] HEADER_SIZES = {
444-
1.5f, 1.4f, 1.3f, 1.2f, 1.1f, 1f,
445-
};
446-
447182
private String mSource;
448183
private XMLReader mReader;
449184
private SpannableStringBuilder spannableStringBuilder;
@@ -476,13 +211,13 @@ public Spanned convert() {
476211
}
477212

478213
// Fix flags and range for paragraph-type markup.
479-
Object[] obj = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), ParagraphStyle.class);
480-
for (int i = 0; i < obj.length; i++) {
481-
if (obj[i] instanceof UnknownHtmlSpan || obj[i] instanceof AztecBlockSpan || obj[i] instanceof AztecMediaSpan || obj[i] instanceof AztecHeadingSpan) {
214+
Object[] paragraphs = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), ParagraphStyle.class);
215+
for (Object paragraph : paragraphs) {
216+
if (paragraph instanceof UnknownHtmlSpan || paragraph instanceof AztecBlockSpan || paragraph instanceof AztecMediaSpan || paragraph instanceof AztecHeadingSpan) {
482217
continue;
483218
}
484-
int start = spannableStringBuilder.getSpanStart(obj[i]);
485-
int end = spannableStringBuilder.getSpanEnd(obj[i]);
219+
int start = spannableStringBuilder.getSpanStart(paragraph);
220+
int end = spannableStringBuilder.getSpanEnd(paragraph);
486221

487222
// If the last line of the range is blank, back off by one.
488223
if (end - 2 >= 0) {
@@ -493,9 +228,9 @@ public Spanned convert() {
493228
}
494229

495230
if (end == start) {
496-
spannableStringBuilder.removeSpan(obj[i]);
231+
spannableStringBuilder.removeSpan(paragraph);
497232
} else {
498-
spannableStringBuilder.setSpan(obj[i], start, end, Spannable.SPAN_PARAGRAPH);
233+
spannableStringBuilder.setSpan(paragraph, start, end, Spannable.SPAN_PARAGRAPH);
499234
}
500235
}
501236

@@ -751,28 +486,6 @@ private static void end(SpannableStringBuilder text, TextFormat textFormat) {
751486
}
752487
}
753488

754-
//<<<<<<< HEAD
755-
// private static void startImg(final SpannableStringBuilder text,
756-
// Attributes attributes, final OnMediaTappedListener onMediaTappedListener,
757-
// final Context context) {
758-
// final int start = text.length();
759-
//
760-
// // TODO: we should some placeholder drawable while loading imges
761-
// Drawable loadingDrawable = ContextCompat.getDrawable(context, R.drawable.ic_image_loading);
762-
// final AztecMediaSpan imageSpan = new AztecMediaSpan(context, loadingDrawable, attributes, onMediaTappedListener);
763-
//
764-
// text.append("\uFFFC");
765-
// text.setSpan(imageSpan, start, text.length(),
766-
// Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
767-
// text.setSpan(
768-
// new AztecMediaClickableSpan(imageSpan),
769-
// start,
770-
// text.length(),
771-
// Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
772-
// }
773-
//=======
774-
//>>>>>>> develop
775-
776489
private static void endFont(SpannableStringBuilder text) {
777490
int len = text.length();
778491
Font font = (Font) getLast(text, Font.class);

aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,8 +604,8 @@ class AztecParser {
604604
return html
605605
.replace("&#8203;", "")
606606
.replace("&#65279;", "")
607-
.replace("(</? ?br>)*</blockquote>".toRegex(), "</blockquote>")
608-
.replace("(</? ?br>)*</p>".toRegex(), "</p>")
609-
.replace("(</? ?br>)*</li>".toRegex(), "</li>")
607+
.replace("(</? ?br>)*((aztec_cursor)?)</blockquote>".toRegex(), "$2</blockquote>")
608+
.replace("(</? ?br>)*((aztec_cursor)?)</p>".toRegex(), "$2</p>")
609+
.replace("(</? ?br>)*((aztec_cursor)?)</li>".toRegex(), "$2</li>")
610610
}
611611
}

aztec/src/main/kotlin/org/wordpress/aztec/AztecTagHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class AztecTagHandler : Html.TagHandler {
7979
val mediaSpan = createImageSpan(attributes, onMediaTappedListener, context)
8080
start(output, mediaSpan)
8181
start(output, AztecMediaClickableSpan(mediaSpan))
82-
output.append("\uFFFC")
82+
output.append(Constants.IMG_CHAR)
8383
} else {
8484
end(output, AztecMediaSpan::class.java)
8585
end(output, AztecMediaClickableSpan::class.java)

aztec/src/main/kotlin/org/wordpress/aztec/Constants.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ object Constants {
55
val MAGIC_STRING = "" + MAGIC_CHAR
66
val ZWJ_CHAR = '\u200B'//'§'
77
val ZWJ_STRING = "" + ZWJ_CHAR
8+
val IMG_CHAR = '\uFFFC'
9+
val IMG_STRING = "" + IMG_CHAR
810
}

0 commit comments

Comments
 (0)