HTML5-DOM

 view release on metacpan or  search on metacpan

utils.c  view on Meta::CPAN

			}
		}
	}
}

// Safe delete nodes only if it has not perl object representation
void html5_tree_node_delete_recursive(myhtml_tree_node_t *node) {
	if (!myhtml_node_get_data(node)) {
		myhtml_tree_node_t *child = myhtml_node_child(node);
		if (child) {
			while (child) {
				myhtml_tree_node_t *next = myhtml_node_next(child);
				myhtml_tree_node_remove(child);
				html5_tree_node_delete_recursive(child);
				child = next;
			}
		}
		myhtml_tree_node_delete(node);
	}
}

/*
	attrs
*/
void html5_dom_replace_attr_value(myhtml_tree_node_t *node, const char *key, size_t key_len, const char *val, size_t val_len, myencoding_t encoding) {
	myhtml_tree_attr_t *attr = myhtml_attribute_by_key(node, key, key_len);
	if (attr) { // edit
		// destroy original value
		mycore_string_destroy(&attr->value, 0);
		
		// set new value
		mycore_string_init(node->tree->mchar, node->tree->mchar_node_id, &attr->value, (val_len + 1));
		
		// apply encoding
		if (encoding == MyENCODING_UTF_8) {
			mycore_string_append(&attr->value, val, val_len);
		} else {
			myencoding_string_append(&attr->value, val, val_len, encoding);
		}
	} else { // add new
		myhtml_attribute_add(node, key, key_len, val, val_len, encoding);
	}
}

/*
	encoding
*/
myencoding_t html5_dom_auto_encoding(html5_dom_options_t *opts, const char **html_str, size_t *html_length) {
	// Try to determine encoding
	myencoding_t encoding;
	if (opts->encoding == MyENCODING_AUTO) {
		encoding = MyENCODING_NOT_DETERMINED;
		if (*html_length) {
			// Search encoding in meta-tags
			if (opts->encoding_use_meta) {
				size_t size = opts->encoding_prescan_limit < *html_length ? opts->encoding_prescan_limit : *html_length;
				encoding = myencoding_prescan_stream_to_determine_encoding(*html_str, size);
			}
			
			if (encoding == MyENCODING_NOT_DETERMINED) {
				// Check BOM
				if (!opts->encoding_use_bom || !myencoding_detect_and_cut_bom(*html_str, *html_length, &encoding, html_str, html_length)) {
					// Check heuristic
					if (!myencoding_detect(*html_str, *html_length, &encoding)) {
						// Can't determine encoding, use default
						encoding = opts->default_encoding;
					}
				}
			}
		} else {
			encoding = opts->default_encoding;
		}
	} else {
		encoding = opts->encoding;
	}
	return encoding;
}



( run in 0.419 second using v1.01-cache-2.11-cpan-524268b4103 )