Java Code Examples for org.jsoup.nodes.Element#removeAttr()

The following examples show how to use org.jsoup.nodes.Element#removeAttr() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HtmlImportProcessorImpl.java    From yarg with Apache License 2.0 6 votes vote down vote up
protected void processFontColor(org.jsoup.nodes.Document document) {
    Elements elements = document.getElementsByTag("font");
    for (Element element : elements) {
        String color = element.attr("color");
        if (color != null) {
            String style = StringUtils.trim(element.attr("style"));
            if (style != null) {
                if (StringUtils.endsWith(style, ";")) {
                    style = style + ";";
                }
                style = style + "color:" + color;
            } else {
                style = "color:" + color;
            }
            element.attr("style", style);
            element.removeAttr("color");
        }
    }
}
 
Example 2
Source File: TextFilterManage.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 处理隐藏标签
 * @param html 富文本内容
 * @param visibleTagList 允许可见的隐藏标签
 * @return
 */
public String processHiddenTag(String html,List<Integer> visibleTagList){
	
	if(!StringUtils.isBlank(html)){
		Document doc = Jsoup.parseBodyFragment(html);
		Elements elements = doc.select("hide");  
		for (Element element : elements) {
			//隐藏标签类型
			String hide_type = element.attr("hide-type"); 
			//隐藏标签输入值
		//	String input_value = element.attr("input-value"); 
			
			if(visibleTagList.contains(Integer.parseInt(hide_type.trim()))){//如果允许可见
				/**
				//替换当前标签为<p>标签
				element.removeAttr("class"); 
				element.removeAttr("hide-type");
				element.removeAttr("input-value");
				element.tagName("p");**/
				//移除匹配的元素但保留他们的内容
				element.unwrap();  
				
			}else{
				if(hide_type.trim().equals(HideTagType.PASSWORD.getName().toString())){//输入密码可见
					element.removeAttr("input-value");
				}
				//清空元素的内容
				element.empty();	
			}
		}
		//prettyPrint(是否重新格式化)、outline(是否强制所有标签换行)、indentAmount(缩进长度)    doc.outputSettings().indentAmount(0).prettyPrint(false);
		doc.outputSettings().prettyPrint(false);
		html = doc.body().html();
	}
	return html;
}
 
Example 3
Source File: TextFilterManage.java    From bbs with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 处理视频播放器标签
 * @param html 富文本内容
 * @param tagId 话题标签  -1表示管理后台打开链接,不校验权限
 * @param secret 密钥
 * @return
 */
public String processVideoPlayer(String html,Long tagId,String secret){
	
	if(!StringUtils.isBlank(html)){
		Document doc = Jsoup.parseBodyFragment(html);
		Elements elements = doc.select("video");  
		for (Element element : elements) {
			//标签src属性
			String src = element.attr("src"); 

			element.removeAttr("src"); 
			//替换当前标签为<player>标签
			element.tagName("player");
			
			
			String url = "";
			if(secret != null && !"".equals(secret.trim())){
				url = SecureLink.createVideoRedirectLink(src,tagId,secret);
			}else{
				url = src;
			}
			element.attr("url",url); 
		
			
		}
		//prettyPrint(是否重新格式化)、outline(是否强制所有标签换行)、indentAmount(缩进长度)    doc.outputSettings().indentAmount(0).prettyPrint(false);
		doc.outputSettings().prettyPrint(false);
		html = doc.body().html();
	}
	return html;
}
 
Example 4
Source File: HtmlView.java    From JavaRushTasks with MIT License 5 votes vote down vote up
private String getUpdatedFileContent(List<Vacancy> vacancies) {

        Document document = null;
        try {
            document = getDocument();

            Element templateOriginal = document.getElementsByClass("template").first();
            Element copyTemplate = templateOriginal.clone();
            copyTemplate.removeAttr("style");
            copyTemplate.removeClass("template");
            document.select("tr[class=vacancy]").remove().not("tr[class=vacancy template");

            for (Vacancy vacancy : vacancies) {
                Element localClone = copyTemplate.clone();
                localClone.getElementsByClass("city").first().text(vacancy.getCity());
                localClone.getElementsByClass("companyName").first().text(vacancy.getCompanyName());
                localClone.getElementsByClass("salary").first().text(vacancy.getSalary());
                Element link =localClone.getElementsByTag("a").first();
                link.text(vacancy.getTitle());
                link.attr("href", vacancy.getUrl());

                templateOriginal.before(localClone.outerHtml());
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "Some exception occurred";
        }
        return document.html();
    }
 
Example 5
Source File: HtmlConverter.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * 将页面转为{@link org.jsoup.nodes.Document}对象,xhtml 格式
 *
 * @param url
 * @return
 * @throws Exception
 */
protected Document url2xhtml(String url) throws Exception {
    Document doc = Jsoup.connect(url).get(); //获得

    if (logger.isDebugEnabled()) {
        logger.debug("baseUri: {}", doc.baseUri());
    }

    for (Element script : doc.getElementsByTag("script")) { //除去所有 script
        script.remove();
    }

    for (Element a : doc.getElementsByTag("a")) { //除去 a 的 onclick,href 属性
        a.removeAttr("onclick");
        a.removeAttr("href");
    }

    Elements links = doc.getElementsByTag("link"); //将link中的地址替换为绝对地址
    for (Element element : links) {
        String href = element.absUrl("href");

        if (logger.isDebugEnabled()) {
            logger.debug("href: {} -> {}", element.attr("href"), href);
        }

        element.attr("href", href);
    }

    doc.outputSettings()
            .syntax(Document.OutputSettings.Syntax.xml)
            .escapeMode(Entities.EscapeMode.xhtml);  //转为 xhtml 格式

    if (logger.isDebugEnabled()) {
        String[] split = doc.html().split("\n");
        for (int c = 0; c < split.length; c++) {
            logger.debug("line {}:\t{}", c + 1, split[c]);
        }
    }
    return doc;
}
 
Example 6
Source File: _WechatArticleImport.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
private String processContentImages(String content, List<String> imageUrls) {

        Document doc = Jsoup.parse(content);
        Elements imgElements = doc.select("img");
        if (imgElements != null) {
            Iterator<Element> iterator = imgElements.iterator();
            while (iterator.hasNext()) {
                Element element = iterator.next();

                String imageUrl = element.hasAttr("src")
                        ? element.attr("src")
                        : element.attr("data-src");

//http://mmbiz.qpic.cn/mmbiz/4gZTdZfnQeDvQqCZFuVvYv8scGS7sEQTRETgISib1blz5iclAtnsccaJhaugmKc
// hhm8mFOtjnicibibumazy8wPS6Xg/640?tp=webp&wxfrom=5&wx_lazy=1&wx_co=1

                imageUrl = replaceLast(imageUrl, "/", "__");
                imageUrl = imageUrl.startsWith("http://")
                        ? imageUrl.replace("http://", "/attachment/")
                        : imageUrl.replace("https://", "/attachment/s");

                imageUrl = imageUrl.replace("?",".png?");

                element.removeAttr("data-src");
                element.attr("src",imageUrl);

                imageUrls.add(imageUrl);
            }
        }

        return doc.toString();
    }
 
Example 7
Source File: ArmaDocumentationAsyncFormattingOperations.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
private static void cleanAttributes(@NotNull Document d) {
	Elements allEles = d.select("*");
	for (Element code : allEles) {
		code.removeAttr("class");
		code.removeAttr("title");
		code.removeAttr("id");
	}
}
 
Example 8
Source File: ArmaDocumentationAsyncFormattingOperations.java    From arma-intellij-plugin with MIT License 5 votes vote down vote up
private static void cleanAttributes(@NotNull Document d) {
	Elements allEles = d.select("*");
	for (Element code : allEles) {
		code.removeAttr("class");
		code.removeAttr("title");
		code.removeAttr("id");
	}
}
 
Example 9
Source File: JsoupCssInliner.java    From ogham with Apache License 2.0 5 votes vote down vote up
/**
 * Transfers styles from the <code>data-cssstyle</code> attribute to the
 * <code>style</code> attribute.
 *
 * @param doc
 *            the html document
 */
private static void applyStyles(Document doc) {
	Elements allStyledElements = doc.getElementsByAttribute(TEMP_STYLE_ATTR);

	for (Element e : allStyledElements) {
		if (isInlineModeAllowed(e, InlineModes.STYLE_ATTR)) {
			String newStyle = e.attr(TEMP_STYLE_ATTR);
			String oldStyle = e.attr(STYLE_ATTR);
			e.attr(STYLE_ATTR, (newStyle.trim() + ";" + oldStyle.trim()).replaceAll(";+", ";").trim());
		}
		e.removeAttr(TEMP_STYLE_ATTR);
	}
}
 
Example 10
Source File: Elements.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove an attribute from every matched element.
 * @param attributeKey The attribute to remove.
 * @return this (for chaining)
 */
public Elements removeAttr(String attributeKey) {
    for (Element element : this) {
        element.removeAttr(attributeKey);
    }
    return this;
}
 
Example 11
Source File: Elements.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove an attribute from every matched element.
 * @param attributeKey The attribute to remove.
 * @return this (for chaining)
 */
public Elements removeAttr(String attributeKey) {
    for (Element element : this) {
        element.removeAttr(attributeKey);
    }
    return this;
}
 
Example 12
Source File: Elements.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Remove an attribute from every matched element.
 * @param attributeKey The attribute to remove.
 * @return this (for chaining)
 */
public Elements removeAttr(String attributeKey) {
    for (Element element : this) {
        element.removeAttr(attributeKey);
    }
    return this;
}
 
Example 13
Source File: Elements.java    From jsoup-learning with MIT License 5 votes vote down vote up
/**
 * Remove an attribute from every matched element.
 * @param attributeKey The attribute to remove.
 * @return this (for chaining)
 */
public Elements removeAttr(String attributeKey) {
    for (Element element : contents) {
        element.removeAttr(attributeKey);
    }
    return this;
}
 
Example 14
Source File: HtmlHelper.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
static void removeTrackingPixels(Context context, Document document) {
    Drawable d = ContextCompat.getDrawable(context, R.drawable.baseline_my_location_24);
    d.setTint(Helper.resolveColor(context, R.attr.colorWarning));

    Bitmap bm = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bm);
    d.setBounds(0, 0, c.getWidth(), c.getHeight());
    d.draw(c);

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, bos);

    StringBuilder sb = new StringBuilder();
    sb.append("data:image/png;base64,");
    sb.append(Base64.encodeToString(bos.toByteArray(), Base64.NO_WRAP));

    // Build list of allowed hosts
    List<String> hosts = new ArrayList<>();
    //for (Element img : document.select("img")) {
    //    String src = img.attr("src");
    //    if (!TextUtils.isEmpty(src) && !isTrackingPixel(img)) {
    //        Uri uri = Uri.parse(img.attr("src"));
    //        String host = uri.getHost();
    //        if (host != null && !hosts.contains(host))
    //            hosts.add(host);
    //    }
    //}

    // Images
    for (Element img : document.select("img")) {
        img.removeAttr("x-tracking");
        String src = img.attr("src");
        if (!TextUtils.isEmpty(src) && isTrackingPixel(img)) {
            Uri uri = Uri.parse(src);
            String host = uri.getHost();
            if (host != null && !hosts.contains(host)) {
                img.attr("src", sb.toString());
                img.attr("alt", context.getString(R.string.title_legend_tracking_pixel));
                img.attr("height", "24");
                img.attr("width", "24");
                img.attr("style", "display:block !important; width:24px !important; height:24px !important;");
                img.attr("x-tracking", src);
            }
        }
    }
}
 
Example 15
Source File: CssInlineUtils.java    From ogham with Apache License 2.0 3 votes vote down vote up
/**
 * Remove attributes that are used only by Ogham:
 * <ul>
 * <li>{@link CssInlinerConstants#INLINE_MODE_ATTR}</li>
 * <li>{@link CssInlinerConstants#INLINED_ATTR}</li>
 * </ul>
 * 
 * @param html
 *            the html to clean
 * @return the cleaned html
 */
public static String removeOghamAttributes(String html) {
	Document doc = Jsoup.parse(html);
	Elements nodes = doc.select("["+INLINE_MODE_ATTR+"], ["+INLINED_ATTR+"]");
	for (Element node : nodes) {
		node.removeAttr(INLINE_MODE_ATTR);
		node.removeAttr(INLINED_ATTR);
	}
	return doc.outerHtml();
}
 
Example 16
Source File: ImageInlineUtils.java    From ogham with Apache License 2.0 3 votes vote down vote up
/**
 * Remove attributes that are used only by Ogham:
 * <ul>
 * <li>{@link ImageInlinerConstants#INLINE_MODE_ATTR}</li>
 * <li>{@link ImageInlinerConstants#INLINED_ATTR}</li>
 * </ul>
 * 
 * @param html
 *            the html to clean
 * @return the cleaned html
 */
public static String removeOghamAttributes(String html) {
	Document doc = Jsoup.parse(html);
	Elements imgs = doc.select("img");
	for (Element img : imgs) {
		img.removeAttr(INLINE_MODE_ATTR);
		img.removeAttr(INLINED_ATTR);
	}
	return doc.outerHtml();
}