[ 
https://issues.apache.org/jira/browse/TIKA-1938?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15260771#comment-15260771
 ] 

ASF GitHub Bot commented on TIKA-1938:
--------------------------------------

GitHub user naegelejd opened a pull request:

    https://github.com/apache/tika/pull/111

    fix for TIKA-1938 contributed by naegelejd

    Adds HtmlParser support for <script> tags within <head>

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/naegelejd/tika TIKA-1938

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/tika/pull/111.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #111
    
----
commit b6d23c189e852fa2e41b441c18bfe3e66e3f67c4
Author: Joseph Naegele <[email protected]>
Date:   2016-04-27T18:35:11Z

    fix for TIKA-1938 contributed by naegelejd
    
    add HtmlParser support for <script> tags within <head>

----


> HtmlParser drops <script> elements found inside <head>
> ------------------------------------------------------
>
>                 Key: TIKA-1938
>                 URL: https://issues.apache.org/jira/browse/TIKA-1938
>             Project: Tika
>          Issue Type: Bug
>          Components: parser
>    Affects Versions: 1.12
>            Reporter: Joseph Naegele
>
> HtmlParser's HtmlHandler does not check for "SCRIPT" in {{startElement}} when 
> parsing <head> (i.e. {{bodylevel == 0 && discardLevel ==0}}). This causes 
> <script> elements found within <head> to be dropped altogether. They should 
> be treated in the same manner as "LINK" elements.
> Here is a sample test case that demonstrates the problem and can be run from 
> within {{HtmlParserTest.java}}, although it could be generalized to check for 
> <a>, <link> and <img> links by using the {{LinkContentHandler}}.
> {code}
> @Test
> public void testScriptSrc() throws Exception {
>     String url = "http://domain.com/logic.js";;
>     String scriptInBody =
>             "<html><body><script src=\"" + url + "\"></script></body></html>";
>     String scriptInHead =
>             "<html><head><script src=\"" + url + "\"></script></head></html>";
>     assertScriptLink(scriptInBody, url);
>     assertScriptLink(scriptInHead, url);
> }
> private void assertScriptLink(String html, String url) throws Exception {
>     // IdentityHtmlMapper needed to extract <script> tags
>     ParseContext context = new ParseContext();
>     context.set(HtmlMapper.class, IdentityHtmlMapper.INSTANCE);
>     Metadata metadata = new Metadata();
>     metadata.set(Metadata.CONTENT_TYPE, "text/html");
>     final List<String> links = new ArrayList<String>();
>     new HtmlParser().parse(
>             new ByteArrayInputStream(html.getBytes(UTF_8)),
>             new DefaultHandler() {
>                 @Override
>                 public void startElement(
>                         String u, String l, String name, Attributes atts) {
>                     if (name.equals("script") && atts.getValue("", "src") != 
> null) {
>                         links.add(atts.getValue("", "src"));
>                     }
>                 }
>             },
>             metadata,
>             context);
>     assertEquals(1, links.size());
>     assertEquals(url, links.get(0));
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to