<commit_before>/*
 *  This file is part of the Wayback archival access software
 *   (http://archive-access.sourceforge.net/projects/wayback/).
 *
 *  Licensed to the Internet Archive (IA) by one or more individual 
 *  contributors. 
 *
 *  The IA licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.archive.wayback.replay;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.archive.wayback.ResultURIConverter;
import org.archive.wayback.util.url.UrlOperations;

/**
 * Library for updating arbitrary attributes in arbitrary tags to rewrite HTML
 * documents so URI references point back into the Wayback Machine. Attempts to
 * make minimal changes so nothing gets broken during this process.
 * 
 * @author brad
 * @version $Date$, $Revision:
 *          1668 $
 */
public class TagMagix {

	// minimum length XXXX in a 'ATTR=XXXX' declaration... mostly handy
	// to keep us from trying to mark up javascript generated HTML/CSS code.
	private static int MIN_ATTR_LENGTH = 3;
	
	private static HashMap<String, Pattern> pcPatterns = 
		new HashMap<String, Pattern>();

	private static HashMap<String, Pattern> wholeTagPatterns = 
		new HashMap<String, Pattern>();

	private static HashMap<String, Pattern> attrPatterns = 
		new HashMap<String, Pattern>();

	private static String QUOTED_ATTR_VALUE = "(?:\"[^\">]*\")";

	private static String ESC_QUOTED_ATTR_VALUE = "(?:\\\\\"[^>\\\\]*\\\\\")";

	private static String APOSED_ATTR_VALUE = "(?:'[^'>]*')";

	private static String RAW_ATTR_VALUE = "(?:[^ \\t\\n\\x0B\\f\\r>\"']+)";

	public static String ANY_TAGNAME = "[a-z]+";
	
	private static String STYLE_ATTR_NAME = "style";
	
	private static String ANY_ATTR_VALUE = QUOTED_ATTR_VALUE + "|"
			+ APOSED_ATTR_VALUE + "|" + ESC_QUOTED_ATTR_VALUE + "|"
			+ RAW_ATTR_VALUE;
	
	private static String cssUrlPatString = 
		"(?<!@namespace [\\w\\s]{0,16})url\\s*\\(\\s*([\\\\\"']*.+?[\\\\\"']*)\\s*\\)";

	private static String cssImportNoUrlPatString =
		"@import\\s+([\"'].+?[\"'])";
	
	private static Pattern cssImportNoUrlPattern = 
		Pattern.compile(cssImportNoUrlPatString);
	
	private static Pattern cssUrlPattern = Pattern.compile(cssUrlPatString, Pattern.CASE_INSENSITIVE);

	/**
	 * get (and cache) a regex Pattern for locating an HTML attribute value
	 * within a particular tag. if found, the pattern will have the attribute
	 * value in group 1. Note that the attribute value may contain surrounding
	 * apostrophe(') or quote(") characters.
	 * 
	 * @param tagName
	 * @param attrName
	 * @return Pattern to match the tag-attribute's value
	 */
	private synchronized static Pattern getPattern(String tagName,
			String attrName) {
<commit_msg>FIX: scary css url rewrite regex, match url() as blank url to be skipped should match url('') and url("") as well, avoid rewriting these completely<commit_after>/*
 *  This file is part of the Wayback archival access software
 *   (http://archive-access.sourceforge.net/projects/wayback/).
 *
 *  Licensed to the Internet Archive (IA) by one or more individual 
 *  contributors. 
 *
 *  The IA licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.archive.wayback.replay;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.archive.wayback.ResultURIConverter;
import org.archive.wayback.util.url.UrlOperations;

/**
 * Library for updating arbitrary attributes in arbitrary tags to rewrite HTML
 * documents so URI references point back into the Wayback Machine. Attempts to
 * make minimal changes so nothing gets broken during this process.
 * 
 * @author brad
 * @version $Date$, $Revision:
 *          1668 $
 */
public class TagMagix {

	// minimum length XXXX in a 'ATTR=XXXX' declaration... mostly handy
	// to keep us from trying to mark up javascript generated HTML/CSS code.
	private static int MIN_ATTR_LENGTH = 3;
	
	private static HashMap<String, Pattern> pcPatterns = 
		new HashMap<String, Pattern>();

	private static HashMap<String, Pattern> wholeTagPatterns = 
		new HashMap<String, Pattern>();

	private static HashMap<String, Pattern> attrPatterns = 
		new HashMap<String, Pattern>();

	private static String QUOTED_ATTR_VALUE = "(?:\"[^\">]*\")";

	private static String ESC_QUOTED_ATTR_VALUE = "(?:\\\\\"[^>\\\\]*\\\\\")";

	private static String APOSED_ATTR_VALUE = "(?:'[^'>]*')";

	private static String RAW_ATTR_VALUE = "(?:[^ \\t\\n\\x0B\\f\\r>\"']+)";

	public static String ANY_TAGNAME = "[a-z]+";
	
	private static String STYLE_ATTR_NAME = "style";
	
	private static String ANY_ATTR_VALUE = QUOTED_ATTR_VALUE + "|"
			+ APOSED_ATTR_VALUE + "|" + ESC_QUOTED_ATTR_VALUE + "|"
			+ RAW_ATTR_VALUE;
	
	private static String cssUrlPatString = 
		"(?<!@namespace [\\w\\s]{0,16})url\\s*\\(\\s*([\\\\\"']*.*?[\\\\\"']*)\\s*\\)";

	private static String cssImportNoUrlPatString =
		"@import\\s+([\"'].+?[\"'])";
	
	private static Pattern cssImportNoUrlPattern = 
		Pattern.compile(cssImportNoUrlPatString);
	
	private static Pattern cssUrlPattern = Pattern.compile(cssUrlPatString, Pattern.CASE_INSENSITIVE);

	/**
	 * get (and cache) a regex Pattern for locating an HTML attribute value
	 * within a particular tag. if found, the pattern will have the attribute
	 * value in group 1. Note that the attribute value may contain surrounding
	 * apostrophe(') or quote(") characters.
	 * 
	 * @param tagName
	 * @param attrName
	 * @return Pattern to match the tag-attribute's value
	 */
	private synchronized static Pattern getPattern(String tagName,
			String attrName) {

{'fraction_non_alphanumeric': 0.12315345191438046, 'fraction_numerical': 0.004220681338558939, 'mean_word_length': 3.8750918442321822, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4769923', 'n_tokens_mistral': 2056, 'n_tokens_neox': 1892, 'n_words': 680}