package net.micode.notes.tool;

import android.os.Build;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;

import org.apache.http.util.TextUtils;

public class HtmlUtils {
    // 解析HTML → Spannable
    public static Spannable fromHtml(String html) {
        if (TextUtils.isEmpty(html)) {
            return new SpannableString(""); // 返回空Spannable
        }
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                return (Spannable) Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
            } else {
                return (Spannable) Html.fromHtml(html);
            }
        } catch (Exception e) {
            // 解析失败时回退到纯文本
            return new SpannableString(html);
        }
    }
    // 新增：Spannable → HTML 转换
    public static String toHtml(Spannable spannable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return Html.toHtml(spannable, Html.FROM_HTML_MODE_LEGACY);
        } else {
            return Html.toHtml(spannable); // 旧版无模式参数
        }
    }
}
